shareQrCode.vue 9.2 KB

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