http.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. var app=getApp();
  2. function request(url, method,header, data, success) {
  3. let promise = new Promise(function(resolve, reject) {
  4. wx.request({
  5. url,
  6. header,
  7. data,
  8. method,
  9. success: res => {
  10. if (typeof res.data === "object") {
  11. if(!!res.header.Authorization){
  12. app.globalData.token=res.header.Authorization.split(" ")[1];
  13. }
  14. if(!!res.data.token){
  15. app.globalData.token=res.data.token;
  16. }
  17. if (res.data.code === 403) {
  18. wx.reLaunch({
  19. url: '/pages/index/index',
  20. })
  21. reject(res.data.msg);
  22. }else if(res.data.code === 601){
  23. wx.reLaunch({
  24. url: '/pages/other/other?type=32',
  25. })
  26. reject(res.data.msg);
  27. }else if(res.data.code != 0&&!data.alertshow&&res.data.code!=undefined) {
  28. wx.showToast({
  29. title: res.data.msg,
  30. icon: "none",
  31. duration: 2000
  32. });
  33. reject(res.data.msg);
  34. }
  35. }
  36. success(res)
  37. },
  38. fail: res => {
  39. reject(res)
  40. },
  41. complete: res => {
  42. // wx.hideLoading()
  43. }
  44. })
  45. })
  46. return promise
  47. }
  48. let fn = function() {}
  49. module.exports = {
  50. $get: function({
  51. url,
  52. header,
  53. data = {},
  54. success = fn
  55. }) {
  56. data.client_type = app.globalData.clientype;
  57. data.client="wxd621d357ca76c526";
  58. return request(url, 'GET',header, data, success)
  59. },
  60. $post: function({
  61. url,
  62. header,
  63. data = {},
  64. success = fn
  65. }) {
  66. data.client_type = app.globalData.clientype;
  67. data.client="wxd621d357ca76c526";
  68. return request(url, 'POST',header, data, success)
  69. }
  70. }