index.ts 3.3 KB

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