Prechádzať zdrojové kódy

fix: map与array的辅助函数

lvkun996 2 rokov pred
rodič
commit
b59d97fc3b
4 zmenil súbory, kde vykonal 71 pridanie a 35 odobranie
  1. 2 2
      src/global.d.ts
  2. 10 12
      src/main.ts
  3. 59 20
      src/pages/Iot/rule/forwardRule.vue
  4. 0 1
      src/router/index.ts

+ 2 - 2
src/global.d.ts

@@ -6,12 +6,12 @@ interface Object {
  /**
   * @description  Map数据格式转为数组
  */
-  mapToArray(map: Map<any, any>): any[];
+  toArray(): any[];
 
 /**
   * @description  对象数组转为MAP结构,需要指定转换的key,
   * 指定value时,MAP的value为指定的key,不指定value的key,
   * 则整个对象变为MAP结构的value
  */
-  arrayToMap(array: Record<string, any>[], key: string, valueKey?: string): Map<string, any>
+  toMap(key: string, valueKey?: string): Map<string, any>
 }

+ 10 - 12
src/main.ts

@@ -17,7 +17,8 @@ const pinia = createPinia()
 pinia.use(piniaPluginPersistedstate)
 
 const app = createApp(App)
-app.use(router)
+app
+  .use(router)
   .use(pinia)
   .use(antd)
   .use(UsePro)
@@ -32,8 +33,9 @@ app.config.warnHandler = (msg, instance, trace) => {
 }
 
 // 全局注册一些辅助性的函数 desc在 global.d.ts下
-window.Object.prototype.mapToArray = function (map: Map<any, any>) {
-  return Array.from(map, ([key, value]) => {
+window.Map.prototype.toArray = function () {
+  if (this === null || this === undefined) return this
+  return Array.from(this, ([key, value]) => {
     if (typeof value === 'object') {
       return { key, ...value }
     } else {
@@ -42,18 +44,14 @@ window.Object.prototype.mapToArray = function (map: Map<any, any>) {
   })
 }
 
-window.Object.prototype.arrayToMap = function (array: Record<string, any>[], key: string, valueKey?: string): Map<string, any> {
-  if (typeof array[0] !== 'object') {
-    throw new Error('仅支持Record<string, any>[]格式的数组')
-  }
+window.Array.prototype.toMap = function (key: string, valueKey?: string): Map<string, any> {
+  if (this && typeof this[0] !== 'object') throw new Error('仅支持Record<string, any>[]格式的数组')
+  else if (this == null || this === undefined) return this
+
   const _map = new Map()
-  array.forEach(item => {
+  this.forEach(item => {
     _map.set(item[key], valueKey ? item[valueKey] : item)
   })
 
   return _map
 }
-
-const a = new Map([['a', 1]])
-
-Object.mapToArray(a)

+ 59 - 20
src/pages/Iot/rule/forwardRule.vue

@@ -1,11 +1,30 @@
 <template>
-  <StatisticsTemplate
+
+<StatisticsTemplate
     title="规则统计"
     :list="state.forwardCount"
-  />
+/>
+
 <a-card style="margin-top: 20px;" >
   <a-row justify="space-between"   >
     <a-col :span="18" >
+
+    </a-col>
+    <a-col>
+      <a-space>
+        <a-button  type="primary" @click="search">搜索</a-button>
+        <a-button type="primary" @click="openModal('add', {})" >创建规则</a-button>
+      </a-space>
+
+    </a-col>
+  </a-row>
+  <table-pro
+    style="margin-top: 20px;"
+    :columns="columns"
+    :service="RuleController.pageForward"
+    :serviceParams="queryParams"
+  >
+    <template #search >
       <a-row style="width: 100%"  :gutter="[8,8]" >
         <a-col :xs="20" :md="12" :xl="5">
           <a-input  allowClear v-model:value="queryParams.ruleId" placeholder="请输入规则id"></a-input>
@@ -47,16 +66,43 @@
               </a-select>
         </a-col>
       </a-row>
-    </a-col>
-    <a-col>
-      <a-space>
-        <a-button  type="primary" @click="search">搜索</a-button>
-        <a-button type="primary" @click="openModal('add', {})" >创建规则</a-button>
-      </a-space>
-
-    </a-col>
-  </a-row>
-  <a-table
+    </template>
+    <template #render="{column, record}"  >
+      <template v-if="column.key === 'id'" >
+          <a  @click="openDetailModal(record.id)">{{record.id}}</a>
+      </template>
+      <template v-if="column.key === 'subjectResource'" >
+        <a-tag >{{RuleController.SubjectResourceMap.get(record.subjectResource)?.name}}</a-tag>
+      </template>
+      <template v-if="column.key === 'subjectEvent'" >
+        <a-tag >{{RuleController.SubjectEventMap.get(record.subjectEvent)?.name}}</a-tag>
+      </template>
+      <template v-if="column.key === 'status'" >
+        <a-switch
+          v-model:checked="record.status"
+          checked-children="运行中"
+          un-checked-children="已停止"
+          @click="changeStatus(record)"
+        />
+      </template>
+      <template v-if="column.key === 'action'" >
+        <a-space>
+              <a @click="openModalDebug(record.id)">调试</a>
+              <a @click="openDetailModal(record.id)">详情</a>
+              <a @click="openModal('update', record)">编辑</a>
+              <a-popconfirm
+                  title="确实要删除吗?"
+                  ok-text="确定"
+                  cancel-text="取消"
+                  @confirm="delForwardRule(record.id)"
+                >
+                  <a>删除</a>
+              </a-popconfirm>
+        </a-space>
+      </template>
+    </template>
+  </table-pro>
+  <!-- <a-table
     style="margin-top: 20px;"
     :columns="columns"
     :data-source="state.dataSource"
@@ -98,7 +144,7 @@
         </a-space>
       </template>
     </template>
-  </a-table>
+  </a-table> -->
 </a-card>
 
 <!-- 创建规则 -->
@@ -187,9 +233,6 @@
       </a-table>
       <a-empty v-else :image="Empty.PRESENTED_IMAGE_SIMPLE" />
     </div>
-    <!-- <div v-if="state.stepCount == 2" >
-      <a-button @click="addForwardRule" >启动规则</a-button>
-    </div> -->
 </modal-pro>
 
 <modal-pro
@@ -429,7 +472,6 @@
 <script lang='ts' setup >
 import { onMounted, reactive, ref } from 'vue'
 import { RuleController } from '@/controller/index'
-import type { FormItemProps } from '@/components/FormPro/index.vue'
 import { Form, Empty, message } from 'ant-design-vue'
 import { computed } from '@vue/reactivity'
 import TestDialog from './components/testDialog.vue'
@@ -568,9 +610,6 @@ const formProo = ref('')
 const testDialogRef = ref('')
 
 let queryParams = reactive({
-  page: 1,
-  pageSize: 10,
-  total: 0,
   ruleId: '',
   ruleLabel: '',
   status: '',

+ 0 - 1
src/router/index.ts

@@ -150,7 +150,6 @@ const iot = {
       path: '/task',
       name: '任务中心',
       redirect: '/task/manage',
-      // <container-outlined />
       icon: 'ContainerOutlined',
       children: [
         {