request.js 3.1 KB

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