api.js 2.7 KB

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