api.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const outLoginList = ["B00103", "B00109", "B00101", "401", "301"];
  2. const http = ({
  3. url = "",
  4. content_type = "",
  5. param = {},
  6. showLoading,
  7. showErrorToast = true,
  8. ...other
  9. } = {}) => {
  10. if (showLoading) {
  11. uni.showLoading({
  12. title: "正在加载中",
  13. mask: true,
  14. });
  15. }
  16. return new Promise((resolve, reject) => {
  17. let userId = wx.getStorageSync("userId");
  18. let Authorization = wx.getStorageSync("token");
  19. let header = {};
  20. const value = uni.getStorageSync("token"); //取存本地的token
  21. //登录接口不传token
  22. if (
  23. value &&
  24. !url.includes("/user/login") &&
  25. !url.includes("/connection/getConnectionTest")
  26. ) {
  27. header = {
  28. "content-type": content_type,
  29. "zoomwin-token": value,
  30. "zoomwin-sid": uni.getStorageSync("userInfo").sessionId,
  31. };
  32. } else {
  33. header = {
  34. "content-type": content_type,
  35. platform: "wxapp",
  36. };
  37. }
  38. uni.request({
  39. url: url,
  40. data: param,
  41. header: header,
  42. ...other,
  43. success: (res) => {
  44. if (showLoading) {
  45. uni.hideLoading();
  46. }
  47. if (outLoginList.includes(res.data.code)) {
  48. // uni.navigateTo({
  49. // url: '/pages/login/login',
  50. // })
  51. // } else if (res.data.code == "B00101") { //刷新token
  52. uni.showToast({
  53. title: "身份验证失效,请重新登录",
  54. icon: "none",
  55. mask: true,
  56. duration: 2500,
  57. });
  58. uni.removeStorageSync("token");
  59. uni.removeStorageSync("userInfo");
  60. setTimeout(() => {
  61. wx.navigateTo({
  62. url: "/pages/login/login",
  63. });
  64. }, 2500);
  65. } else if (res.data.code == 0) {
  66. resolve(res.data);
  67. } else {
  68. if (showErrorToast) {
  69. wx.showToast({
  70. title: res.data.message || res.data.error,
  71. icon: "none",
  72. });
  73. }
  74. reject(res.data);
  75. }
  76. },
  77. fail: (err) => {
  78. reject(err);
  79. },
  80. });
  81. });
  82. };
  83. // get方法
  84. const get = (url, param = {}, showLoading, showErrorToast, others = {}) => {
  85. return http({
  86. url,
  87. param,
  88. showLoading,
  89. showErrorToast,
  90. content_type: "application/x-www-form-urlencoded",
  91. ...others,
  92. });
  93. };
  94. // get方法
  95. const getJ = (url, param = {}, showLoading, showErrorToast) => {
  96. return http({
  97. url,
  98. param,
  99. showLoading,
  100. showErrorToast,
  101. content_type: "application/json",
  102. });
  103. };
  104. // post方法
  105. const post = (url, param = {}, showLoading, showErrorToast) => {
  106. return http({
  107. url,
  108. param,
  109. showLoading,
  110. showErrorToast,
  111. method: "POST",
  112. content_type: "application/x-www-form-urlencoded",
  113. });
  114. };
  115. // post方法 请求文件
  116. const postB = (url, param = {}, showLoading, showErrorToast) => {
  117. return http({
  118. url,
  119. param,
  120. showLoading,
  121. showErrorToast,
  122. method: "POST",
  123. content_type: "application/x-www-form-urlencoded",
  124. responseType: "blob",
  125. });
  126. };
  127. // post方法
  128. const postJ = (url, param = {}, showLoading, showErrorToast) => {
  129. return http({
  130. url,
  131. param,
  132. showLoading,
  133. showErrorToast,
  134. method: "post",
  135. content_type: "application/json;charset=UTF-8",
  136. });
  137. };
  138. module.exports = {
  139. get,
  140. getJ,
  141. post,
  142. postB,
  143. postJ,
  144. };