api.js 3.0 KB

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