request.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // switch(method){
  54. // case 'POST':
  55. // options.headers = {
  56. // 'Accept': 'application/json',
  57. // 'Content-Type': '"Content-Type": "application/json',
  58. // "uid": 'c2e13090a563447c8744a8c03171d1db',
  59. // 'token': await storage.load({
  60. // key: 'token'
  61. // })
  62. // };
  63. // break;
  64. // case 'PUT':
  65. // options.headers = {
  66. // 'Accept': 'application/json',
  67. // 'Content-Type': 'application/x-www-form-urlencoded',
  68. // "uid": 'c2e13090a563447c8744a8c03171d1db',
  69. // // 'token': await storage.load({
  70. // // key: 'token'
  71. // // })
  72. // };
  73. // break;
  74. // default:
  75. // options.headers = {
  76. // 'token': await storage.load({
  77. // key: 'token'
  78. // })
  79. // };
  80. // break;
  81. // }
  82. const token = await storage.load({
  83. key: 'token'
  84. });
  85. if (method === 'POST' || method === 'PATCH' || method === 'DELETE' || method === 'PUT') {
  86. options.headers = {
  87. 'Accept': 'application/json',
  88. 'Content-Type': 'application/json',
  89. "uid": 'c2e13090a563447c8744a8c03171d1db',
  90. 'token': token? token : null
  91. };
  92. options.body = JSON.stringify(options.body)
  93. }else {
  94. options.headers = {
  95. 'token': await storage.load({
  96. key: 'token'
  97. })
  98. };
  99. }
  100. }
  101. console.log('options',options)
  102. return fetch(url, options)
  103. .then(checkStatus)
  104. .then(parseJSON)
  105. .then(checkAPIDatas)
  106. .catch(checkAPIError);
  107. }
  108. export default request