request.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const http = ({
  2. url = "",
  3. content_type = "",
  4. param = {},
  5. showLoading,
  6. ...other
  7. } = {}) => {
  8. if (showLoading) {
  9. uni.showLoading({
  10. title: "正在加载中",
  11. });
  12. }
  13. return new Promise((resolve, reject) => {
  14. // let userId = wx.getStorageSync('userId')
  15. // let Authorization = wx.getStorageSync('token')
  16. let header = {};
  17. const value = uni.getStorageSync("token"); //取存本地的token
  18. //登录接口不传token
  19. if (value && !url.includes("/user/login")) {
  20. header = {
  21. "content-type": content_type,
  22. "zoomwin-token": value,
  23. Authorization: value,
  24. "zoomwin-sid": uni.getStorageSync("userInfo").sessionId,
  25. };
  26. } else {
  27. header = {
  28. "content-type": content_type,
  29. platform: "wxapp",
  30. };
  31. }
  32. uni.request({
  33. url: url,
  34. data: param,
  35. header: header,
  36. ...other,
  37. complete: (res) => {
  38. if (showLoading) {
  39. uni.hideLoading();
  40. }
  41. if (res.data.code == "B00103") {
  42. uni.navigateTo({
  43. url: "/pages/login/login",
  44. });
  45. } else if (res.data.code == "B00101") {
  46. //刷新token
  47. uni.showToast({
  48. title: "身份验证已过期,请重新登录",
  49. icon: "none",
  50. mask: true,
  51. duration: 1500,
  52. });
  53. console.log('身份验证已过期,请重新登录------');
  54. uni.removeStorageSync("token");
  55. uni.removeStorageSync("userInfo");
  56. uni.removeStorageSync('treeList')
  57. setTimeout(() => {
  58. wx.navigateTo({
  59. url: "/pages/login/login",
  60. });
  61. }, 1500);
  62. } else if (res.data.code == -1) {
  63. wx.showToast({
  64. title: res.data.message,
  65. icon: "none",
  66. });
  67. } else {
  68. resolve(res.data);
  69. }
  70. },
  71. });
  72. });
  73. };
  74. // get方法
  75. const get = (url, param = {}, showLoading) => {
  76. return http({
  77. url,
  78. param,
  79. showLoading,
  80. content_type: "application/x-www-form-urlencoded",
  81. });
  82. };
  83. // get方法
  84. const getJ = (url, param = {}, showLoading) => {
  85. return http({
  86. url,
  87. param,
  88. showLoading,
  89. content_type: "application/json",
  90. });
  91. };
  92. // post方法
  93. const post = (url, param = {}, showLoading) => {
  94. return http({
  95. url,
  96. param,
  97. showLoading,
  98. method: "POST",
  99. content_type: "application/x-www-form-urlencoded",
  100. });
  101. };
  102. // post方法
  103. const postJ = (url, param = {}, showLoading) => {
  104. return http({
  105. url,
  106. param,
  107. showLoading,
  108. method: "post",
  109. content_type: "application/json",
  110. });
  111. };
  112. module.exports = {
  113. get,
  114. getJ,
  115. post,
  116. postJ,
  117. };