fetchRequest.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import {getStrValue, isObjNull} from "./common";
  2. import store from './../redux/store/store'
  3. export function fetchPost(url, params, header) {
  4. if (window.navigator.onLine == false) {
  5. return Promise.reject('网络连接失败,请检查网络连接')
  6. }
  7. const userInfo = store.getState().redUserInfo
  8. if (isObjNull(header)) {
  9. header = {}
  10. }
  11. let formData = new FormData()
  12. if (params) {
  13. for (let key in params) {
  14. // if ((typeof params[key]) === 'string') {
  15. // formData.append(key, encodeURI(params[key].toString()))
  16. // } else {
  17. formData.append(key, params[key])
  18. // }
  19. }
  20. }
  21. const request = fetch(url, {
  22. method: 'POST',
  23. body: formData,
  24. mode: 'cors',
  25. // credentials: 'include',
  26. // cache: "force-cache",
  27. headers: new Headers({
  28. 'Accept': 'application/json',
  29. // "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
  30. "Authorization": userInfo.token,
  31. ...header
  32. })
  33. })
  34. return fetchResult(request)
  35. }
  36. export function fetchPostObj(url, params, header) {
  37. if (window.navigator.onLine == false) {
  38. return Promise.reject('网络连接失败,请检查网络连接')
  39. }
  40. const userInfo = store.getState().redUserInfo
  41. if (isObjNull(header)) {
  42. header = {}
  43. }
  44. const request = fetch(url, {
  45. method: 'POST',
  46. body: params,
  47. mode: 'cors',
  48. // credentials: 'include',
  49. // cache: "force-cache",
  50. headers: new Headers({
  51. 'Accept': 'application/json',
  52. // "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
  53. "Authorization": userInfo.token,
  54. ...header
  55. })
  56. })
  57. return fetchResult(request)
  58. }
  59. export function fetchGet(url, params, header) {
  60. if (window.navigator.onLine == false) {
  61. return Promise.reject('网络连接失败,请检查网络连接')
  62. }
  63. const userInfo = store.getState().redUserInfo
  64. if (isObjNull(header)) {
  65. header = {}
  66. }
  67. if (params) {
  68. let paramsArray = [];
  69. //拼接参数
  70. Object.keys(params).forEach(key =>
  71. paramsArray.push(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 > 30) {
  120. throw '接口请求异常'
  121. } else {
  122. throw result.exceptionInfo
  123. }
  124. } else {
  125. throw result
  126. }
  127. }
  128. }).then(result => {
  129. console.log(result)
  130. if (result.success) {
  131. return result
  132. } else {
  133. if (result.exceptionInfo) {
  134. if (result.exceptionInfo.length > 30) {
  135. throw '接口请求异常'
  136. } else {
  137. throw result.exceptionInfo
  138. }
  139. } else if (result.message) {
  140. if (result.message.length > 30) {
  141. throw '接口请求异常'
  142. } else {
  143. throw result.message
  144. }
  145. } else {
  146. throw result
  147. }
  148. }
  149. })
  150. } catch (e) {
  151. return Promise.reject('请求异常')
  152. }
  153. }