| 1 | import type { PublishOptionValueItem, PublishOptionValuesVo } from '@/api/channels/channel.types' |
| 2 | import type { BiblPartItem } from '@/components/PublishDialog/publishDialog.type' |
| 3 | import http from '@/utils/request' |
| 4 | |
| 5 | // Source: plat/bilibili.ts |
| 6 | function toNumber(value: unknown) { |
| 7 | if (typeof value === 'number') |
| 8 | return Number.isFinite(value) ? value : 0 |
| 9 | |
| 10 | if (typeof value === 'string') { |
| 11 | const parsedValue = Number(value) |
| 12 | return Number.isFinite(parsedValue) ? parsedValue : 0 |
| 13 | } |
| 14 | |
| 15 | return 0 |
| 16 | } |
| 17 | |
| 18 | function mapPublishOptionItemsToBilibiliPartitions(items: PublishOptionValueItem[]): BiblPartItem[] { |
| 19 | return items.map(item => ({ |
| 20 | description: item.description ?? '', |
| 21 | id: toNumber(item.value), |
| 22 | name: item.label, |
| 23 | parent: toNumber(item.extra?.parent), |
| 24 | children: item.children ? mapPublishOptionItemsToBilibiliPartitions(item.children) : [], |
| 25 | })) |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * 获取B站分区列表 |
| 30 | * @param accountId 账户ID |
| 31 | * @returns B 站分区选项 |
| 32 | */ |
| 33 | export function apiGetBilibiliPartitions(accountId: string) { |
| 34 | return http |
| 35 | .get<PublishOptionValuesVo>(`v2/channels/accounts/${encodeURIComponent(accountId)}/publish-options/tid/values`) |
| 36 | .then((response) => { |
| 37 | if (!response) |
| 38 | return response |
| 39 | |
| 40 | return { |
| 41 | ...response, |
| 42 | data: response.code === 0 ? mapPublishOptionItemsToBilibiliPartitions(response.data.items) : [], |
| 43 | } |
| 44 | }) |
| 45 | } |
| 46 |