api.js 3.3 KB

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