shareQrCode.vue 12 KB

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