http.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. if (url.indexOf("cc/") > -1) {
  57. let apiUrl = url.split("cc/")[1];
  58. if (apiUrl == 'api/users/code2sessionmini') {
  59. data.client = "3";
  60. }
  61. }
  62. data.client_type = app.globalData.clientype;
  63. return request(url, 'GET',header, data, success)
  64. },
  65. $post: function({
  66. url,
  67. header,
  68. data = {},
  69. success = fn
  70. }) {
  71. if (url.indexOf("cc/") > -1) {
  72. let apiUrl = url.split("cc/")[1];
  73. if (apiUrl == 'api/users/code2sessionmini') {
  74. data.client = "3";
  75. }
  76. }
  77. data.client_type = app.globalData.clientype;
  78. return request(url, 'POST',header, data, success)
  79. }
  80. }