UserPassword.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <script setup lang="ts">
  2. import type { UserModel } from '@/model/system.model';
  3. import { updateUserPasswordMethod } from '@/request/api/system.api';
  4. import { useRequest } from 'alova/client';
  5. import type { VxeFormListeners, VxeFormProps } from 'vxe-pc-ui';
  6. interface FormModel {
  7. userId: string;
  8. password?: string;
  9. twice?: string;
  10. }
  11. const props = defineProps<{ data: UserModel }>();
  12. const emits = defineEmits<{
  13. submit: [],
  14. }>();
  15. const { loading: submitting, send: submit } = useRequest(updateUserPasswordMethod, { immediate: false }).onSuccess(
  16. ({ data }) => {
  17. emits('submit');
  18. });
  19. const formProps = reactive<VxeFormProps<FormModel>>({
  20. titleWidth: 100,
  21. titleAlign: 'right',
  22. titleColon: true,
  23. data: { userId: props.data.userId },
  24. items: [
  25. { field: 'password', title: '新密码', span: 24, itemRender: { name: 'VxeInput', props: { type: 'password' } } },
  26. { field: 'twice', title: '再次输入', span: 24, itemRender: { name: 'VxeInput', props: { type: 'password' } } },
  27. { align: 'center', span: 24, slots: { default: 'active' } },
  28. ],
  29. rules: {
  30. password: [
  31. { required: true, validator: 'ValidPassword' },
  32. ],
  33. twice: [
  34. { required: true, message: '请再次输入密码' },
  35. { required: true, validator: 'ValidPassword' },
  36. {
  37. required: true, validator: (params) => {
  38. if ( !params.itemValue || !params.data.password ) return;
  39. if ( params.data.password !== params.itemValue ) return new Error(`两次输入的密码不一致`);
  40. },
  41. },
  42. ],
  43. },
  44. });
  45. const formEmits: VxeFormListeners<FormModel> = {
  46. submit({ data }) { submit(data); },
  47. };
  48. </script>
  49. <template>
  50. <vxe-form v-bind="formProps" v-on="formEmits">
  51. <template #active>
  52. <vxe-button type="submit" status="primary" content="提交" :loading="submitting"></vxe-button>
  53. </template>
  54. </vxe-form>
  55. </template>
  56. <style scoped lang="scss">
  57. </style>