UserPassword.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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, update?: boolean }>();
  12. const emits = defineEmits<{
  13. submit: [],
  14. }>();
  15. const { loading: submitting, send: submit } = useRequest(
  16. (data) => updateUserPasswordMethod(data, props.update),
  17. { immediate: false },
  18. ).onSuccess(({ data }) => {
  19. emits('submit');
  20. });
  21. const formProps = reactive<VxeFormProps<FormModel>>({
  22. titleWidth: 100,
  23. titleAlign: 'right',
  24. titleColon: true,
  25. data: { userId: props.data.userId },
  26. items: [
  27. {
  28. visible: props.update,
  29. field: 'old',
  30. title: '旧密码',
  31. span: 24,
  32. itemRender: { name: 'VxeInput', props: { type: 'password' } },
  33. },
  34. { field: 'password', title: '新密码', span: 24, itemRender: { name: 'VxeInput', props: { type: 'password' } } },
  35. { field: 'twice', title: '再次输入', span: 24, itemRender: { name: 'VxeInput', props: { type: 'password' } } },
  36. { align: 'center', span: 24, slots: { default: 'active' } },
  37. ],
  38. rules: {
  39. old: [
  40. { required: props.update, validator: 'ValidPassword' },
  41. ],
  42. password: [
  43. { required: true, validator: 'ValidPassword' },
  44. ],
  45. twice: [
  46. { required: true, message: '请再次输入密码' },
  47. { required: true, validator: 'ValidPassword' },
  48. {
  49. required: true, validator: (params) => {
  50. if ( !params.itemValue || !params.data.password ) return;
  51. if ( params.data.password !== params.itemValue ) return new Error(`两次输入的密码不一致`);
  52. },
  53. },
  54. ],
  55. },
  56. });
  57. const formEmits: VxeFormListeners<FormModel> = {
  58. submit({ data }) { submit(data); },
  59. };
  60. </script>
  61. <template>
  62. <vxe-form v-bind="formProps" v-on="formEmits">
  63. <template #active>
  64. <vxe-button type="submit" status="primary" content="提交" :loading="submitting"></vxe-button>
  65. </template>
  66. </vxe-form>
  67. </template>
  68. <style scoped lang="scss">
  69. </style>