task-93(店铺中心): 实现店铺凭证字段分离

新增 shop-credential-model.ts:凭证明文仅经凭证端点解码且落 SensitiveString,
与列表行(仅掩码)字段分离;shop-manage-api.ts 增加 fetchShopCredential
(GET /api/admin/shop-manages/{id}/credential?shop_name=…)。

TDD: task-93.test.ts 8 用例先 RED 后 GREEN。
This commit is contained in:
2026-09-05 16:48:26 +08:00
parent 813ab5db7b
commit 7fa9b5ccf3
3 changed files with 127 additions and 1 deletions
@@ -0,0 +1,42 @@
/** 店铺明文凭据解析模型(任务 93):凭证明文仅来自凭证端点且落 SensitiveString
* 与列表行(仅掩码)字段分离;纯逻辑。 */
import { unwrap } from '../../api/envelope.ts'
import { toSensitiveString, type ShopCredential } 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 toShopCredential(raw: unknown): ShopCredential | 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 password = text(record.password)
if (!password) return null
const credential: ShopCredential = {
id,
groupId: numberOrNull(record.groupId ?? record.group_id),
groupName: text(record.groupName ?? record.group_name),
shopName: text(record.shopName ?? record.shop_name),
mallName: text(record.mallName ?? record.mall_name),
znUsername: text(record.znUsername ?? record.zn_username),
account: text(record.account),
password: toSensitiveString(password),
}
return credential
}
/** 解包凭证端点负载(data 为 ShopManageCredentialVo)为明文凭据;无效时抛可操作错误。 */
export function parseShopCredential(payload: unknown): ShopCredential {
const credential = toShopCredential(unwrap<unknown>(payload))
if (!credential) {
throw new Error('读取店铺凭证响应异常:未返回有效的明文凭据')
}
return credential
}
@@ -1,11 +1,13 @@
/** 店铺列表/分组/新增适配(任务 88/90/91):GET|POST /api/admin/shop-manages + 归一与解析。 */
/** 店铺列表/分组/新增/编辑/凭证适配(任务 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,
@@ -61,3 +63,13 @@ export async function submitUpdateShopManage(id: number, form: ShopManageFormVal
}
return item
}
/** 读取店铺明文凭据:GET /api/admin/shop-manages/{id}/credential?shop_name=…;明文仅在内存短时使用。 */
export async function fetchShopCredential(id: number, shopName: string): Promise<ShopCredential> {
const { data } = await http.get<unknown>(`${SHOP_MANAGE_ENDPOINT}/${id}/credential`, { params: { shop_name: shopName } })
const credential = toShopCredential(unwrap<unknown>(data))
if (!credential) {
throw new Error('读取店铺凭证响应异常:未返回有效记录')
}
return credential
}