陶杰
2024-08-22 4aa0bd47801606b4d0e0a6a2ed8fe92a7e5f2444
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<template>
    <view>
        <view class="button p-t-12 bg-white">
            <button :disabled="canvasImages == '' ? true : false" type="primary" @click="downloadImg">下载二维码</button>
            <button :disabled="canvasImages != '' ? false : true" @click="previewHandle">预览二维码</button>
            <!-- #ifdef MP-WEIXIN -->
            <!-- <button type="warn" :disabled="canvasImages != '' ? false : true" open-type="share">分享给朋友</button> -->
            <!-- #endif -->
        </view>
        <shareImages ref="canvas" :canvasWidth="canvasWidth" :canvasHeight="canvasHeight" :shareTitle="shareTitle"
            :goodsTitle="goodsTitle" :shareImage="shareImage" :goodsTitle2="goodsTitle2" :qrSize="qrSize" :qrUrl="qrUrl"
            @success="shareSuccess" :canvasID="canvasID">
        </shareImages>
    </view>
</template>
 
<script>
    import shareImages from '@/components/hj-placard/shareImages.vue'
    export default {
        components: {
            shareImages
        },
        data() {
            return {
                name: '',
                canvasID: "myQrcode",
                canvasImages: '',
                canvasWidth: 375, // 宽度
                canvasHeight: 650, // 高度
                shareTitle: '我是这张图片的标题', // 分享标题
                goodsTitle: '\n', // 商品宣传标题
                goodsTitle2: '',
                shareImage: 'https://hmy-flower.oss-cn-shanghai.aliyuncs.com/d4/d43cdefc7b8f4c3e91fb451a236a4435WechatIMG2882.jpg', // 背景图片
                qrSize: 100, // 二维码大小
                qrUrl: '', // 生成二维码的链接
            }
        },
        onLoad() {
            if (!this.currentInfo.partnerDTO) {
                this.$message.showToast('请先完善合伙人信息')
                return
            }
            this.name = this.currentInfo.partnerDTO.name || '佚名'
            this.qrUrl =
                `https://scan.hmyxianhua.com/wx/?partnerUserId=${this.currentInfo.id||'-'}&partnerUserName=${this.name||'-'}`
            this.goodsTitle = `${this.name||'-'}的推广二维码`
            this.goodsTitle2 = `扫码注册绑定合伙人`
            this.$message.showLoading()
            setTimeout(() => {
                this.createsShareImage()
            }, 500)
            setTimeout(() => {
                this.$message.hideLoading()
            }, 2000)
            
        },
        methods: {
            // 生成分享图片
            createsShareImage() {
                // console.log(this.$refs.canvas)
                this.$refs.canvas.canvasCreate();
            },
            // 预览图片
            previewHandle() {
                uni.previewImage({
                    urls: [this.canvasImages],
                });
            },
            // 回调图片地址
            shareSuccess(e) {
                // console.log('地址',e)
                this.canvasImages = e
            },
            downloadImg() {
                // this.$refs.canvas.downloadImg();
                uni.saveImageToPhotosAlbum({
                    filePath: this.canvasImages,
                    success: function() {
                        console.log('save success');
                        uni.showToast({
                            title: '保存成功'
                        })
                    },
                    fail(res) {
                        console.error(res)
                        uni.showToast({
                            title: '保存失败。',
                            icon: 'error'
                        })
                    }
                })
 
 
            },
            // 分享
            onShareAppMessage(res) {
                // if (res.from === 'button') {
                //     console.log(res.target)
                // }
                return {
                    title: `${this.name||'-'}的推广二维码`,
                    path: this.canvasImages
                }
            }
        }
    }
</script>
 
<style scoped>
    .button {
        display: flex;
    }
</style>