plus-ui/src/utils/ossContent.ts

32 lines
850 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { listByIds } from '@/api/system/oss';
const OSS_MARKER_RE = /oss:\/\/([\w-]+)/g;
/**
* 将 HTML 中的 oss://{ossId} 标记批量解析为真实的 OSS 授权 URL
*
* 适用于富文本内容展示场景Editor 组件 / 详情页只读渲染等)
*
* @example
* const html = await resolveOssContent('<p><img src="oss://12345"/></p>');
*/
export async function resolveOssContent(html: string): Promise<string> {
if (!html) return html;
const matches = [...html.matchAll(OSS_MARKER_RE)];
if (matches.length === 0) return html;
const ossIds = [...new Set(matches.map(m => m[1]))];
try {
const res = await listByIds(ossIds.join(','));
let result = html;
for (const oss of res.data) {
result = result.replaceAll(`oss://${oss.ossId}`, oss.url);
}
return result;
} catch {
return html;
}
}