zhangwei
2024-09-02 a2ddd3bec584b6eb91ab8845353fdb40c9b37ebe
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
<template>
    <view>
        <view class="wrap">
            <image src="@/static/logo.png" mode="widthFix" class="tip_img"></image>
            <view class="login_tip">
                为了提供更优质的服务,需要获取您的头像和昵称。
            </view>
            <view class="login_btn">
                <!-- #ifdef MP-WEIXIN || MP-QQ || MP-BAIDU -->
                <button v-if="canIUseGetPhoneNumber" class="uni-tip-button" style="backgroundColor:#fece01;borderColor:#fece01"
                    @click="mobileAuthLogin">授权并登录</button>
                <button style="backgroundColor:#fece01;borderColor:#fece01" v-else open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"
                    class="uni-tip-button">授权并登录</button>
                <view class="no_login" @click="back" style="color:#fece01;borderColor:#fece01">
                    暂不授权
                </view>
                <!-- #endif  -->
                <checkbox-group @change="changeAgree">
                    <view class="regisiter-agreement">
                        <label>
                            <checkbox value="1" :checked="isChecked" color="#fece01" style="transform:scale(0.7)" />
                            已阅读并同意以下协议
                            <text class="color-base-text" @click.stop="toArticle(1)">《用户服务协议》</text>
                            <!-- 和 <text class="color-base-text" @click.stop="toArticle(2)">《隐私政策》</text> -->
                        </label>
                    </view>
                </checkbox-group>
            </view>
        </view>
        <uni-popup ref="popup" type="center">
            <view class="bind-wrap">
                <!-- #ifdef MP-WEIXIN || MP-QQ || MP-BAIDU -->
                <view class="heard">提示</view>
                <view class="bind-tips">为了方便您接收通知消息,需要您完成授权</view>
                <view class="auth-login ns-btn-default-all color-base-bg" @click="toWeb">
                    去授权
                </view>
                <!-- <button open-type="getPhoneNumber" class="auth-login ns-btn-default-all color-base-bg"
                    @getphonenumber="mobileAuthLogin"><text>点击绑定手机号码</text></button> -->
                <!-- #endif  -->
            </view>
        </uni-popup>
        <uni-popup ref="bindMobile">
            <view class="bind-wrap">
                <!-- #ifdef MP-WEIXIN || MP-QQ || MP-BAIDU -->
                <view class="heard">提示</view>
                <view class="bind-tips">为了方便联系您,需要绑定您的手机号码</view>
                <button open-type="getPhoneNumber" class="auth-login ns-btn-default-all color-base-bg"
                    @getphonenumber="mobileAuthLogin"><text>点击绑定手机号码</text></button>
                <!-- #endif  -->
            </view>
        </uni-popup>
    </view>
</template>
 
<script>
    export default {
        data() {
            return {
                isChecked: false,
                isCheckedSan: false,
                canIUseGetPhoneNumber: false
            };
        },
        created() {
            // #ifdef MP-WEIXIN
            if (uni.getStorageSync('queryPhoneNumber')) this.canIUseGetPhoneNumber = true;
            // #endif
        },
        onShow() {
            if (uni.getStorageSync('wx_openid_set') && uni.getStorageSync('wx_openid_set') == 1) {
                uni.showLoading({
                    title: '加载中',
                    mask: true
                })
                this.$api.temLogin({
                    data: {
                        uid: uni.getStorageSync('authInfo').id
                    },
                    success: res => {
 
                        if (res.code == 1) {
                            // uni.hideLoading()
                            // if (res.data.openid) this.authInfo.weapp_openid = res.data.openid;
                            // if (res.data.unionid) this.authInfo.wx_unionid = res.data.unionid;
                            if (res.data.wx_openid) {
                                this.$util.showToast({
                                    title: "登录成功",
                                    icon: "success"
                                })
                                
                                uni.setStorage({
                                    key: 'uid',
                                    data: res.data.id,
                                    success: () => {
                                        uni.removeStorageSync('wx_openid_set');
                                        uni.removeStorageSync('source_member');
                                        uni.removeStorageSync('authInfo');
                                        this.$api.temLogin({
                                            data: {
                                                uid: res.data.id,
                                            },
                                            success: res => {
                                                if (res.code == 1) {
                                                    console.log(res)
                                                    uni.setStorageSync(
                                                        'is_perfect_user', '1')
                                                } else {
                                                    uni.setStorageSync(
                                                        'is_perfect_user', res.code
                                                    )
                                                }
                                            },
                                            fail: res => {
                                                this.$util.showToast({
                                                    title: "接口错误"
                                                });
                                            }
                                        });
                                        // this.$store.dispatch('getCartNumber');
                                        setTimeout(() => {
                                            this.$util.redirectTo(
                                                '/pages/index/index', {},
                                                'tabbar')
                                        }, 1000)
                                    }
                                });
                            } else {
                                this.$util.showToast({
                                    title: '授权失败'
                                });
                            }
                            if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
                        } else {
                            uni.removeStorageSync('uid')
                            this.$forceUpdate()
                            this.$util.showToast({
                                title: res.msg
                            });
                            if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
                        }
                    }
                })
            }
        },
        methods: {
            toArticle(type) {
                if(type==1){
                    uni.navigateTo({
                        url:'/pages/article/article'
                    })
                }else{
                    uni.navigateTo({
                        url:'/pages/article/privacy'
                    })
                }
                
            },
            changeAgree(e) {
                console.log(e)
                if (e.detail.value.length > 0) {
                    this.isChecked = true
                } else {
                    this.isChecked = false
                }
            },
            toWeb() {
                if (!this.isChecked) {
                    return this.$util.showToast({
                        title: "请先同意协议"
                    })
                }
                uni.setStorageSync('wx_openid_set', 1)
                this.$util.redirectTo('/otherPages/member/webView/webView', {
                    uid: uni.getStorageSync('authInfo').id
                })
            },
            back() {
                uni.navigateBack()
            },
            getPhoneNumber(e) {
                if (e.detail.errMsg == 'getPhoneNumber:ok') {
                    this.$db.set('queryPhoneNumber', 'getPhoneNumber:ok');
                    uni.login({
                        provider: 'weixin',
                        timeout: 3000,
                        success: res => {
                            let obj = {
                                bindgetphonenumber: e.detail.code,
                                jsCode: res.code
                            }
                            this.$api.CreateUser(obj).then(result => {
                                if (result.code == 1) {
                                    this.$db.set('userToken', result.data.accessToken);
                                    this.$api.getUser().then(res => {
                                        this.$db.set('userInfo', JSON.stringify(res.data))
                                    })
                                    this.$util.redirectTo(
                                        '/pages/index/index', {},
                                        'tabbar')
                                } else {
                                    this.$util.showToast({
                                        title: result.msg
                                    });
                                }
                            }).catch((err) => {
                                console.log(err, 'uni.loginerr')
                            })
                        },
                    })
                }
                console.log(e)
            },
            mobileAuthLogin(e) {
                if (!this.isChecked) {
                    return this.$common.showToast({
                        title: "请先同意协议"
                    })
                }
 
                uni.login({
                    provider: 'weixin',
                    timeout: 3000,
                    success: res => {
                        this.$api.temLogin(res.code).then((result) => {
                            if (result.code == 1) {
                                this.$api.getUser().then(res => {
                                    this.$db.set('userInfo', JSON.stringify(res.data))
                                })
                                this.$db.set('userToken', result.data.accessToken);
                                this.$util.redirectTo(
                                    '/pages/index/index', {},
                                    'tabbar')
                            } else {
                                this.$util.showToast({
                                    title: result.msg
                                });
                            }
                        }).catch((err) => {
                            console.log('err', err);
                        })
                    },
                })
            },
            getCode(userInfo, callback) {
                uni.login({
                    provider: 'weixin',
                    timeout: 3000,
                    success: res => {
                        console.log(res, 'getCode')
                        let data = {
                            code: res.code,
                            nickname: userInfo.nickName,
                            avatar: userInfo.avatarUrl,
 
                        }
                        // if (uni.getStorageSync('source_member')) {
                        //     data.source_member = uni.getStorageSync('source_member')
                        // }
                        // if (res.code) {
                        //     this.$api.temLogin(data)
                        // }
                    },
                    fail: () => {
                        this.$util.showToast({
                            title: '请求失败'
                        });
                    }
                })
            },
            login(e) {
                if (!this.isChecked) {
                    return this.$common.showToast({
                        title: "请先同意协议"
                    })
                }
                uni.login({
                    provider: 'weixin',
                    timeout: 3000,
                    success: res => {},
                })
            },
        }
    }
</script>
 
<style lang="scss" scoped>
    .wrap {
        background: linear-gradient(to bottom, #fbec99, #ffffff);
        display: flex;
        align-items: center;
        flex-direction: column;
        padding: 0 80rpx;
        height: 100vh;
 
        .login_tip {
            font-size: 28rpx;
            color: #333;
            padding: 40rpx 0;
            width: 400rpx;
            text-align: center;
        }
 
        .tip_img {
            width: 160rpx;
            margin-top: 300rpx;
        }
 
        .login_btn {
            width: 100%;
 
            .regisiter-agreement {
                text-align: center;
                margin-top: 30rpx;
                font-size: 28rpx;
                color: #838383;
                line-height: 60rpx;
 
                .color-base-text {
                    color: #fece01;
                }
            }
 
            .uni-tip-button {
                width: 100%;
                font-size: 28rpx;
                height: 70rpx;
                line-height: 70rpx;
                background-color: #35A6FF;
                border-radius: 70rpx;
                padding: 0;
            }
 
            .no_login {
                font-size: 28rpx;
                height: 70rpx;
                line-height: 70rpx;
                border: 2rpx solid #35A6FF;
                color: #35A6FF;
                border-radius: 70rpx;
                text-align: center;
                margin-top: 30rpx;
            }
        }
 
        .mobile_login {
            margin: 40rpx 0;
            font-size: 32rpx;
            color: #666;
            line-height: 1;
        }
 
        .agreement_wrap {
            margin-top: 40rpx;
 
            .agree_item {
                display: flex;
                margin-bottom: 30rpx;
 
                .item_icon {
                    width: 34rpx;
                    height: 34rpx;
                    margin-right: 10rpx;
                    flex-shrink: 0;
 
                    .item_select_img {
                        width: 100%;
                        height: 100%;
                    }
                }
 
                .item_content {
                    font-size: 28rpx;
                    line-height: 36rpx;
                    color: #999;
 
                    .agreement {
                        color: #35A6FF;
                    }
                }
            }
        }
    }
 
    .bind-wrap {
        width: 600rpx;
        background: #fff;
        box-sizing: border-box;
        border-radius: 20rpx;
        overflow: hidden;
 
        .heard {
            font-size: 40rpx;
            text-align: center;
            font-weight: 600;
            padding: 40rpx 0 0;
        }
 
        .footer {
            border-top: 1px solid #eee;
            display: flex;
 
            view {
                flex: 1;
                height: 100rpx;
                line-height: 100rpx;
                text-align: center;
 
                &:first-child {
                    font-size: 28rpx;
                    border-right: 1px solid #eee;
                }
            }
        }
 
        .bind-tips {
            color: #aaa;
            font-size: 28rpx;
            padding: 20rpx 50rpx;
            text-align: center;
        }
 
        .auth-login {
            width: 300rpx;
            margin: 20rpx auto 60rpx auto;
        }
 
        .bind-tip-icon {
            padding-top: 80rpx;
            width: 100%;
            text-align: center;
 
            image {
                width: 300rpx;
            }
        }
    }
 
    .ns-btn-default-all {
        width: 100%;
        height: 70rpx;
        border-radius: 70rpx;
        text-align: center;
        line-height: 70rpx;
        font-size: 28rpx;
        color: #ffffff;
        background-color: #35A6FF;
    }
</style>