shareQrCode.vue 11 KB

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