api.js 3.3 KB

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