fetchRequest.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { getStrValue, isObjNull } from './common'
  2. export function fetchPost (url, params, header) {
  3. if (window.navigator.onLine == false) {
  4. return Promise.reject('网络连接失败,请检查网络连接')
  5. }
  6. // const userInfo = store.getState().redUserInfo
  7. if (isObjNull(header)) {
  8. header = {}
  9. }
  10. let formData = new FormData()
  11. if (params) {
  12. for (let key in params) {
  13. // if ((typeof params[key]) === 'string') {
  14. // formData.append(key, encodeURI(params[key].toString()))
  15. // } else {
  16. formData.append(key, params[key])
  17. // }
  18. }
  19. }
  20. const request = fetch(url, {
  21. method: 'POST',
  22. body: formData,
  23. mode: 'cors',
  24. credentials: 'include',
  25. // cache: "force-cache",
  26. headers: new Headers({
  27. 'Accept': 'application/json',
  28. // "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
  29. // "Authorization": userInfo.token,
  30. ...header,
  31. }),
  32. })
  33. return fetchResult(request)
  34. }
  35. export function fetchPostObj (url, params, header) {
  36. if (window.navigator.onLine == false) {
  37. return Promise.reject('网络连接失败,请检查网络连接')
  38. }
  39. // const userInfo = store.getState().redUserInfo
  40. if (isObjNull(header)) {
  41. header = {}
  42. }
  43. const request = fetch(url, {
  44. method: 'POST',
  45. body: JSON.stringify(params),
  46. mode: 'cors',
  47. credentials: 'include',
  48. // cache: "force-cache",
  49. headers: new Headers({
  50. 'Accept': 'application/json',
  51. // "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
  52. // "Authorization": userInfo.token,
  53. ...header,
  54. }),
  55. })
  56. return fetchResult(request)
  57. }
  58. export function fetchGet (url, params, header) {
  59. if (window.navigator.onLine == false) {
  60. return Promise.reject('网络连接失败,请检查网络连接')
  61. }
  62. // const userInfo = store.getState().redUserInfo
  63. if (isObjNull(header)) {
  64. header = {}
  65. }
  66. if (params) {
  67. let paramsArray = []
  68. //拼接参数
  69. Object.keys(params).forEach(key =>
  70. paramsArray.push(
  71. key + '=' + encodeURI(getStrValue(params, key).toString())))
  72. if (paramsArray.length > 0) {
  73. if (url.search(/\?/) === -1) {
  74. url += '?' + paramsArray.join('&')
  75. } else {
  76. url += '&' + paramsArray.join('&')
  77. }
  78. }
  79. }
  80. const request = fetch(url, {
  81. method: 'GET',
  82. mode: 'cors',
  83. credentials: 'include',
  84. // cache: "force-cache",
  85. headers: new Headers({
  86. 'Accept': 'application/json',
  87. 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
  88. // "Authorization": userInfo.token,
  89. ...header,
  90. }),
  91. })
  92. return fetchResult(request)
  93. }
  94. /**
  95. * 处理网络请求结果
  96. * @param request
  97. * @returns {*}
  98. */
  99. function fetchResult (request) {
  100. try {
  101. return request.then(response => {
  102. if (response.status == 200) {
  103. return response
  104. } else {
  105. throw response
  106. }
  107. }).catch(error => {
  108. if (error.json) {
  109. return error.json()
  110. } else {
  111. return Promise.reject('请求异常')
  112. }
  113. }).then(result => {
  114. if (result.status == 200) {
  115. let resultJson = result.json()
  116. return resultJson
  117. } else {
  118. if (result.exceptionInfo) {
  119. if (result.exceptionInfo.length > 80) {
  120. throw '接口请求异常'
  121. } else {
  122. throw result.exceptionInfo.replace(/<[\/\!]*[^<>]*>/ig, '')
  123. }
  124. } else if (result.message) {
  125. if (result.message.length > 80) {
  126. throw '接口请求异常'
  127. } else {
  128. throw result.message.replace(/<[\/\!]*[^<>]*>/ig, '')
  129. }
  130. } else {
  131. throw result
  132. }
  133. }
  134. })
  135. } catch (e) {
  136. return Promise.reject('请求异常')
  137. }
  138. }