table-action-modal.vue 5.5 KB

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