request.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. setTimeout(() => {
  57. wx.navigateTo({
  58. url: "/pages/login/login",
  59. });
  60. }, 1500);
  61. } else if (res.data.errMsg == "request:fail ") {
  62. wx.showToast({
  63. title: "获取数据失败,请重试",
  64. icon: "none",
  65. });
  66. } else {
  67. resolve(res.data);
  68. }
  69. },
  70. });
  71. });
  72. };
  73. // get方法
  74. const get = (url, param = {}, showLoading) => {
  75. return http({
  76. url,
  77. param,
  78. showLoading,
  79. content_type: "application/x-www-form-urlencoded",
  80. });
  81. };
  82. // get方法
  83. const getJ = (url, param = {}, showLoading) => {
  84. return http({
  85. url,
  86. param,
  87. showLoading,
  88. content_type: "application/json",
  89. });
  90. };
  91. // post方法
  92. const post = (url, param = {}, showLoading) => {
  93. return http({
  94. url,
  95. param,
  96. showLoading,
  97. method: "POST",
  98. content_type: "application/x-www-form-urlencoded",
  99. });
  100. };
  101. // post方法
  102. const postJ = (url, param = {}, showLoading) => {
  103. return http({
  104. url,
  105. param,
  106. showLoading,
  107. method: "post",
  108. content_type: "application/json",
  109. });
  110. };
  111. module.exports = {
  112. get,
  113. getJ,
  114. post,
  115. postJ,
  116. };