api.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import config from './neutral.js';
  2. // token 失效全局拦截:清除登录态并跳转登录页(并发请求去重)
  3. let isRedirecting = false;
  4. const handleTokenExpired = () => {
  5. uni.removeStorageSync('session_key');
  6. uni.removeStorageSync('isLink'); // 清理旧版本遗留的跳转标记
  7. // 并发请求同时返回 403 时只处理一次
  8. if (isRedirecting) {
  9. return;
  10. }
  11. isRedirecting = true;
  12. // 已在登录页则不重复跳转
  13. const pages = getCurrentPages();
  14. const currentRoute = pages.length ? pages[pages.length - 1].route : '';
  15. if (currentRoute === 'pages/login/login') {
  16. isRedirecting = false;
  17. return;
  18. }
  19. uni.showToast({
  20. title: '登录已过期,请重新登录!',
  21. icon: 'none',
  22. });
  23. // reLaunch 清空页面栈,避免返回键回到已失效页面
  24. uni.reLaunch({
  25. url: '/pages/login/login',
  26. complete: () => {
  27. isRedirecting = false;
  28. },
  29. });
  30. };
  31. export const myRequest = (options) => {
  32. let BASE_URL = uni.getStorageSync('http');
  33. console.log(BASE_URL, 'my request', process.env.NODE_ENV);
  34. if (BASE_URL == '') {
  35. // BASE_URL =
  36. // process.env.NODE_ENV === 'development'
  37. // ? 'https://uat.hnyfwlw.com'
  38. // : 'https://web.hnyfwlw.com';
  39. // BASE_URL = 'http://192.168.1.107:8000'
  40. // BASE_URL = 'http://218.28.198.186:10508';
  41. // BASE_URL = 'http://8.136.98.49:8002';
  42. }
  43. // BASE_URL = config.productAPI;
  44. BASE_URL = config.developAPI;
  45. var session_key = '';
  46. session_key = uni.getStorageSync('session_key');
  47. let url = '';
  48. let data = options.data || {};
  49. let sfType = options.sfType || '';
  50. if (options.url.split('=')[1]) {
  51. url = options.url.split('=')[1];
  52. } else {
  53. url = options.url.split('api/')[1];
  54. }
  55. // console.log(url)
  56. if (
  57. url != 'user.login.login_user' &&
  58. url != 'pest.pests.insect_discern' &&
  59. url != 'pest.pests.plant_discern' &&
  60. url != 'pest.pests.pests_contrast' &&
  61. url != 'pest.pests.pests_expert_img' &&
  62. url != 'pest.pests.pests_img' &&
  63. url != 'recognizationSys' &&
  64. url != 'base.bases.base_photo' &&
  65. url != 'pest.warning_record.rolemanage_img' &&
  66. url != 'home.homes.personal_photo' &&
  67. url != 'ascend.ascend_manage.product_info' &&
  68. url != 'ascend.ascend_manage.quality_info' &&
  69. url != 'ascend.ascend_manage.grow_info' &&
  70. url != 'ascend.ascend_manage.all_ascend' &&
  71. url != 'after_sale.after_sale_manage.device_check' &&
  72. url != 'after_sale.after_sale_manage.aftersale_apply'
  73. ) {
  74. data.token = session_key;
  75. }
  76. return new Promise((resolve, reject) => {
  77. uni.request({
  78. url: BASE_URL + options.url,
  79. method: options.method || 'POST',
  80. header: options.header || {
  81. 'Content-Type': 'application/x-www-form-urlencoded',
  82. },
  83. data: data,
  84. success: (res) => {
  85. // 全局拦截:token 失效(errorCode 403),清除登录态并跳转登录页
  86. if (res.data.errorCode == 403) {
  87. handleTokenExpired();
  88. return reject(res.data.message || '登录已过期');
  89. }
  90. if (res.data.message) {
  91. if (
  92. res.data.message == '识别无结果' ||
  93. res.data.message == '该设备未绑定SIM' ||
  94. res.data.message == 'success'
  95. ) {
  96. resolve(res.data.data);
  97. } else {
  98. reject(res.data.message);
  99. return uni.showToast({
  100. title: res.data.message,
  101. icon: 'none',
  102. });
  103. }
  104. }
  105. if (sfType) {
  106. resolve(res.data);
  107. return;
  108. }
  109. if (res.data.data) {
  110. resolve(res.data.data);
  111. } else {
  112. resolve(res.data);
  113. }
  114. },
  115. fail: (err) => {
  116. uni.showToast({
  117. title: '请求接口失败',
  118. icon: 'none',
  119. });
  120. reject(err);
  121. },
  122. });
  123. });
  124. };