Browse Source

feat(other): 增加事件执行设置

Yi 2 years ago
parent
commit
38b7ad47f2

+ 6 - 0
src/constants/business.ts

@@ -115,3 +115,9 @@ export const EventIsShowOptions: Common.OptionWithKey<BackgroundEvent.IsShowKey>
   { value: 1, label: EventIsShowLabels['1'] },
   { value: 2, label: EventIsShowLabels['2'] }
 ];
+
+export const EventFieldOptions: Common.OptionWithKey<BackgroundEvent.IsShowKey>[] = [
+  { value: 0, label: EventIsShowLabels['0'] },
+  { value: 1, label: EventIsShowLabels['1'] },
+  { value: 2, label: EventIsShowLabels['2'] }
+];

+ 1 - 0
src/typings/event.d.ts

@@ -22,5 +22,6 @@ declare namespace ApiBackground {
      * 事件执行的条件
      */
     conditions: string | null;
+    event_field: [] | null;
   }
 }

+ 78 - 3
src/views/background/event/components/table-action-modal.vue

@@ -13,6 +13,10 @@
         <n-form-item-grid-item :span="25" label="事件描述" path="remarks">
           <n-input v-model:value="formModel.remarks" />
         </n-form-item-grid-item>
+        <!--        <n-form-item-grid-item :span="25" label="字段关联" path="remarks">
+          <n-select v-model:value="value" multiple :options="options" />
+          <n-select v-model:value="value" multiple disabled :options="options" />
+        </n-form-item-grid-item>-->
       </n-grid>
       <n-space class="w-full pt-16px" :size="25" justify="end">
         <n-button class="w-72px" @click="closeModal">取消</n-button>
@@ -25,9 +29,78 @@
 <script setup lang="ts">
 import { ref, computed, reactive, watch } from 'vue';
 import type { FormInst, FormItemRule } from 'naive-ui';
+import { NButton } from 'naive-ui';
 import { EventIsShowOptions } from '@/constants';
 import { createRequiredFormRule } from '@/utils';
 
+// eslint-disable-next-line vue/no-export-in-script-setup
+/*
+export default defineComponent({
+  setup() {
+    return {
+      value: ref(['song3']),
+      options: [
+        {
+          label: "Everybody's Got Something to Hide Except Me and My Monkey",
+          value: 'song0',
+          disabled: true
+        },
+        {
+          label: 'Drive My Car',
+          value: 'song1'
+        },
+        {
+          label: 'Norwegian Wood',
+          value: 'song2'
+        },
+        {
+          label: "You Won't See",
+          value: 'song3',
+          disabled: true
+        },
+        {
+          label: 'Nowhere Man',
+          value: 'song4'
+        },
+        {
+          label: 'Think For Yourself',
+          value: 'song5'
+        },
+        {
+          label: 'The Word',
+          value: 'song6'
+        },
+        {
+          label: 'Michelle',
+          value: 'song7',
+          disabled: true
+        },
+        {
+          label: 'What goes on',
+          value: 'song8'
+        },
+        {
+          label: 'Girl',
+          value: 'song9'
+        },
+        {
+          label: "I'm looking through you",
+          value: 'song10'
+        },
+        {
+          label: 'In My Life',
+          value: 'song11'
+        },
+        {
+          label: 'Wait',
+          value: 'song12'
+        }
+      ]
+    };
+  }
+});
+*/
+
 export interface Props {
   /** 弹窗可见性 */
   visible: boolean;
@@ -78,7 +151,7 @@ const title = computed(() => {
 
 const formRef = ref<HTMLElement & FormInst>();
 
-type FormModel = Pick<BackgroundEvent.Event, 'name' | 'is_show' | 'remarks' | 'conditions'>;
+type FormModel = Pick<BackgroundEvent.Event, 'name' | 'is_show' | 'remarks' | 'conditions' | 'event_field'>;
 
 const formModel = reactive<FormModel>(createDefaultFormModel());
 
@@ -86,7 +159,8 @@ const rules: Record<keyof FormModel, FormItemRule | FormItemRule[]> = {
   name: createRequiredFormRule('请输入事件名称'),
   is_show: createRequiredFormRule('是否启用'),
   remarks: createRequiredFormRule('请输入事件描述'),
-  conditions: createRequiredFormRule('请输入事件执行条件')
+  conditions: createRequiredFormRule('请输入事件执行条件'),
+  event_field: createRequiredFormRule('请输入事件关联字段')
 };
 
 function createDefaultFormModel(): FormModel {
@@ -94,7 +168,8 @@ function createDefaultFormModel(): FormModel {
     name: '',
     is_show: null,
     remarks: '',
-    conditions: ''
+    conditions: '',
+    event_field: null
   };
 }
 

+ 187 - 17
src/views/background/event/components/table-condition-modal.vue

@@ -1,28 +1,198 @@
 <template>
-  <n-space :vertical="true" :size="16">
-    <n-card title="Tab Detail" :bordered="false" size="small" class="shadow-sm rounded-16px">
-      <n-space :vertical="true" :size="12">
-        <div>当前路由的描述数据(meta):</div>
-        <div>{{ route.meta }}</div>
-        <div>当前路由的查询数据(query):</div>
-        <div>{{ route.query }}</div>
-        <n-button @click="handleToTab">返回Tab</n-button>
+  <n-modal v-model:show="conditionVisible" preset="card" title="事件执行条件设置" style="width: 70%">
+    <n-form ref="formRef" label-placement="left" :label-width="80" :model="formModel" :rules="rules">
+      <n-grid :cols="48" :x-gap="18">
+        <n-form-item-grid-item :span="25" label="事件名称" path="name">
+          <n-input v-model:value="formModel.name" />
+        </n-form-item-grid-item>
+      </n-grid>
+      <n-space :size="25" justify="end">
+        <n-button type="primary" @click="addCondition">添加条件</n-button>
+        <n-button type="primary" @click="addConditionGroup">添加条件组</n-button>
       </n-space>
-    </n-card>
-  </n-space>
+      <n-data-table :columns="columns" :data="data" style="margin-top: 20px" />
+      <!-- <pre>{{ JSON.stringify(data, null, 2) }}</pre> -->
+    </n-form>
+  </n-modal>
 </template>
 
 <script setup lang="ts">
-import { useRoute } from 'vue-router';
-import { routeName } from '@/router';
-import { useRouterPush } from '@/composables';
+import { ref, computed, reactive, watch, h } from 'vue';
+import { NSelect, NInput } from 'naive-ui';
+import type { DataTableColumns, FormInst, FormItemRule } from 'naive-ui';
+import { createRequiredFormRule } from '@/utils';
+export interface Props {
+  /** 弹窗可见性 */
+  conditionVisible: boolean;
+  /**
+   * 弹窗类型
+   * add: 新增
+   * edit: 编辑
+   */
+  type?: 'add' | 'edit';
+  /** 编辑的表格行数据 */
+  editData?: BackgroundEvent.Event | null;
+}
+
+export type ModalConditionType = NonNullable<Props['type']>;
+
+defineOptions({ name: 'TableConditionModal' });
+
+type RowData = { key: number; name: string; age: string; address: string };
+const createData = (): RowData[] => [
+  {
+    key: 0,
+    name: 'John Brown',
+    age: '32',
+    address: 'New York No. 1 Lake Park'
+  },
+  { key: 1, name: 'Jim Green', age: '42', address: 'London No. 1 Lake Park' },
+  { key: 2, name: 'Joe Black', age: '32', address: 'Sidney No. 1 Lake Park' }
+];
+const data = ref(createData());
+const createColumns = (): DataTableColumns<RowData> => [
+  {
+    title: '条件组',
+    key: 'name',
+    render(row, index) {
+      return h(NInput, {
+        value: row.name,
+        onUpdateValue(v) {
+          data.value[index].name = v;
+        }
+      });
+    }
+  },
+  {
+    title: '选择事件',
+    key: 'age',
+    render(row, index) {
+      return h(NSelect, {
+        value: row.age,
+        onUpdateValue(v) {
+          data.value[index].age = v;
+        }
+      });
+    }
+  },
+  {
+    title: '选择字段',
+    key: 'address',
+    render(row, index) {
+      return h(NSelect, {
+        value: row.address,
+        onUpdateValue(v) {
+          data.value[index].address = v;
+        }
+      });
+    }
+  },
+  {
+    title: '条件',
+    key: 'address',
+    render(row, index) {
+      return h(NSelect, {
+        value: row.address,
+        onUpdateValue(v) {
+          data.value[index].address = v;
+        }
+      });
+    }
+  },
+  {
+    title: '条件值',
+    key: 'address',
+    render(row, index) {
+      return h(NInput, {
+        value: row.address,
+        onUpdateValue(v) {
+          data.value[index].address = v;
+        }
+      });
+    }
+  }
+];
+const columns = ref(createColumns());
+
+const props = withDefaults(defineProps<Props>(), {
+  type: 'add',
+  editData: null
+});
+
+interface Emits {
+  (e: 'update:conditionVisible', eventModelVisible: boolean): void;
+}
+
+const emit = defineEmits<Emits>();
+
+const conditionVisible = computed({
+  get() {
+    return props.conditionVisible;
+  },
+  set(visible) {
+    emit('update:conditionVisible', visible);
+  }
+});
+const closeModal = () => {
+  conditionVisible.value = false;
+};
+
+const formRef = ref<HTMLElement & FormInst>();
 
-const route = useRoute();
-const { routerPush } = useRouterPush();
+type FormModel = Pick<BackgroundEvent.Event, 'name'>;
 
-function handleToTab() {
-  routerPush({ name: routeName('function_tab') });
+const formModel = reactive<FormModel>(createDefaultFormModel());
+
+const rules: Record<keyof FormModel, FormItemRule | FormItemRule[]> = {
+  name: createRequiredFormRule('请输入事件名称')
+};
+
+function createDefaultFormModel(): FormModel {
+  return {
+    name: ''
+  };
+}
+
+function handleUpdateFormModel(model: Partial<FormModel>) {
+  Object.assign(formModel, model);
+}
+
+function handleUpdateFormModelByModalType() {
+  const handlers: Record<ModalConditionType, () => void> = {
+    add: () => {
+      const defaultFormModel = createDefaultFormModel();
+      handleUpdateFormModel(defaultFormModel);
+    },
+    edit: () => {
+      if (props.editData) {
+        handleUpdateFormModel(props.editData);
+      }
+    }
+  };
+
+  handlers[props.type]();
+}
+
+// 添加条件
+async function addCondition() {
+  await formRef.value?.validate();
+  window.$message?.success('添加成功!');
+  closeModal();
+}
+// 添加条件组
+async function addConditionGroup() {
+  await formRef.value?.validate();
+  window.$message?.success('添加成功!');
+  closeModal();
 }
+watch(
+  () => props.conditionVisible,
+  newValue => {
+    if (newValue) {
+      handleUpdateFormModelByModalType();
+    }
+  }
+);
 </script>
 
 <style scoped></style>

+ 17 - 7
src/views/background/event/index.vue

@@ -25,6 +25,11 @@
       </n-space>
       <n-data-table :columns="columns" :data="tableData" :loading="loading" :pagination="pagination" />
       <table-action-modal v-model:visible="visible" :type="modalType" :edit-data="editData" />
+      <table-condition-modal
+        v-model:conditionVisible="conditionVisible"
+        :type="modalConditionType"
+        :edit-data="editData"
+      />
     </n-card>
   </div>
 </template>
@@ -35,18 +40,19 @@ import type { Ref } from 'vue';
 import { NButton, NPopconfirm, NSpace, NTag } from 'naive-ui';
 import type { DataTableColumns, PaginationProps } from 'naive-ui';
 import { EventIsShowLabels } from '@/constants';
-import { routeName } from '@/router';
-import { useRouterPush } from '@/composables';
 import { useBoolean, useLoading } from '@/hooks';
 import { fetchEventDelete, fetchEventList } from '@/service/api/event';
 import TableActionModal from './components/table-action-modal.vue';
 import type { ModalType } from './components/table-action-modal.vue';
+import type { ModalConditionType } from './components/table-condition-modal.vue';
+import TableConditionModal from './components/table-condition-modal.vue';
 
 const { loading, startLoading, endLoading } = useLoading(false);
 const { bool: visible, setTrue: openModal } = useBoolean();
+const { bool: conditionVisible, setTrue: openEventModal } = useBoolean();
+
 const eventName = ref('');
 const tableData = ref<BackgroundEvent.Event[]>([]);
-const { routerPush } = useRouterPush();
 
 function setTableData(data: BackgroundEvent.Event[]) {
   tableData.value = data;
@@ -145,11 +151,16 @@ const columns: Ref<DataTableColumns<BackgroundEvent.Event>> = ref([
 ]) as Ref<DataTableColumns<BackgroundEvent.Event>>;
 
 const modalType = ref<ModalType>('add');
+const modalConditionType = ref<ModalConditionType>('add');
 
 function setModalType(type: ModalType) {
   modalType.value = type;
 }
 
+function setModalConditionType(type: ModalType) {
+  modalConditionType.value = type;
+}
+
 const editData = ref<BackgroundEvent.Event | null>(null);
 
 function setEditData(data: BackgroundEvent.Event | null) {
@@ -185,9 +196,8 @@ function handleCondition(rowId: number) {
   if (findItem) {
     setEditData(findItem);
   }
-  routerPush({ name: routeName('function_tab-detail'), query: { name: 'abc' }, hash: '#DEMO_HASH' });
-
-  openModal();
+  setModalConditionType('edit');
+  openEventModal();
 }
 
 function handleDeleteTable(rowId: number) {
@@ -197,7 +207,7 @@ function handleDeleteTable(rowId: number) {
       window.$message?.success('删除成功!');
     }
   });
-  init();
+  getTableData();
 }
 
 const pagination: PaginationProps = reactive({