Files
crawler-plugin/admin-frontend-vue/src/stores/company-tab.ts
T
huangzd1997 2078e9efde fix(admin): 公司保存后重拉 tab 目录改走 store 具名出口,摘掉 company-config 棘轮红
ee613db0 给公司管理页加了「保存后刷共享公司目录」,却让这页自己持了一份
useCompanyTabStore(),形状与「给这页上了公司 tab」分不开 ⇒
tests/company-config-entry-super-only.test.ts 的 pageFiles 分母当场红
(公司管理按 2026-10-06 控制方口径本就该永远在名单之外)。

改法:把动作收进 src/stores/company-tab.ts 的具名导出 refreshCompanyDirectory()
(内部仍走 refresh() 的 force 档),页面只调函数、不留 store 实例。语义不变:
本页列表照常重拉,全站 18 个带 tab 页面下一跳拿到新名单;失败仍 error 落 store
+ console.warn 不弹新提示(§12 第 5 条口径)。

验证:company-config-entry-super-only 4/4、相关护栏 26/26、vue-tsc 0 错。
2026-11-17 14:55:15 +08:00

101 lines
6.3 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.
import { defineStore } from 'pinia'
import { fetchCompanies } from '@/api/companies'
import type { CompanyItem } from '@/api/companies-dto'
import { useAdminSessionStore } from '@/stores/admin-session'
import { usePlatformTabStore } from '@/stores/platform-tab'
import type { PlatformTabId } from '@/config/platform-scope'
import { firstSelectableCompanyId, companyIdForRequest } from '@/layout/company-tab-model'
/** 公司 tab 的状态。成员 user ids **不在这里缓存**——每次请求交后端解析,
* 否则"改了成员没刷新页面"会出现一个看起来筛了、实际按旧名单筛的范围。 */
export const useCompanyTabStore = defineStore('company-tab', {
state: () => ({
companies: [] as CompanyItem[],
activeId: null as number | null,
loading: false,
loaded: false,
/** 当前这份目录是按哪个平台 tab 拉的(V206 平台分侧,2026-11-17 用户口径「公司 tab
* 条也按平台分侧,两边各看各的公司」)。空串=还没按平台拉过。早退闸拿它对撞当前
* tab:一致才免拉,平台切了就重拉一份;`fetchCompanies` 失败时不翻转它,旧清单
* (可能过时)继续顶着,下一次切台/重进页面会再试。 */
loadedPlatform: '' as PlatformTabId | '',
/** GET /companies 失败时的原因。**只住 store 与 console,不在模板里渲染**——
* §12 第 5 条没批任何新的用户可见提示,接口暴露禁令高于"失败可见"。
* 取不到目录时 loaded 保持 false ⇒ visibleForOperator 为 false ⇒ tab 条整体不画(fail-closed,观感与改前一致)。 */
error: '',
}),
getters: {
/** 只有超管需要这份目录(Task 2 之后非超管连 GET 都不该发)。
* 「该不该画 tab 条」这条判断**只住在这里**:组件里再写一遍 `length > 1` 就是第二把尺子,
* 将来改口径会两边不一致。loaded 也计入——没拉完之前不画,免得先闪出一条空 tab。
* `activeId !== null` 是「存在至少一家启用公司」——activeId 由 firstSelectableCompanyId 算,
* 用的就是同一把尺子,不是第二处判定。缺它的话两家全停用时 `companies.length > 1` 仍成立,
* 会画出一条容器在、按钮为零的空 tab 条(超管会把它报成"tab 坏了"而不是"公司都停了")。 */
visibleForOperator(): boolean {
return this.loaded
&& useAdminSessionStore().isSuperAdmin
&& this.companies.length > 1
&& this.activeId !== null
},
requestCompanyId(): number | null {
return companyIdForRequest(useAdminSessionStore().isSuperAdmin, this.activeId)
},
},
actions: {
/** 拉公司目录(按当前平台 tab 过滤,后端 GET /api/admin/companies?platform=…)。
* `force`=绕开「该平台已拉过」的早退闸(公司编辑弹窗翻档/启停后的 refresh 用),
* 旧清单保留到成功那刻再整份替换,取数期间 tab 条不闪空。 */
async initialize(options?: { force?: boolean }) {
const session = useAdminSessionStore()
const platform = usePlatformTabStore().active
const alreadyLoadedForThisPlatform = this.loaded && this.loadedPlatform === platform
if ((!options?.force && alreadyLoadedForThisPlatform) || this.loading || !session.isSuperAdmin) return
this.loading = true
// 失败后 loaded 仍是 false ⇒ 下一次 watch 命中超管会重试;重试前先清掉旧原因,免得留着一条已经翻篇的错误。
this.error = ''
try {
const companies = await fetchCompanies(platform)
this.companies = companies
this.loadedPlatform = platform
// 换台重拉后旧选中未必还在新清单里(跨平台公司不重叠):还在就保住(别的页可能正按它取数),
// 不在就回落「id 最小的启用公司」。force 重拉同平台时,选中公司仍在 ⇒ 选中不跳。
const stillListed = this.activeId !== null && companies.some((c) => c.id === this.activeId)
if (!stillListed) {
this.activeId = firstSelectableCompanyId(companies)
}
this.loaded = true
} catch (error) {
// 2026-10-05 修复轮 1:改前只有 try/finally ⇒ GET /companies 抛错时零痕迹(loaded 永远 false、
// tab 条静默消失,异常以 unhandled rejection 落进控制台;全仓 src 里 errorHandler/unhandledrejection 0 命中)。
// 口径照对照组 src/stores/admin-session.ts:37-39:catch 出 error 状态**后再抛**,由调用方决定怎么落地
// ——不在这里吞掉,否则除了 CompanyTabs 之外的调用方会以为目录拿到了。
// 不做的事:不把 error 渲染成提示条/图标(§12 第 5 条未批的界面暴露),也不翻转 loaded(fail-closed)。
this.error = error instanceof Error ? error.message : '公司目录读取失败'
throw error
} finally {
this.loading = false
}
},
/** 公司目录被编辑(公司管理翻档/启停/改名)后的强制重拉:不受 loadedPlatform 早退闸
* 限制。失败语义与 initialize 同口径(error + rethrow),调用方自行决定要不要提示。 */
async refresh() {
await this.initialize({ force: true })
},
setActiveId(id: number) {
this.activeId = id
},
},
})
/** 公司目录失效通知:公司管理页保存(新建/改名/启停/翻档)之后调这里,让全站 tab 条重拉一份。
*
* 为什么是 store 里的具名出口而不是让页面自己持一份 store:公司管理页**不参与公司隔离**
* (控制方 2026-10-06「公司管理不要有 tab」),它要的不是 tab 状态,只是「别让 18 个页面的
* tab 条端着旧名单」这一个动作。页面里一旦出现 `useCompanyTabStore()`,形状就和「给这页上了
* tab」分不开——company-config-entry-super-only 的 pageFiles 分母会当场红(2026-11-17 实测:
* ee613db0 给这页加了保存后重拉,正是踩在这条上)。
* 失败语义同 refresh:error 落 store 再 rethrow,调用方决定要不要提示。 */
export function refreshCompanyDirectory(): Promise<void> {
return useCompanyTabStore().refresh()
}