refactor(channels): normalize at cache fill and eliminate frontend as-cast
- channel.go: convert normalizeBillingModelSource into a (*Channel) method for entity cohesion - channel_service.go: normalize in populateChannelCache so every cache-backed reader (gateway, billing, future endpoints) sees the default; drop the duplicate fallback inside resolveMapping - table: tighten Row with status?: ChannelStatus / billing_model_source?: BillingModelSource, remove the [key: string]: unknown index signature - admin view: drop the `as ChannelStatus` / `as BillingModelSource` assertions and add statusStyleOf / billingSourceLabelOf helpers with runtime fallback so unseen values render as "-" instead of crashing
This commit is contained in:
@@ -62,6 +62,7 @@ import DataTable from '@/components/common/DataTable.vue'
|
||||
import Icon from '@/components/icons/Icon.vue'
|
||||
import SupportedModelChip from './SupportedModelChip.vue'
|
||||
import type { UserSupportedModel } from '@/api/channels'
|
||||
import type { ChannelStatus, BillingModelSource } from '@/constants/channel'
|
||||
|
||||
interface GroupRef {
|
||||
id: number
|
||||
@@ -75,7 +76,10 @@ interface Row {
|
||||
groups: GroupRef[]
|
||||
// 复用 user 侧最小 DTO;admin 侧 SupportedModel 结构上是其超集,可直接传入。
|
||||
supported_models: UserSupportedModel[]
|
||||
[key: string]: unknown
|
||||
// admin 独有字段:用精确类型代替 `unknown`,让消费端无需 `as` 断言,
|
||||
// 也能在后端新增 union 成员时让前端 Record 查表立刻出空而非崩溃。
|
||||
status?: ChannelStatus
|
||||
billing_model_source?: BillingModelSource
|
||||
}
|
||||
|
||||
interface Column {
|
||||
|
||||
@@ -46,16 +46,16 @@
|
||||
|
||||
<template #cell-status="{ row }">
|
||||
<span
|
||||
:class="statusStyles[row.status as ChannelStatus].cls"
|
||||
:class="statusStyleOf(row.status).cls"
|
||||
class="inline-flex items-center rounded px-2 py-0.5 text-xs font-medium"
|
||||
>
|
||||
{{ statusStyles[row.status as ChannelStatus].label }}
|
||||
{{ statusStyleOf(row.status).label }}
|
||||
</span>
|
||||
</template>
|
||||
|
||||
<template #cell-billing_model_source="{ row }">
|
||||
<span class="text-xs text-gray-700 dark:text-gray-300">
|
||||
{{ billingSourceLabels[row.billing_model_source as BillingModelSource] }}
|
||||
{{ billingSourceLabelOf(row.billing_model_source) }}
|
||||
</span>
|
||||
</template>
|
||||
</AvailableChannelsTable>
|
||||
@@ -101,7 +101,7 @@ const columns = computed(() => [
|
||||
|
||||
/**
|
||||
* 显示样式:i18n label + Tailwind class,按 ChannelStatus 完整穷举。
|
||||
* 用 Record<ChannelStatus, ...> 强制未来新增状态时 TS 编译失败,避免遗漏分支。
|
||||
* Record 键类型强制未来新增 ChannelStatus 成员时 TS 编译失败,避免遗漏分支。
|
||||
*/
|
||||
const statusStyles = computed<Record<ChannelStatus, { label: string; cls: string }>>(() => ({
|
||||
[CHANNEL_STATUS_ACTIVE]: {
|
||||
@@ -124,6 +124,19 @@ const billingSourceLabels = computed<Record<BillingModelSource, string>>(() => (
|
||||
[BILLING_MODEL_SOURCE_CHANNEL_MAPPED]: t('admin.availableChannels.billingSource.channel_mapped')
|
||||
}))
|
||||
|
||||
// 运行时兜底:即便 service 层归一化漏点或后端新增未同步的 enum 值传入,
|
||||
// 也不会触发 undefined.cls 崩溃;统一降级为 "-"。
|
||||
const DEFAULT_STATUS_STYLE = { label: '-', cls: '' }
|
||||
const DEFAULT_BILLING_LABEL = '-'
|
||||
|
||||
function statusStyleOf(status: ChannelStatus | undefined): { label: string; cls: string } {
|
||||
return status ? statusStyles.value[status] : DEFAULT_STATUS_STYLE
|
||||
}
|
||||
|
||||
function billingSourceLabelOf(src: BillingModelSource | undefined): string {
|
||||
return src ? billingSourceLabels.value[src] : DEFAULT_BILLING_LABEL
|
||||
}
|
||||
|
||||
const filteredChannels = computed(() => {
|
||||
const q = searchQuery.value.trim().toLowerCase()
|
||||
if (!q) return channels.value
|
||||
|
||||
Reference in New Issue
Block a user