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.

101 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. const client = 'ios';
  29. options.header = {
  30. ...options.header,
  31. 'source-client': 'miniapp',
  32. // 标准头与文档头同时设置,确保兼容
  33. 'content-type': 'application/json',
  34. 'contentType': 'application/json',
  35. 'version': '1',
  36. 'client': client,
  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 = '1b3a58424c5324e40d4bf4d085e18047'
  43. if (token) {
  44. options.header.token = token
  45. }
  46. // 避免误用 Authorization 头,后端要求的是 token 头
  47. // if (options.header.Authorization) delete options.header.Authorization
  48. return options
  49. }
  50. }
  51. // 添加拦截器
  52. uni.addInterceptor('request', httpInterceptor)
  53. uni.addInterceptor('uploadFile', httpInterceptor)
  54. // 添加请求函数
  55. export const http = (options) => {
  56. return new Promise((resolve, reject) => {
  57. uni.request({
  58. ...options,
  59. // 1.请求成功
  60. success: (result) => {
  61. if (result.statusCode >= 200 && result.statusCode < 300) {
  62. if (result.data.code === 401) {
  63. uni.showToast({
  64. title: '请先登录',
  65. icon: 'none'
  66. })
  67. }
  68. resolve(result.data)
  69. } else if (result.statusCode === 401) {
  70. // 清除登录信息
  71. const memberStore = useUserStore()
  72. memberStore.clearUserInfo()
  73. // 提示用户重新登录
  74. uni.navigateTo({
  75. url: '/pages/login/login'
  76. })
  77. reject(result)
  78. } else {
  79. uni.showToast({
  80. title: (result.data).msg || '请求失败',
  81. icon: 'none'
  82. })
  83. reject(result)
  84. }
  85. },
  86. // 2.请求失败
  87. fail: (err) => {
  88. reject(err)
  89. uni.showToast({
  90. title: '网络错误',
  91. icon: 'none'
  92. })
  93. }
  94. })
  95. })
  96. }