request.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //设置域名
  2. const dev_baseUrl = 'https://test.hz-hanghui.com:18890/patrol-management'; //测试
  3. const pro_baseUrl = 'https://hhsz.hz-hanghui.com:8088/patrol-management'; //正式
  4. //是否是正式环境
  5. const env = true
  6. // const baseUrl = env ? pro_baseUrl : dev_baseUrl;
  7. let app = getApp();
  8. // https请求
  9. const request = e => {
  10. let token = uni.getStorageSync('token');
  11. return new Promise((resolve, reject) => {
  12. uni.request({
  13. url: env ? (pro_baseUrl + e.url) : (dev_baseUrl + e.url),
  14. method: e.method,
  15. data: e.data,
  16. header: {
  17. 'Content-Type': e.type,
  18. 'patrol-token': token ? token : null,
  19. },
  20. success: res => {
  21. if (res.data.code == 200) {
  22. resolve(res.data)
  23. uni.hideLoading(); //关闭loading
  24. } else if (res.data.code == 401) {
  25. uni.showToast({
  26. icon: 'none',
  27. title: '登录过期,请重新登录',
  28. duration: 2000,
  29. mask: true,
  30. success: () => {
  31. setTimeout(() => {
  32. uni.navigateTo({
  33. url: '/pages/login/index',
  34. })
  35. }, 800)
  36. }
  37. })
  38. uni.hideLoading(); //关闭loading
  39. } else {
  40. setTimeout(function() {
  41. uni.showToast({
  42. icon: 'none',
  43. title: res.data.msg,
  44. duration: 3000
  45. })
  46. }, 500);
  47. reject(res.data)
  48. uni.hideLoading(); //关闭loading
  49. };
  50. },
  51. fail: error => {
  52. wx.showToast({
  53. icon: 'none',
  54. title: error.errMsg,
  55. duration: 3000,
  56. });
  57. uni.hideLoading(); //关闭loading
  58. // reject(error);
  59. },
  60. complete: () => {
  61. uni.hideLoading(); //关闭loading
  62. }
  63. })
  64. })
  65. }
  66. // 链接另一个小程序
  67. // 是否是正式环境
  68. const wx_rlxf = true
  69. //设置域名
  70. const dev_baseUrl_wxrlxf = 'http://192.168.11.172:9111/yx-fyzd'; //测试
  71. const pro_baseUrl_wxrlxf = 'https://tx.hz-hanghui.com:8088/yx-fyzd'; //正式
  72. const baseUrl = wx_rlxf ? pro_baseUrl_wxrlxf : dev_baseUrl_wxrlxf;
  73. //https请求
  74. const request_wxrlxf = e => {
  75. return new Promise(function(resolve, reject) {
  76. wx.request({
  77. url: wx_rlxf ? (pro_baseUrl_wxrlxf + e.url) : (dev_baseUrl_wxrlxf + e.url),
  78. data: e.data,
  79. method: e.method,
  80. header: {
  81. 'content-type': e.type
  82. },
  83. success: res => {
  84. // app.globalData.requestTit = 'res:' + res
  85. if (res.statusCode == 200) {
  86. resolve(res.data)
  87. } else {
  88. reject(res.data)
  89. };
  90. },
  91. fail: error => {
  92. // app.globalData.requestTit = 'error:' + JSON.stringify(error) + 'data:' + JSON
  93. // .stringify(e)
  94. reject(error.errMsg)
  95. }
  96. })
  97. })
  98. }
  99. export {
  100. request,
  101. wx_rlxf,
  102. dev_baseUrl_wxrlxf,
  103. pro_baseUrl_wxrlxf,
  104. request_wxrlxf,
  105. }