index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <template>
  2. <view class="container">
  3. <navBar title="二维码进入的页面" :back="true" color="white" background="green" />
  4. <button @click="getAccessToken">生成AccessToken</button>
  5. <button @click="getAccessToken">生成进入小程序的二维码</button>
  6. <button @click="createPoster">测试生成海报</button>
  7. <image @click="previewPic" :src="testUrl" mode=""></image>
  8. <u-popup :show="showPoster" mode="bottom" @close="showPoster = false">
  9. <!-- <view :style="{height: tobheight+'px'}"></view> -->
  10. <scroll-view scroll-y="true" :style="{maxHeight: posterheight +'px'}">
  11. <wike-painter :board="posterObj" @done="posterSuccess" ref="painter"></wike-painter>
  12. <view style="height: 78px;"></view>
  13. <view class="dygbhg">
  14. <view class="whole" @click="showPoster = false">取消</view>
  15. <view class="distinguish" @click="toSave">保存/分享</view>
  16. </view>
  17. </scroll-view>
  18. </u-popup>
  19. </view>
  20. </template>
  21. <script>
  22. import wikePainter from '@/components/wike-painter/wike-painter.vue'
  23. var util = require('@/utils/decodeQrQuery.js')
  24. import {
  25. getBase64Image
  26. } from '@/utils/streamDown.js'
  27. import uploadUrl from '@/common/config.js'
  28. export default {
  29. components: {
  30. wikePainter
  31. },
  32. data() {
  33. return {
  34. kcardCode: '',
  35. testUrl: '',
  36. info: {
  37. poster_bg_img_path: 'http://file.shopro.top/imgs/commission/commission_card_bg.png',
  38. poster_bg_width: '750',
  39. poster_bg_height: '1334',
  40. poster_qrcode_x: '514',
  41. poster_qrcode_y: '1174',
  42. poster_bg_img: "1671",
  43. poster_bg_img_path: "https://nywhcm.com/addons/wike_aging/public/static/storage/20230625/74f8be11f3f174e63576a777c71d644b.jpg",
  44. // poster_bg_img_path: "https://nywhcm.com/addons/wike_aging/public/static/storage/20230322/17859fbc20087bd430f224905f5d9370.jpg",
  45. poster_bg_img_filename: "分销海报.jpg",
  46. poster_qrcode_width: "135",
  47. poster_qrcode_width_path: "https://nywhcm.com/addons/wike_aging/public/static/storage/20230322/17859fbc20087bd430f224905f5d9370.jpg",
  48. poster_qrcode_width_filename: "tmp_c1537e2c3beb0a3d14f87a63da8b84cc548ea7871f918188.jpg",
  49. },
  50. qrcode: 'https://nywhcm.com/addons/wike_aging/public/h5/?uniacid=5059&spm=4825.1.0.1.1',
  51. // qrcode: 'https://nywhcm.com/addons/wike_aging/public/static/storage/20230322/17859fbc20087bd430f224905f5d9370.jpg',
  52. showPoster: false,
  53. posterObj: {},
  54. posterheight: 0
  55. }
  56. },
  57. onLoad(options) {
  58. const that = this;
  59. if (this.platform == 'wxMiniProgram') {
  60. var menumtop = uni.getMenuButtonBoundingClientRect().top - uni.getSystemInfoSync().statusBarHeight
  61. var paddingtop = uni.getSystemInfoSync().statusBarHeight + menumtop
  62. this.tobheight = (menumtop + paddingtop + uni.getMenuButtonBoundingClientRect().height)
  63. }
  64. this.posterheight = uni.$u.sys().windowHeight - this.tobheight
  65. console.log('二维码页面onLoad:', options);
  66. // 获取小程序启动时的参数
  67. let obj = wx.getLaunchOptionsSync();
  68. let query = null;
  69. if (options.scene || obj.query.scene) {
  70. // 这里是用开发者工具模拟的时候要先用 encodeURIComponent 编译,模拟真实传递参数的效果,项目发布审核的时候这句话要注释掉
  71. // query = options ? encodeURIComponent(options.scene) : encodeURIComponent(obj.query.scene);
  72. query = options ? decodeURIComponent(options.scene) : decodeURIComponent(obj.query.scene);
  73. // query = decodeURIComponent(obj.query.scene);
  74. //解析参数
  75. let web_key = query.split("-")[0];
  76. let web_template = query.split("-")[1];
  77. let web_id = query.split("-")[2];
  78. console.log("第一个参数:", web_key);
  79. console.log("第二个参数:", web_template);
  80. console.log("第三个参数:", web_id);
  81. }
  82. // 'scene=asaa234354565-32323432kjkjkjkHHHHH-你好'
  83. // if (options.kcardCode) {
  84. // console.log('需要获取的二维码参数kcardCode:', options.kcardCode);
  85. // }
  86. // var that = this;
  87. // console.log("options携带参数:" + JSON.stringify(options))
  88. // //在此函数中获取扫描普通链接二维码参数
  89. // // 二维码链接内容会以参数q的形式带给页面,在onLoad事件中提取q参数并自行decodeURIComponent一次,即可获取原二维码的完整内容。
  90. // if (options.q !== undefined) {
  91. // let q = decodeURIComponent(options.q);
  92. // console.log("url=" + q)
  93. // console.log("kcardCode=" + util.getkcard(q, 'kcardCode'))
  94. // var kcardCode = util.getkcard(q, 'kcardCode');
  95. // console.log("解析获取到的kcardCode值:" + kcardCode);
  96. // that.kcardCode = kcardCode
  97. // } else {
  98. // console.log("未获取到扫码信息:" + kcardCode);
  99. // uni.showToast({
  100. // title: '错误:未获取到扫码信息!',
  101. // duration: 3000
  102. // })
  103. // return false
  104. // }
  105. },
  106. onShow() {
  107. },
  108. methods: {
  109. previewPic() {
  110. uni.previewImage({
  111. urls: [this.testUrl]
  112. })
  113. },
  114. getAccessToken() {
  115. let that = this
  116. let appId = 'wx98ab2939999e13de'
  117. let secret = '841f428c4d3e550e800d8ad0fd5e0354'
  118. wx.request({
  119. url: `https://api.weixin.qq.com/cgi-bin/token?appid=${appId}&secret=${secret}&grant_type=client_credential`,
  120. data: {},
  121. header: {
  122. 'content-type': 'json'
  123. },
  124. success: function(res) {
  125. console.log(res);
  126. that.getUnlimitedQRCode(res.data.access_token)
  127. }
  128. })
  129. },
  130. // getWxQrCode(access_token) {
  131. getUnlimitedQRCode(access_token) {
  132. let that = this
  133. wx.request({
  134. url: `https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${access_token}`,
  135. responseType: 'arraybuffer',
  136. data: JSON.stringify({
  137. scene: 'kcardCode=123456',
  138. // page: 'pages/index/index',
  139. // scene: 'a=1-b=2-c=李万涛',
  140. page: 'pages/index/index',
  141. check_path: false
  142. }),
  143. header: {
  144. 'content-type': 'json',
  145. },
  146. method: 'POST',
  147. success: function(res) {
  148. // const imageArrayBuffer = res.data;
  149. // const base64Str = wx.arrayBufferToBase64(imageArrayBuffer);
  150. // let filePath = `${wx.env.USER_DATA_PATH}/temp${Date.now()}.jpg`
  151. // wx.getFileSystemManager().writeFile({
  152. // filePath,
  153. // data: base64Str,
  154. // encoding: 'base64',
  155. // success: function(res) {
  156. // that.testUrl = filePath
  157. // console.log('wx.getFileSystemManager()', res,
  158. // `${wx.env.USER_DATA_PATH}/temp.jpg`);
  159. // // wx.saveImageToPhotosAlbum({
  160. // // filePath: res.filePath,
  161. // // success: function() {
  162. // // console.log('保存成功');
  163. // // },
  164. // // fail: function(err) {
  165. // // console.log('保存失败', err);
  166. // // }
  167. // // });
  168. // },
  169. // fail: function(err) {
  170. // console.log('创建临时文件失败', err);
  171. // }
  172. // });
  173. // that.upImg(`${wx.env.USER_DATA_PATH}/temp.jpg`, 2)
  174. // return
  175. // console.log(res);
  176. const base64 = wx.arrayBufferToBase64(res.data);
  177. that.testUrl = 'data:image/jpeg;base64,' + base64;
  178. that.qrcode = 'data:image/jpeg;base64,' + base64
  179. // that.createPoster()
  180. // console.log('testUrl----base64', that.testUrl, base64, res.data);
  181. }
  182. })
  183. },
  184. genQr() {
  185. },
  186. upImg(file, type = 1) {
  187. // console.log('upImg的file', file)
  188. let _this = this
  189. uni.uploadFile({
  190. url: uploadUrl.baseUrl, // 上传的 URL 地址
  191. filePath: file, // 要上传的图片本地路径
  192. name: 'file', // 上传图片时使用的字段名
  193. header: { // 自定义请求头
  194. 'Content-Type': 'multipart/form-data'
  195. },
  196. formData: {
  197. 'fileType': 'images',
  198. 'dirName': 'cert'
  199. },
  200. success: function(uploadRes) {
  201. let result = JSON.parse(uploadRes.data)
  202. // localStorage.set('imgUrl', result.data.fileUrl)
  203. if (result.code == 0) {
  204. if (type == 2) {
  205. _this.testUrl = result.data.url
  206. } else {
  207. _this.avatarUrl = result.data.url
  208. }
  209. // _this.user_img = result.data.url
  210. console.log('上传后的阿里云url地址', result.data.url)
  211. }
  212. },
  213. fail: function(err) {
  214. console.log('upload failed:', err)
  215. }
  216. })
  217. },
  218. // 测试生成海报
  219. async createPoster(e) {
  220. let that = this;
  221. let httpsurl = '';
  222. console.log('httpsurl----', httpsurl);
  223. uni.showLoading({
  224. title: '海报渲染中'
  225. });
  226. if (that.info.poster_bg_img_path.indexOf("https") < 0) {
  227. httpsurl = await that.getdownload(that.info.poster_bg_img_path.replace("http:", "https:"));
  228. } else {
  229. httpsurl = await that.getdownload(that.info.poster_bg_img_path);
  230. }
  231. if (httpsurl == 'fail') {
  232. uni.hideLoading();
  233. uni.showToast({
  234. title: '生成失败',
  235. icon: 'none'
  236. })
  237. return;
  238. }
  239. let proportionally = this.info.poster_bg_width / uni.$u.sys().windowWidth;
  240. (this.posterObj = {
  241. width: (this.info.poster_bg_width / proportionally) + 'px',
  242. height: (this.info.poster_bg_height / proportionally) + 'px',
  243. background: '#fff',
  244. borderRadius: '16rpx',
  245. views: [{
  246. src: httpsurl,
  247. type: "image",
  248. css: {
  249. width: (this.info.poster_bg_width / proportionally) + 'px',
  250. height: (this.info.poster_bg_height / proportionally) + 'px',
  251. }
  252. },
  253. {
  254. type: 'view',
  255. css: {
  256. left: (this.info.poster_qrcode_x / proportionally) + 'px',
  257. top: (this.info.poster_qrcode_y / proportionally) + 'px',
  258. position: 'fixed',
  259. },
  260. // #ifdef H5
  261. views: [{
  262. type: 'qrcode',
  263. text: this.qrcode,
  264. css: {
  265. width: (this.info.poster_qrcode_width / proportionally) + 'px',
  266. height: (this.info.poster_qrcode_width / proportionally) + 'px',
  267. background: '#fff'
  268. }
  269. }],
  270. // #endif
  271. // #ifdef MP-WEIXIN
  272. views: [{
  273. type: 'image',
  274. src: this.qrcode,
  275. css: {
  276. width: (this.info.poster_qrcode_width / proportionally) + 'px',
  277. height: (this.info.poster_qrcode_width / proportionally) + 'px',
  278. background: '#fff'
  279. }
  280. }],
  281. // #endif
  282. }
  283. ]
  284. // #ifdef H5
  285. // #endif
  286. // #ifdef MP-WEIXIN
  287. // #endif
  288. }),
  289. (this.showPoster = true);
  290. },
  291. toSave() {
  292. uni.showLoading({
  293. title: '海报生成中'
  294. });
  295. this.$refs.painter.canvasToTempFilePathSync({
  296. // 在nvue里是jpeg
  297. fileType: 'jpg',
  298. quality: 1,
  299. success: res => {
  300. // #ifdef MP-WEIXIN
  301. wx.showShareImageMenu({
  302. path: res.tempFilePath
  303. })
  304. this.showPoster = false;
  305. uni.hideLoading();
  306. // #endif
  307. // #ifdef H5
  308. this.showPoster = false;
  309. uni.hideLoading();
  310. uni.showModal({
  311. confirmColor: '#26B3A0',
  312. confirmText: '查看图片',
  313. title: '提示',
  314. content: '查看图片后长按图片即可保存或分享',
  315. success(src) {
  316. if (src.confirm) {
  317. uni.previewImage({
  318. urls: [res.tempFilePath]
  319. });
  320. } else if (src.cancel) {
  321. this.showPoster = true;
  322. }
  323. }
  324. });
  325. // #endif
  326. }
  327. });
  328. },
  329. posterSuccess() {
  330. uni.hideLoading();
  331. },
  332. getdownload(e) {
  333. return new Promise((resolve, reject) => {
  334. uni.request({
  335. url: e,
  336. method: 'GET',
  337. responseType: 'arraybuffer',
  338. success: ress => {
  339. let base64 = wx.arrayBufferToBase64(ress.data);
  340. base64 = 'data:image/jpeg;base64,' + base64
  341. resolve(base64);
  342. },
  343. fail: (e) => {
  344. resolve('fail');
  345. }
  346. })
  347. });
  348. },
  349. }
  350. }
  351. </script>
  352. <style lang="scss" scoped>
  353. @import "./index.scss";
  354. </style>