request.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.error('报错接口:',BASE_URL + options.url,'报错参数:',options.data,'报错信息:', res.data);
  44. // token过期或出了问题
  45. if (
  46. res.data.exception &&
  47. res.data.exception.type ==
  48. "AuthenticationCredentialsNotFoundException"
  49. ) {
  50. if (uni.getStorageSync('refreshToken') && uni.getStorageSync('token') ) {
  51. uni.showLoading({
  52. title: "刷新登录中",
  53. });
  54. if (errorCount <= MAX_ERROR_COUNT) {
  55. console.log("errorCount", errorCount);
  56. errorCount++;
  57. if (!uni.getStorageSync("refreshToken")) {
  58. uni.clearStorageSync();
  59. uni.reLaunch({
  60. url: "/pages/index/index",
  61. });
  62. }
  63. // 刷新token
  64. uni.request({
  65. url: BASE_URL + "/zswl-cloud-bdb/user/refreshToken",
  66. method: "GET",
  67. data: {
  68. refreshToken: uni.getStorageSync("refreshToken"),
  69. },
  70. success(r) {
  71. uni.hideLoading();
  72. if (r.data.content == "刷新令牌错误") {
  73. uni.showToast({
  74. title: "登录失效",
  75. icon: "fail",
  76. });
  77. uni.clearStorageSync();
  78. uni.reLaunch({
  79. url: "/pages/index/index",
  80. });
  81. } else {
  82. uni.setStorageSync("token", r.data.content); //登录状态
  83. // // 刷新页面
  84. var pages = getCurrentPages();
  85. var url = "/" + pages[pages.length - 1].route;
  86. uni.reLaunch({
  87. url,
  88. });
  89. }
  90. },
  91. });
  92. }else{
  93. uni.hideLoading();
  94. uni.clearStorageSync();
  95. uni.reLaunch({
  96. url: "/pages/index/index",
  97. });
  98. }
  99. }else{
  100. uni.showModal({
  101. title:'请登录',
  102. confirmText:'去登录',
  103. success(res){
  104. if(res.confirm){
  105. var pages = getCurrentPages()
  106. let parmas = pages[pages.length - 1].options
  107. let str = ''
  108. for (let key in parmas) {
  109. str += `&${key}=${parmas[key]}`
  110. }
  111. var url = '/' + pages[pages.length - 1].route
  112. uni.reLaunch({
  113. url:'/login/login/login?redirect='+url+str
  114. })
  115. }else if (res.cancel) {
  116. console.log('拒绝登录,跳转首页');
  117. uni.switchTab({
  118. url:'/pages/index/index'
  119. })
  120. }
  121. }
  122. })
  123. }
  124. }
  125. }
  126. resolve(res.data);
  127. },
  128. //请求失败
  129. fail: (err) => {
  130. uni.showToast({
  131. title: "请求接口失败",
  132. });
  133. reject(err);
  134. },
  135. });
  136. });
  137. };