shareQrCode.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <template>
  2. <view class="main">
  3. <view class="qrimg">
  4. <tki-qrcode :cid="cid" ref="qrcode" :val="val" :size="size" :unit="unit" :background="background"
  5. :foreground="foreground" :pdground="pdground" :icon="icon" :iconSize="iconSize" :lv="lv" :onval="onval" :showLoading="showLoading"
  6. :loadMake="loadMake" :usingComponents="usingComponents" @result="qrR" />
  7. </view>
  8. <view class="" :style="{'height': height*1/2/2 + 'px'}">
  9. </view>
  10. <!-- <canvas :style="{ width: '600rpx', height: '1066rpx',}" canvas-id="myCanvas" id="myCanvas" class="hb"></canvas> -->
  11. <canvas :style="{ width: '600rpx', height: '1066rpx',}" canvas-id="myCanvas" id="myCanvas" class="qrimg"></canvas>
  12. <image :style="{'display': 'block', width: '600rpx', height: '1066rpx',margin: 'auto'}" :src="shareQrcodeUrl" mode="" id="saveImg" class="hb"></image>
  13. <!-- #ifdef H5 -->
  14. <!-- #endif -->
  15. <!--#ifndef H5 -->
  16. <!-- <canvas :style="{ width: '600rpx', height: '1066rpx',}" canvas-id="myCanvas" id="myCanvas" class="hb"></canvas> -->
  17. <!-- #endif -->
  18. <view class="scrop">
  19. </view>
  20. <!-- #ifdef H5 -->
  21. <view v-if="fina" class="bottom-btn">
  22. 长按图片保存到相册
  23. </view>
  24. <!-- #endif -->
  25. <!-- #ifdef MP -->
  26. <view v-if="fina" class="bottom-btn" @click="saveShareQrcode">
  27. 保存分享海报
  28. </view>
  29. <!-- #endif -->
  30. <!-- #ifdef APP-PLUS -->
  31. <view class="btm-btn" v-if="fina">
  32. <view class="btn" @click="comfirm">
  33. <image src="../../static/icon/fzlj.png" mode=""></image>
  34. <view class="">
  35. 复制邀请链接
  36. </view>
  37. </view>
  38. <view class="btn" @click="saveShareQrcode">
  39. <image src="../../static/icon/fxhb.png" mode=""></image>
  40. <view class="">
  41. 保存分享海报
  42. </view>
  43. </view>
  44. </view>
  45. <!-- #endif -->
  46. </view>
  47. </template>
  48. <script>
  49. import {
  50. spreadBanner
  51. } from '@/api/user.js';
  52. import {
  53. mapState,
  54. mapMutations
  55. } from 'vuex';
  56. import {
  57. getUserInfo
  58. } from '@/api/user.js';
  59. var that
  60. export default {
  61. data() {
  62. return {
  63. height: '',
  64. cid: 'tki-qrcode-canvas', //canvasId,页面存在多个二维码组件时需设置不同的ID
  65. size:300, //生成的二维码大小
  66. unit: 'upx', //大小单位尺寸
  67. // show: true,//默认使用组件中的image标签显示二维码
  68. val: '', //要生成的内容
  69. background: '#ffffff', //二维码背景色
  70. foreground: '#333333', //二维码前景色
  71. pdground: '#333333', //二维码角标色
  72. icon: '', //二维码图标URL(必须是本地图片,网络图需要先下载至本地)
  73. iconSize: 40, //二维码图标大小
  74. lv: 3, //容错级别
  75. onval: true, //监听val值变化自动重新生成二维码
  76. loadMake: true, //组件初始化完成后自动生成二维码,val需要有值
  77. usingComponents: false, //是否使用了自定义组件模式(主要是为了修复非自定义组件模式时 v-if 无法生成二维码的问题)
  78. showLoading: false, //是否显示loading
  79. erweimasrc: '',
  80. canvasW: 0, // 画布宽
  81. canvasH: 0, // 画布高
  82. SystemInfo: {}, // 设备信息
  83. goodsImg: {}, // 商品主图
  84. ewmImg: {}, // 二维码图片
  85. ewmW: 0, // 二维码大小
  86. title: '', // 商品标题
  87. canvasShow: true,
  88. shareQrcodeUrl: '', //canvas本地路径
  89. ratio: '', //画布比例
  90. fina: false,
  91. }
  92. },
  93. computed: {
  94. ...mapState(['baseURL', 'urlFile']),
  95. ...mapState('user', ['userInfo', 'orderInfo', 'hasLogin'])
  96. },
  97. onReady(res) {
  98. var that = this;
  99. uni.getSystemInfo({
  100. success: resu => {
  101. const query = uni.createSelectorQuery();
  102. query.select('.scrop').boundingClientRect();
  103. query.exec(function(res) {
  104. that.height = resu.windowHeight - res[0].top;
  105. });
  106. },
  107. fail: res => {}
  108. });
  109. },
  110. onLoad(options) {
  111. const obj = this;
  112. if (!this.userInfo.uid) {
  113. getUserInfo({}).then(e => {
  114. // 保存返回用户数据
  115. obj.setUserInfo(e.data);
  116. //成功跳转首页
  117. uni.switchTab({
  118. url: '/pages/index/index'
  119. });
  120. });
  121. } else {
  122. this.val = this.baseURL + '/index/#/pages/index/index?spread=' + this.userInfo.phone
  123. }
  124. },
  125. methods: {
  126. spreadBanner() {
  127. spreadBanner({
  128. type: 2
  129. }).then(res => {
  130. console.log(res);
  131. })
  132. },
  133. uniCopy(content) {
  134. /**
  135. * 小程序端 和 app端的复制逻辑
  136. */
  137. //#ifndef H5
  138. uni.setClipboardData({
  139. data: content,
  140. success: function() {
  141. console.log('success');
  142. return true;
  143. }
  144. });
  145. //#endif
  146. /**
  147. * H5端的复制逻辑
  148. */
  149. // #ifdef H5
  150. if (!document.queryCommandSupported('copy')) {
  151. //为了兼容有些浏览器 queryCommandSupported 的判断
  152. // 不支持
  153. return false;
  154. }
  155. let textarea = document.createElement('textarea');
  156. textarea.value = content;
  157. textarea.readOnly = 'readOnly';
  158. document.body.appendChild(textarea);
  159. textarea.select(); // 选择对象
  160. textarea.setSelectionRange(0, content.length); //核心
  161. let result = document.execCommand('copy'); // 执行浏览器复制命令
  162. textarea.remove();
  163. return result;
  164. // #endif
  165. },
  166. comfirm() {
  167. const result = this.uniCopy(this.val);
  168. if (result === false) {
  169. uni.showToast({
  170. title: '不支持'
  171. });
  172. } else {
  173. uni.showToast({
  174. title: '复制成功',
  175. icon: 'none'
  176. });
  177. }
  178. },
  179. qrR(res) {
  180. this.erweimasrc = res
  181. console.log(res, 'erweima');
  182. //
  183. spreadBanner({
  184. type: 2
  185. }).then(res => {
  186. this.createPoster(res.data[0].pic)
  187. console.log(res.data[0].pic,'res.data[0].pic')
  188. })
  189. },
  190. async createPoster(goodimg) {
  191. let that = this
  192. // 获取设备信息,主要获取宽度,赋值给canvasW 也就是宽度:100%
  193. this.SystemInfo = await this.getSystemInfo();
  194. // 获取商品主图,二维码信息,APP端会返回图片的本地路径(H5端只能返回原路径)
  195. this.goodsImg = await this.getImageInfo(goodimg);
  196. this.ewmImg = await this.getImageInfo(this.erweimasrc);
  197. // this.canvasW = this.SystemInfo.windowWidth; // 画布宽度
  198. this.ratio = this.SystemInfo.windowWidth / 750;
  199. this.canvasW = 600 * this.ratio;
  200. this.canvasH = 1066 * this.ratio;
  201. // #ifdef APP-PLUS
  202. this.ewmW = 250 * this.ratio;
  203. // #endif
  204. // #ifdef H5
  205. this.ewmW = 250 * this.ratio;
  206. // #endif
  207. console.log(this.canvasH, 'this.canvasH')
  208. // this.canvasH = this.goodsImg.height + this.ewmW + 200; // 画布高度 = 主图高度+二维码高度 + 文字图片的间距(大概50)
  209. // 如果主图,二维码图片,设备信息都获取成功,开始绘制海报,这里需要用setTimeout延时绘制,否则可能会出现图片不显示。
  210. if (this.goodsImg.errMsg == 'getImageInfo:ok' && this.ewmImg.errMsg == 'getImageInfo:ok' && this
  211. .SystemInfo.errMsg == 'getSystemInfo:ok') {
  212. console.log('ok')
  213. uni.showToast({
  214. icon: 'loading',
  215. mask: true,
  216. duration: 10000,
  217. title: '海报绘制中',
  218. });
  219. setTimeout(() => {
  220. var ctx = uni.createCanvasContext('myCanvas', this);
  221. // 填充背景色,白色
  222. ctx.setFillStyle('#fff'); // 默认白色
  223. ctx.fillRect(0, 0, this.canvasW, this.canvasH) // fillRect(x,y,宽度,高度)
  224. // 绘制商品主图,二维码
  225. ctx.drawImage(this.goodsImg.path, 0, 0, this.canvasW, this
  226. .canvasH) // drawImage(图片路径,x,y,绘制图像的宽度,绘制图像的高度)
  227. // #ifdef APP-PLUS
  228. ctx.drawImage(this.ewmImg.path, (this.canvasW / 2 - this.ewmW / 2),this.canvasH - 130*this.ratio - this.ewmW,
  229. this.ewmW, this.ewmW) // drawImage(图片路径,x,y,绘制图像的宽度,绘制图像的高度,二维码的宽,高)
  230. // #endif
  231. // #ifdef H5
  232. ctx.drawImage(this.ewmImg.path, (this.canvasW / 2 - this.ewmW / 2),this.canvasH - 280*this.ratio - this.ewmW,
  233. this.ewmW, this.ewmW) // drawImage(图片路径,x,y,绘制图像的宽度,绘制图像的高度,二维码的宽,高)
  234. // #endif
  235. // 3、绘制商品标题,多余文字自动换行
  236. ctx.setFontSize(24); // setFontSize() 设置字体字号
  237. ctx.setFillStyle('#fff'); // setFillStyle() 设置字体颜色
  238. ctx.textAlign = "center";
  239. // 邀请码
  240. ctx.fillText('邀请码:' + this.userInfo.uid,this.canvasW/2,this.canvasH -50*this.ratio)
  241. ctx.draw(false, (ret) => { // draw方法 把以上内容画到 canvas 中。
  242. console.log(ret)
  243. uni.showToast({
  244. icon: 'none',
  245. title: '生成成功!',
  246. });
  247. that.fina = true
  248. uni.canvasToTempFilePath({ // 保存canvas为图片
  249. canvasId: 'myCanvas',
  250. quality: 1,
  251. fileType: 'jpg',
  252. complete: function(res) {
  253. // 在H5平台下,tempFilePath 为 base64, // 图片提示跨域 H5保存base64失败,APP端正常输出临时路径
  254. console.log(res)
  255. that.canvasShow = false
  256. that.shareQrcodeUrl = res.tempFilePath
  257. that.$forceUpdate()
  258. setTimeout(function() {
  259. console.log(that.shareQrcodeUrl, that
  260. .canvasShow)
  261. }, 2000)
  262. },
  263. })
  264. });
  265. }, 1500)
  266. } else {
  267. console.log('err')
  268. }
  269. },
  270. // 获取图片信息
  271. getImageInfo(image) {
  272. return new Promise((req, rej) => {
  273. uni.getImageInfo({
  274. src: image,
  275. success: function(res) {
  276. req(res)
  277. },
  278. });
  279. })
  280. },
  281. // 获取设备信息
  282. getSystemInfo() {
  283. return new Promise((req, rej) => {
  284. uni.getSystemInfo({
  285. success: function(res) {
  286. req(res)
  287. }
  288. });
  289. })
  290. },
  291. //保存图片
  292. saveShareQrcode() {
  293. console.log(this.shareQrcodeUrl)
  294. uni.saveImageToPhotosAlbum({
  295. filePath: this.shareQrcodeUrl,
  296. success: (res) => {
  297. uni.showToast({
  298. icon: 'none',
  299. position: 'bottom',
  300. title: "成功保存到相册",
  301. });
  302. },
  303. fail: (err) => {
  304. //重新提示用户打开保存图片的授权
  305. if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
  306. uni.showModal({
  307. title: '提示',
  308. content: '需要您授权保存相册',
  309. showCancel: false,
  310. success(res) {
  311. if (res.confirm) {
  312. uni.openSetting({
  313. success(settingdata) {
  314. if (settingdata.authSetting[
  315. 'scope.writePhotosAlbum']) {
  316. uni.showModal({
  317. title: '提示',
  318. content: '获取权限成功,再次保存图片即可成功',
  319. showCancel: false,
  320. })
  321. } else {
  322. uni.showModal({
  323. title: '提示',
  324. content: '获取权限失败,无法保存到相册',
  325. showCancel: false
  326. })
  327. }
  328. }
  329. })
  330. }
  331. }
  332. })
  333. }
  334. },
  335. })
  336. }
  337. }
  338. }
  339. </script>
  340. <style lang="scss">
  341. .hb {
  342. display: block;
  343. margin: auto;
  344. }
  345. .qrimg {
  346. position: absolute;
  347. left: -9999rpx;
  348. top: -9999rpx;
  349. }
  350. button {
  351. height: 88upx;
  352. background-color: #feca00;
  353. color: #fff;
  354. border-radius: 44upx;
  355. text-align: center;
  356. line-height: 88upx;
  357. width: 60%;
  358. margin: 0 auto;
  359. margin-top: 30upx;
  360. }
  361. .btm-btn {
  362. position: fixed;
  363. bottom: 0;
  364. width: 750rpx;
  365. height: 200rpx;
  366. display: flex;
  367. background-color: #fff;
  368. border-radius: 20rpx 20rpx 0 0;
  369. .btn {
  370. width: 50%;
  371. display: flex;
  372. flex-direction: column;
  373. justify-content: center;
  374. align-items: center;
  375. font-size: 26rpx;
  376. image {
  377. width: 66rpx;
  378. height: 66rpx;
  379. margin-bottom: 20rpx;
  380. }
  381. }
  382. }
  383. .bottom-btn {
  384. // background-color: #fff;
  385. // display: block;
  386. display: flex;
  387. justify-content: center;
  388. align-items: center;
  389. width: 550rpx;
  390. height: 80rpx;
  391. background-color: $base-color;
  392. color: #fff;
  393. margin: 30rpx auto;
  394. text-align: center;
  395. border-radius: 40rpx;
  396. font-size: 28rpx;
  397. }
  398. </style>