request.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. timeout: 600000,
  37. ...other,
  38. complete: (res) => {
  39. if (showLoading) {
  40. uni.hideLoading();
  41. }
  42. if (res.data.code == "B00103") {
  43. uni.navigateTo({
  44. url: "/pages/login/login",
  45. });
  46. } else if (res.data.code == "B00101") {
  47. //刷新token
  48. uni.showToast({
  49. title: "身份验证已过期,请重新登录",
  50. icon: "none",
  51. mask: true,
  52. duration: 1500,
  53. });
  54. console.log('身份验证已过期,请重新登录------');
  55. uni.removeStorageSync("token");
  56. uni.removeStorageSync("userInfo");
  57. uni.removeStorageSync('treeList')
  58. setTimeout(() => {
  59. wx.navigateTo({
  60. url: "/pages/login/login",
  61. });
  62. }, 1500);
  63. } else if (res.data.code == -1) {
  64. wx.showToast({
  65. title: res.data.message,
  66. icon: "none",
  67. });
  68. } else {
  69. resolve(res.data);
  70. }
  71. },
  72. });
  73. });
  74. };
  75. // get方法
  76. const get = (url, param = {}, showLoading) => {
  77. return http({
  78. url,
  79. param,
  80. showLoading,
  81. content_type: "application/x-www-form-urlencoded",
  82. });
  83. };
  84. // get方法
  85. const getJ = (url, param = {}, showLoading) => {
  86. return http({
  87. url,
  88. param,
  89. showLoading,
  90. content_type: "application/json",
  91. });
  92. };
  93. // post方法
  94. const post = (url, param = {}, showLoading) => {
  95. return http({
  96. url,
  97. param,
  98. showLoading,
  99. method: "POST",
  100. content_type: "application/x-www-form-urlencoded",
  101. });
  102. };
  103. // post方法
  104. const postJ = (url, param = {}, showLoading) => {
  105. return http({
  106. url,
  107. param,
  108. showLoading,
  109. method: "post",
  110. content_type: "application/json",
  111. });
  112. };
  113. // put方法
  114. const put = (url, param = {}, showLoading) => {
  115. return http({
  116. url,
  117. data: param,
  118. showLoading,
  119. method: "PUT",
  120. content_type: "application/x-www-form-urlencoded",
  121. });
  122. };
  123. const putJ = (url, param = {}, showLoading) => {
  124. return http({
  125. url,
  126. data: param,
  127. showLoading,
  128. method: "PUT",
  129. content_type: "application/json",
  130. });
  131. };
  132. //删除
  133. const deleteApi = (url, data = {}, showLoading) => {
  134. return http({
  135. url,
  136. data,
  137. showLoading,
  138. method: "DELETE",
  139. content_type: "application/json",
  140. });
  141. };
  142. module.exports = {
  143. get,
  144. getJ,
  145. post,
  146. postJ,
  147. put,
  148. putJ,
  149. deleteApi
  150. };