xuxueyang
2024-07-15 f09d9ec905b6247b36a394158662fe90d02197a5
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import {
    mapState
} from 'vuex'
import store from "../store";
 
import environments from '@/environments'
 
const mixinsCommon = {
    computed: {
        ...mapState({
            hasLogin: state => {
                // console.log('computed hasLogin',state.hasLogin,state)
                return state.hasLogin || false
            },
            selftype: state => {
                return (state.currentInfo || {}).type || ''
            },
            currentInfo: state => {
                return state.currentInfo || {}
            },
            cache_address: state => {
                return state.cache_address || {}
            },
            cache_user: state => {
                return state.currentInfo || {}
            },
            sign: state => {
                return state.sign || {}
            },
            // currentEnId: state=>{
            //     // console.log('currentEnId',state.currentInfo.enId,state.currentInfo)
            //     return state.currentInfo.enId || ''
            // }
        }),
    },
    data() {
        return {
            list: [],
            query: {},
            page: {
                size: 10,
                current: 1,
                total: 0,
            },
            style: {
                'color': '#fff'
            },
            listApi: '',
            regUserName: '用户-' + this.getRandomName(Math.floor(Math.random() * (6 - 2) + 3)),
            regAvatarUrl: 'https://youzhen123.oss-cn-huhehaote.aliyuncs.com/WechatOwnerProperty/images/mrtx.png',
 
        }
    },
    methods: {
        onChooseavatar(e) {
            let self = this;
            let {
                avatarUrl
            } = e.detail;
            this.regAvatarUrl = avatarUrl
            console.log('avatarUrl', this.regAvatarUrl)
        },
        onBindblur(e) {
            this.regUserName = e.detail.value; // 获取微信昵称
        },
        onBindinput(e) {
            this.regUserName = e.detail.value; // 获取微信昵称
        },
        randomAccess(min, max) {
            return Math.floor(Math.random() * (min - max) + max)
        },
        getRandomName(NameLength) {
            let name = ""
            for (let i = 0; i < NameLength; i++) {
                let unicodeNum = ""
                unicodeNum = this.randomAccess(0x4e00, 0x9fa5).toString(16)
                name += this.decodeUnicode(unicodeNum)
            }
            return name
        },
        decodeUnicode(str) {
            //Unicode显示方式是\u4e00
            str = "\\u" + str
            str = str.replace(/\\/g, "%");
            //转换中文
            str = unescape(str);
            //将其他受影响的转换回原来
            str = str.replace(/%/g, "\\");
            return str;
        },
        async onWxCheckSubmit() {
            if (this.regUserName == '微信用户') {
                this.$message.showToast('昵称不合规,不可以叫做:微信用户哈~')
                return
            }
            //保存一下
            //先上传图片
            if (this.regAvatarUrl ===
                'https://youzhen123.oss-cn-huhehaote.aliyuncs.com/WechatOwnerProperty/images/mrtx.png') {
                var picture = this.regAvatarUrl
                //更新接口
                this.$message.showLoading()
                const {
                    code
                } = await this.$http.request('post', "/api/current/user/update", {
                    data: {
                        ...this.dto,
                        nickName: this.regUserName,
                        picture: picture
                    }
                })
 
                this.$message.hideLoading()
                this.$forceUpdate()
                if (code == 0) {
                    this.$refs.popup_info.close()
                    this.$message.showToast('设置成功')
                    this.currentInfo.picture = picture
                    this.currentInfo.nickName = this.regUserName
                } else {
 
                }
            } else {
                this.$http.upload(this.regAvatarUrl).then(async res => {
                    var picture = res.data && res.data.length > 0 && res.data[0].url || ''
                    //更新接口
                    this.$message.showLoading()
                    const {
                        code
                    } = await this.$http.request('post', "/api/current/user/update", {
                        data: {
                            ...this.currentInfo,
                            nickName: this.regUserName,
                            picture: picture
                        }
                    })
 
                    this.$message.hideLoading()
                    this.$forceUpdate()
                    if (code == 0) {
                        this.$refs.popup_info.close()
                        this.$message.showToast('设置成功')
                        this.currentInfo.picture = picture
                        this.currentInfo.nickName = this.regUserName
                    } else {
 
                    }
                })
            }
        },
 
 
        async getUserProfile(e) {
            this.$message.showLoading()
            const res = await wx.getUserProfile({
                desc: '用于完善会员资料',
            });
            console.log(res);
            this.$message.hideLoading()
            this.login_Wx(res.userInfo.avatarUrl, res.userInfo.nickName)
        },
        login_Wx(imgurl, nickname) {
            //获取openId
            // /wx/jscode2session
            let inviter = this.$storage.getItem('inviter') || ''
            let inviterName = this.$storage.getItem('inviterName') || ''
            //有效24小时
            if (inviter) {
                var inviterTime = this.$storage.getItem('inviterTime')
                if (new Date().getTime() > parseInt(inviterTime) + 1000 * 3600 * 24) {
                    inviter = ''
                    inviterName = ''
                    this.$storage.removeItem('inviter')
                    this.$storage.removeItem('inviterTime')
                    this.$storage.removeItem('inviterName')
                }
            }
 
 
            const tmp = this;
            this.$message.showLoading()
            this.openid = this.$storage.getItem('openid');
            this.tel = this.$storage.getItem('tel');
            console.log('info,info,info,info,info,info', this.openid, this.tel);
            if (!!this.openid && !!this.tel) {
                // this.login = true;
            } else {
                // this.login = false;
                console.log('wx.login');
                wx.login({
                    success: async res => {
                        if (res.code) {
                            //微信登录成功 已拿到code
                            // tmp.login = true;
                            // tmp.jsCode = res.code; //保存获取到的code
                            console.log('abcd', res.code);
                            // this.$http.request('post',`/api/login/wechat`,{data:{code:tmp.jsCode}})
                            this.$storage.setItem('jsCode', res.code);
                            const res2 = await wx.getUserInfo();
                            console.log('res', res2);
                            this.tcode = res.code || ''
                            // if (nickname == '微信用户' || true) {
                            //     //跳出弹出框,获取具体用户信息
                            //     tmp.$message.hideLoading()
 
                            //     this.$refs.popup_info.open()
 
                            // } else {
                            await this.$store.dispatch('loginwx', {
                                code: res.code,
                                imgurl: imgurl,
                                nickname: nickname,
                                inviter: inviter
                            });
                            tmp.$message.hideLoading()
                            this.$forceUpdate()
                            //延迟,如果用户昵称是微信用户的话?
                            
                            setTimeout(() => {
                                if (tmp.currentInfo && tmp.currentInfo.id) {
                                    if (tmp.currentInfo.nickName == '微信用户') {
                                        tmp.$refs.popup_info && tmp.$refs.popup_info.open()
                                    }
                                }
                            }, 500)
                            // }
 
                        } else {
                            tmp.$message.hideLoading()
                            tmp.$message.showToast('微信登录失败');
 
                        }
                    },
                    error: res => {
                        console.log('wx.login error', res);
                        tmp.$message.hideLoading()
 
                        tmp.$message.showToast('微信授权失败');
                    }
                });
            }
        },
 
        previewImg(url) {
            if (url) {
                uni.previewImage({
                    urls: [url]
                })
            }
        },
        checkFormValues(dto, keys) {
            if (keys) {
                for (var key of keys) {
                    if (!dto[key]) {
                        return false
                    }
                }
            }
            return true
        },
        async refreshList(){
            this.page.current = 1
            await this.getList()
        },
        async getList(type='get') {
            if (this.listApi) {
                this.$message.showLoading()
                const {
                    data
                } = await this.$http.request(type, this.listApi, {
                    params: {
                        ...this.query,
                        ...this.page
                    },
                    data:{
                        ...this.query,
                        ...this.page
                    }
                })
                if (data) {
                    if (this.page.current == 1) {
                        this.list = data.records || []
                    } else {
                        //根据id去重正常
                        var ids = []
                        for (var item of this.list) {
                            ids.push(item.id)
                        }
                        for (var item of data.records) {
                            if (ids.indexOf(item.id) < 0) {
                                this.list.push(item)
                            }
                        }
 
                    }
                    this.page.total = data.total || 0
                }
                this.$message.hideLoading()
            }
 
        },
        async getMore(type='get') {
            if (this.page.total > this.page.current * this.page.size) {
                this.page.current += 1
                await this.getList(type)
            }
        },
        backHome() {
            uni.reLaunch({
                url: '/pages/home/home'
            })
        },
        goto(url, check = false) {
            if (check) {
                console.log('currentInfo', this.currentInfo)
                if (!this.currentInfo.id) {
                    this.$message.showToast('请先登陆')
                    return
                }
 
            }
            uni.navigateTo({
                url
            })
        },
        backpage() {
            // this.$router.go(-1)
            uni.navigateBack()
        }
        // #ifdef APP || H5
        ,
        async appdownload(url, name = '') {
                await this.$message.confirm('确定下载此文件吗')
                let _this = this
                if (url) {
                    // var url2 = environments.httpBaseUri + `/api/download/file?fileName=${name}&filePath=` + url
                    // uni.downloadFile({
                    //     url: url2,
                    //     success: (res) => {
                    //         if (res.statusCode === 200) {
                    //             _this.$message.showToast('下载成功')
                    //         } else {
                    //             console.log(res)
                    //             _this.$message.showToast('下载失败')
                    //         }
                    //     },
                    //     fail: (res) => {
                    //         console.log(res)
                    //         _this.$message.showToast('下载失败')
                    //     }
                    // });
                    _this.$message.showLoading()
 
                    uni.downloadFile({
                        url: url, //下载地址接口返回
                        success: (data) => {
                            _this.$message.hideLoading()
                            console.log('success', data)
                            if (data.statusCode === 200) {
                                //文件保存到本地
                                uni.saveFile({
                                    tempFilePath: data.tempFilePath, //临时路径
                                    success: function(res) {
                                        uni.showToast({
                                            icon: 'none',
                                            mask: true,
                                            title: '文件已保存:' + res
                                                .savedFilePath, //保存路径
                                            duration: 2000,
                                        });
                                        setTimeout(() => {
                                            //打开文档查看
                                            uni.openDocument({
                                                filePath: res.savedFilePath,
                                                success: function(res) {
                                                    // console.log('打开文档成功');
                                                }
                                            });
                                        }, 2000)
                                    },
                                    fail: (err) => {
 
                                        console.log(err);
                                        uni.showToast({
                                            icon: 'none',
                                            mask: true,
                                            title: '下载成功保存失败',
                                        });
                                    },
                                });
                            }
                        },
                        fail: (err) => {
                            _this.$message.hideLoading()
 
                            console.log(err);
                            uni.showToast({
                                icon: 'none',
                                mask: true,
                                title: '失败请重新下载',
                            });
                        },
                    });
                }
            }
            // #endif
            // #ifdef MP
            ,
        async wxdownload(url, name = '') {
            await this.$message.confirm('是否要下载此文件')
            var url2 = environments.httpBaseUri + `/api/download/file?fileName=${name}&filePath=` + url
 
            let that = this
            that.$message.showLoading()
            // wx.sa
            // wx.downloadFile({
            //     url: url2,
            //     success: res => {
            //         that.$message.showToast('下载成功')
 
            //     },
            //     fail: res => {
            //         that.$message.showToast('下载失败')
 
            //     },
            //     complete: res => {
            //         that.$message.hideLoading()
            //     }
            // })
            wx.downloadFile({
                url: url,
                timeout: 120000,
                success(res) {
                    that.$message.hideLoading()
                    console.log('res', res)
                    if (res.statusCode === 200) {
                        const filePath = res.tempFilePath
                        if (filePath.endsWith('.jpg') ||
                            filePath.endsWith('.jpeg') ||
                            filePath.endsWith('.png') ||
                            filePath.endsWith('.jpg')) {
                            wx.saveImageToPhotosAlbum({
                                filePath: filePath,
                                success: function(res) {
                                    that.$message.showToast('保存图片成功')
                                },
                                fail: function(res) {
                                    that.$message.showToast('打开文档失败,非支持的类型')
                                }
                            })
 
                        } else if (filePath.endsWith('.mp4') ||
                            filePath.endsWith('.flv') ||
                            filePath.endsWith('.avi') ||
                            filePath.endsWith('.wmv') ||
                            filePath.endsWith('.mpeg')) {
                            wx.saveVideoToPhotosAlbum({
                                filePath: filePath,
                                success: function(res) {
                                    that.$message.showToast('保存视频成功')
                                },
                                fail: function(res) {
                                    that.$message.showToast('打开文档失败,非支持的类型')
                                }
                            })
 
                        } else {
                            wx.openDocument({
                                filePath: filePath,
                                showMenu: true, //关键点
                                success: function(res) {
                                    console.log('打开文档成功')
                                },
                                fail: function(res) {
                                    that.$message.showToast('打开文档失败,非支持的类型')
                                }
                            })
                        }
                    } else {
                        that.$message.showToast('下载失败,服务器内部错误')
 
                    }
                },
                fail: res => {
                    that.$message.hideLoading()
                    that.$message.showToast('下载失败')
                },
                complete: res => {
 
                }
            })
        }
        // #endif
    }
}
export default mixinsCommon