mergeConfig.js 3.1 KB

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