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.

100 lines
3.9 KiB

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