index.vue 8.6 KB

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