shareQrCode.vue 10 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. // #ifdef APP-PLUS
  83. this.val = 'http://hgd.liuniu946.com/appdom/index.html?spread=' + this.userInfo.uid
  84. // #endif
  85. // #ifdef H5
  86. this.val = 'http://hgd.liuniu946.com/index/#/pages/index/index?spread=' + this.userInfo.uid
  87. // #endif
  88. }
  89. },
  90. methods: {
  91. uniCopy(content) {
  92. /**
  93. * 小程序端 和 app端的复制逻辑
  94. */
  95. //#ifndef H5
  96. uni.setClipboardData({
  97. data: content,
  98. success: function() {
  99. console.log('success');
  100. return true;
  101. }
  102. });
  103. //#endif
  104. /**
  105. * H5端的复制逻辑
  106. */
  107. // #ifdef H5
  108. if (!document.queryCommandSupported('copy')) {
  109. //为了兼容有些浏览器 queryCommandSupported 的判断
  110. // 不支持
  111. return false;
  112. }
  113. let textarea = document.createElement('textarea');
  114. textarea.value = content;
  115. textarea.readOnly = 'readOnly';
  116. document.body.appendChild(textarea);
  117. textarea.select(); // 选择对象
  118. textarea.setSelectionRange(0, content.length); //核心
  119. let result = document.execCommand('copy'); // 执行浏览器复制命令
  120. textarea.remove();
  121. return result;
  122. // #endif
  123. },
  124. comfirm() {
  125. const result = this.uniCopy(this.val);
  126. if (result === false) {
  127. uni.showToast({
  128. title: '不支持'
  129. });
  130. } else {
  131. uni.showToast({
  132. title: '复制成功',
  133. icon: 'none'
  134. });
  135. }
  136. },
  137. qrR(res) {
  138. this.erweimasrc = res
  139. console.log(res, 'erweima');
  140. this.createPoster()
  141. },
  142. async createPoster() {
  143. let that = this
  144. // 获取设备信息,主要获取宽度,赋值给canvasW 也就是宽度:100%
  145. this.SystemInfo = await this.getSystemInfo();
  146. // 获取商品主图,二维码信息,APP端会返回图片的本地路径(H5端只能返回原路径)
  147. this.goodsImg = await this.getImageInfo('/static/img/sharebase.png');
  148. this.ewmImg = await this.getImageInfo(this.erweimasrc);
  149. // this.canvasW = this.SystemInfo.windowWidth; // 画布宽度
  150. this.ratio = this.SystemInfo.windowWidth / 750;
  151. this.canvasW = 600 * this.ratio;
  152. this.canvasH = 1066 * this.ratio;
  153. // #ifdef APP-PLUS
  154. this.ewmW = 350 * this.ratio;
  155. // #endif
  156. // #ifdef H5
  157. this.ewmW = 250 * this.ratio;
  158. // #endif
  159. console.log(this.canvasH, 'this.canvasH')
  160. // this.canvasH = this.goodsImg.height + this.ewmW + 200; // 画布高度 = 主图高度+二维码高度 + 文字图片的间距(大概50)
  161. // 如果主图,二维码图片,设备信息都获取成功,开始绘制海报,这里需要用setTimeout延时绘制,否则可能会出现图片不显示。
  162. if (this.goodsImg.errMsg == 'getImageInfo:ok' && this.ewmImg.errMsg == 'getImageInfo:ok' && this
  163. .SystemInfo.errMsg == 'getSystemInfo:ok') {
  164. console.log('ok')
  165. uni.showToast({
  166. icon: 'loading',
  167. mask: true,
  168. duration: 10000,
  169. title: '海报绘制中',
  170. });
  171. setTimeout(() => {
  172. var ctx = uni.createCanvasContext('myCanvas', this);
  173. // 填充背景色,白色
  174. ctx.setFillStyle('#fff'); // 默认白色
  175. ctx.fillRect(0, 0, this.canvasW, this.canvasH) // fillRect(x,y,宽度,高度)
  176. // 绘制商品主图,二维码
  177. ctx.drawImage(this.goodsImg.path, 0, 0, this.canvasW, this
  178. .canvasH) // drawImage(图片路径,x,y,绘制图像的宽度,绘制图像的高度)
  179. // #ifdef APP-PLUS
  180. ctx.drawImage(this.ewmImg.path, (this.canvasW / 2 - this.ewmW / 2),this.canvasH - 85*this.ratio - this.ewmW,
  181. this.ewmW, this.ewmW) // drawImage(图片路径,x,y,绘制图像的宽度,绘制图像的高度,二维码的宽,高)
  182. // #endif
  183. // #ifdef H5
  184. ctx.drawImage(this.ewmImg.path, (this.canvasW / 2 - this.ewmW / 2),this.canvasH - 85*this.ratio - this.ewmW,
  185. this.ewmW, this.ewmW) // drawImage(图片路径,x,y,绘制图像的宽度,绘制图像的高度,二维码的宽,高)
  186. // #endif
  187. // 3、绘制商品标题,多余文字自动换行
  188. ctx.setFontSize(16); // setFontSize() 设置字体字号
  189. ctx.setFillStyle('#333'); // setFillStyle() 设置字体颜色
  190. ctx.draw(false, (ret) => { // draw方法 把以上内容画到 canvas 中。
  191. console.log(ret)
  192. uni.showToast({
  193. icon: 'none',
  194. title: '生成成功!',
  195. });
  196. that.fina = true
  197. uni.canvasToTempFilePath({ // 保存canvas为图片
  198. canvasId: 'myCanvas',
  199. quality: 1,
  200. fileType: 'jpg',
  201. complete: function(res) {
  202. // 在H5平台下,tempFilePath 为 base64, // 图片提示跨域 H5保存base64失败,APP端正常输出临时路径
  203. console.log(res)
  204. that.canvasShow = false
  205. that.shareQrcodeUrl = res.tempFilePath
  206. that.$forceUpdate()
  207. setTimeout(function() {
  208. console.log(that.shareQrcodeUrl, that
  209. .canvasShow)
  210. }, 2000)
  211. },
  212. })
  213. });
  214. }, 1500)
  215. } else {
  216. console.log('err')
  217. }
  218. },
  219. // 获取图片信息
  220. getImageInfo(image) {
  221. return new Promise((req, rej) => {
  222. uni.getImageInfo({
  223. src: image,
  224. success: function(res) {
  225. req(res)
  226. },
  227. });
  228. })
  229. },
  230. // 获取设备信息
  231. getSystemInfo() {
  232. return new Promise((req, rej) => {
  233. uni.getSystemInfo({
  234. success: function(res) {
  235. req(res)
  236. }
  237. });
  238. })
  239. },
  240. //保存图片
  241. saveShareQrcode() {
  242. console.log(this.shareQrcodeUrl)
  243. uni.saveImageToPhotosAlbum({
  244. filePath: this.shareQrcodeUrl,
  245. success: (res) => {
  246. uni.showToast({
  247. icon: 'none',
  248. position: 'bottom',
  249. title: "成功保存到相册",
  250. });
  251. },
  252. fail: (err) => {
  253. //重新提示用户打开保存图片的授权
  254. if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
  255. uni.showModal({
  256. title: '提示',
  257. content: '需要您授权保存相册',
  258. showCancel: false,
  259. success(res) {
  260. if (res.confirm) {
  261. uni.openSetting({
  262. success(settingdata) {
  263. if (settingdata.authSetting[
  264. 'scope.writePhotosAlbum']) {
  265. uni.showModal({
  266. title: '提示',
  267. content: '获取权限成功,再次保存图片即可成功',
  268. showCancel: false,
  269. })
  270. } else {
  271. uni.showModal({
  272. title: '提示',
  273. content: '获取权限失败,无法保存到相册',
  274. showCancel: false
  275. })
  276. }
  277. }
  278. })
  279. }
  280. }
  281. })
  282. }
  283. },
  284. })
  285. }
  286. }
  287. }
  288. </script>
  289. <style lang="scss">
  290. .hb {
  291. margin: auto;
  292. }
  293. .qrimg {
  294. position: absolute;
  295. left: -9999rpx;
  296. top: -9999rpx;
  297. }
  298. button {
  299. height: 88upx;
  300. background-color: #feca00;
  301. color: #fff;
  302. border-radius: 44upx;
  303. text-align: center;
  304. line-height: 88upx;
  305. width: 60%;
  306. margin: 0 auto;
  307. margin-top: 30upx;
  308. }
  309. .btm-btn {
  310. position: fixed;
  311. bottom: 0;
  312. width: 750rpx;
  313. height: 200rpx;
  314. display: flex;
  315. background-color: #fff;
  316. border-radius: 20rpx 20rpx 0 0;
  317. .btn {
  318. width: 50%;
  319. display: flex;
  320. flex-direction: column;
  321. justify-content: center;
  322. align-items: center;
  323. font-size: 26rpx;
  324. image {
  325. width: 66rpx;
  326. height: 66rpx;
  327. margin-bottom: 20rpx;
  328. }
  329. }
  330. }
  331. </style>