Browse Source

array类型查询处理

lrf 9 months ago
parent
commit
98e222a6e1
2 changed files with 53 additions and 25 deletions
  1. 50 22
      src/utils/axios-wrapper.js
  2. 3 3
      src/views/thr/index.vue

+ 50 - 22
src/utils/axios-wrapper.js

@@ -1,7 +1,7 @@
 /* eslint-disable no-console */
 /* eslint-disable no-param-reassign */
 
-import { get, isObject } from 'lodash-es'
+import { get, isObject, isArray } from 'lodash-es'
 import Axios from 'axios'
 import { trimData, isNullOrUndefined } from './util-methods'
 import { ErrorCode } from './error-code'
@@ -36,6 +36,27 @@ export class AxiosWrapper {
     })
     return uri
   }
+  // 新函数,重新生成url,并且把参数拼进uri中
+  static getUriWithQuery(uri, query) {
+    if (!uri || !query) return uri
+    uri = `${uri}?`
+    const arr = []
+    for (const i in query) {
+      const val = query[i]
+      if (isArray(val)) {
+        for (const v of val) {
+          const str = `${i}=${v}`
+          arr.push(str)
+        }
+      } else {
+        const str = `${i}=${val}`
+        arr.push(str)
+      }
+    }
+    const sign = '&'
+    uri = `${uri}${arr.join(sign)}`
+    return uri
+  }
 
   $get(uri, query, options) {
     return this.$request(uri, undefined, query, options)
@@ -48,27 +69,34 @@ export class AxiosWrapper {
     options = { ...options, method: 'delete' }
     return this.$request(uri, data, query, options)
   }
-  async $request(uri, data, query, options) {
-    if (query && isObject(query)) {
-      const keys = Object.keys(query)
-      for (const key of keys) {
-        const val = get(query, key)
-        if (val === '') {
-          delete query[key]
-        }
-      }
-    }
-    if (isObject(query) && isObject(options)) {
-      options = { ...options, params: query, method: 'get' }
-    } else if (isObject(query) && !query.params) {
-      options = { params: query }
-    } else if (isObject(query) && query.params) {
-      options = query
-    }
-    if (!options) options = {}
-    if (options.params) options.params = trimData(options.params, null, null)
-    const params = get(options, 'params')
-    const url = AxiosWrapper.merge(uri, params)
+  async $request(uri, data, query, options = {}) {
+    // 此处需要重写,因为现在出现了同一个名称的变量要出现多次并且是不同的值 的情况
+    // if (query && isObject(query)) {
+    //   const keys = Object.keys(query)
+    //   for (const key of keys) {
+    //     const val = get(query, key)
+    //     if (val === '') {
+    //       delete query[key]
+    //     }
+    //   }
+    // }
+    /**
+     * 需要处理 query,options
+     * query: 将值拼入uri中
+     * options保持原数据即可,一般不会用
+     */
+    const url = AxiosWrapper.getUriWithQuery(uri, query)
+    // if (isObject(query) && isObject(options)) {
+    //   options = { ...options, params: query, method: 'get' }
+    // } else if (isObject(query) && !query.params) {
+    //   options = { params: query }
+    // } else if (isObject(query) && query.params) {
+    //   options = query
+    // }
+    // if (!options) options = {}
+    // if (options.params) options.params = trimData(options.params, null, null)
+    // const params = get(options, 'params')
+    // const url = AxiosWrapper.merge(uri, params)
     currentRequests += 1
     // Indicator.open({
     //   spinnerType: 'fading-circle',

+ 3 - 3
src/views/thr/index.vue

@@ -134,15 +134,15 @@ const toSearchInfo = async (data, type) => {
 // 筛选条件
 const toSearchFind = async () => {
   if (industry.value && industry.value.length > 0) {
-    searchForm.value.industryList = industry.value.map((i) => {
+    searchForm.value.industry = industry.value.map((i) => {
       return i.title
     })
   } else if (field.value && field.value.length > 0) {
-    searchForm.value.fieldList = field.value.map((i) => {
+    searchForm.value.field = field.value.map((i) => {
       return i.label
     })
   } else if (city.value && city.value.length > 0) {
-    searchForm.value.cityList = city.value.map((i) => {
+    searchForm.value.area = city.value.map((i) => {
       return i.name
     })
   } else searchForm.value = {}