shareQrCode.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <template>
  2. <view class="content">
  3. <image src="../../static/img/sharebg.png" mode="" class="bg"></image>
  4. <!-- <view class="qrimg"> -->
  5. <!-- <tki-qrcode :key="'http://shop.zhengyjk1994.com/register?petent=' + userInfo.uid" cid="tki-qrcode-canvas"
  6. ref="qrcode"
  7. :val="'http://shop.zhengyjk1994.com/register?petent=' + userInfo.uid"
  8. :size="300" unit="upx" background="#ffffff" foreground="#333333" pdground="#333333" icon=""
  9. :iconSize="40" :lv="3" :onval="true" :loadMake="true" :usingComponents="true" /> -->
  10. <!-- </view> -->
  11. <tki-qrcode :key="baseURL + '/index?petent=' + userInfo.id" cid="tki-qrcode-canvas" ref="qrcode"
  12. :val="baseURL + '/index?petent=' + userInfo.id" :size="300" unit="upx" background="#ffffff"
  13. foreground="#333333" pdground="#333333" icon="" :iconSize="40" :lv="3" :onval="true" :loadMake="true"
  14. :usingComponents="true" />
  15. <!-- #ifndef MP-ALIPAY -->
  16. <!-- <canvas :class="{ qrimg: !loading }" @longtap="alertCanv" id="qrShareBox" canvas-id="qrShareBox" class="tki-qrcode-canvas" /> -->
  17. <!-- #endif -->
  18. <!-- #ifdef MP-ALIPAY -->
  19. <!-- <canvas :class="{ qrimg: !loading }" @longtap="alertCanv" id="qrShareBox" class="tki-qrcode-canvas" /> -->
  20. <!-- #endif -->
  21. <!-- <view :style="{ display: loading ? 'none' : 'block' }" class="tki-qrcode-canvas"><image :src="ctxSrc" mode="scaleToFill" class="tki-qrcode-canvas"></image></view>
  22. <view class="share-bottom flex" :class="{ 'action-share-bottom': !loading }" @click="loading ? showImg() : ''">
  23. <text>{{ loading ? '点击生成图片' : '长按二维码下载' }}</text>
  24. </view> -->
  25. </view>
  26. </template>
  27. <script>
  28. import tkiQrcode from '@/components/tki-qrcode/tki-qrcode.vue';
  29. import {
  30. spreadBanner
  31. } from '@/api/user.js';
  32. import {
  33. mapState,
  34. mapMutations
  35. } from 'vuex';
  36. export default {
  37. components: {
  38. tkiQrcode
  39. },
  40. computed: {
  41. ...mapState('user', ['userInfo', 'orderInfo', 'hasLogin']),
  42. ...mapState(['baseURL'])
  43. },
  44. data() {
  45. return {
  46. cid: 'tki-qrcode-canvas', //canvasId,页面存在多个二维码组件时需设置不同的ID
  47. size: 180, //生成的二维码大小
  48. unit: 'upx', //大小单位尺寸
  49. // show: true,//默认使用组件中的image标签显示二维码
  50. val: '', //要生成的内容
  51. background: '#ffffff', //二维码背景色
  52. foreground: '#333333', //二维码前景色
  53. pdground: '#333333', //二维码角标色
  54. icon: '', //二维码图标URL(必须是本地图片,网络图需要先下载至本地)
  55. iconSize: 40, //二维码图标大小
  56. lv: 3, //容错级别
  57. onval: true, //监听val值变化自动重新生成二维码
  58. loadMake: false, //组件初始化完成后自动生成二维码,val需要有值
  59. usingComponents: false, //是否使用了自定义组件模式(主要是为了修复非自定义组件模式时 v-if 无法生成二维码的问题)
  60. showLoading: true, //是否显示loading
  61. loadingText: '二维码生成中', //loading文字
  62. src: '', // 二维码生成后的图片地址或base64
  63. ratio: 1, //页面比例用于计算
  64. ctxSrc: '', //要显示的图片
  65. loading: true, //是否载入图片中
  66. canHeight: '', //画布高度
  67. canWeidth: '' //画布宽度
  68. };
  69. },
  70. onLoad() {
  71. // this.loadCodeList()
  72. },
  73. // onReady() {
  74. // let obj = this;
  75. // let query = uni.createSelectorQuery();
  76. // // 获取页面比例
  77. // query
  78. // .select('.content')
  79. // .fields(
  80. // {
  81. // size: true
  82. // },
  83. // e => {
  84. // // 保存比例
  85. // this.ratio = e.width / 750;
  86. // }
  87. // )
  88. // .exec();
  89. // // 获取画布宽高信息
  90. // query
  91. // .select('#qrShareBox')
  92. // .fields(
  93. // {
  94. // size: true
  95. // },
  96. // e => {
  97. // // 保存画布宽高信息
  98. // obj.canHeight = e.height;
  99. // obj.canWeidth = e.width;
  100. // }
  101. // )
  102. // .exec();
  103. // },
  104. methods: {
  105. loadCodeList() {
  106. // 加载二维码信息
  107. spreadBanner({
  108. // #ifdef H5
  109. type: 2,
  110. // #endif
  111. // #ifdef MP
  112. type: 1
  113. // #endif
  114. }).then(e => {
  115. // #ifdef MP
  116. // 保存二维码图片
  117. uni.downloadFile({
  118. url: e.data[0].wap_posterQr,
  119. success(res) {
  120. if (res.errMsg == 'downloadFile:ok') {
  121. obj.src = res.tempFilePath;
  122. // 生成画布
  123. obj.loadImg(obj.src);
  124. uni.hideLoading();
  125. }
  126. console.log(res);
  127. },
  128. fail(e) {
  129. console.log(e);
  130. }
  131. })
  132. // #endif
  133. // #ifdef H5
  134. // 保存二维码图片
  135. this.src = e.data[0].wap_posterQr;
  136. // 生成画布
  137. this.loadImg(e.data[0].wap_posterQr);
  138. // #endif
  139. }).catch((e) => {
  140. uni.showModal({
  141. title: '生成失败请刷新页面',
  142. showCancel: false
  143. });
  144. uni.hideLoading();
  145. });;
  146. },
  147. // 长按画布事件
  148. alertCanv() {
  149. uni.showModal({
  150. title: '请先点击生成图片再下载',
  151. showCancel: false
  152. });
  153. },
  154. // 生成图片
  155. showImg() {
  156. uni.showLoading({
  157. title: '图片生成中',
  158. mask: true
  159. });
  160. let obj = this;
  161. //因为和uni.showLoading载入效果冲突需要延迟执行生成图片方法
  162. setTimeout(function() {
  163. uni.canvasToTempFilePath({
  164. x: 0,
  165. y: 0,
  166. width: obj.canWeidth,
  167. height: obj.canHeight,
  168. destWidth: obj.canWeidth,
  169. destHeight: obj.canHeight,
  170. fileType: 'jpg',
  171. quality: 1,
  172. canvasId: 'qrShareBox',
  173. success: res => {
  174. uni.hideLoading();
  175. uni.showModal({
  176. title: '创建成功,长按二维码下载图片',
  177. showCancel: false
  178. });
  179. // 显示生成的图片
  180. obj.loading = false;
  181. // 保存图片base64
  182. obj.ctxSrc = res.tempFilePath;
  183. },
  184. fail(e) {
  185. console.log(e);
  186. }
  187. });
  188. }, 50);
  189. },
  190. // 开始渲染画布
  191. loadImg(src) {
  192. const obj = this;
  193. const cavWidth = 523; //画布宽度
  194. const cavHeight = 700; //画布高度
  195. const ratio = obj.ratio; //获取页面比例
  196. const ctxBg = '/static/img/img14.jpg'; //画布背景
  197. let context = uni.createCanvasContext('qrShareBox');
  198. const codeSize = obj.size * ratio; //计算二维码大小
  199. const codeX = ((cavWidth - obj.size) * ratio) / 2; //二维码所在x轴位置
  200. const codeY = cavHeight * ratio; //二维码所在y轴位置
  201. const codeBoxColor = '#FFFFFF'; //包裹框颜色
  202. const codeBoxWidht = 0; //包裹边框宽度
  203. const codeBoxSize = (codeBoxWidht / 2) * ratio; //计算二维码白色包裹框大小
  204. const codeBoxX = codeX - codeBoxSize; //包裹框初始X轴
  205. const codeBoxY = codeY - codeBoxSize; //包裹框初始Y轴
  206. const codeBoxEnd = codeSize + codeBoxWidht * ratio; //计算包裹框大小
  207. const fontTop = codeY + codeBoxEnd + (codeBoxWidht + 20) * this.ratio; //文字距离上边距高度
  208. const fontSize = 24 * ratio; //文字大小
  209. const fontText = ''; //文字内容
  210. const fontLeft = (codeSize - fontSize * fontText.length) / 2 + codeX; //文字左侧距离
  211. // 插入背景图片
  212. context.drawImage(ctxBg, 0, 0, obj.canWeidth, obj.canHeight);
  213. // // 插入文字
  214. context.setFontSize(fontSize);
  215. context.fillText(fontText, fontLeft, fontTop);
  216. // 插入边框
  217. context.beginPath(); //开始画线
  218. context.setLineJoin('round'); //边框类型
  219. context.setLineWidth(codeBoxWidht * ratio);
  220. context.setStrokeStyle(codeBoxColor); //设置包裹框颜色
  221. context.strokeRect(codeBoxX, codeBoxY, codeBoxEnd, codeBoxEnd);
  222. context.stroke(); //渲染线条
  223. // 插入二维码
  224. context.drawImage(src, codeX, codeY, codeSize, codeSize);
  225. // 开始渲染
  226. context.draw();
  227. },
  228. // 創建二维码
  229. creatQrcode() {
  230. this.$refs.qrcode._makeCode();
  231. },
  232. // 保存二维码到图库
  233. saveQrcode() {
  234. this.$refs.qrcode._saveCode();
  235. },
  236. // 生成二维码后返回base64
  237. qrR(res) {
  238. this.src = res;
  239. },
  240. //清空二维码(清空二维码会触发result回调 返回值为空)
  241. clearQrcode(e) {
  242. this.$refs.qrcode._clearCode();
  243. this.val = '';
  244. }
  245. }
  246. };
  247. </script>
  248. <style lang="scss">
  249. .content {
  250. // padding-top: 30rpx;
  251. height: calc(100vh - 44px);
  252. position: relative;
  253. }
  254. .bg {
  255. position: relative;
  256. height: calc(100vh - 44px);
  257. width: 100%;
  258. }
  259. // #qrShareBox {
  260. // position: absolute;
  261. // left: -9999rpx;
  262. // top: -9999rpx;
  263. // }
  264. .qrimg {
  265. position: absolute;
  266. left: -9999rpx;
  267. top: -9999rpx;
  268. }
  269. .tki-qrcode-canvas {
  270. // width: 700rpx;
  271. // height: 1245rpx;
  272. width: 532rpx;
  273. height: 945rpx;
  274. margin: 0 auto;
  275. }
  276. .share-bottom {
  277. width: 560rpx;
  278. height: 80rpx;
  279. color: #ffffff;
  280. background-color: $base-color;
  281. margin: 0 auto;
  282. font-size: $font-lg - 2rpx;
  283. margin-top: 30rpx;
  284. border-radius: 99rpx;
  285. justify-content: center;
  286. &.action-share-bottom {
  287. background-color: $color-gray;
  288. }
  289. }
  290. .tki-qrcode {
  291. width: 300rpx;
  292. height: 300rpx;
  293. position: fixed;
  294. bottom: 10vh;
  295. right: 0;
  296. left: 0;
  297. margin: auto;
  298. }
  299. </style>