shareQrCode.vue 11 KB

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