fix: support authenticated video citation playback

This commit is contained in:
key
2026-08-02 22:04:09 +08:00
parent 6ad8a017a4
commit a07bbcca07
7 changed files with 83 additions and 26 deletions
+7
View File
@@ -38,6 +38,13 @@ const normalizeUrl = (url: string) => `${currentApiBase()}${url.startsWith('/')
export const apiUrl = normalizeUrl;
export const apiMediaUrl = (url: string) => {
const normalized = normalizeUrl(url);
if (/^https?:\/\//i.test(normalized) || typeof window === 'undefined') return normalized;
const origin = window.location?.origin;
return /^https?:\/\//i.test(origin || '') ? new URL(normalized, `${origin}/`).toString() : normalized;
};
export const authHeaders = (json = true, includeAuth = true) => {
const auth = getAuth();
return {
+2 -2
View File
@@ -9,7 +9,7 @@ import type {
SummaryCardResponse,
KnowledgeCitationDetail
} from '@/types/api';
import { apiRequest, apiUrl, authHeaders, readTextPayload } from './api';
import { apiMediaUrl, apiRequest, apiUrl, authHeaders, readTextPayload } from './api';
import { getSelectedPosition } from './auth';
import { fileSelectionFailure } from '../utils/file-selection';
import { withPlatformFileSelection } from './file-selection-lifecycle';
@@ -264,7 +264,7 @@ export const loadKnowledgeCitationMediaUrl = async (detailRef: string) => {
clearAuthOnUnauthorized: false
});
if (!response.url?.startsWith('/api/knowledge/resources/')) throw new Error('视频地址无效');
return apiUrl(response.url);
return apiMediaUrl(response.url);
};
const normalizeUnifiedResponse = (response: UnifiedKnowledgeResponse): KnowledgeSearchResponse => {
@@ -1,6 +1,8 @@
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import test from 'node:test';
import { fileURLToPath } from 'node:url';
import { createServer } from 'vite';
const source = (path) => readFile(new URL(path, import.meta.url), 'utf8');
@@ -28,3 +30,30 @@ test('引用详情页释放媒体临时 URL', async () => {
assert.match(detail, /onUnmounted\(releaseMedia\)/);
assert.match(detail, /releaseAuthenticatedFileUrl/);
});
test('H5 video media URLs stay rooted at the dev API instead of the /h5 app base', async () => {
globalThis.window = { location: { origin: 'https://wygj-mobile-uni.localhost' } };
globalThis.uni = {
getStorageSync: () => '',
removeStorageSync: () => {}
};
const vite = await createServer({
root: fileURLToPath(new URL('..', import.meta.url)),
configFile: false,
optimizeDeps: { noDiscovery: true },
server: { middlewareMode: true, hmr: false },
appType: 'custom'
});
try {
const { apiMediaUrl } = await vite.ssrLoadModule('/src/services/api.ts');
assert.equal(
apiMediaUrl('/api/knowledge/resources/2069/download?ticket=opaque'),
'https://wygj-mobile-uni.localhost/dev-api/api/knowledge/resources/2069/download?ticket=opaque'
);
} finally {
await vite.close();
delete globalThis.window;
delete globalThis.uni;
}
});