index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import buildURL from '../helpers/buildURL'
  2. import buildFullPath from '../core/buildFullPath'
  3. import settle from '../core/settle'
  4. import {isUndefined} from "../utils"
  5. /**
  6. * 返回可选值存在的配置
  7. * @param {Array} keys - 可选值数组
  8. * @param {Object} config2 - 配置
  9. * @return {{}} - 存在的配置项
  10. */
  11. const mergeKeys = (keys, config2) => {
  12. let config = {}
  13. keys.forEach(prop => {
  14. if (!isUndefined(config2[prop])) {
  15. config[prop] = config2[prop]
  16. }
  17. })
  18. return config
  19. }
  20. export default (config) => {
  21. return new Promise((resolve, reject) => {
  22. let fullPath = buildURL(buildFullPath(config.baseURL, config.url), config.params, config.paramsSerializer)
  23. const _config = {
  24. url: fullPath,
  25. header: config.header,
  26. complete: (response) => {
  27. config.fullPath = fullPath
  28. response.config = config
  29. response.rawData = response.data
  30. try {
  31. let jsonParseHandle = false
  32. const forcedJSONParsingType = typeof config.forcedJSONParsing
  33. if (forcedJSONParsingType === 'boolean') {
  34. jsonParseHandle = config.forcedJSONParsing
  35. } else if (forcedJSONParsingType === 'object') {
  36. const includesMethod = config.forcedJSONParsing.include || []
  37. jsonParseHandle = includesMethod.includes(config.method)
  38. }
  39. // 对可能字符串不是json 的情况容错
  40. if (jsonParseHandle && typeof response.data === 'string') {
  41. response.data = JSON.parse(response.data)
  42. }
  43. // eslint-disable-next-line no-empty
  44. } catch (e) {
  45. }
  46. settle(resolve, reject, response)
  47. }
  48. }
  49. let requestTask
  50. if (config.method === 'UPLOAD') {
  51. delete _config.header['content-type']
  52. delete _config.header['Content-Type']
  53. let otherConfig = {
  54. // #ifdef MP-ALIPAY
  55. fileType: config.fileType,
  56. // #endif
  57. filePath: config.filePath,
  58. name: config.name
  59. }
  60. const optionalKeys = [
  61. // #ifdef APP-PLUS || H5
  62. 'files',
  63. // #endif
  64. // #ifdef H5
  65. 'file',
  66. // #endif
  67. // #ifdef H5 || APP-PLUS || MP-WEIXIN || MP-ALIPAY || MP-TOUTIAO || MP-KUAISHOU
  68. 'timeout',
  69. // #endif
  70. 'formData'
  71. ]
  72. requestTask = uni.uploadFile({..._config, ...otherConfig, ...mergeKeys(optionalKeys, config)})
  73. } else if (config.method === 'DOWNLOAD') {
  74. const optionalKeys = [
  75. // #ifdef H5 || APP-PLUS || MP-WEIXIN || MP-ALIPAY || MP-TOUTIAO || MP-KUAISHOU
  76. 'timeout',
  77. // #endif
  78. // #ifdef MP
  79. 'filePath',
  80. // #endif
  81. ]
  82. requestTask = uni.downloadFile({..._config, ...mergeKeys(optionalKeys, config)})
  83. } else {
  84. const optionalKeys = [
  85. 'data',
  86. 'method',
  87. // #ifdef H5 || APP-PLUS || MP-ALIPAY || MP-WEIXIN
  88. 'timeout',
  89. // #endif
  90. 'dataType',
  91. // #ifndef MP-ALIPAY
  92. 'responseType',
  93. // #endif
  94. // #ifdef APP-PLUS
  95. 'sslVerify',
  96. // #endif
  97. // #ifdef H5
  98. 'withCredentials',
  99. // #endif
  100. // #ifdef APP-PLUS
  101. 'firstIpv4',
  102. // #endif
  103. // #ifdef MP-WEIXIN
  104. 'enableHttp2',
  105. 'enableQuic',
  106. // #endif
  107. // #ifdef MP-TOUTIAO || MP-WEIXIN
  108. 'enableCache',
  109. // #endif
  110. // #ifdef MP-WEIXIN
  111. 'enableHttpDNS',
  112. 'httpDNSServiceId',
  113. 'enableChunked',
  114. 'forceCellularNetwork',
  115. // #endif
  116. // #ifdef MP-ALIPAY
  117. 'enableCookie',
  118. // #endif
  119. // #ifdef MP-BAIDU
  120. 'cloudCache',
  121. 'defer'
  122. // #endif
  123. ]
  124. requestTask = uni.request({..._config, ...mergeKeys(optionalKeys, config)})
  125. }
  126. if (config.getTask) {
  127. config.getTask(requestTask, config)
  128. }
  129. })
  130. }