request.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { logoutSuccess } from "@/store/user";
  2. import ToastUtil from "@/utils/toast_utils";
  3. import Taro from "@tarojs/taro";
  4. import { useDispatch } from "react-redux";
  5. interface RequestParam {
  6. url: string;
  7. method: 'POST' | 'GET' | 'DELETE' | 'PUT';
  8. data?: Record<string, any>;
  9. showAlert?: boolean;
  10. }
  11. interface Resp {
  12. statusCode?: number;
  13. header?: any;
  14. data?: any;
  15. errMsg?: string;
  16. };
  17. async function getStorage(key: string) {
  18. try {
  19. const res = await Taro.getStorage({ key });
  20. return res.data;
  21. } catch {
  22. return '';
  23. }
  24. }
  25. //X-Language:语言,X-Device-Id:设备唯一码,X-Platform:小程序/android/ios,X-Location:地区,X-Device:登录设备
  26. // header['X-Language'] = ''
  27. // header['X-Device-Id'] = ''
  28. // header['X-Platform'] = ''
  29. // header['X-Location'] = ''
  30. // header['X-Device'] = ''
  31. // header['X-Time-Zone-Id'] = Intl.DateTimeFormat().resolvedOptions().timeZone
  32. // header['Authorization'] = 'Bearer ' + wx.getStorageSync('token');
  33. // header['Authorization'] = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJhY2NvdW50Iiwic3ViIjoiMmQ5OWNlYzI0ZDFlMzE0Y2U1MjhlODM4MWMzYzk0MzgiLCJpc3MiOiJDT0RFUEFBUy5DT00iLCJuaWNrbmFtZSI6IueOi-a4nSIsInR5cCI6IkJlYXJlciIsInNlc3Npb25fc3RhdGUiOiIyN2RjNmU4ZDdjMWU1MTVmNDQwNzVjZTFlODk2ZmUzNCIsImV4cCI6MTcxNjY0Mzk5MSwiaWF0IjoxNjg1MDIxNTkxfQ.fmFj0OVNRzjLkdebSyGJyk8EScPJFpDiz0L25W35zoA'
  34. export async function request<T>(param: RequestParam): Promise<T> {
  35. const kTimeout = 4000;
  36. const kRetryCount = 2;
  37. let retryCount = 0;
  38. function performRequest(resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) {
  39. const { url, method, data } = param;
  40. var requestTask: any = null;
  41. let header: any = {};
  42. const token = global.token ? global.token : ''; //await getStorage('token')
  43. var split = new Date().toString().split(' ');
  44. var timeZoneFormatted = split[split.length - 2];
  45. header['content-type'] = 'application/json'
  46. header['X-Time-Zone'] = timeZoneFormatted; //new Date().getTimezoneOffset() / 60
  47. header['X-Platform'] = 'IOS'; //IOS ANDROID
  48. header['X-Lang'] = 'zh' //zh en
  49. header['X-Client-Type'] = 'WX_APP' //WX_APP APP
  50. header['X-Client_Version'] = '1.0'
  51. if (token.length > 0) {
  52. header['Authorization'] = `Bearer ${token}`;
  53. }
  54. const timer = setTimeout(() => {
  55. requestTask && requestTask.abort();
  56. console.log('timeout');
  57. if (retryCount < kRetryCount) {
  58. // Retry the request
  59. console.log(`Retrying request (${retryCount + 1}/3)...`);
  60. retryCount++;
  61. performRequest(resolve, reject);
  62. return;
  63. }
  64. // global.postBtnUpdateStatus('idle')
  65. ToastUtil.getInstance().showToast(method == 'GET' ? '网络连接失败,请检查网络' : '操作失败,请检查网络');
  66. // Taro.showToast({
  67. // icon:'none',
  68. // title: method=='GET'?'网络连接失败,请检查网络':'操作失败,请检查网络',
  69. // })
  70. reject('timeout');
  71. }, kTimeout);
  72. requestTask = Taro.request({
  73. url: url,
  74. method: method,
  75. header: header,
  76. // timeout: kTimeout,
  77. data: data || {},
  78. success: (response: Resp | { [key: string]: any }) => {
  79. clearTimeout(timer);
  80. const { statusCode, data } = response;
  81. if (statusCode != 200) {
  82. console.log(response)
  83. }
  84. if (statusCode >= 200 && statusCode < 300) {
  85. //正常数据返回
  86. var resp = {} as T;
  87. if (response.data) {
  88. resp = response.data as T;
  89. }
  90. resolve(resp);
  91. } else if (statusCode == 401) {
  92. // global.postBtnUpdateStatus('idle')
  93. //未登录或挤下线处理
  94. global.dispatch(logoutSuccess());
  95. } else if (statusCode == 500 && response.data.error_code == 'WX_STEP_PARSE_FAIL') {
  96. //单独对计步第一次请求失败处理
  97. resolve(response.data);
  98. } else {
  99. //通用错误处理
  100. Taro.showToast({
  101. icon: 'none',
  102. title: data.error_message,
  103. });
  104. reject(data);
  105. }
  106. },
  107. fail: err => {
  108. console.log('oooooooddd', err)
  109. // global.postBtnUpdateStatus('idle')
  110. // console.log('oppsu');
  111. // clearTimeout(timer);
  112. // reject(err);
  113. },
  114. });
  115. }
  116. return new Promise<T>((resolve, reject) => {
  117. performRequest(resolve, reject);
  118. });
  119. }