UserEdit.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <script setup lang="ts">
  2. import { type UserModel } from '@/model/system.model';
  3. import { branchMethod, editUserMethod, rolesAllMethod, userMethod } from '@/request/api/system.api';
  4. import { useRequest } from 'alova/client';
  5. import { type VxeFormListeners, type VxeFormProps } from 'vxe-pc-ui';
  6. type FormModel = Partial<UserModel>
  7. const defaultModel = {};
  8. const props = defineProps<{ data: FormModel }>();
  9. const emits = defineEmits<{
  10. submit: [ data?: UserModel ],
  11. }>();
  12. const { data: branch, loading: branchLoading } = useRequest(branchMethod);
  13. const { data: roles, loading: rolesLoading } = useRequest(rolesAllMethod);
  14. const { loading, send: load } = useRequest(userMethod, { immediate: false, initialData: props.data ?? defaultModel })
  15. .onSuccess(({ data }) => {
  16. formProps.data = { ...data };
  17. });
  18. const { loading: submitting, send: submit } = useRequest(editUserMethod, { immediate: false })
  19. .onSuccess(({ data }) => {
  20. emits('submit');
  21. });
  22. const formProps = reactive<VxeFormProps<FormModel>>({
  23. titleWidth: 120,
  24. titleAlign: 'right',
  25. titleColon: true,
  26. titleAsterisk: true,
  27. data: { ...props.data },
  28. items: [
  29. { field: 'userName', title: '系统账号', span: 24, itemRender: { name: 'VxeInput' } },
  30. {
  31. field: 'password',
  32. title: '密码',
  33. span: 24,
  34. itemRender: { name: 'VxeInput', props: { type: 'password' } },
  35. visible: !props.data?.userId,
  36. },
  37. {
  38. field: 'roleIds', title: '角色', span: 24, itemRender: {
  39. name: 'VxeSelect', props: {
  40. multiple: true, clearable: true,
  41. loading: computed(() => rolesLoading.value),
  42. options: computed(() => roles.value),
  43. optionProps: { label: 'roleName', value: 'roleId' },
  44. },
  45. },
  46. },
  47. { field: 'nickName', title: '姓名', span: 24, itemRender: { name: 'VxeInput' } },
  48. {
  49. field: 'deptId', title: '医院 / 科室', span: 24, itemRender: {
  50. name: 'VxeTreeSelect',
  51. props: {
  52. loading: computed(() => branchLoading.value),
  53. options: computed(() => branch.value),
  54. optionProps: { value: 'id' },
  55. },
  56. },
  57. },
  58. { field: 'jobnumber', title: '工号', span: 24, itemRender: { name: 'VxeInput' } },
  59. { field: 'phonenumber', title: '手机号码', span: 24, itemRender: { name: 'VxeInput', props: { maxlength: 11 } } },
  60. { align: 'center', span: 24, slots: { default: 'active' } },
  61. ],
  62. rules: {
  63. userName: [
  64. { required: true, message: '请输入系统账号' },
  65. ],
  66. password: [
  67. { required: !props.data?.userId, validator: 'ValidPassword' },
  68. ],
  69. roleIds: [
  70. { required: true, message: '请选择角色' },
  71. ],
  72. nickName: [
  73. { required: true, message: '请输入姓名' },
  74. ],
  75. deptId: [
  76. { required: true, message: '请选择医院 / 科室' },
  77. ],
  78. jobnumber: [
  79. { required: true, message: '请输入工号' },
  80. ],
  81. phonenumber: [
  82. { required: true, validator: 'ValidMobile' },
  83. ],
  84. },
  85. });
  86. const formEmits: VxeFormListeners<FormModel> = {
  87. submit({ data }) { submit(data); },
  88. reset() { formProps.data = { ...props.data }; },
  89. };
  90. onBeforeMount(() => {
  91. if ( props.data?.userId ) load(props.data);
  92. });
  93. </script>
  94. <template>
  95. <vxe-form v-bind="formProps" v-on="formEmits" :loading>
  96. <template #active>
  97. <vxe-button type="submit" status="primary" content="提交" :loading="submitting"></vxe-button>
  98. <vxe-button type="reset" content="重置" :disabled="submitting"></vxe-button>
  99. </template>
  100. </vxe-form>
  101. </template>
  102. <style scoped lang="scss"></style>