UpdatePassword.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <script setup lang="ts">
  2. import type { DialogProps, FormInstance } from 'vant';
  3. import { updateUserPassword } from '@/request/api/manage';
  4. export interface UpdatePasswordProps {
  5. pid: string | number;
  6. title?: string;
  7. token?: string;
  8. }
  9. const { title = '修改密码', pid, token } = defineProps<UpdatePasswordProps>();
  10. const emits = defineEmits<{
  11. complete: [];
  12. cancel: [];
  13. }>();
  14. const show = defineModel('show', { default: false });
  15. const pending = ref(false);
  16. const form = useTemplateRef<FormInstance>('form');
  17. const model = ref({ password: '', again: '' });
  18. const dialog = reactive<Partial<DialogProps>>({
  19. showCancelButton: true,
  20. className: 'reset-password-dialog',
  21. width: 480,
  22. beforeClose(action) {
  23. return action !== 'confirm' || onSubmit();
  24. },
  25. });
  26. const onCancel = () => {
  27. if (pending.value) return;
  28. form.value?.resetValidation();
  29. model.value.password = '';
  30. model.value.again = '';
  31. emits('cancel');
  32. };
  33. const onSubmit = async () => {
  34. try {
  35. await form.value?.validate();
  36. await updateUserPassword({ pid, ...model.value }, token).send(true);
  37. pending.value = true;
  38. show.value = false;
  39. await showConfirmDialog({ title: '请重新登录', showCancelButton: false });
  40. emits('complete');
  41. return true;
  42. } catch {
  43. return false;
  44. }
  45. };
  46. const validator: any = {
  47. required: { required: true, message: '请输入', trigger: 'onChange' },
  48. password: {
  49. trigger: ['onBlur', 'onSubmit'],
  50. pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d]{6,18}$/,
  51. message: '密码必须由大小写字母和数字三种组合,6-18 位字符',
  52. validateEmpty: false,
  53. },
  54. again: {
  55. trigger: ['onChange', 'onBlur', 'onSubmit'],
  56. validator(value: string) {
  57. return !model.value.password || model.value.password === value;
  58. },
  59. message: '两次输入的密码不同',
  60. },
  61. };
  62. </script>
  63. <template>
  64. <van-dialog v-bind="dialog" v-model:show="show" :title @closed="onCancel()">
  65. <van-form ref="form" @submit="onSubmit" class="py-4">
  66. <van-cell-group inset>
  67. <van-field v-model="model.password" type="password" name="password" label="新密码" placeholder="请输入新密码" :rules="[validator.required, validator.password]" />
  68. <van-field
  69. v-model="model.again"
  70. type="password"
  71. name="again"
  72. label="确认密码"
  73. placeholder="再次输入新密码"
  74. :rules="[validator.required, validator.again, validator.password]"
  75. />
  76. </van-cell-group>
  77. </van-form>
  78. </van-dialog>
  79. </template>
  80. <style scoped lang="scss"></style>