123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <template>
- <view class="content">
-
-
- <canvas :class="{ qrimg: !loading }" @longtap="alertCanv" id="qrShareBox" canvas-id="qrShareBox" class="tki-qrcode-canvas" />
-
-
- <canvas :class="{ qrimg: !loading }" @longtap="alertCanv" id="qrShareBox" class="tki-qrcode-canvas" />
-
- <view :style="{ display: loading ? 'none' : 'block' }" class="tki-qrcode-canvas"><image :src="ctxSrc" mode="scaleToFill" class="tki-qrcode-canvas"></image></view>
- <view class="share-bottom flex" :class="{ 'action-share-bottom': !loading }" @click="loading ? showImg() : ''">
- <text>{{ loading ? '点击生成图片' : '长按二维码下载' }}</text>
- </view>
- </view>
- </template>
- <script>
- import { spreadBanner } from '@/api/user.js';
- export default {
-
- data() {
- return {
- cid: 'tki-qrcode-canvas',
- size: 180,
- unit: 'upx',
-
- val: '',
- background: '#ffffff',
- foreground: '#333333',
- pdground: '#333333',
- icon: '',
- iconSize: 40,
- lv: 3,
- onval: false,
- loadMake: false,
- usingComponents: false,
- showLoading: false,
- loadingText: '二维码生成中',
- src: '',
- ratio: 1,
- ctxSrc: '',
- loading: true,
- canHeight: '',
- canWeidth: ''
- };
- },
- onLoad() {
- this.loadCodeList()
- },
- onReady() {
- let obj = this;
- let query = uni.createSelectorQuery();
-
- query
- .select('.content')
- .fields(
- {
- size: true
- },
- e => {
-
- this.ratio = e.width / 750;
- }
- )
- .exec();
-
- query
- .select('#qrShareBox')
- .fields(
- {
- size: true
- },
- e => {
-
- obj.canHeight = e.height;
- obj.canWeidth = e.width;
- }
- )
- .exec();
- },
- methods: {
- loadCodeList() {
-
- spreadBanner({
-
- type: 2,
-
-
- type: 1
-
- }).then(e => {
-
-
- uni.downloadFile({
- url: e.data[0].wap_posterQr,
- success(res) {
- if (res.errMsg == 'downloadFile:ok') {
- obj.src = res.tempFilePath;
-
- obj.loadImg(obj.src);
- uni.hideLoading();
- }
- console.log(res);
- },
- fail(e) {
- console.log(e);
- }
- })
-
-
-
- this.src = e.data[0].wap_posterQr;
-
- this.loadImg(e.data[0].wap_posterQr);
-
-
- }).catch((e) => {
- uni.showModal({
- title: '生成失败请刷新页面',
- showCancel: false
- });
- uni.hideLoading();
- });;
- },
-
- alertCanv() {
- uni.showModal({
- title: '请先点击生成图片再下载',
- showCancel: false
- });
- },
-
- showImg() {
- uni.showLoading({
- title: '图片生成中',
- mask: true
- });
- let obj = this;
-
- setTimeout(function() {
- uni.canvasToTempFilePath({
- x: 0,
- y: 0,
- width: obj.canWeidth,
- height: obj.canHeight,
- destWidth: obj.canWeidth,
- destHeight: obj.canHeight,
- fileType: 'jpg',
- quality: 1,
- canvasId: 'qrShareBox',
- success: res => {
- uni.hideLoading();
- uni.showModal({
- title: '创建成功,长按二维码下载图片',
- showCancel: false
- });
-
- obj.loading = false;
-
- obj.ctxSrc = res.tempFilePath;
- },
- fail(e) {
- console.log(e);
- }
- });
- }, 50);
- },
-
- loadImg(src) {
- const obj = this;
- const cavWidth=523;
- const cavHeight=700;
- const ratio = obj.ratio;
- const ctxBg = '/static/img/img14.jpg';
- let context = uni.createCanvasContext('qrShareBox');
- const codeSize = obj.size * ratio;
- const codeX = ((cavWidth - obj.size) * ratio) / 2;
- const codeY = cavHeight * ratio;
- const codeBoxColor = '#FFFFFF';
- const codeBoxWidht = 0;
- const codeBoxSize = (codeBoxWidht / 2) * ratio;
- const codeBoxX = codeX - codeBoxSize;
- const codeBoxY = codeY - codeBoxSize;
- const codeBoxEnd = codeSize + codeBoxWidht * ratio;
- const fontTop = codeY + codeBoxEnd + (codeBoxWidht + 20) * this.ratio;
- const fontSize = 24 * ratio;
- const fontText = '';
- const fontLeft = (codeSize - fontSize * fontText.length) / 2 + codeX;
-
- context.drawImage(ctxBg, 0, 0, obj.canWeidth, obj.canHeight);
-
- context.setFontSize(fontSize);
- context.fillText(fontText, fontLeft, fontTop);
-
- context.beginPath();
- context.setLineJoin('round');
- context.setLineWidth(codeBoxWidht * ratio);
- context.setStrokeStyle(codeBoxColor);
- context.strokeRect(codeBoxX, codeBoxY, codeBoxEnd, codeBoxEnd);
- context.stroke();
-
- context.drawImage(src, codeX, codeY, codeSize, codeSize);
-
- context.draw();
- },
-
- creatQrcode() {
- this.$refs.qrcode._makeCode();
- },
-
- saveQrcode() {
- this.$refs.qrcode._saveCode();
- },
-
- qrR(res) {
- this.src = res;
- },
-
- clearQrcode(e) {
- this.$refs.qrcode._clearCode();
- this.val = '';
- }
- }
- };
- </script>
- <style lang="scss">
- .content {
- padding-top: 30rpx;
- }
- // #qrShareBox {
- // position: absolute;
- // left: -9999rpx;
- // top: -9999rpx;
- // }
- .qrimg {
- position: absolute;
- left: -9999rpx;
- top: -9999rpx;
- }
- .tki-qrcode-canvas {
- // width: 700rpx;
- // height: 1245rpx;
- width: 532rpx;
- height: 945rpx;
- margin: 0 auto;
- }
- .share-bottom {
- width: 560rpx;
- height: 80rpx;
- color: #ffffff;
- background-color: $base-color;
- margin: 0 auto;
- font-size: $font-lg - 2rpx;
- margin-top: 30rpx;
- border-radius: 99rpx;
- justify-content: center;
- &.action-share-bottom {
- background-color: $color-gray;
- }
- }
- </style>
|