shareQrCode.vue 12 KB

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