http.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // 本地环境
  2. //const $perfix = 'http://localhost:8111';
  3. // 测试环境
  4. const $perfix = 'http://114.135.61.188:58082';
  5. // 演示环境
  6. // const $perfix = 'https://water.zoniot.com';
  7. // 正式环境
  8. //const $perfix = 'https://water.zoniot.com';
  9. const toLogin = (status = 0) => {
  10. if (status !== -406) return;
  11. wx.removeStorageSync('token');
  12. getApp().token = '';
  13. };
  14. const httpFun = (url, data, type, isForm) => {
  15. let token = getApp().token || wx.getStorageSync('token');
  16. return new Promise(resolve => {
  17. const filterUrl = [
  18. '/user/openidLogin',
  19. ];
  20. if (token && filterUrl.indexOf(url) == -1) {
  21. token = 'Bearer ' + token;
  22. } else {
  23. token = null;
  24. }
  25. wx.request({
  26. url: `${$perfix}/api${url}`,
  27. data: data,
  28. method: type,
  29. header: {
  30. [token && "Authorization"]: token,
  31. [isForm && "Content-Type"]: "application/x-www-form-urlencoded"
  32. },
  33. complete(res) {
  34. if (res.statusCode === 200) {
  35. toLogin(res.data.status);
  36. resolve(res.data);
  37. } else if (res.statusCode >= 500 && res.statusCode < 520) {
  38. wx.hideLoading();
  39. wx.showModal({
  40. title: '提示',
  41. content: '网络或服务异常,点击“确定”返回当前页',
  42. showCancel: false,
  43. success(res) {
  44. getApp().globalData = {};
  45. getApp().getUserInfo(getApp().token);
  46. }
  47. })
  48. resolve(res);
  49. return;
  50. } else {
  51. // wx.showToast({
  52. // title: res.statusCode + '',
  53. // icon: 'none'
  54. // })
  55. resolve(res);
  56. }
  57. }
  58. })
  59. })
  60. }
  61. module.exports = {
  62. domain: $perfix,
  63. postForm(url, data) {
  64. return httpFun(url, data, 'post', true);
  65. },
  66. post(url, data) {
  67. return httpFun(url, data, 'post', false);
  68. },
  69. deleteForm(url, data) {
  70. return httpFun(url, data, 'delete', true);
  71. },
  72. get(url, data) {
  73. return httpFun(url, data, 'get', false);
  74. }
  75. }