editPassword.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <div>
  3. <h3>{{ $t('setting.editPassword') }}</h3>
  4. <v-divider class="mb-4"></v-divider>
  5. <div class="login-user mb-4">当前登录账号: <span style="color: var(--v-primary-base);">{{ userInfo.phone }}</span></div>
  6. <v-stepper v-model="stepper" color="primary">
  7. <v-stepper-header>
  8. <v-stepper-item title="手机验证" value="1"></v-stepper-item>
  9. <v-divider></v-divider>
  10. <v-stepper-item title="修改密码" value="2"></v-stepper-item>
  11. </v-stepper-header>
  12. <v-stepper-window>
  13. <v-stepper-window-item value="1">
  14. <div class="d-flex justify-center">
  15. <PhonePage ref="phoneRef" style="width: 300px;"></PhonePage>
  16. </div>
  17. <div class="text-center">
  18. <v-btn class="buttons" color="primary" @click="handleValidate">立即验证</v-btn>
  19. </div>
  20. </v-stepper-window-item>
  21. <v-stepper-window-item value="2">
  22. <div class="d-flex justify-center">
  23. <v-form ref="passwordRef" style="width: 300px;">
  24. <v-text-field
  25. v-model="query.password"
  26. placeholder="请输入密码"
  27. variant="outlined"
  28. density="compact"
  29. color="primary"
  30. prepend-inner-icon="mdi-lock-outline"
  31. :append-inner-icon="passwordType ? 'mdi-eye-outline' : 'mdi-eye-off-outline'"
  32. :type="passwordType ? 'text' : 'password'"
  33. :rules="[v=> !!v || '请填写密码', validPassword]"
  34. @click:append-inner="passwordType = !passwordType"
  35. ></v-text-field>
  36. <v-text-field
  37. v-model="query.checkPassword"
  38. placeholder="请再次输入密码"
  39. variant="outlined"
  40. density="compact"
  41. color="primary"
  42. prepend-inner-icon="mdi-lock-outline"
  43. :append-inner-icon="show ? 'mdi-eye-outline' : 'mdi-eye-off-outline'"
  44. :type="show ? 'text' : 'password'"
  45. :rules="[v=> !!v || '请再次输入密码', passwordCheck]"
  46. @click:append-inner="show = !show"
  47. ></v-text-field>
  48. </v-form>
  49. </div>
  50. <div class="text-center">
  51. <v-btn class="buttons" color="primary" @click="handleSubmit" :loading="loading">确认密码</v-btn>
  52. </div>
  53. </v-stepper-window-item>
  54. </v-stepper-window>
  55. </v-stepper>
  56. </div>
  57. </template>
  58. <script setup name="editPassword">
  59. import PhonePage from '@/components/VerificationCode'
  60. import { updatePassword } from '@/api/common/index'
  61. import { ref, reactive, computed } from 'vue'
  62. import Snackbar from '@/plugins/snackbar'
  63. const phoneRef = ref()
  64. const passwordRef = ref()
  65. const passwordType = ref(false)
  66. const show = ref(false)
  67. const loading = ref(false)
  68. const stepper = ref('1')
  69. const query = reactive({
  70. password: '',
  71. checkPassword: ''
  72. })
  73. // 密码效验
  74. const regex = /^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{4,10}$/
  75. const validPassword = computed(() => {
  76. return regex.test(query.password) || '请输入4-10位数由数字、大小写字母组成的密码'
  77. })
  78. const passwordCheck = computed(() => {
  79. return (query.checkPassword === query.password && regex.test(query.checkPassword)) || '两次密码输入不一致'
  80. })
  81. // 当前登录的用户信息
  82. const userInfo = JSON.parse(localStorage.getItem('userInfo'))
  83. const handleSubmit = async () => {
  84. const { valid} = await passwordRef.value.validate()
  85. if (!valid) return
  86. loading.value = true
  87. try {
  88. await updatePassword({ password: query.password, code: phoneRef.value.loginData.code })
  89. Snackbar.success('修改成功')
  90. } finally {
  91. loading.value = false
  92. }
  93. }
  94. const handleValidate = async () => {
  95. const { valid } = await phoneRef.value.phoneForm.validate()
  96. if (!valid) return
  97. stepper.value = '2'
  98. }
  99. </script>
  100. <style lang="scss" scoped>
  101. h3 {
  102. font-size: 20px;
  103. text-align: left;
  104. font-weight: 600;
  105. padding-bottom: 25px;
  106. }
  107. .login-user {
  108. color: var(--color-666);
  109. font-weight: 600;
  110. }
  111. </style>