shareQrCode.vue 10.0 KB

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