陶杰
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<template>
    <!-- 联系我们 -->
    <!--  -->
    <view class="contact-container">
        <view class="container" @click="saveCode" v-if="!codeUrl">
            <canvas type="2d" id="myQrcode" class="code"></canvas>
            <view class="info">
                <view class="desc">
                    <p>{{name||'-'}}的推广二维码</p>
                    <p style="font-size: 600;color: #000;">点击保存二维码</p>
                </view>
            </view>
        </view>
        <view v-if="codeUrl" @click="saveImg">
            <image :src="codeUrl" class="code-img"></image>
            <view class="info">
                <view class="desc">
                    <p>{{name||'-'}}的推广二维码</p>
                    <p style="font-size: 600;color: #000;">点击保存二维码</p>
                </view>
            </view>
        </view>
    </view>
</template>
 
<script>
    import drawQrcode from '@/plugins/weapp.qrcode.esm.js'
 
    export default {
        data() {
            return {
                name: '',
                userId: '',
                codeUrl: '',
                canvas: undefined,
            }
        },
        // onShareAppMessage() {
        //     let params = `id=${this.info.Id}`
        //     if (this.isGroup) {
        //         params += `&groupId=${this.groupId}`
        //     }
        //     if (this.memberId) {
        //         params += `&inviteId=${this.memberId}`
        //     }
        //     let path = `/pages/home/product/detail?${params}`
        //     return {
        //         title: this.info.Name,
        //         path: path
        //     }
        // }
        onLoad(options) {
            if (!this.currentInfo.partnerDTO) {
                this.$message.showToast('请先完善合伙人信息')
                return
            }
            //有定制二维码,显示定制二维码
            this.codeUrl = this.currentInfo.partnerDTO.codeUrl || ''
            this.name = this.currentInfo.partnerDTO.name || '佚名'
            // this.userId = this.currentInfo.userId
            if (!this.codeUrl)
                this.initcode()
        },
        methods: {
            async saveImg() {
                await this.$message.confirm('确定保存此文件吗')
                let _this = this
                _this.$message.showLoading()
 
                uni.downloadFile({
                    url: _this.codeUrl, //下载地址接口返回
                    success: (data) => {
                        _this.$message.hideLoading()
                        console.log('success', data)
                        if (data.statusCode === 200) {
                            //文件保存到本地
                            uni.saveImageToPhotosAlbum({
                                filePath: data.tempFilePath,
                                success: function() {
                                    console.log('save success');
                                    uni.showToast({
                                        title: '保存成功'
                                    })
                                },
                                fail(res) {
                                    console.error(res)
                                    uni.showToast({
                                        title: '保存失败。',
                                        icon: 'error'
                                    })
                                }
                            })
 
                        }else{
                            uni.showToast({
                                title: '保存失败。.',
                                icon: 'error'
                            })
                        }
                    },
                    fail: (err) => {
                        _this.$message.hideLoading()
 
                        console.log(err);
                        uni.showToast({
                            icon: 'none',
                            mask: true,
                            title: '失败请重新下载',
                        });
                    },
                });
            },
            initcode() {
                let that = this
                const query = uni.createSelectorQuery()
                var qrUrl =
                    `https://scan.hmyxianhua.com/wx/?partnerUserId=${this.currentInfo.id||'-'}&partnerUserName=${this.name||'-'}`
                query.select('#myQrcode')
                    .fields({
                        node: true,
                        size: true
                    })
                    .exec((res) => {
                        var canvas = res[0].node
                        that.canvas = canvas
                        // 调用方法drawQrcode生成二维码
                        drawQrcode({
                            callback: function(e) {
                                console.log('drawQrcode callback', e)
                            },
                            canvas: canvas,
                            canvasId: 'myQrcode',
                            width: 480,
                            height: 480,
                            padding: 30,
                            background: '#ffffff',
                            foreground: '#000000',
                            // text: JSON.stringify({
                            //     'name': that.name,
                            //     'userId': that.currentInfo.id,
                            //     'version': '1.1.0'
                            // }),
                            text: qrUrl
                            // text: `https://www.hmyxianhua.com/wx-c-jump/sub_pages/customer/customer-info/customer-info?name=${that.name}&userId=${that.currentInfo.id}`
                            // JSON.stringify({
                            //     'name': that.name,
                            //     'userId': this.currentIn fo.id
                            // }),
                        })
                        // .catch(e) {
                        //     console.log('drawQrcode', e)
                        // }
                    })
            },
            saveCode() {
 
                // 获取临时路径(得到之后,想干嘛就干嘛了)
                uni.canvasToTempFilePath({
                    canvasId: 'myQrcode',
                    canvas: this.canvas,
                    x: 0,
                    y: 0,
                    width: 480,
                    height: 480,
                    destWidth: 260,
                    destHeight: 260,
                    success(res) {
                        console.log('二维码临时路径:', res.tempFilePath)
                        uni.saveImageToPhotosAlbum({
                            filePath: res.tempFilePath,
                            success: function() {
                                console.log('save success');
                                uni.showToast({
                                    title: '保存成功'
                                })
                            },
                            fail(res) {
                                // console.error(res)
                                uni.showToast({
                                    title: '保存失败。',
                                    icon: 'error'
                                })
                            }
                        })
                    },
                    fail(res) {
                        uni.showToast({
                            title: '保存失败',
                            icon: 'error'
                        })
                    }
                })
            }
        }
    }
</script>
 
<style lang="scss" scoped>
    .contact-container {
        position: relative;
 
        .code-img {
            width: 600rpx;
            min-height: 600rpx;
            display: inline-block;
            margin: 0 auto;
            margin-top: 100rpx;
        }
 
        .container {
            position: absolute;
            top: 148rpx;
            // width: 694rpx;
            left: 30rpx;
            right: 30rpx;
            min-height: 712rpx;
            // background: #FFFFFF;
            border-radius: 20rpx;
 
            .code {
                margin-left: auto;
                margin-right: auto;
                margin-top: 24rpx;
                width: 482rpx;
                height: 482rpx;
                margin-bottom: 32rpx;
                display: block;
            }
 
            .info {
                text-align: center;
 
                .desc {
                    font-weight: 400;
                    font-size: 28rpx;
                    color: #666666;
                    line-height: 40rpx;
                    font-style: normal;
                    margin-bottom: 126rpx;
 
                    p {
                        line-height: 48rpx;
                    }
                }
 
                .main {
                    font-weight: 600;
                    font-size: 48rpx;
                    color: #000000;
                    margin-top: 48rpx;
                    margin-bottom: 32rpx;
 
                }
            }
        }
 
        .top-image {
            width: 418rpx;
            height: 228rpx;
            position: absolute;
            top: 16rpx;
            left: 50%;
            transform: translateX(-50%);
 
        }
    }
</style>