wangrong
2026-08-30 4c949c0dc9bcae785c281c9d8125295f09810b15
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
<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { useI18n } from '@/hooks/web/useI18n'
import { cloneDeep } from 'lodash-es'
import request from '@/config/axios'
import { rsaEncryp } from '@/utils/encryption'
import { ElMessage } from 'element-plus-secondary'
import { logoutHandler } from '@/utils/logout'
import { CustomPassword } from '@/components/custom-password'
 
const { t } = useI18n()
 
const defaultForm = {
  pwd: '',
  newPwd: '',
  confirm: ''
}
const pwdForm = reactive(cloneDeep(defaultForm))
 
const validatePwd = (_: any, value: any, callback: any) => {
  if (value === pwdForm.pwd) {
    callback(new Error(t('system.be_the_same')))
  }
  const pattern =
    /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[~!@#$%^&*()_+\-\={}|":<>?`[\];',.\/])[a-zA-Z0-9~!@#$%^&*()_+\-\={}|":<>?`[\];',.\/]{8,20}$/
  const regep = new RegExp(pattern)
  if (!regep.test(value)) {
    const msg = t('user.pwd_pattern_error')
    callback(new Error(msg))
  } else {
    callback()
  }
}
 
const validateConfirmPwd = (_: any, value: any, callback: any) => {
  if (value !== pwdForm.newPwd) {
    callback(new Error(t('system.twice_are_inconsistent')))
  } else {
    callback()
  }
}
 
const rule = {
  pwd: [
    {
      required: true,
      message: t('common.require'),
      trigger: 'blur'
    },
    {
      min: 6,
      max: 20,
      message: t('commons.input_limit', [6, 20]),
      trigger: 'blur'
    }
  ],
  newPwd: [
    {
      required: true,
      message: t('common.require'),
      trigger: 'blur'
    },
    { validator: validatePwd, trigger: 'blur' }
  ],
  confirm: [
    {
      required: true,
      message: t('common.require'),
      trigger: 'blur'
    },
    {
      min: 8,
      max: 20,
      message: t('commons.input_limit', [8, 20]),
      trigger: 'blur'
    },
    { validator: validateConfirmPwd, trigger: 'blur' }
  ]
}
const updatePwdForm = ref()
 
const save = () => {
  updatePwdForm.value.validate(val => {
    if (val) {
      const pwd = rsaEncryp(pwdForm.pwd)
      const newPwd = rsaEncryp(pwdForm.newPwd)
      request.post({ url: '/user/modifyPwd', data: { pwd, newPwd } }).then(() => {
        ElMessage.success(t('system.log_in_again'))
        logoutHandler()
      })
    }
  })
}
</script>
 
<template>
  <el-form
    ref="updatePwdForm"
    require-asterisk-position="right"
    :model="pwdForm"
    :rules="rule"
    class="mt16"
    label-width="80px"
    label-position="top"
  >
    <el-form-item :label="t('system.original_password')" prop="pwd">
      <CustomPassword
        v-model="pwdForm.pwd"
        show-password
        type="password"
        :placeholder="t('system.the_original_password')"
      />
    </el-form-item>
    <el-form-item :label="t('system.new_password')" prop="newPwd">
      <CustomPassword
        v-model="pwdForm.newPwd"
        show-password
        type="password"
        :placeholder="t('system.the_new_password')"
      />
    </el-form-item>
    <el-form-item :label="t('system.confirm_password')" prop="confirm">
      <CustomPassword
        v-model="pwdForm.confirm"
        show-password
        type="password"
        :placeholder="t('system.the_confirmation_password')"
      />
    </el-form-item>
    <el-button @click="save" type="primary">
      {{ t('common.save') }}
    </el-button>
  </el-form>
</template>
 
<style lang="less" scoped>
.mt16 {
  margin-top: 16px;
  .ed-form-item {
    margin-bottom: 20px;
    :deep(label) {
      line-height: 22px !important;
    }
  }
}
</style>