table-action-modal.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <n-modal v-model:show="modalVisible" preset="card" :title="title" class="w-700px">
  3. <n-form ref="formRef" label-placement="left" :label-width="80" :model="formModel" :rules="rules">
  4. <n-grid :cols="24" :x-gap="18">
  5. <!-- <n-form-item-grid-item :span="12" label="id" path="createUid">
  6. <n-input-number v-model:value="formModel.createUid" />
  7. </n-form-item-grid-item> -->
  8. <n-form-item-grid-item :span="12" label="学科名称" path="name">
  9. <n-input v-model:value="formModel.name" />
  10. </n-form-item-grid-item>
  11. <n-form-item-grid-item :span="12" label="学科描述" path="description">
  12. <n-input v-model:value="formModel.description" clearable />
  13. </n-form-item-grid-item>
  14. <n-form-item-grid-item :span="12" label="学科分类" path="categoryId">
  15. <n-input v-model:value="formModel.categoryId" />
  16. </n-form-item-grid-item>
  17. <n-form-item-grid-item :span="12" label="创建用户ID" path="createUid">
  18. <n-input-number v-model:value="formModel.createUid" />
  19. </n-form-item-grid-item>
  20. <n-form-item-grid-item :span="12" label="状态" path="disabled">
  21. <n-input v-model:value="formModel.disabled" :options="userStatusOptions" />
  22. </n-form-item-grid-item>
  23. </n-grid>
  24. <n-space class="w-full pt-16px" :size="24" justify="end">
  25. <n-button class="w-72px" @click="closeModal">取消</n-button>
  26. <n-button class="w-72px" type="primary" v-if="props.type === 'add'" @click="handleSubmit">添加</n-button>
  27. <n-button class="w-72px" type="primary" v-if="props.type === 'edit'" @click="handleupdate">修改</n-button>
  28. </n-space>
  29. </n-form>
  30. </n-modal>
  31. </template>
  32. <script setup lang="ts">
  33. import { ref, computed, reactive, watch, inject } from 'vue';
  34. import type { FormInst, FormItemRule } from 'naive-ui';
  35. import { addSubject, updateSubjects, SelectByConditionParams } from '~/src/service/api/crouse';
  36. import { userStatusOptions } from '@/constants';
  37. import { createRequiredFormRule } from '@/utils';
  38. import dayjs from 'dayjs';
  39. const init = inject('init');
  40. export interface Props {
  41. /** 弹窗可见性 */
  42. visible: boolean;
  43. /**
  44. * 弹窗类型
  45. * add: 新增
  46. * edit: 编辑
  47. */
  48. type?: 'add' | 'edit';
  49. /** 编辑的表格行数据 */
  50. editData?: SelectByConditionParams | null;
  51. }
  52. export type ModalType = NonNullable<Props['type']>;
  53. defineOptions({ name: 'TableActionModal' });
  54. const props = withDefaults(defineProps<Props>(), {
  55. type: 'add' || 'edit',
  56. editData: null
  57. });
  58. interface Emits {
  59. (e: 'update:visible', visible: boolean): void;
  60. }
  61. const emit = defineEmits<Emits>();
  62. // const emits = defineEmits()
  63. const modalVisible = computed({
  64. get() {
  65. return props.visible;
  66. },
  67. set(visible) {
  68. emit('update:visible', visible);
  69. }
  70. });
  71. const closeModal = () => {
  72. modalVisible.value = false;
  73. };
  74. const title = computed(() => {
  75. const titles: Record<ModalType, string> = {
  76. add: '添加用户',
  77. edit: '编辑用户'
  78. };
  79. return titles[props.type];
  80. });
  81. const formRef = ref<HTMLElement & FormInst>();
  82. type FormModel = Pick<UserManagement1.User, 'inputValue' | 'id' | 'name' | 'description' | 'categoryId' | 'createTime' | 'modifyTime' | 'createUid' | 'disabled'>;
  83. const formModel = reactive<FormModel>(createDefaultFormModel());
  84. const rules: Record<keyof FormModel, FormItemRule | FormItemRule[]> = {
  85. id: createRequiredFormRule('请输入id'),
  86. name: createRequiredFormRule('请输入用户名'),
  87. description: createRequiredFormRule('请输入年龄'),
  88. categoryId: createRequiredFormRule('请选择性别'),
  89. createTime: createRequiredFormRule('请选择用户状态'),
  90. modifyTime: createRequiredFormRule('请选择用户状态'),
  91. createUid: createRequiredFormRule('请选择用户状态'),
  92. disabled: createRequiredFormRule('请选择用户状态'),
  93. inputValue: createRequiredFormRule('请选择用户状态')
  94. };
  95. function createDefaultFormModel(): FormModel {
  96. return {
  97. id: 0,
  98. name: '',
  99. description: '',
  100. categoryId: '',
  101. createTime: '',
  102. modifyTime: '',
  103. createUid: 0,
  104. disabled: '',
  105. inputValue: '',
  106. };
  107. }
  108. function handleUpdateFormModel(model: Partial<FormModel>) {
  109. Object.assign(formModel, model);
  110. }
  111. function handleUpdateFormModelByModalType() {
  112. const handlers: Record<ModalType, () => void> = {
  113. add: () => {
  114. const defaultFormModel = createDefaultFormModel();
  115. handleUpdateFormModel(defaultFormModel);
  116. },
  117. edit: () => {
  118. if (props.editData) {
  119. handleUpdateFormModel(props.editData);
  120. }
  121. }
  122. };
  123. handlers[props.type]();
  124. }
  125. function initYe() {
  126. init();
  127. }
  128. async function handleSubmit() {
  129. await formRef.value?.validate();
  130. addSubject(formModel).then(() => {
  131. initYe();
  132. window.$message?.success('新增成功!');
  133. })
  134. closeModal();
  135. }
  136. async function handleupdate() {
  137. await formRef.value?.validate();
  138. window.$message?.success('修改成功!');
  139. // formModel.modifyTime = Date()
  140. const timer = dayjs().format('YYYY-MM-DD HH:mm:ss')
  141. handleUpdateFormModel({ modifyTime: timer });
  142. updateSubjects(formModel).then(() => {
  143. initYe();
  144. })
  145. closeModal();
  146. }
  147. watch(
  148. () => props.visible,
  149. newValue => {
  150. if (newValue) {
  151. handleUpdateFormModelByModalType();
  152. }
  153. }
  154. );
  155. </script>
  156. <style scoped></style>