task-90(店铺中心): 实现店铺分组选项加载
新增 shop-group-model.ts(ShopManageGroupItemVo 下拉项解析) 并在 shop-manage-api.ts 增加 fetchShopManageGroups(GET /api/admin/shop-manages/groups, 后端限定普通管理员可访问分组)。 TDD: task-90.test.ts 8 用例先 RED 后 GREEN。
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
/** 店铺分组选项解析模型(任务 90):解析 ShopManageGroupItemVo 数组为下拉选项,纯逻辑。 */
|
||||
import { unwrap } from '../../api/envelope.ts'
|
||||
import type { ShopGroupOption } from './shop-dto.ts'
|
||||
|
||||
function text(value: unknown): string {
|
||||
return typeof value === 'string' ? value.trim() : ''
|
||||
}
|
||||
|
||||
function numberOrNull(value: unknown): number | null {
|
||||
return typeof value === 'number' && Number.isFinite(value) ? Math.floor(value) : null
|
||||
}
|
||||
|
||||
/** 解析单条分组下拉项;缺 id 视为无效。 */
|
||||
export function toShopGroupOption(raw: unknown): ShopGroupOption | null {
|
||||
if (!raw || typeof raw !== 'object') return null
|
||||
const record = raw as Record<string, unknown>
|
||||
const id = numberOrNull(record.id)
|
||||
if (id === null) return null
|
||||
const option: ShopGroupOption = { id, groupName: text(record.groupName ?? record.group_name) }
|
||||
const memberCount = numberOrNull(record.memberCount ?? record.member_count)
|
||||
if (memberCount !== null) option.memberCount = memberCount
|
||||
const leaderUsername = text(record.leaderUsername ?? record.leader_username)
|
||||
if (leaderUsername) option.leaderUsername = leaderUsername
|
||||
return option
|
||||
}
|
||||
|
||||
/** 归一化分组下拉负载(信封或已解包数组)为选项列表;空/无效项过滤。 */
|
||||
export function parseShopManageGroups(payload: unknown): ShopGroupOption[] {
|
||||
const data = unwrap<unknown>(payload)
|
||||
if (!Array.isArray(data)) return []
|
||||
return data
|
||||
.map((raw) => toShopGroupOption(raw))
|
||||
.filter((option): option is ShopGroupOption => option !== null)
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
/** 店铺列表加载适配(任务 88):GET /api/admin/shop-manages + 分页/筛选归一与解析。 */
|
||||
/** 店铺列表加载与分组选项适配(任务 88/90):GET /api/admin/shop-manages(/groups) + 归一与解析。 */
|
||||
import { http } from '@/api/http'
|
||||
import { parseShopManagePage } from './shop-manage-model.ts'
|
||||
import { parseShopManageGroups } from './shop-group-model.ts'
|
||||
import {
|
||||
normalizeShopListParams,
|
||||
toShopManagePageQuery,
|
||||
type ShopGroupOption,
|
||||
type ShopManageListParams,
|
||||
type ShopPageResult,
|
||||
} from './shop-dto.ts'
|
||||
@@ -15,3 +17,11 @@ export async function fetchShopManageList(params: Partial<ShopManageListParams>
|
||||
const { data } = await http.get<unknown>(SHOP_MANAGE_ENDPOINT, { params: toShopManagePageQuery(normalized) })
|
||||
return parseShopManagePage(data)
|
||||
}
|
||||
|
||||
export const SHOP_MANAGE_GROUPS_ENDPOINT = '/api/admin/shop-manages/groups'
|
||||
|
||||
/** 加载店铺分组下拉选项(超级管理员取全部,普通管理员由后端限定可访问分组)。 */
|
||||
export async function fetchShopManageGroups(): Promise<ShopGroupOption[]> {
|
||||
const { data } = await http.get<unknown>(SHOP_MANAGE_GROUPS_ENDPOINT)
|
||||
return parseShopManageGroups(data)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user