api.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // const BASE_URL='http://8.136.98.49:8002'
  2. // let BASE_URL = 'http://192.168.1.112:8002'
  3. export const myRequest = (options) => {
  4. let BASE_URL = uni.getStorageSync('http');
  5. console.log(BASE_URL, 'my request', process.env.NODE_ENV);
  6. if (BASE_URL == '') {
  7. // BASE_URL = 'http://114.55.0.7:8002'//测试
  8. // BASE_URL = process.env.NODE_ENV === 'development' ? "http://192.168.2.118:8003" : "http://8.136.98.49:8002"
  9. // BASE_URL =
  10. // process.env.NODE_ENV === 'development'
  11. // ? 'http://114.55.0.7:8002'
  12. // : 'http://8.136.98.49:8002';
  13. BASE_URL =
  14. process.env.NODE_ENV === 'development'
  15. ? 'http://8.136.98.49:8002'
  16. : 'http://8.136.98.49:8002';
  17. // BASE_URL = 'http://192.168.1.77:8002'
  18. // BASE_URL = 'http://218.28.198.186:10508';
  19. }
  20. // BASE_URL = 'http://192.168.1.52:8003'
  21. // console.log(BASE_URL)
  22. var session_key = '';
  23. session_key = uni.getStorageSync('session_key');
  24. let url = '';
  25. let data = options.data || {};
  26. let sfType = options.sfType || '';
  27. if (options.url.split('=')[1]) {
  28. url = options.url.split('=')[1];
  29. } else {
  30. url = options.url.split('api/')[1];
  31. }
  32. // console.log(url)
  33. if (
  34. url != 'user.login.login_user' &&
  35. url != 'pest.pests.insect_discern' &&
  36. url != 'pest.pests.plant_discern' &&
  37. url != 'pest.pests.pests_contrast' &&
  38. url != 'pest.pests.pests_expert_img' &&
  39. url != 'pest.pests.pests_img' &&
  40. url != 'recognizationSys' &&
  41. url != 'base.bases.base_photo' &&
  42. url != 'pest.warning_record.rolemanage_img' &&
  43. url != 'home.homes.personal_photo' &&
  44. url != 'ascend.ascend_manage.product_info' &&
  45. url != 'ascend.ascend_manage.quality_info' &&
  46. url != 'ascend.ascend_manage.grow_info' &&
  47. url != 'ascend.ascend_manage.all_ascend' &&
  48. url != 'after_sale.after_sale_manage.device_check' &&
  49. url != 'after_sale.after_sale_manage.aftersale_apply'
  50. ) {
  51. data.token = session_key;
  52. }
  53. return new Promise((resolve, reject) => {
  54. uni.request({
  55. url: BASE_URL + options.url,
  56. method: options.method || 'POST',
  57. header: options.header || {
  58. 'Content-Type': 'application/x-www-form-urlencoded',
  59. },
  60. data: data,
  61. success: (res) => {
  62. if (res.data.errorCode == 403) {
  63. uni.removeStorageSync('session_key');
  64. uni.showToast({
  65. title: '登录已过期,请重新登录!',
  66. icon: 'none',
  67. });
  68. if (uni.getStorageSync('isLink')) {
  69. return false;
  70. } else {
  71. uni.setStorageSync('isLink', true);
  72. return uni.navigateTo({
  73. url: '/pages/login/login',
  74. });
  75. }
  76. }
  77. if (res.data.message) {
  78. if (
  79. res.data.message == '识别无结果' ||
  80. res.data.message == '该设备未绑定SIM'
  81. ) {
  82. resolve(res.data.data);
  83. } else {
  84. return uni.showToast({
  85. title: res.data.message,
  86. icon: 'none',
  87. });
  88. }
  89. }
  90. if (sfType) {
  91. resolve(res.data);
  92. return;
  93. }
  94. if (res.data.data) {
  95. resolve(res.data.data);
  96. } else {
  97. resolve(res.data);
  98. }
  99. },
  100. fail: (err) => {
  101. uni.showToast({
  102. title: '请求接口失败',
  103. icon: 'none',
  104. });
  105. reject(err);
  106. },
  107. });
  108. });
  109. };