zhangzengfei
2021-06-04 570c4304b907150335f3f8e9d19c4bec6a425757
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
<template>
    <view class="content">
        <view class="reg-logo">
            <img src="../../static/img/logo.png" width="80px"></img>
        </view>
        <view class="input-group">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
                <el-form-item label="" prop="invitaionCode">
                    <el-input placeholder="请输入邀请码" prefix-icon="el-icon-user-solid" v-model="ruleForm.invitaionCode"></el-input>
                </el-form-item>
                <el-form-item label="" prop="nickName">
                    <el-input placeholder="请输入昵称" prefix-icon="el-icon-user" v-model="ruleForm.nickName"></el-input>
                </el-form-item>
                <el-form-item label="" prop="mobile">
                    <el-input placeholder="请输入注册手机号" prefix-icon="el-icon-mobile-phone" v-model="ruleForm.mobile"></el-input>
                </el-form-item>
                <el-form-item label="" prop="smsCode">
                    <el-input placeholder="请输入短信验证码" prefix-icon="el-icon-message" v-model="ruleForm.smsCode">
                        <el-button slot="suffix" type="primary" size="mini" plain @click="sendSmsCode()">{{codeDuration ? codeDuration + 's' : '发送验证码' }}</el-button>
                    </el-input>
                </el-form-item>
                <el-form-item label="" prop="password">
                    <el-input placeholder="请输入密码" prefix-icon="el-icon-lock" v-model="ruleForm.password"></el-input>
                </el-form-item>
                <el-form-item label="" prop="confirmPassword">
                    <el-input placeholder="请确认密码" prefix-icon="el-icon-lock" v-model="ruleForm.confirmPassword"></el-input>
                </el-form-item>
                <el-form-item label="">
                    <el-checkbox v-model="agreement">同意《用户服务协议》</el-checkbox>
                </el-form-item>
            </el-form>
        </view>
 
        <view class="btn-row action-row" style="text-align: center;">
            <button type="primary" class="primary-btn" @tap="register">注册</button>
            <!-- <el-button type="text"  @click="toLogin"></el-button> -->
            <navigator url="../login/login">已有账户, 立即登录</navigator>
        </view>
    </view>
</template>
 
<script>
    import {
        registerUser,
        sendCode
    } from "@/api/account.js"
    import {
        isPhone
    } from "@/common/validate.js"
 
    import mInput from '../../components/m-input.vue';
 
    export default {
        components: {
            mInput
        },
        data() {
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                codeDuration: 0,
                codeDuration: '',
                agreement: false,
                ruleForm: {
                    invitaionCode: '',
                    nickName: '',
                    mobile: '',
                    smsCode: '',
                    password: '',
                    confirmPassword: ''
                },
                rules: {
                    invitaionCode: [{
                            required: false,
                            message: '请输入邀请码',
                            trigger: 'change'
                        },
                        {
                            min: 6,
                            max: 6,
                            message: '邀请码格式不正确',
                            trigger: 'change'
                        }
                    ],
                    nickName: [{
                            required: true,
                            message: '请输入昵称',
                            trigger: 'change'
                        },
                        {
                            min: 4,
                            max: 100,
                            message: '昵称长度最少4个字符',
                            trigger: 'blur'
                        }
                    ],
                    mobile: [{
                        required: true,
                        validator: isPhone,
                        trigger: 'blur'
                    }],
                    smsCode: [{
                        required: true,
                        message: '输入验证码',
                        trigger: 'change'
                    }, ],
                    password: [{
                            required: true,
                            message: '密码不能为空',
                            trigger: 'blur'
                        },
                        {
                            min: 6,
                            max: 100,
                            message: '密码最短为 6 个字符',
                            trigger: 'blur'
                        }
                    ],
                    confirmPassword: [{
                        required: true,
                        validator: validatePass2,
                        trigger: 'blur'
                    }]
                }
            }
        },
        methods: {
            toLogin() {
                uni.reLaunch({
                    url: '../login/login'
                })
            },
            register() {
                this.$refs["ruleForm"].validate((valid) => {
                    if (!valid) {
                        return
                    }
 
                    const data = {
                        username: this.username,
                        password: this.password
                    }
 
                    registerUser(data).then(res => {
                        if (res && res.success) {
                            uni.showToast({
                                icon: "none",
                                title: '注册成功'
                            });
                            uni.setStorageSync('uni_id_token', res.data.token)
                            uni.setStorageSync('username', res.data.username)
 
                            setTimeout(() => {
                                uni.reLaunch({
                                    url: '../main/main',
                                });
                            }, 3000)
 
                        } else {
                            uni.showModal({
                                content: JSON.stringify(res.message),
                                showCancel: false
                            })
                        }
                    }).catch(err => {
                        uni.showModal({
                            content: err,
                            showCancel: false
                        })
                    })
                })
            },
            sendSmsCode() {
 
                if (this.codeDuration) {
                    uni.showToast({
                        title: `请在${this.codeDuration}秒后重试`,
                        icon: 'none'
                    })
                    return
                }
                if (!/^1\d{10}$/.test(this.ruleForm.mobile)) {
                    uni.showToast({
                        title: `手机号码填写错误`,
                        icon: 'none'
                    })
                    return
                }
                let data = {
                    mobile: this.ruleForm.mobile,
                    type: 'register'
                }
 
                sendCode(data).then(rsp => {
                    if (rsp && rsp.success) {
                        uni.showToast({
                            icon: 'none',
                            title: '验证码发送成功,请注意查收'
                        });
                        this.codeDuration = 60
                        this.codeInterVal = setInterval(() => {
                            this.codeDuration--
                            if (this.codeDuration === 0) {
                                if (this.codeInterVal) {
                                    clearInterval(this.codeInterVal)
                                    this.codeInterVal = null
                                }
                            }
                        }, 1000)
                    } else {
                        uni.showToast({
                            icon: 'none',
                            title: '验证码发送失败:' + rsp.message
                        });
                    }
                }).catch(err => {
                    uni.showToast({
                        icon: 'none',
                        title: '验证码发送失败:'
                    })
                })
            },
        }
    }
</script>
 
<style scoped>
    .reg-logo {
        display: flex;
        justify-content: center;
        margin: 1%;
    }
 
    .action-row {
        text-align: center;
    }
 
    .action-row {}
 
    navigator {
        color: #007aff;
        padding-top: 20px;
    }
</style>