request.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * 检查HTTP请求状态
  3. */
  4. function checkStatus(response) {
  5. console.log('res1',response)
  6. if (response.status >= 200 && response.status < 300) {
  7. return response;
  8. }
  9. const error = new Error(response.statusText);
  10. error.response = response;
  11. throw error;
  12. }
  13. /**
  14. * 将Response内容转为JSON格式
  15. */
  16. function parseJSON(response) {
  17. return response.json();
  18. }
  19. /**
  20. * 统一拦截接口返回数据的状态,提示错误消息
  21. */
  22. function checkAPIDatas(data) {
  23. console.log('data',data)
  24. if (!data.success) {
  25. const code = data;
  26. }
  27. return data
  28. }
  29. function checkAPIError(err) {
  30. console.log('err',err)
  31. // const code = err.response.code;
  32. // const status = err.response.status;
  33. // const { url, type, oStatus, statusText } = err.response;
  34. // Message.error(`地址:${url} 类型:${type} 状态:${oStatus} 信息:${statusText}`, 10);
  35. return {
  36. err
  37. };
  38. }
  39. /**
  40. * Requests a URL, returning a promise.
  41. * @param {string} url The URL we want to request
  42. * @param {object} [options] The options we want to pass to "fetch"
  43. * @return {object} An object containing either "data" or "err"
  44. */
  45. const request = async (url, options) => {
  46. // global.storage.save({
  47. // key:'token',
  48. // data: '123123123123',
  49. // expires: null
  50. // });
  51. if (options) {
  52. const method = options.method.toUpperCase();
  53. options.method = method;
  54. // switch(method){
  55. // case 'POST':
  56. // options.headers = {
  57. // 'Accept': 'application/json',
  58. // 'Content-Type': '"Content-Type": "application/json',
  59. // "uid": 'c2e13090a563447c8744a8c03171d1db',
  60. // 'token': await storage.load({
  61. // key: 'token'
  62. // })
  63. // };
  64. // break;
  65. // case 'PUT':
  66. // options.headers = {
  67. // 'Accept': 'application/json',
  68. // 'Content-Type': 'application/x-www-form-urlencoded',
  69. // "uid": 'c2e13090a563447c8744a8c03171d1db',
  70. // // 'token': await storage.load({
  71. // // key: 'token'
  72. // // })
  73. // };
  74. // break;
  75. // default:
  76. // options.headers = {
  77. // 'token': await storage.load({
  78. // key: 'token'
  79. // })
  80. // };
  81. // break;
  82. // }
  83. // const token = await storage.load({
  84. // key: 'token'
  85. // });
  86. if (method === 'POST' || method === 'PATCH' || method === 'DELETE' || method === 'PUT') {
  87. options.headers = {
  88. 'Accept': 'application/json',
  89. 'Content-Type': 'application/json',
  90. "uid": 'c2e13090a563447c8744a8c03171d1db',
  91. // 'token': token? token : null,
  92. };
  93. options.body = JSON.stringify(options.body)
  94. }else {
  95. options.headers = {
  96. // 'token': await storage.load({
  97. // key: 'token'
  98. // }),
  99. "uid": 'c2e13090a563447c8744a8c03171d1db',
  100. };
  101. }
  102. }
  103. console.log('options',options)
  104. return fetch(url, options)
  105. .then(checkStatus)
  106. .then(parseJSON)
  107. .then(checkAPIDatas)
  108. .catch(checkAPIError);
  109. }
  110. export default request