table-action-modal.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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="48" :x-gap="18">
  5. <n-form-item-grid-item :span="25" label="指标名称" path="name">
  6. <n-input v-model:value="formModel.name" />
  7. </n-form-item-grid-item>
  8. <n-form-item-grid-item :span="25" label="分类ID" path="category_id">
  9. <n-input-number v-model:value="formModel.category_id" />
  10. </n-form-item-grid-item>
  11. <n-form-item-grid-item :span="25" label="是否展示" path="is_show">
  12. <n-select v-model:value="formModel.is_show" :options="IndicatorsOptions" />
  13. </n-form-item-grid-item>
  14. <n-form-item-grid-item :span="25" label="指标描述" path="description">
  15. <n-input v-model:value="formModel.description" type="textarea" placeholder="请输入指标描述" />
  16. </n-form-item-grid-item>
  17. </n-grid>
  18. <n-space class="w-full pt-16px" :size="24" justify="end">
  19. <n-button class="w-72px" @click="closeModal">取消</n-button>
  20. <n-button class="w-72px" type="primary" @click="handleSubmit">确定</n-button>
  21. </n-space>
  22. </n-form>
  23. </n-modal>
  24. </template>
  25. <script setup lang="ts">
  26. import { ref, computed, reactive, watch } from 'vue';
  27. import type { FormInst, FormItemRule } from 'naive-ui';
  28. import { IndicatorsOptions } from '@/constants';
  29. import { createRequiredFormRule } from '@/utils';
  30. import { fetchIndicatorsAdd, fetchIndicatorsEdit } from '@/service/api/event';
  31. export interface Props {
  32. /** 弹窗可见性 */
  33. visible: boolean;
  34. /**
  35. * 弹窗类型
  36. * add: 新增
  37. * edit: 编辑
  38. */
  39. type?: 'add' | 'edit';
  40. /** 编辑的表格行数据 */
  41. editData?: BackgroundField.Field | null;
  42. }
  43. export type ModalType = NonNullable<Props['type']>;
  44. defineOptions({ name: 'TableActionModal' });
  45. const props = withDefaults(defineProps<Props>(), {
  46. type: 'add',
  47. editData: null
  48. });
  49. interface Emits {
  50. (e: 'update:visible', visible: boolean): void;
  51. }
  52. const emit = defineEmits<Emits>();
  53. const modalVisible = computed({
  54. get() {
  55. return props.visible;
  56. },
  57. set(visible) {
  58. emit('update:visible', visible);
  59. }
  60. });
  61. const closeModal = () => {
  62. modalVisible.value = false;
  63. };
  64. const titles: Record<ModalType, string> = {
  65. add: '添加字段',
  66. edit: '编辑字段'
  67. };
  68. const title = computed(() => {
  69. return titles[props.type];
  70. });
  71. const formRef = ref<HTMLElement & FormInst>();
  72. type FormModel = Pick<
  73. ApiBackground.Indicators,
  74. 'id' | 'name' | 'category_id' | 'particle_size' | 'is_show' | 'description'
  75. >;
  76. const formModel = reactive<FormModel>(createDefaultFormModel());
  77. const rules: Record<keyof FormModel, FormItemRule | FormItemRule[]> = {
  78. name: createRequiredFormRule('请输入指标字段名'),
  79. particle_size: createRequiredFormRule('请选择指标颗粒度'),
  80. category_id: createRequiredFormRule('请选择分类id'),
  81. description: createRequiredFormRule('请输入字段描述'),
  82. is_show: createRequiredFormRule(),
  83. id: createRequiredFormRule()
  84. };
  85. function createDefaultFormModel(): FormModel {
  86. return {
  87. id: 0,
  88. name: '',
  89. particle_size: null,
  90. category_id: null,
  91. is_show: null,
  92. description: ''
  93. };
  94. }
  95. function handleUpdateFormModel(model: Partial<FormModel>) {
  96. Object.assign(formModel, model);
  97. }
  98. function handleUpdateFormModelByModalType() {
  99. const handlers: Record<ModalType, () => void> = {
  100. add: () => {
  101. const defaultFormModel = createDefaultFormModel();
  102. handleUpdateFormModel(defaultFormModel);
  103. },
  104. edit: () => {
  105. if (props.editData) {
  106. handleUpdateFormModel(props.editData);
  107. }
  108. }
  109. };
  110. handlers[props.type]();
  111. }
  112. async function handleSubmit() {
  113. formRef.value?.validate();
  114. const params: Array<ApiBackground.Indicators> = [formModel];
  115. if (props.type === 'add') {
  116. const data = fetchIndicatorsAdd(params);
  117. data.then(res => {
  118. if (res.data) {
  119. window.$message?.success(`${titles[props.type]}成功!`);
  120. }
  121. });
  122. }
  123. if (props.type === 'edit') {
  124. const data = fetchIndicatorsEdit(formModel);
  125. data.then(res => {
  126. if (res.data) {
  127. window.$message?.success(`${titles[props.type]}成功!`);
  128. }
  129. });
  130. }
  131. closeModal();
  132. }
  133. watch(
  134. () => props.visible,
  135. newValue => {
  136. if (newValue) {
  137. handleUpdateFormModelByModalType();
  138. }
  139. }
  140. );
  141. </script>
  142. <style scoped></style>