editPassword.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <div class="mt-3">
  3. <PhonePage ref="phoneRef" style="width: 370px;" :phone="phone" :scene="scene" openVerify :phoneDisabled="phone ? true : false"></PhonePage>
  4. <v-form ref="passwordRef" style="width: 370px;">
  5. <v-text-field
  6. v-model="query.password"
  7. placeholder="请输入新密码"
  8. variant="outlined"
  9. density="compact"
  10. color="primary"
  11. prepend-inner-icon="mdi-lock-outline"
  12. :append-inner-icon="passwordType ? 'mdi-eye-outline' : 'mdi-eye-off-outline'"
  13. :type="passwordType ? 'text' : 'password'"
  14. :rules="[v=> !!v || '请填写新密码', validPassword]"
  15. @click:append-inner="passwordType = !passwordType"
  16. ></v-text-field>
  17. <v-text-field
  18. v-model="query.checkPassword"
  19. placeholder="请再次输入新密码"
  20. variant="outlined"
  21. density="compact"
  22. color="primary"
  23. prepend-inner-icon="mdi-lock-outline"
  24. :append-inner-icon="show ? 'mdi-eye-outline' : 'mdi-eye-off-outline'"
  25. :type="show ? 'text' : 'password'"
  26. :rules="[v=> !!v || '请再次输入密码', confirmPassword]"
  27. @click:append-inner="show = !show"
  28. ></v-text-field>
  29. </v-form>
  30. <slot name="custom"></slot>
  31. <div class="text-center mt-5">
  32. <v-btn v-if="props.showCancelBtn" class="mr-5" color="primary" variant="outlined" @click="handleClose">取 消</v-btn>
  33. <v-btn color="primary" :max-width="props.showCancelBtn ? 370 : 95" :min-width="props.showCancelBtn ? 95 : 370" @click="handleSubmit" :loading="loading">确认修改</v-btn>
  34. </div>
  35. </div>
  36. <div class="VerifyBox">
  37. </div>
  38. </template>
  39. <script setup>
  40. defineOptions({ name: 'editPasswordCommonPage'})
  41. import { ref, reactive, computed } from 'vue'
  42. import PhonePage from '@/components/VerificationCode'
  43. import { updatePassword, resetPassword } from '@/api/common/index'
  44. import Snackbar from '@/plugins/snackbar'
  45. const emit = defineEmits(['cancel'])
  46. const props = defineProps({
  47. phone: {
  48. type: String,
  49. default: ''
  50. },
  51. showCancelBtn: {
  52. type: Boolean,
  53. default: true
  54. },
  55. isReset: {
  56. type: Boolean,
  57. default: false
  58. }
  59. })
  60. const scene = ref()
  61. scene.value = props.isReset ? 33 : 32 // 32-修改密码 33-忘记密码
  62. // scene.value = 30 // 暂时都用30
  63. let query = reactive({
  64. password: '',
  65. checkPassword: ''
  66. })
  67. const passwordRef = ref(false)
  68. const show = ref(false)
  69. const loading = ref(false)
  70. const passwordType = ref(false)
  71. const phoneRef = ref()
  72. // 密码效验
  73. // const regex = /^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{8,16}$/
  74. // 请输入8-16位数由数字、大小写字母组成的密码
  75. const regex = /^.{8,}$/
  76. const validPassword = computed(() => {
  77. return regex.test(query.password) || '请输入至少8位数的密码'
  78. })
  79. const confirmPassword = computed(() => {
  80. return query.checkPassword === query.password || '两次输入密码不一致'
  81. })
  82. const handleClose = () => {
  83. query = {
  84. password: '',
  85. checkPassword: ''
  86. }
  87. passwordType.value = false
  88. loading.value = false
  89. emit('cancel')
  90. }
  91. // 修改
  92. const handleSubmit = async () => {
  93. const passwordValid = await passwordRef.value.validate()
  94. const phoneValid = await phoneRef.value.phoneForm.validate()
  95. if (!passwordValid.valid || !phoneValid.valid) return
  96. const data = {
  97. password: query.password,
  98. code: phoneRef.value.loginData.code
  99. }
  100. // 重置密码需要手机号
  101. if (props.isReset) data.phone = phoneRef.value.loginData.phone
  102. loading.value = true
  103. const api = props.isReset ? resetPassword : updatePassword
  104. try {
  105. await api(data)
  106. Snackbar.success('修改成功')
  107. } finally {
  108. loading.value = false
  109. handleClose()
  110. }
  111. }
  112. </script>
  113. <style scoped lang="scss">
  114. </style>