request.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //用BASEURL来表示域名地址
  2. // const BASE_URL = 'http://192.168.110.153:9002'
  3. // const BASE_URL = 'https://zswl.api.jpy.wang/zswl-cloud-bdb'
  4. // const BASE_URL = 'https://api.dev.zonelife.cn'
  5. import { BASE_URL } from "@/utils/config.js";
  6. let errorCount = 0;
  7. const MAX_ERROR_COUNT = 2;
  8. //封装请求方法
  9. export const request = (options) => {
  10. return new Promise((resolve, reject) => {
  11. //请求路径拼接,,其中options.url就是通过下面方法myRequest获取到接口部分的url
  12. //method--请求方法,不是method的post就是get
  13. //请求的参数,当没有参数的时候就是空对象
  14. // BASE_URL = BASE_UR.includes('dev') ?
  15. let BASE_URL2 = BASE_URL;
  16. // if(options.url.includes('/zswl-cloud-shop')){
  17. // BASE_URL2 = 'http://g3710170f8.zicp.fun'
  18. // options.url = options.url.replace("/zswl-cloud-shop","")
  19. // }
  20. uni.request({
  21. url: BASE_URL2 + options.url,
  22. method: options.method || "GET",
  23. data: options.data || {},
  24. header: {
  25. accessToken: uni.getStorageSync("token"),
  26. },
  27. success: (res) => {
  28. if (res.statusCode !== 200) {
  29. return uni.showToast({
  30. title: "获取失败",
  31. });
  32. // uni.hideLoading()
  33. } else if (res.data.msg != "成功") {
  34. //接口返回报错
  35. uni.showToast({
  36. title:
  37. res.data.content ||
  38. res.data.msg ||
  39. res.data.exception.message ||
  40. "请求失败",
  41. icon: "error",
  42. });
  43. console.log("报错:", res);
  44. // token过期或出了问题
  45. if (
  46. res.data.exception &&
  47. res.data.exception.type ==
  48. "AuthenticationCredentialsNotFoundException"
  49. ) {
  50. uni.showLoading({
  51. title: "刷新登录中",
  52. });
  53. if (errorCount <= MAX_ERROR_COUNT) {
  54. console.log("errorCount", errorCount);
  55. errorCount++;
  56. if (!uni.getStorageSync("refreshToken")) {
  57. uni.clearStorageSync();
  58. uni.reLaunch({
  59. url: "/pages/index/index",
  60. });
  61. }
  62. // 刷新token
  63. uni.request({
  64. url: BASE_URL + "/zswl-cloud-bdb/user/refreshToken",
  65. method: "GET",
  66. data: {
  67. refreshToken: uni.getStorageSync("refreshToken"),
  68. },
  69. success(r) {
  70. uni.hideLoading();
  71. if (r.data.content == "刷新令牌错误") {
  72. uni.showToast({
  73. title: "登录失效",
  74. icon: "fail",
  75. });
  76. uni.clearStorageSync();
  77. uni.reLaunch({
  78. url: "/pages/index/index",
  79. });
  80. } else {
  81. uni.setStorageSync("token", r.data.content); //登录状态
  82. // 刷新页面
  83. let list = ["/pay/pay"];
  84. var pages = getCurrentPages();
  85. var url = "/" + pages[pages.length - 1].route;
  86. if (list.indexOf(url) == -1) {
  87. uni.reLaunch({
  88. url,
  89. });
  90. }
  91. }
  92. },
  93. });
  94. }
  95. }
  96. }
  97. resolve(res.data);
  98. },
  99. //请求失败
  100. fail: (err) => {
  101. uni.showToast({
  102. title: "请求接口失败",
  103. });
  104. reject(err);
  105. },
  106. });
  107. });
  108. };