request.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. reject(res.data)
  69. } else {
  70. resolve(res.data);
  71. }
  72. },
  73. });
  74. });
  75. };
  76. // get方法
  77. const get = (url, param = {}, showLoading) => {
  78. return http({
  79. url,
  80. param,
  81. showLoading,
  82. content_type: "application/x-www-form-urlencoded",
  83. });
  84. };
  85. // get方法
  86. const getJ = (url, param = {}, showLoading) => {
  87. return http({
  88. url,
  89. param,
  90. showLoading,
  91. content_type: "application/json",
  92. });
  93. };
  94. // post方法
  95. const post = (url, param = {}, showLoading) => {
  96. return http({
  97. url,
  98. param,
  99. showLoading,
  100. method: "POST",
  101. content_type: "application/x-www-form-urlencoded",
  102. });
  103. };
  104. // post方法
  105. const postJ = (url, param = {}, showLoading) => {
  106. return http({
  107. url,
  108. param,
  109. showLoading,
  110. method: "post",
  111. content_type: "application/json",
  112. });
  113. };
  114. // put方法
  115. const put = (url, param = {}, showLoading) => {
  116. return http({
  117. url,
  118. data: param,
  119. showLoading,
  120. method: "PUT",
  121. content_type: "application/x-www-form-urlencoded",
  122. });
  123. };
  124. const putJ = (url, param = {}, showLoading) => {
  125. return http({
  126. url,
  127. data: param,
  128. showLoading,
  129. method: "PUT",
  130. content_type: "application/json",
  131. });
  132. };
  133. //删除
  134. const deleteApi = (url, data = {}, showLoading) => {
  135. return http({
  136. url,
  137. data,
  138. showLoading,
  139. method: "DELETE",
  140. content_type: "application/json",
  141. });
  142. };
  143. module.exports = {
  144. get,
  145. getJ,
  146. post,
  147. postJ,
  148. put,
  149. putJ,
  150. deleteApi
  151. };