shareQrCode.vue 8.4 KB

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