fix(site): 静默处理站点图标缺失

This commit is contained in:
jxxghp
2026-08-26 21:44:59 +08:00
parent c840286a8d
commit dc6c0462bd
7 changed files with 13 additions and 8 deletions
+1 -1
View File
@@ -66,7 +66,7 @@ async function getSiteIcon() {
try {
const icon = await getCachedSiteIcon(siteId, async () => {
const response = await api.get<{ icon?: string }>(`site/icon/${siteId}`)
const response = await api.get<{ icon?: string }>(`site/icon/${siteId}`, { feedback: 'silent' })
return response?.icon || defaultSiteIcon
})
siteIcon.value = getDisplayImageUrl(icon, globalSettingsStore.globalSettings.GLOBAL_IMAGE_CACHE)
+1 -1
View File
@@ -44,7 +44,7 @@ async function getSiteIcon() {
try {
const icon = await getCachedSiteIcon(subtitle.value.site, async () => {
try {
const response = await api.get<{ icon?: string }>(`site/icon/${subtitle.value?.site}`)
const response = await api.get<{ icon?: string }>(`site/icon/${subtitle.value?.site}`, { feedback: 'silent' })
return response?.icon || ''
} catch (error) {
console.error('Failed to load site icon:', error)
+1 -1
View File
@@ -43,7 +43,7 @@ async function getSiteIcon() {
try {
const icon = await getCachedSiteIcon(subtitle.value.site, async () => {
try {
const response = await api.get<{ icon?: string }>(`site/icon/${subtitle.value?.site}`)
const response = await api.get<{ icon?: string }>(`site/icon/${subtitle.value?.site}`, { feedback: 'silent' })
return response?.icon || ''
} catch (error) {
console.error('Failed to load site icon:', error)
+1 -1
View File
@@ -56,7 +56,7 @@ async function getSiteIcon(site: number | undefined) {
try {
const icon = await getCachedSiteIcon(site, async () => {
try {
const response = await api.get<{ icon?: string }>(`site/icon/${site}`)
const response = await api.get<{ icon?: string }>(`site/icon/${site}`, { feedback: 'silent' })
return response?.icon || ''
} catch (error) {
console.error(error)
+1 -1
View File
@@ -41,7 +41,7 @@ async function getSiteIcon(site: number | undefined) {
try {
const icon = await getCachedSiteIcon(site, async () => {
try {
const response = await api.get<{ icon?: string }>(`site/icon/${site}`)
const response = await api.get<{ icon?: string }>(`site/icon/${site}`, { feedback: 'silent' })
return response?.icon || ''
} catch (error) {
console.error('Failed to load site icon:', error)
@@ -237,10 +237,10 @@ describe('SiteCard interactions', () => {
expect(mocks.openSharedDialog).not.toHaveBeenCalled()
})
it('falls back to the default icon when the icon request fails', async () => {
it('silently falls back to the default icon when the icon is unavailable', async () => {
const site = createSite()
const requested = vi.fn()
server.use(siteIconHandler(site.id, null, 500, requested))
server.use(siteIconHandler(site.id, null, 200, requested))
const { container } = await renderWithProviders(SiteCard, {
global: { stubs: imageStubs },
props: { site },
@@ -249,5 +249,6 @@ describe('SiteCard interactions', () => {
await waitFor(() => expect(requested).toHaveBeenCalledOnce())
await waitFor(() => expect(getActiveRequestsCount()).toBe(0))
await waitFor(() => expect(container.querySelector<HTMLImageElement>('img')?.src).toContain('/site.webp'))
expect(mocks.toastError).not.toHaveBeenCalled()
})
})
+5 -1
View File
@@ -133,6 +133,7 @@ function response(body: JsonBodyType, status: number) {
return HttpResponse.json(body, { status })
}
/** 模拟站点图标接口,并保留后端缺失图标时的业务失败语义。 */
export function siteIconHandler(
id: number,
icon: string | null,
@@ -141,7 +142,10 @@ export function siteIconHandler(
) {
return http.get(siteApiUrls.icon(id), async () => {
await onRequest()
return response(apiEnvelope(icon ? { icon } : {}, Boolean(icon)), status)
return response(
apiEnvelope(icon ? { icon } : {}, Boolean(icon), icon ? '' : '站点图标不存在!'),
status,
)
})
}