index.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import type { AxiosResponse } from 'axios'
  2. import type { LeagdoApiResponse } from './api'
  3. import API, { setWebsocketOnError, setApiEntryPoint } from './api'
  4. import ajax from './axios'
  5. import { validatorHttpUrl } from '@/utils/utils'
  6. const LeagdoApiResponseKeys: string[] = Array.of('isSuccess', 'errorMsg')
  7. const notification = ElMessage
  8. /** Axios.Interceptor: check if resp is LeagaoLeagdoApiResponse*/
  9. const responseCheckInterceptor = (resp: AxiosResponse) => {
  10. let isLeagdoApiResponse = true
  11. try {
  12. const data = resp.data
  13. for (const key of LeagdoApiResponseKeys) {
  14. if (!(key in data)) {
  15. isLeagdoApiResponse = false
  16. LeagdoApiResponseKeys.length = 0
  17. }
  18. }
  19. if ((data as LeagdoApiResponse<unknown>).isSuccess === true) {
  20. if (!('data' in data)) {
  21. isLeagdoApiResponse = false
  22. }
  23. }
  24. } catch {
  25. isLeagdoApiResponse = false
  26. }
  27. if (isLeagdoApiResponse === false) {
  28. notification.warning({ message: '后端返回内容格式错误', grouping: true })
  29. throw new Error()
  30. }
  31. return resp
  32. }
  33. const axiosErrorInterceptor = (err: unknown) => {
  34. notification.error({
  35. message: '后端连接失败,请检查阅读WEB服务或者设置其它可用链接',
  36. grouping: true,
  37. })
  38. throw err
  39. }
  40. // http全局
  41. ajax.interceptors.response.use(responseCheckInterceptor, axiosErrorInterceptor)
  42. // websocket
  43. setWebsocketOnError(axiosErrorInterceptor)
  44. /**
  45. * 按照阅读的默认规则 解析阅读HTTP WebSocket API入口地址
  46. * @returns [http_url, webSocekt_url]
  47. */
  48. export const parseLeagdoHttpUrlWithDefault = (
  49. http_url: string | URL,
  50. ): [string, string] => {
  51. let url = new URL(location.origin) //默认当前网址的origin部分
  52. if (validatorHttpUrl(http_url)) {
  53. url = new URL(http_url)
  54. }
  55. const { protocol, port } = url
  56. // websocket服务端口 为http服务端口 + 1
  57. let legado_webSocket_port
  58. if (port !== '') {
  59. legado_webSocket_port = String(Number(port) + 1)
  60. } else {
  61. legado_webSocket_port = protocol.startsWith('https:') ? '444' : '81'
  62. }
  63. // websocket协议是否为加密版本
  64. const legado_webSocket_protocol = protocol.startsWith('https:')
  65. ? 'wss://'
  66. : 'ws://'
  67. const http_entry_point = url.toString()
  68. url.protocol = legado_webSocket_protocol
  69. url.port = legado_webSocket_port
  70. const webSocket_entry_point = url.toString()
  71. console.info('legado_api_config:')
  72. console.table({
  73. 'http API入口': http_entry_point,
  74. 'webSocket API入口': webSocket_entry_point,
  75. })
  76. return [http_entry_point, webSocket_entry_point]
  77. }
  78. //export const useLeagdoRemoteUrlDialog = () => { }
  79. setApiEntryPoint(
  80. ...parseLeagdoHttpUrlWithDefault(ajax.defaults.baseURL as string),
  81. )
  82. export default API
  83. export * from './api'