zhangzeli
2021-10-22 ac82481de1ed2421eddbbffc51e4ac6c34ccf61f
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
<template>
  <div class="reset-wrap">
    <Row
      type="flex"
      justify="center"
      align="middle"
      @keydown.enter.native="submitReset"
      style="height: 100%"
    >
      <Col class="content">
        <div>
          <Header />
          <div class="reset">
            <Tabs value="1">
              <TabPane :label="label" name="1" :icon="icon"></TabPane>
            </Tabs>
            <Form ref="resetForm" :model="form" :rules="rules" class="form">
              <div v-if="type == '0'">
                <FormItem prop="mobile" :error="errorMobile">
                  <Input
                    v-model="form.mobile"
                    size="large"
                    clearable
                    placeholder="请输入注册或绑定手机号"
                  ></Input>
                </FormItem>
                <FormItem prop="code">
                  <Row type="flex" justify="space-between">
                    <Input
                      v-model="form.code"
                      size="large"
                      clearable
                      placeholder="请输入短信验证码"
                      :maxlength="10"
                      class="input-verify"
                    />
                    <CountDownButton
                      ref="countDown"
                      @on-click="showVaptcha"
                      :autoCountDown="false"
                      size="large"
                      :loading="sending"
                      :text="getSms"
                    />
                  </Row>
                </FormItem>
              </div>
              <div v-else>
                <FormItem prop="email" :error="errorEmail">
                  <Input
                    v-model="form.email"
                    size="large"
                    clearable
                    placeholder="请输入绑定邮箱"
                  ></Input>
                </FormItem>
                <FormItem prop="code">
                  <Row type="flex" justify="space-between">
                    <Input
                      v-model="form.code"
                      size="large"
                      clearable
                      placeholder="请输入您收到的邮件中的验证码"
                      :maxlength="10"
                      class="input-verify"
                    />
                    <CountDownButton
                      ref="countDownEmail"
                      @on-click="showVaptcha"
                      :autoCountDown="false"
                      size="large"
                      :loading="sending"
                      :text="getSms"
                    />
                  </Row>
                </FormItem>
              </div>
              <FormItem prop="password">
                <SetPassword
                  v-model="form.password"
                  size="large"
                  @on-change="changeInputPass"
                />
              </FormItem>
              <FormItem prop="confirmPass">
                <Input
                  type="password"
                  :maxlength="20"
                  v-model="form.confirmPass"
                  size="large"
                  clearable
                  placeholder="请再次输入确认密码"
                />
              </FormItem>
            </Form>
            <Button
              style="margin-bottom: 3vh"
              type="primary"
              size="large"
              :loading="loading"
              @click="submitReset"
              long
              >重置密码</Button
            >
            <Row type="flex">
              <a @click="$router.go(-1)" class="back">
                <Icon
                  type="md-arrow-round-back"
                  style="margin-right: 3px"
                />返回
              </a>
            </Row>
          </div>
        </div>
        <Footer />
      </Col>
      <LangSwitch />
    </Row>
  </div>
</template>
 
<script>
import {
  vaptchaID,
  vaptchaOffline,
  sendResetSms,
  resetByMobile,
  sendResetEmail,
  resetByEmail,
} from "@/api/index";
import { validateMobile, validatePassword } from "@/libs/validate";
import Header from "@/views/main-components/header";
import Footer from "@/views/main-components/footer";
import LangSwitch from "@/views/main-components/lang-switch";
import CountDownButton from "@/views/my-components/xboot/count-down-button";
import SetPassword from "@/views/my-components/xboot/set-password";
export default {
  components: {
    CountDownButton,
    LangSwitch,
    SetPassword,
    Header,
    Footer,
  },
  data() {
    const validateConfirmPass = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码长度不得小于6位"));
      } else if (value !== this.form.password) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      label: "使用手机号重置密码",
      icon: "md-phone-portrait",
      loading: false,
      type: "0",
      form: {
        mobile: "",
        email: "",
        token: "",
        password: "",
        passStrength: "",
      },
      sended: false,
      sending: false,
      getSms: "获取验证码",
      select: "86",
      errorMobile: "",
      errorEmail: "",
      rules: {
        mobile: [
          {
            required: true,
            message: "手机号不能为空",
            trigger: "change",
          },
          {
            validator: validateMobile,
            trigger: "change",
          },
        ],
        email: [
          {
            required: true,
            message: "请输入邮箱地址",
            trigger: "change",
          },
          {
            type: "email",
            message: "邮箱格式不正确",
            trigger: "change",
          },
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
            trigger: "change",
          },
          {
            validator: validatePassword,
            trigger: "change",
          },
        ],
        code: [
          {
            required: true,
            message: "验证码不能为空",
            trigger: "change",
          },
        ],
        confirmPass: [
          {
            required: true,
            message: "确认密码不能为空",
            trigger: "change",
          },
          {
            validator: validateConfirmPass,
            trigger: "change",
          },
        ],
      },
      vaptchaObject: null
    };
  },
  methods: {
    initVaptcha() {
      let that = this;
      vaptcha({
        //配置参数
        vid: vaptchaID, // 验证单元id
        type: "invisible", // 展现类型 隐藏式
        offline_server: vaptchaOffline, // 离线验证接口地址 可选但此处不能为空
      }).then(function (vaptchaObj) {
        that.vaptchaObject = vaptchaObj;
        vaptchaObj.listen("pass", function () {
          that.form.token = vaptchaObj.getToken();
          // 验证成功 发送验证码
          if (that.type == "0") {
            that.sendSmsCode();
          } else if (that.type == "1") {
            that.sendEmailCode();
          }
        });
      });
    },
    showVaptcha() {
      if (this.type == "0") {
        var reg = /^[1][3,4,5,6,7,8][0-9]{9}$/;
        if (!reg.test(this.form.mobile)) {
          this.errorMobile = "手机号格式错误";
          return;
        } else {
          this.errorMobile = "";
        }
      } else if (this.type == "1") {
        var reg = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
        if (!reg.test(this.form.email)) {
          this.errorEmail = "邮箱格式错误";
          return;
        } else {
          this.errorEmail = "";
        }
      }
      this.vaptchaObject.validate(); // 若没验证验证码 开始验证
    },
    sendSmsCode() {
      this.sending = true;
      this.getSms = "发送中";
      sendResetSms(this.form.mobile, this.form).then((res) => {
        this.getSms = "获取验证码";
        this.sending = false;
        this.vaptchaObject.reset();
        if (res.success) {
          this.$Message.success("发送短信验证码成功");
          // 开始倒计时
          this.$refs.countDown.startCountDown();
        }
      });
    },
    sendEmailCode() {
      this.sending = true;
      this.getSms = "发送中";
      sendResetEmail(this.form.email, this.form).then((res) => {
        this.getSms = "获取验证码";
        this.sending = false;
        this.vaptchaObject.reset();
        if (res.success) {
          this.$Message.success("发送邮件验证码成功,请注意查收");
          // 开始倒计时
          this.$refs.countDownEmail.startCountDown();
        }
      });
    },
    changeInputPass(v, grade, strength) {
      this.form.passStrength = strength;
    },
    submitReset() {
      this.$refs.resetForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.type == "0") {
            resetByMobile(this.form).then((res) => {
              this.loading = false;
              if (res.success) {
                this.$Message.success("重置密码成功,请牢记您的新密码");
                this.$router.push({
                  name: "login",
                });
              } else {
                this.vaptchaObject.reset();
              }
            });
          } else if (this.type == "1") {
            resetByEmail(this.form).then((res) => {
              this.loading = false;
              if (res.success) {
                this.$Message.success("重置密码成功,请牢记您的新密码");
                this.$router.push({
                  name: "login",
                });
              } else {
                this.vaptchaObject.reset();
              }
            });
          }
        }
      });
    },
  },
  mounted() {
    let type = this.$route.query.type;
    if (type == "1") {
      this.type = type;
      this.label = "使用邮箱重置密码";
      this.icon = "ios-mail-outline";
    }
    this.initVaptcha();
  },
};
</script>
 
<style lang="less">
@import "./reset.less";
</style>