shareQrCode.vue 11 KB

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