shareQrCode.vue 10.0 KB

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