http.js 2.1 KB

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