Files
crawler-plugin/admin-frontend-vue/src/api/companies-model.ts
T
huangzd1997 15b8e46af8 feat(多公司): 两端接上公司身份 —— 后台公司目录页、建号必选公司、顶栏显示所属公司
- 新后台新增「公司管理」页(列/新建/改名/启停,不提供删除),路由 menuKey 与
  columns 表的 admin_companies 逐字一致,由用例钉住;操作指引补该菜单一条。
- 建号弹窗对超管必须显式选一家(后端对"不传"仍落操作者自己公司,是给旧后台兜底);
  admin 只能落自己公司,越权直接 403。账号列表显示 company_name,可按公司筛。
- 客户端登录后把 companyId/companyName 落到 localStorage 并在顶栏显示;
  两个登出出口(handleKicked 与切号/登出)连公司名一起清 —— 残留会把上一个账号
  的归属显示给下一个账号。
- 契约分层:列表端点用 snake_case(company_id/company_name),current-user 与登录
  回包用 camelCase;解析器读到另一套写法必须忽略,防止前后端各猜一套。

admin-frontend-vue 1678/1678、frontend-vue 672/672,两边 build(含 vue-tsc --noEmit)干净。
2026-10-04 07:11:47 +08:00

49 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/** 公司目录响应解析:Java CompanyVo(snake_case) → 前端 CompanyItem(camelCase)。 */
import { unwrap } from './envelope.ts'
import {
COMPANY_STATUS_ACTIVE,
COMPANY_STATUS_DISABLED,
text,
trimmedOrNull,
type CompanyItem,
} from './companies-dto.ts'
function numberOrNull(value: unknown): number | null {
return typeof value === 'number' && Number.isFinite(value) ? Math.floor(value) : null
}
/** 单条映射;无 id 视为无效行(列表中直接丢弃,避免编辑时打到 /companies/undefined)。 */
export function toCompanyItem(raw: unknown): CompanyItem | null {
if (!raw || typeof raw !== 'object') return null
const r = raw as Record<string, unknown>
const id = numberOrNull(r.id)
if (id === null) return null
return {
id,
companyName: text(r.company_name),
// status 列 NOT NULL,取不到就是链路异常;未知一律按停用,不把异常状态当启用。
status: r.status === COMPANY_STATUS_ACTIVE ? COMPANY_STATUS_ACTIVE : COMPANY_STATUS_DISABLED,
remark: trimmedOrNull(r.remark),
createdAt: trimmedOrNull(r.created_at),
updatedAt: trimmedOrNull(r.updated_at),
}
}
/** 解析 GET /api/admin/companies:接受 data 为数组或 {items:[...]},success=false 抛后端文案。 */
export function parseCompanyList(payload: unknown): CompanyItem[] {
const core = unwrap<unknown>(payload)
if (!core || typeof core !== 'object') return []
const candidate = Array.isArray(core) ? core : (core as { items?: unknown }).items
if (!Array.isArray(candidate)) return []
return candidate
.map((raw) => toCompanyItem(raw))
.filter((item): item is CompanyItem => item !== null)
}
/** 解析 POST/PUT 回包(data 为 CompanyVo)。 */
export function parseCompany(payload: unknown): CompanyItem | null {
const core = unwrap<unknown>(payload)
if (!core || typeof core !== 'object' || Array.isArray(core)) return null
return toCompanyItem(core)
}