/** 店铺列表/分组/新增/编辑/凭证适配(任务 88-93):/api/admin/shop-manages 系端点 + 归一与解析。 */ import { http } from '@/api/http' import { unwrap } from '@/api/envelope' import { parseShopManagePage, toShopSummary } from './shop-manage-model.ts' import { parseShopManageGroups } from './shop-group-model.ts' import { toShopCredential } from './shop-credential-model.ts' import { normalizeShopListParams, toShopManagePageQuery, type ShopCredential, type ShopGroupOption, type ShopManageListParams, type ShopPageResult, type ShopSummary, } from './shop-dto.ts' import { toShopManageCreateRequest, toShopManageUpdateRequest, validateShopManageForm, type ShopManageFormValues, } from './shop-manage-form-model.ts' export const SHOP_MANAGE_ENDPOINT = '/api/admin/shop-manages' export async function fetchShopManageList(params: Partial = {}): Promise { const normalized = normalizeShopListParams(params) const { data } = await http.get(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 { const { data } = await http.get(SHOP_MANAGE_GROUPS_ENDPOINT) return parseShopManageGroups(data) } /** 提交新增店铺:先校验(失败抛首条错误不发请求),POST /api/admin/shop-manages,解析返回记录。 */ export async function submitCreateShopManage(form: ShopManageFormValues, createdById: number): Promise { const { valid, errors } = validateShopManageForm(form) if (!valid) { throw new Error(Object.values(errors)[0] || '店铺表单校验未通过') } const { data } = await http.post(SHOP_MANAGE_ENDPOINT, toShopManageCreateRequest(form, createdById)) const item = toShopSummary(unwrap(data)) if (!item) { throw new Error('新增店铺响应异常:未返回有效记录') } return item } /** 提交编辑店铺:先校验(失败抛首条错误不发请求),PUT /api/admin/shop-manages/{id}。 */ export async function submitUpdateShopManage(id: number, form: ShopManageFormValues): Promise { const { valid, errors } = validateShopManageForm(form) if (!valid) { throw new Error(Object.values(errors)[0] || '店铺表单校验未通过') } const { data } = await http.put(`${SHOP_MANAGE_ENDPOINT}/${id}`, toShopManageUpdateRequest(form)) const item = toShopSummary(unwrap(data)) if (!item) { throw new Error('更新店铺响应异常:未返回有效记录') } return item } /** 读取店铺明文凭据:GET /api/admin/shop-manages/{id}/credential?shop_name=…;明文仅在内存短时使用。 */ export async function fetchShopCredential(id: number, shopName: string): Promise { const { data } = await http.get(`${SHOP_MANAGE_ENDPOINT}/${id}/credential`, { params: { shop_name: shopName } }) const credential = toShopCredential(unwrap(data)) if (!credential) { throw new Error('读取店铺凭证响应异常:未返回有效记录') } return credential } /** 删除店铺:DELETE /api/admin/shop-manages/{id};成功后由调用方按目标页刷新。 */ export async function deleteShopManage(id: number): Promise { await http.delete(`${SHOP_MANAGE_ENDPOINT}/${id}`) }