You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
4.9 KiB

3 weeks ago
4 weeks ago
4 weeks ago
  1. import { useUserStore } from "../stores/modules/userInfo"
  2. import { useDeviceStore } from "../stores/modules/deviceInfo"
  3. import { useLoginStore } from "../stores/modules/login"
  4. const baseURL = "https://dbqb.nfdxy.net/testApi"
  5. const httpInterceptor = {
  6. // 拦截前触发
  7. invoke(options) {
  8. // 打印当前 baseURL 便于定位来源
  9. console.log('HTTP(baseURL)=', baseURL)
  10. // 若出现旧代码将 url 设为 http://localhost:8888,强制改为真实域名
  11. if (options.url.startsWith('http://localhost:8888')) {
  12. options.url = baseURL.replace(/\/$/, '') + options.url.replace('http://localhost:8888', '')
  13. } else if (options.url.startsWith('https://localhost:8888')) {
  14. options.url = baseURL.replace(/\/$/, '') + options.url.replace('https://localhost:8888', '')
  15. }
  16. // 仅当明确配置了 baseURL 时才拼接前缀(去掉 baseURL 尾部的斜杠,避免双斜杠)
  17. if (baseURL && !options.url.startsWith('http')) {
  18. options.url = baseURL.replace(/\/$/, '') + options.url
  19. }
  20. // 打印最终请求地址
  21. console.log('HTTP(finalUrl)=', options.url)
  22. // 2.请求超时,默认60s
  23. options.timeout = 30000
  24. console.log(options)
  25. //3 添加小程序端请求头
  26. const sys = uni.getSystemInfoSync();
  27. // 为对齐后端文档示例,client 固定为 ios(如需按平台设置再改回)
  28. const deviceInfo = useDeviceStore()
  29. options.header = {
  30. ...options.header,
  31. // 标准头与文档头同时设置,确保兼容
  32. 'content-type': 'application/json',
  33. 'contentType': 'application/json',
  34. 'version': uni.getSystemInfoSync().appVersion,
  35. 'client': uni.getSystemInfoSync().platform == 'ios' ? 'ios' : 'android',
  36. 'deviceId': deviceInfo.deviceInfo.deviceId
  37. }
  38. //4 添加token,优先用store,没有则回退到body中的token,保持与Apifox一致
  39. const memberStore = useUserStore()
  40. const token = memberStore.userInfo?.token || options.data?.token
  41. // const token = '2d0b5654409646713cdd40ec0d0bb56c'
  42. // const token = '1b3a58424c5324e40d4bf4d085e18047'
  43. if (token) {
  44. options.header.token = token
  45. }
  46. console.log("最终请求参数:", options)
  47. // 避免误用 Authorization 头,后端要求的是 token 头
  48. // if (options.header.Authorization) delete options.header.Authorization
  49. return options
  50. }
  51. }
  52. // 添加拦截器
  53. uni.addInterceptor('request', httpInterceptor)
  54. uni.addInterceptor('uploadFile', httpInterceptor)
  55. // 添加请求函数
  56. export const http = (options) => {
  57. return new Promise((resolve, reject) => {
  58. uni.request({
  59. ...options,
  60. // 1.请求成功
  61. success: (result) => {
  62. if (result.statusCode >= 200 && result.statusCode < 300) {
  63. if (result.data.code === 401) {
  64. const loginStore = useLoginStore()
  65. loginStore.setLoginInfo("true")
  66. console.log("1loginStore.loginInfo", loginStore.loginInfo);
  67. loginStore.setLoginInfo("false")
  68. console.log("2loginStore.loginInfo", loginStore.loginInfo);
  69. }
  70. if (result.data.code === 400) {
  71. const userStore = useUserStore()
  72. userStore.clearUserInfo()
  73. uni.showModal({
  74. title: "登录失效",
  75. content: "点击确定跳转登录页面",
  76. confirmText: "确定",
  77. cancelText: "取消",
  78. success: (res) => {
  79. uni.navigateTo({
  80. url: '/pages/start/login/login'
  81. })
  82. },
  83. });
  84. }
  85. resolve(result.data)
  86. } else if (result.statusCode === 401) {
  87. // 清除登录信息
  88. const memberStore = useUserStore()
  89. memberStore.clearUserInfo()
  90. // 提示用户重新登录
  91. uni.navigateTo({
  92. url: '/pages/start/login/login'
  93. })
  94. reject(result)
  95. } else {
  96. // uni.showToast({
  97. // title: (result.data).msg || '请求失败',
  98. // icon: 'none'
  99. // })
  100. reject(result)
  101. }
  102. },
  103. // 2.请求失败
  104. fail: (err) => {
  105. reject(err)
  106. uni.showToast({
  107. title: '请求超时',
  108. icon: 'none'
  109. })
  110. }
  111. })
  112. })
  113. }