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.

111 lines
4.4 KiB

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