fuliqi
2024-01-24 29c1e7eb5ac16e90d8991a86c1c071bc312ec8d9
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
<template>
  <div>
    <el-dialog :visible.sync="dialogVisible" title="修改密码" :close-on-click-modal="false"
               :modal-append-to-body="false" v-if="dialogVisible" width="40%">
      <el-form size="mini" :model="form" label-width="120px" ref="form" :rules="formRules">
        <el-form-item label="账号:" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入账号" autocomplete="off"
                    @blur="blurUserName" @input="inputUserName">
          </el-input>
        </el-form-item>
        <el-form-item label="手机号码:" prop="phone" ref="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码" disabled autocomplete="off">
          </el-input>
        </el-form-item>
        <el-form-item label="验证码:" prop="verificationCode" ref="verificationCode">
          <el-input placeholder="请输入验证码" v-model="form.verificationCode" autocomplete="off">
            <template slot="append">
              <el-button type="text" :style="{color: btnColor,width:'140px'}" @click="getCode">
                {{btnTxt}}
              </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="新密码:" prop="newPassword">
          <el-input v-model="form.newPassword" type="password" placeholder="请输入新密码" clearable
                    autocomplete="new-password">
          </el-input>
        </el-form-item>
        <el-form-item label="确认密码:" prop="checkPassword">
          <el-input v-model="form.checkPassword" type="password" placeholder="请输入确认密码" clearable
                    autocomplete="new-password">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="submit" type="primary" :loading="loading">保存</el-button>
        <el-button size="mini" @click="dialogVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
 
<script>
import { mapState, mapMutations } from 'vuex'
import { validateUsername, validatePwd } from '@/utils/validator'
import ChangePwdApi from '@api/oauth/byPhoneUpdateInfo'
const sm2 = require('sm-crypto').sm2
export default {
  name: "changePasswordByPhone",
  props: {
    show: {
      type: Boolean,
      default: false
    },
  },
  data() {
    // 手机号码效验
    const validatePhone = (rule, value, callback) => {
      const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
      if (value) {
        if (!reg.test(value)) {
          callback(new Error('请输入正确的手机号码'))
        } else {
          callback()
        }
      } else {
        callback(new Error('请输入手机号码'))
      }
    }
    return {
      //验证码部分所需的数据
      btnColor: '#409eff',
      btnTxt: "获取验证码",
      time: 0,
      form: {
        userName: null,
        phone: null,
        verificationCode: null,
        newPassword: null,
        checkPassword: null
      },
      loading: false,
      formRules: {
        userName: [{ required: true, validator: validateUsername, trigger: 'change' }],
        phone: [{ required: true, validator: validatePhone, trigger: 'change' }],
        verificationCode: [
          { required: true, message: '请输入验证码', trigger: 'change' },
        ],
        newPassword: [{ required: true, validator: validatePwd, trigger: 'change' }],
        checkPassword: [{
          required: true,
          validator: (rule, value, callback) => {
            if (!value) {
              callback(new Error('请再次输入密码'))
            } else if (value !== this.form.newPassword) {
              callback(new Error('两次输入密码不一致'))
            } else {
              callback()
            }
          },
          trigger: 'change'
        }]
      },
      dialogVisible: false
    }
  },
  watch: {
    show(newValue, oldValue) {
      this.dialogVisible = newValue
    },
    dialogVisible(newValue, oldValue) {
      this.$emit('update:show', newValue)
      if (!newValue) {
        this.initData()
      }
    }
  },
  methods: {
    ...mapMutations(['logout']),
    doSm3AndSm2Encrypt(sourceStr) {
      return '04' + sm2.doEncrypt(sourceStr, process.env.VUE_APP_PUBLICKEY, 1)
    },
    //根据账号获取手机号码
    blurUserName(event) {
      let _value = event.target.value;
      if (!_value || !/^[0-9a-zA-Z_]{2,32}$/.test(_value)) {
        return;
      }
      ChangePwdApi.getInfo({ userAccount: _value }).then(res => {
        if (res.code === '0' && res.data) {
          this.form.phone = res.data.phone;
        }
      }).catch((error) => {
        if (error.data.code === '500') {
          this.$message({
            message: '当前账户未关联手机号,请联系管理员',
            type: 'warning',
          });
        }
      })
    },
    //账号变更清空手机号码
    inputUserName() {
      this.$refs['phone'].resetField();
      this.$refs['verificationCode'].resetField();
      this.btnColor = '#409eff';
      this.btnTxt = "获取验证码";
      this.time = 0;
    },
    //操作token
    handleToken(token) {
      if (token) {
        window.localStorage.setItem('mall-operate-access_token', token)
      } else {
        window.localStorage.removeItem('mall-operate-access_token')
      }
    },
    //获取验证码
    getCode() {
      if (!this.form.phone) {
        //手机号码校验不通过
        this.$message({
          message: '手机号码不能为空!',
          type: 'warning',
        });
      } else if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.form.phone)) {
        this.$message({
          message: '请输入正确的手机号码',
          type: 'warning',
        });
      } else {
        this.sendVerificationCode();
      }
    },
    //发送验证码
    async sendVerificationCode() {
      try {
        let params = {
          phone: this.form.phone
        }
        const res = await ChangePwdApi.sendVerificationCode(params)
        if (res.code === '0') {
          //【手机号码校验通过】发送axios请求 获取验证码
          this.time = 60;   //设置倒计时的时间(单位:秒)
          this.disabled = true;
          this.timer();     //调用倒计时方法
        }
      } catch (error) { }
    },
    //倒计时方法
    timer() {
      if (this.time > 0) {
        this.time--;
        this.btnTxt = this.time + "s后重新获取验证码";
        this.btnColor = '#c0c4cc';
        setTimeout(this.timer, 1000);
      } else {
        this.time = 0;
        this.btnTxt = "获取验证码";
        this.btnColor = '#409eff';
        this.disabled = false;
      }
    },
    //提交密码
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.byCodeGetToken();
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //核销验证码并生成token
    async byCodeGetToken() {
      try {
        let params = {
          phone: this.form.phone,
          userAccount: this.form.userName,
          verificationCode: this.form.verificationCode
        }
        const res = await ChangePwdApi.checkVerificationCodeAndGetToken(params)
        if (res.code === '0' && res.data) {
          this.handleToken(res.data);
          this.handleUpdatePwd();
        }
      } catch (error) {
        if (error.data.code === '500') {
          this.$message({
            message: '验证码错误,请重新输入',
            type: 'warning',
          });
        }
      }
    },
    //修改密码
    handleUpdatePwd() {
      let params = {
        password: this.doSm3AndSm2Encrypt(this.form.newPassword),
        confirmPassword: this.doSm3AndSm2Encrypt(this.form.checkPassword),
        token: window.localStorage.getItem('mall-operate-access_token'),
        userAccount: this.form.userName,
      }
      if (this.loading) {
        return
      }
      this.loading = true
      ChangePwdApi.changePsCode(params).then(res => {
        if (res.code === '0') {
          this.$message({
            message: '密码修改成功,请重新登录',
            type: 'success',
          });
          this.logout()
          this.dialogVisible = false;
        } else {
          this.handleToken();
        }
        this.loading = false
      }).catch(() => {
        this.handleToken();
        this.loading = false
      })
    },
    //初始化
    initData() {
      for (const key in this.form) {
        this.form[key] = null
      }
      this.btnColor = '#409eff';
      this.btnTxt = "获取验证码";
      this.time = 0;
    },
  }
}
</script>
 
<style>
</style>