request.js 3.0 KB

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