mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
fix(web): connectStatus not change when connected backend abort connection; fix relative URL parse; fix loadWebCatalog ElMessage bug
This commit is contained in:
@@ -22,9 +22,10 @@ export let legado_http_entry_point = ''
|
||||
export let legado_webSocket_entry_point = ''
|
||||
|
||||
let wsOnError: typeof WebSocket.prototype.onerror = () => {}
|
||||
export const setWebsocketOnError = (
|
||||
callback: typeof WebSocket.prototype.onerror,
|
||||
) => {
|
||||
let wsOnMessage: typeof WebSocket.prototype.onmessage = () => {}
|
||||
export const setWebsocketOnMessage = (callback: typeof wsOnMessage) =>
|
||||
(wsOnMessage = callback)
|
||||
export const setWebsocketOnError = (callback: typeof wsOnError) => {
|
||||
//WebSocket.prototype.onerror = callback
|
||||
wsOnError = callback
|
||||
}
|
||||
@@ -33,15 +34,15 @@ export const setApiEntryPoint = (
|
||||
http_entry_point: string,
|
||||
webSocket_entry_point: string,
|
||||
) => {
|
||||
legado_http_entry_point = http_entry_point
|
||||
legado_webSocket_entry_point = webSocket_entry_point
|
||||
ajax.defaults.baseURL = http_entry_point.toString()
|
||||
legado_http_entry_point = new URL(http_entry_point).toString()
|
||||
legado_webSocket_entry_point = new URL(webSocket_entry_point).toString()
|
||||
ajax.defaults.baseURL = legado_http_entry_point
|
||||
}
|
||||
|
||||
// 书架API
|
||||
// Http
|
||||
const getReadConfig = async (http_url = legado_http_entry_point) => {
|
||||
const { data } = await ajax.get<LeagdoApiResponse<string>>('/getReadConfig', {
|
||||
const { data } = await ajax.get<LeagdoApiResponse<string>>('getReadConfig', {
|
||||
baseURL: http_url.toString(),
|
||||
timeout: 3000,
|
||||
})
|
||||
@@ -52,27 +53,27 @@ const getReadConfig = async (http_url = legado_http_entry_point) => {
|
||||
}
|
||||
}
|
||||
const saveReadConfig = (config: webReadConfig) =>
|
||||
ajax.post<LeagdoApiResponse<string>>('/saveReadConfig', config)
|
||||
ajax.post<LeagdoApiResponse<string>>('saveReadConfig', config)
|
||||
|
||||
/** @deprecated: 使用`API.saveBookProgressWithBeacon`以确保在页面或者直接关闭的情况下保存进度 */
|
||||
const saveBookProgress = (bookProgress: BookProgress) =>
|
||||
ajax.post('/saveBookProgress', bookProgress)
|
||||
ajax.post('saveBookProgress', bookProgress)
|
||||
|
||||
/**主要在直接关闭浏览器情况下可靠发送书籍进度 */
|
||||
const saveBookProgressWithBeacon = (bookProgress: BookProgress) => {
|
||||
if (!bookProgress) return
|
||||
// 常规请求可能会被取消 使用Fetch keep-alive 或者 navigator.sendBeacon
|
||||
navigator.sendBeacon(
|
||||
new URL('/saveBookProgress', legado_http_entry_point),
|
||||
new URL('saveBookProgress', legado_http_entry_point),
|
||||
JSON.stringify(bookProgress),
|
||||
)
|
||||
}
|
||||
|
||||
const getBookShelf = () => ajax.get<LeagdoApiResponse<Book[]>>('/getBookshelf')
|
||||
const getBookShelf = () => ajax.get<LeagdoApiResponse<Book[]>>('getBookshelf')
|
||||
|
||||
const getChapterList = (/** @type {string} */ bookUrl: string) =>
|
||||
ajax.get<LeagdoApiResponse<BookChapter[]>>(
|
||||
'/getChapterList?url=' + encodeURIComponent(bookUrl),
|
||||
'getChapterList?url=' + encodeURIComponent(bookUrl),
|
||||
)
|
||||
|
||||
const getBookContent = (
|
||||
@@ -80,7 +81,7 @@ const getBookContent = (
|
||||
/** @type {number} */ chapterIndex: number,
|
||||
) =>
|
||||
ajax.get<LeagdoApiResponse<string>>(
|
||||
'/getBookContent?url=' +
|
||||
'getBookContent?url=' +
|
||||
encodeURIComponent(bookUrl) +
|
||||
'&index=' +
|
||||
chapterIndex,
|
||||
@@ -93,16 +94,17 @@ const search = (
|
||||
onFinish: () => void,
|
||||
) => {
|
||||
const socket = new WebSocket(
|
||||
new URL('/searchBook', legado_webSocket_entry_point),
|
||||
new URL('searchBook', legado_webSocket_entry_point),
|
||||
)
|
||||
socket.onerror = wsOnError
|
||||
|
||||
socket.onopen = () => {
|
||||
socket.send(`{"key":"${searchKey}"}`)
|
||||
}
|
||||
socket.onmessage = ({ data }) => {
|
||||
socket.onmessage = event => {
|
||||
try {
|
||||
onReceive(JSON.parse(data))
|
||||
onReceive(JSON.parse(event.data))
|
||||
wsOnMessage?.call(socket, event)
|
||||
} catch {
|
||||
onFinish()
|
||||
}
|
||||
@@ -114,31 +116,31 @@ const search = (
|
||||
}
|
||||
|
||||
const saveBook = (book: BaseBook) =>
|
||||
ajax.post<LeagdoApiResponse<string>>('/saveBook', book)
|
||||
ajax.post<LeagdoApiResponse<string>>('saveBook', book)
|
||||
const deleteBook = (book: BaseBook) =>
|
||||
ajax.post<LeagdoApiResponse<string>>('/deleteBook', book)
|
||||
ajax.post<LeagdoApiResponse<string>>('deleteBook', book)
|
||||
|
||||
const isBookSource = /bookSource/i.test(location.href)
|
||||
|
||||
// 源编辑API
|
||||
// Http
|
||||
const getSources = () =>
|
||||
isBookSource ? ajax.get('/getBookSources') : ajax.get('/getRssSources')
|
||||
isBookSource ? ajax.get('getBookSources') : ajax.get('getRssSources')
|
||||
|
||||
const saveSource = (data: Source) =>
|
||||
isBookSource
|
||||
? ajax.post<LeagdoApiResponse<string>>('/saveBookSource', data)
|
||||
: ajax.post<LeagdoApiResponse<string>>('/saveRssSource', data)
|
||||
? ajax.post<LeagdoApiResponse<string>>('saveBookSource', data)
|
||||
: ajax.post<LeagdoApiResponse<string>>('saveRssSource', data)
|
||||
|
||||
const saveSources = (data: Source[]) =>
|
||||
isBookSource
|
||||
? ajax.post<LeagdoApiResponse<Source[]>>('/saveBookSources', data)
|
||||
: ajax.post<LeagdoApiResponse<Source[]>>('/saveRssSources', data)
|
||||
? ajax.post<LeagdoApiResponse<Source[]>>('saveBookSources', data)
|
||||
: ajax.post<LeagdoApiResponse<Source[]>>('saveRssSources', data)
|
||||
|
||||
const deleteSource = (data: Source[]) =>
|
||||
isBookSource
|
||||
? ajax.post<LeagdoApiResponse<string>>('/deleteBookSources', data)
|
||||
: ajax.post<LeagdoApiResponse<string>>('/deleteRssSources', data)
|
||||
? ajax.post<LeagdoApiResponse<string>>('deleteBookSources', data)
|
||||
: ajax.post<LeagdoApiResponse<string>>('deleteRssSources', data)
|
||||
|
||||
// webSocket
|
||||
const debug = (
|
||||
@@ -148,7 +150,7 @@ const debug = (
|
||||
/** @type {() => void} */ onFinish: () => void,
|
||||
) => {
|
||||
const url = new URL(
|
||||
`/${isBookSource ? 'bookSource' : 'rssSource'}Debug`,
|
||||
`${isBookSource ? 'bookSource' : 'rssSource'}Debug`,
|
||||
legado_webSocket_entry_point,
|
||||
)
|
||||
|
||||
@@ -157,7 +159,10 @@ const debug = (
|
||||
socket.onopen = () => {
|
||||
socket.send(JSON.stringify({ tag: sourceUrl, key: searchKey }))
|
||||
}
|
||||
socket.onmessage = ({ data }) => onReceive(data)
|
||||
socket.onmessage = event => {
|
||||
onReceive(event.data)
|
||||
wsOnMessage?.call(socket, event)
|
||||
}
|
||||
|
||||
socket.onclose = () => {
|
||||
onFinish()
|
||||
@@ -171,7 +176,7 @@ const debug = (
|
||||
const getProxyCoverUrl = (coverUrl: string) => {
|
||||
if (coverUrl.startsWith(legado_http_entry_point)) return coverUrl
|
||||
return new URL(
|
||||
'/cover?path=' + encodeURIComponent(coverUrl),
|
||||
'cover?path=' + encodeURIComponent(coverUrl),
|
||||
legado_http_entry_point,
|
||||
).toString()
|
||||
}
|
||||
@@ -188,7 +193,7 @@ const getProxyImageUrl = (
|
||||
) => {
|
||||
if (src.startsWith(legado_http_entry_point)) return src
|
||||
return new URL(
|
||||
'/image?path=' +
|
||||
'image?path=' +
|
||||
encodeURIComponent(src) +
|
||||
'&url=' +
|
||||
encodeURIComponent(bookUrl) +
|
||||
|
||||
@@ -1,9 +1,21 @@
|
||||
import type { AxiosResponse } from 'axios'
|
||||
import type { LeagdoApiResponse } from './api'
|
||||
import API, { setWebsocketOnError, setApiEntryPoint } from './api'
|
||||
import API, {
|
||||
setWebsocketOnError,
|
||||
setApiEntryPoint,
|
||||
legado_http_entry_point,
|
||||
setWebsocketOnMessage,
|
||||
} from './api'
|
||||
import ajax from './axios'
|
||||
import { validatorHttpUrl } from '@/utils/utils'
|
||||
|
||||
import { createApp } from 'vue'
|
||||
import App from '@/App.vue'
|
||||
import store, { useConnectionStore } from '@/store'
|
||||
|
||||
createApp(App).use(store)
|
||||
const connectionStore = useConnectionStore()
|
||||
|
||||
const LeagdoApiResponseKeys: string[] = Array.of('isSuccess', 'errorMsg')
|
||||
|
||||
const notification = ElMessage
|
||||
@@ -31,6 +43,8 @@ const responseCheckInterceptor = (resp: AxiosResponse) => {
|
||||
notification.warning({ message: '后端返回内容格式错误', grouping: true })
|
||||
throw new Error()
|
||||
}
|
||||
connectionStore.setConnectType('primary')
|
||||
connectionStore.setConnectStatus('已连接 ' + legado_http_entry_point)
|
||||
return resp
|
||||
}
|
||||
|
||||
@@ -39,13 +53,18 @@ const axiosErrorInterceptor = (err: unknown) => {
|
||||
message: '后端连接失败,请检查阅读WEB服务或者设置其它可用链接',
|
||||
grouping: true,
|
||||
})
|
||||
connectionStore.setConnectType('danger')
|
||||
connectionStore.setConnectStatus('连接异常')
|
||||
throw err
|
||||
}
|
||||
// http全局
|
||||
ajax.interceptors.response.use(responseCheckInterceptor, axiosErrorInterceptor)
|
||||
// websocket
|
||||
setWebsocketOnError(axiosErrorInterceptor)
|
||||
|
||||
setWebsocketOnMessage(() => {
|
||||
connectionStore.setConnectType('primary')
|
||||
connectionStore.setConnectStatus('已连接 ' + legado_http_entry_point)
|
||||
})
|
||||
/**
|
||||
* 按照阅读的默认规则 解析阅读HTTP WebSocket API入口地址
|
||||
* @returns [http_url, webSocekt_url]
|
||||
|
||||
Reference in New Issue
Block a user