Procházet zdrojové kódy

表单下拉和重复请求优化

zhaoke před 1 rokem
rodič
revize
0c6706902e
1 změnil soubory, kde provedl 68 přidání a 13 odebrání
  1. 68 13
      src/components/publicPageForm/index.vue

+ 68 - 13
src/components/publicPageForm/index.vue

@@ -76,7 +76,6 @@
 import { Query } from "@/api/webApi"
 import uploadItem from './upload.vue'
 import JSEncrypt from "jsencrypt"
-import { mapGetters } from 'vuex'
 
 export default {
   name: 'PublicPageForm',
@@ -120,9 +119,6 @@ export default {
       loading: false
     }
   },
-  computed: {
-    ...mapGetters(['authMsg']),
-  },
   watch: {
     formItem: {
       handler (array) {
@@ -186,32 +182,67 @@ export default {
       })
       return filterItem
     },
+    //去除重复请求
+    reduceHttp (arr = [], formItem = []) {
+      const datas = _.cloneDeep(arr)
+      const dataMap = new Map()
+      const [columnArrs1, columnArrs2, allResult, allResultKey] = [[], [], [], []]
+      if (!datas.length) return
+      datas.map(item => {
+        const { datacontent, dropdownlist } = item
+        const nameter = Object.entries(datacontent.filter)[0]
+        const namekey = dropdownlist + nameter[0] + nameter[1]
+        if (!dataMap.get(namekey)) {
+          dataMap.set(namekey, item)
+          columnArrs1.push(item)
+        } else {
+          columnArrs2.push(item)
+        }
+      })
+      columnArrs1.map(({ dropdownlist, pagecode, datacontent }) => {
+        allResultKey.push(pagecode)
+        allResult.push(Query({ serviceid: dropdownlist, datacontent, event: '0', page: 1, size: 9999 }))
+      })
+      this.getAnscyData(allResultKey, allResult, _.cloneDeep(formItem), _.cloneDeep(columnArrs1), _.cloneDeep(columnArrs2))
+    },
     //获取表单下拉数据
     getSelectData (formItem = []) {
       if (!formItem.length || !Array.isArray(formItem)) return
       const formItemCopy = _.cloneDeep(formItem)
-      const [allResult, allResultKey] = [[], []]
+      const [allResult, allResultKey, allResultList] = [[], [], []]
       formItemCopy.map(({ dropdownlist, pagecode, defaultparameters }) => {
         if (((dropdownlist || dropdownlist == 0) && dropdownlist != "")) {
           const datacontent = defaultparameters ? { filter: this.formatDefault(defaultparameters) } : { filter: { 1: 1 } }
-          allResultKey.push(pagecode)
-          allResult.push(Query({ serviceid: dropdownlist, datacontent, event: '0', page: 1, size: 9999 }))
+          const params = {
+            dropdownlist,
+            pagecode,
+            datacontent
+          }
+          allResultList.push(params)
         }
       })
-      this.getAnscyData(allResultKey, allResult, _.cloneDeep(formItem))
+      this.reduceHttp(allResultList, formItem)
     },
     //获取异步数据
-    async getAnscyData (allResultKey = [], allResult = [], formItem = []) {
+    async getAnscyData (allResultKey = [], allResult = [], formItem = [], columnArrs1 = [], columnArrs2 = []) {
       this.loading = true
       if (allResult.length && allResultKey.length) {
         const results = await Promise.allSettled(allResult)
+        const resultDatas = []
         results.map((item, index) => {
           const { status, value } = item
           if (status == 'fulfilled') {
             const { code, returnData } = value
+            const nitem = {
+              pagecode: allResultKey[index],
+              dropdownlist: columnArrs1[index].dropdownlist,
+              returnData
+            }
+            resultDatas.push(nitem)
             this.tableOptions[allResultKey[index]] = code == 0 && returnData?.length ? this.formatData(returnData) : []
           }
         })
+        if (resultDatas?.length) this.serRepets(_.cloneDeep(resultDatas), columnArrs1, columnArrs2)
         this.formItemArr = formItem
       } else {
         this.formItemArr = formItem
@@ -219,6 +250,30 @@ export default {
       this.setPassters(this.formPass)
       this.loading = false
     },
+    //设置重复下拉数据
+    serRepets (resultDatas = [], columnArrs1, columnArrs2) {
+      resultDatas.map(item => {
+        columnArrs1.map(ci => {
+          if (item.pagecode == ci.pagecode) {
+            item.datacontent = ci.datacontent
+          }
+        })
+      })
+      const result = [...resultDatas]
+      result.map(item => {
+        const { datacontent, dropdownlist, returnData } = item
+        const nameterItem = Object.entries(datacontent.filter)[0]
+        const namekeyItem = dropdownlist + nameterItem[0] + nameterItem[1]
+        columnArrs2.map(ci => {
+          const { datacontent, dropdownlist, pagecode } = ci
+          const nameterCi = Object.entries(datacontent.filter)[0]
+          const namekeyCi = dropdownlist + nameterCi[0] + nameterCi[1]
+          if (namekeyItem == namekeyCi) {
+            this.tableOptions[pagecode] = returnData
+          }
+        })
+      })
+    },
     //设置下拉数据
     setPassters (array = []) {
       const passitem = this.formItemArr.filter(item => item.pagecode == 'passparameters')
@@ -239,10 +294,10 @@ export default {
     },
     //获取表单下拉数据
     changeSelect (code) {
-      if (code == 'passparameters') {
-        return
-      }
-      const ndata = this.authMsg.filter(item => item.labelcode && item.pagecode == code)
+      if (code == 'passparameters') return
+      const datas = this.$store.state.auth.authMsg
+      if (!datas?.length) return
+      const ndata = datas.filter(item => item.labelcode && item.pagecode == code)
       if (ndata?.length) {
         const nitem = ndata[0]
         const nval = this.tableForm[code]