spring
4 天以前 435881d494e2be4ba5ce8bfccb02d6ef49e07314
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
<template>
    <view>
        <view class="list">
            <view class="list-call">
                <u-icon class="u-icon" size="40" name="account"></u-icon>
                <input class="u-input" type="text" v-model="model.mobile" maxlength="32"
                    :placeholder="$t('login.placeholderMobile')" />
            </view>
            <view class="list-call">
                <u-icon class="u-icon" size="40" name="coupon"></u-icon>
                <input class="u-input" type="text" v-model="model.code" maxlength="4" placeholder="手机验证码" />
                <u-button :disabled="smsCodeBtnDisabled" @click="getSmsCode">
                    <text v-if="!smsCodeBtnDisabled">获取验证码</text>
                    <text v-else>{{
                      `重新发送 (${codeSeconds})`
                    }}
                    </text>
                </u-button>
 
            </view>
            <view class="button" hover-class="button-hover" @click="toLogin">
                <text>绑定用户</text>
            </view>
        </view>
    </view>
</template>
 
<script>
    import moment from '@/common/moment';
 
    export default {
        name: 'smsCode',
        data() {
            return {
                smsCodeBtnDisabled: false, //验证码按钮
                codeSeconds: 0, // 验证码发送时间间隔
                model: {
                    mobile: '',
                    code: ''
                }, //绑定的手机号、验证码数据
            };
        },
        onLoad() {
            //验证码发送间隔
            const time = moment().valueOf() / 1000 - uni.getStorageSync('loginSmsCodeTime');
 
            //是否允许重新发送验证码
            if (time < 60) {
                this.codeSeconds = 60 - parseInt(time, 10);
                this.handleSmsCodeTime(this.codeSeconds);
            } else {
                this.codeSeconds = 60
                this.smsCodeBtnDisabled = false;
                uni.removeStorageSync('loginSmsCodeTime');
            }
        },
        methods: {
            // 发送验证码并进入倒计时
            getSmsCode() {
                //表单校验
                if (this.model.mobile.length === 0) {
                    this.$u.toast("请输入手机号");
                    return;
                }
 
                this.$u.api.sendCode({
                    mobile: this.model.mobile
                }, {
                    'Authorization': 'Basic c29jaWFsOnNvY2lhbA=='
                }).then(r => {
                    if (r.data) {
                        this.$u.toast(`验证码发送成功!`);
                        this.smsCodeBtnDisabled = true;
                        uni.setStorageSync('loginSmsCodeTime', moment().valueOf() / 1000);
                        this.handleSmsCodeTime(59);
                        console.log(`验证码是${r.msg}`)
                    } else {
                        this.$u.toast(r.msg === '手机号未注册' ? '手机号未绑定账号' : r.msg);
                    }
                })
            },
            handleSmsCodeTime(time) {
                let timer = setInterval(() => {
                    if (time === 0) {
                        clearInterval(timer);
                        this.smsCodeBtnDisabled = false;
                    } else {
                        this.codeSeconds = time;
                        this.smsCodeBtnDisabled = true;
                        time--;
                    }
                }, 1000);
            },
            // 提交表单
            async toLogin() {
                //表单校验
                if (this.model.mobile.length === 0) {
                    this.$u.toast('请输入手机号');
                    return;
                }
                if (this.model.code.length === 0) {
                    this.$u.toast('请输入验证码');
                    return;
                }
 
                //绑定用户信息
                await this.setUserBind()
            },
 
            // 微信绑定用户
            async setUserBind() {
                let ctx = this
                let mobile = this.model.mobile
                //获取code
                uni.login({
                    provider: 'weixin',
                    success: function(res) {
                        let code = res.code
                        console.log('vxCode:', code)
                        //手机号登录
                        ctx.$u.api.smsLogin({
                            mobile,
                            code
                        }, {
                            Authorization: 'Basic c29jaWFsOnNvY2lhbA=='
                        }).then(res => {
                            let token = res.access_token
                            console.log('手机登录成功:', res)
 
                            //储存用户信息
                            ctx.$u.vuex('vuex_token', res.access_token)
                            ctx.$u.vuex('vuex_refresh_token', res.refresh_token)
                            ctx.$u.vuex('vuex_username', res.username)
                            ctx.$u.vuex('vuex_userId', res.user_id)
                            ctx.$u.vuex('vuex_client_id', res.client_id)
 
                            //绑定用户
                            ctx.$u.api.bingUser({
                                code
                            }, {
                                'Authorization': 'Bearer ' + token
                            }).then(res => {
                                console.log('绑定用户成功')
                                uni.reLaunch({
                                    url: '/pages/sys/home/index'
                                });
                            })
                        })
                    }
                })
            }
 
        }
    };
</script>
 
 
<style lang="scss">
    @import 'index.scss';
 
    .logo {
        width: 260rpx;
        height: 260rpx;
        //background: rgba(59, 121, 235, 1);
        //box-shadow: 0rpx 5rpx 20rpx 5rpx rgba(45, 127, 235, 0.5);
        border-radius: 50%;
        margin: 70rpx auto 10rpx auto;
    }
 
    .logo image {
        width: 260rpx;
        height: 260rpx;
        border-radius: 50%;
    }
 
    .base-url js-select {
        width: 100%;
    }
 
    .button {
        margin: 30rpx auto 0;
    }
 
    .footer {
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
        text-align: center;
        color: #46afff;
        height: 40rpx;
        line-height: 40rpx;
        font-size: 35rpx;
        margin-top: 60rpx;
    }
 
    .footer text {
        font-size: 30rpx;
        margin-left: 25rpx;
        margin-right: 25rpx;
    }
 
    .oauth2 {
        display: flex;
        flex-direction: row;
        justify-content: space-around;
        margin: 55rpx 100rpx;
 
        image {
            height: 100rpx;
            width: 100rpx;
        }
    }
</style>