绿满眶商城微信小程序-uniapp
xiangpei
2025-05-09 c3e6cdbb29580e77444541c7953aca33581a4267
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import {deepMerge, isObject} from '../utils'
 
/**
 * 合并局部配置优先的配置,如果局部有该配置项则用局部,如果全局有该配置项则用全局
 * @param {Array} keys - 配置项
 * @param {Object} globalsConfig - 当前的全局配置
 * @param {Object} config2 - 局部配置
 * @return {{}}
 */
const mergeKeys = (keys, globalsConfig, config2) => {
  let config = {}
  keys.forEach(prop => {
    if (typeof config2[prop] !== 'undefined') {
      config[prop] = config2[prop]
    } else if (typeof globalsConfig[prop] !== 'undefined') {
      config[prop] = globalsConfig[prop]
    }
  })
  return config
}
/**
 *
 * @param globalsConfig - 当前实例的全局配置
 * @param config2 - 当前的局部配置
 * @return - 合并后的配置
 */
export default (globalsConfig, config2 = {}) => {
  const method = config2.method || globalsConfig.method || 'GET'
  let config = {
    baseURL: globalsConfig.baseURL || '',
    method: method,
    url: config2.url || ''
  }
  const mergeDeepPropertiesKeys = ['header', 'params', 'custom']
  const defaultToConfig2Keys = ['getTask', 'validateStatus']
  mergeDeepPropertiesKeys.forEach(prop => {
    if (isObject(config2[prop])) {
      config[prop] = deepMerge(globalsConfig[prop], config2[prop])
    } else if (typeof config2[prop] !== 'undefined') {
      config[prop] = config2[prop]
    } else if (isObject(globalsConfig[prop])) {
      config[prop] = deepMerge(globalsConfig[prop])
    } else if (typeof globalsConfig[prop] !== 'undefined') {
      config[prop] = globalsConfig[prop]
    }
  })
  config = {...config, ...mergeKeys(defaultToConfig2Keys, globalsConfig, config2)}
 
  // eslint-disable-next-line no-empty
  if (method === 'DOWNLOAD') {
 
  } else if (method === 'UPLOAD') {
    if (isObject(config.header)) {
      delete config.header['content-type']
      delete config.header['Content-Type']
    }
    const uploadKeys = [
      // #ifdef APP-PLUS || H5
      'files',
      // #endif
      // #ifdef MP-ALIPAY
      'fileType',
      // #endif
      // #ifdef H5
      'file',
      // #endif
      'filePath',
      'name',
      'formData',
    ]
    uploadKeys.forEach(prop => {
      if (typeof config2[prop] !== 'undefined') {
        config[prop] = config2[prop]
      }
    })
  } else {
    const defaultsKeys = [
      'data',
      // #ifdef MP-ALIPAY || MP-WEIXIN
      'timeout',
      // #endif
      'dataType',
      // #ifndef MP-ALIPAY || APP-PLUS
      'responseType',
      // #endif
      // #ifdef APP-PLUS
      'sslVerify',
      // #endif
      // #ifdef H5
      'withCredentials'
      // #endif
    ]
    config = {...config, ...mergeKeys(defaultsKeys, globalsConfig, config2)}
  }
 
  return config
}