api.js 3.5 KB

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