同步桌面端更新:重连/重试、断线自动重连、图片容错与鉴权参数

移植桌面端 2025-03-22 之后的更新到移动端,保留移动端自身布局:

- 补齐管理子路径鉴权参数:缩略图、在线图片、/cache 请求带上 AuthCode;
  原线上靠临时热补丁脚本 online-reader-auth-fix.js 绕过,现由源码原生支持。
- 断线自动重连:新增 src/utils/taskSocket.js,指数退避重连并在重连后重新同步
  任务列表与用量;App.vue 接入页面可见性/网络恢复事件,Side.vue 显示连接状态。
- 未完成任务重试:新增 retryGallery action 与重试按钮;补 "等待压缩" 状态。
- 图片容错:新增 src/utils/progressiveImages.js,单图加载失败不再阻塞后续页,
  支持单图重试。
- store 修复:收藏列表残留(slice→splice)、action 内改 context.state、
  gid 判空(null→undefined)、deleteTask 跳过下一元素、resetUndone 用 state 授权码、
  axios 全局错误拦截器、getShortname 简化。
- 下载链接统一由 buildGalleryDownloadUrl 生成(按 gid 而非名字)。
- 构建配置:base 改为 /asserts/mobile/、固定产物名 index.js/index.css、新增 .gitignore。
- 升级 Vite 8.3.0 + @vitejs/plugin-vue 6.0.9,npm audit 0 漏洞。
- 新增 tests/(11 项 Node 测试,全部通过)。
This commit is contained in:
2026-09-20 14:03:10 +08:00
parent 6ddd383294
commit 169925c197
17 changed files with 1740 additions and 990 deletions
+33
View File
@@ -0,0 +1,33 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import {startImages, settleImage, retryImage} from '../src/utils/progressiveImages.js'
test('a failed image does not prevent later pages from loading', () => {
const urls = ['first', 'second', 'third']
const items = startImages(urls)
settleImage(items, urls, items[0], true)
assert.equal(items[0].failed, true)
assert.equal(items[1].url, 'second')
settleImage(items, urls, items[1])
assert.equal(items[2].url, 'third')
})
test('retry does not append a duplicate page and bypasses a failed URL cache', () => {
const urls = ['first?gid=1', 'second', 'third']
const items = startImages(urls)
settleImage(items, urls, items[0], true)
retryImage(items[0])
assert.equal(items[0].src, 'first?gid=1&retry=1')
settleImage(items, urls, items[0])
assert.equal(items.length, 2)
assert.equal(items[0].failed, false)
})
test('late image events cannot append content after changing pages', () => {
const old = startImages(['old'])[0]
const urls = ['new', 'next']
const items = startImages(urls)
settleImage(items, urls, old)
assert.equal(items.length, 1)
assert.deepEqual(startImages([]), [])
})
+95
View File
@@ -0,0 +1,95 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import fs from 'node:fs'
import vm from 'node:vm'
function fixture() {
let code = fs.readFileSync(new URL('../src/store/index.js', import.meta.url), 'utf8')
.replace(/^import .*$/gm, '')
.replace('export default new vuex.Store(', 'globalThis.store = new vuex.Store(')
const messages = []
const showMessage = message => messages.push(message)
showMessage.error = message => messages.push(message)
const axios = {interceptors: {response: {use() {}}}}
const context = vm.createContext({axios, vuex: {Store: function (config) {Object.assign(this, config)}},
ElMessage: showMessage, URLSearchParams, console,
qs: {stringify: () => ''}})
vm.runInContext(code, context)
const store = context.store
const api = {state: store.state,
commit: (name, data) => store.mutations[name](store.state, data),
dispatch: (name, data) => Promise.resolve(store.actions[name](api, data))}
return {store, messages, axios, api}
}
test('link search matches a numeric gid and jumps to the right page', () => {
const {store, messages} = fixture()
store.state.currentTasks = [{gid: 1}, {gid: 2}, {gid: 12345, name: 'sample', shortName: 'sample'}]
store.state.length = 2
store.mutations._searchLocalByLink(store.state, 'https://example.org/g/12345/key/')
assert.equal(store.state.page, 2)
assert.match(messages[0], /已跳转/)
})
test('missing gid still reports no match', () => {
const {store, messages} = fixture()
store.state.currentTasks = [{gid: 12345}]
store.mutations._searchLocalByLink(store.state, 'https://example.org/g/999/key/')
assert.equal(messages[0], '未找到此任务')
})
test('submission reloads persisted status without duplicating a completed task', async () => {
const {store, axios, api} = fixture()
const gallery = {gid: 123, name: 'sample', status: '下载完成', thumb_link: '', createTime: 1, downloader: 7}
store.state.chosenGallery = {...gallery, status: '已提交'}
store.state.totalGalleryTask.push(gallery)
axios.post = async () => ({data: {result: 'success'}})
axios.get = async url => ({data: {result: 'success', data: JSON.stringify(url.endsWith('weekUsedAmount') ? {} : [gallery])}})
await store.actions.postGalleryTask(api, {targetResolution: 'original'})
assert.equal(store.state.totalGalleryTask.length, 1)
assert.equal(store.state.totalGalleryTask[0].status, '下载完成')
assert.equal(store.state.chosenGallery, false)
})
test('gallery management subrequests carry the active auth code', async () => {
const {store, axios, api} = fixture()
store.state.AuthCode = 'test-auth'
let call
axios.post = async (...args) => {
call = args
return {data: {result: args[0].endsWith('/cache') ? 'failure' : 'success', data: 'expected test response'}}
}
await store.actions.reconnect(api)
assert.equal(call[0], 'https://downloader.lionwebsite.xyz/GalleryManage/reconnect')
assert.equal(call[1], null)
assert.equal(call[2].params.AuthCode, 'test-auth')
await store.actions.readOnlineGallery(api, {link: 'https://example.org/g/1/key/', images: []})
assert.equal(call[0], 'https://downloader.lionwebsite.xyz/GalleryManage/cache')
assert.equal(call[1], null)
assert.deepEqual({...call[2].params}, {
url: 'https://example.org/g/1/key/',
AuthCode: 'test-auth'
})
})
test('thumbnail and online image URLs carry the active auth code', () => {
const {store} = fixture()
store.state.AuthCode = 'code with spaces'
store.mutations._updateGalleryTasks(store.state, [{
gid: 123,
name: 'sample',
status: '已提交',
thumb_link: 'https://example.org/thumb?a=1&b=2',
createTime: 1,
downloader: 7
}])
const thumbnail = new URL(store.state.totalGalleryTask[0].thumb_link)
assert.equal(thumbnail.searchParams.get('AuthCode'), 'code with spaces')
assert.equal(thumbnail.searchParams.get('path'), 'https://example.org/thumb?a=1&b=2')
store.mutations._setReadingGallery(store.state, {gid: 123, pages: 1})
const image = new URL(store.state.readingGallery.images[0])
assert.equal(image.searchParams.get('gid'), '123')
assert.equal(image.searchParams.get('AuthCode'), 'code with spaces')
})
+63
View File
@@ -0,0 +1,63 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import {createTaskSocket} from '../src/utils/taskSocket.js'
function fixture() {
const sockets = [], states = [], messages = [], timers = new Map()
let id = 0, opens = 0
const manager = createTaskSocket({url: 'wss://example.invalid',
onOpen: () => opens++, onState: value => states.push(value), onMessage: event => messages.push(event.data),
setTimer: (fn, delay) => { timers.set(++id, {fn, delay}); return id },
clearTimer: key => timers.delete(key),
createSocket: () => {
const socket = {sent: [], send(value) { this.sent.push(value) }, close() { this.closed = true }}
sockets.push(socket)
return socket
}
})
function tick(delay) {
const entry = [...timers].find(([, timer]) => timer.delay === delay)
assert.ok(entry, `expected timer ${delay}`)
timers.delete(entry[0]); entry[1].fn()
}
return {manager, sockets, states, messages, timers, tick, opens: () => opens}
}
test('reconnects and refreshes after each successful connection', () => {
const f = fixture(); f.manager.start()
f.sockets[0].onopen()
assert.deepEqual(f.sockets[0].sent, ['DownloaderWebsocket'])
f.sockets[0].onclose()
f.tick(1000)
f.sockets[1].onopen()
assert.equal(f.opens(), 2)
assert.equal(f.states.at(-1), 'connected')
assert.equal(f.timers.size, 0)
f.manager.stop()
})
test('backs off and times out connection attempts', () => {
const f = fixture(); f.manager.start()
f.tick(10000)
assert.equal(f.sockets[0].closed, true)
f.tick(1000); f.sockets[1].onerror(); f.tick(2000)
f.sockets[2].onerror(); f.tick(4000)
assert.equal(f.sockets.length, 4)
f.manager.stop()
})
test('stop cancels timers and ignores callbacks from replaced sockets', () => {
const f = fixture(); f.manager.start()
const oldOpen = f.sockets[0].onopen, oldMessage = f.sockets[0].onmessage
f.sockets[0].onerror()
f.manager.stop()
assert.equal(f.timers.size, 0)
f.manager.start()
oldOpen(); oldMessage({data: 'stale'})
assert.equal(f.opens(), 0)
assert.deepEqual(f.messages, [])
f.sockets[1].onopen()
f.sockets[1].onmessage({data: 'current'})
assert.deepEqual(f.messages, ['current'])
f.manager.stop()
})