Files
zkxw/web-admin/src/utils/oa-menu-badge.ts
T
zlwuandCursor 5dad1bb0e3 fix: 协议子资源单事务保存,并补齐样本编码/OA/质检等近期能力
锁定缴费校验失败时整单回滚,避免样本半成功落库;前端禁止删除锁定缴费样本,改码走更正编码。同步落地样本更正、采集微信通知、OA提及未读、细胞产品套餐与相关迁移文档。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-26 11:02:25 +08:00

94 lines
3.1 KiB
TypeScript

import {
fetchOaCcUnreadCount,
fetchOaInstanceDraftCount,
fetchOaMentionUnreadCount,
fetchOaPendingTaskCount
} from '@/api/oaApproval'
import {
OA_MENU_CODE_CC,
OA_MENU_CODE_DRAFTS,
OA_MENU_CODE_MENTION,
OA_MENU_CODE_PENDING,
useOaMenuBadgeStore
} from '@/store/modules/oa-menu-badge'
import { useMenuStore } from '@/store/modules/menu'
import type { AppRouteRecord } from '@/types/router'
const OA_BADGE_CODES = new Set([
OA_MENU_CODE_CC,
OA_MENU_CODE_DRAFTS,
OA_MENU_CODE_PENDING,
OA_MENU_CODE_MENTION
])
export function resolveOaMenuBadgeCode(item: AppRouteRecord): string | undefined {
const code = (item.meta.menuCode || '').trim()
if (OA_BADGE_CODES.has(code)) return code
const name = String(item.name || '')
if (name === 'OaApprovalCc') return OA_MENU_CODE_CC
if (name === 'OaApprovalDrafts') return OA_MENU_CODE_DRAFTS
if (name === 'OaApprovalPending') return OA_MENU_CODE_PENDING
if (name === 'OaApprovalMention') return OA_MENU_CODE_MENTION
// path 兜底仅限 oa: 菜单,避免命中旧工作流 pending
if (!code.startsWith('oa:')) return undefined
const path = String(item.path || '')
if (path.endsWith('/cc') || path === 'cc') return OA_MENU_CODE_CC
if (path.endsWith('/drafts') || path === 'drafts') return OA_MENU_CODE_DRAFTS
if (path.endsWith('/pending') || path === 'pending') return OA_MENU_CODE_PENDING
if (path.endsWith('/mentions') || path === 'mentions') return OA_MENU_CODE_MENTION
return undefined
}
export function collectVisibleOaMenuBadgeCodes(menuList: AppRouteRecord[]): string[] {
const codes = new Set<string>()
const walk = (items: AppRouteRecord[]) => {
for (const item of items) {
const code = resolveOaMenuBadgeCode(item)
if (code) codes.add(code)
if (item.children?.length) walk(item.children)
}
}
walk(menuList || [])
return [...codes]
}
export async function refreshOaMenuBadges() {
const menuStore = useMenuStore()
const badgeStore = useOaMenuBadgeStore()
const visibleCodes = collectVisibleOaMenuBadgeCodes(menuStore.menuList)
if (!visibleCodes.length) return
const jobs: Promise<void>[] = []
if (visibleCodes.includes(OA_MENU_CODE_CC)) {
jobs.push(
fetchOaCcUnreadCount()
.then((data) => badgeStore.setBadge(OA_MENU_CODE_CC, data?.count ?? 0))
.catch(() => badgeStore.setBadge(OA_MENU_CODE_CC, 0))
)
}
if (visibleCodes.includes(OA_MENU_CODE_DRAFTS)) {
jobs.push(
fetchOaInstanceDraftCount()
.then((data) => badgeStore.setBadge(OA_MENU_CODE_DRAFTS, data?.count ?? 0))
.catch(() => badgeStore.setBadge(OA_MENU_CODE_DRAFTS, 0))
)
}
if (visibleCodes.includes(OA_MENU_CODE_PENDING)) {
jobs.push(
fetchOaPendingTaskCount()
.then((data) => badgeStore.setBadge(OA_MENU_CODE_PENDING, data?.count ?? 0))
.catch(() => badgeStore.setBadge(OA_MENU_CODE_PENDING, 0))
)
}
if (visibleCodes.includes(OA_MENU_CODE_MENTION)) {
jobs.push(
fetchOaMentionUnreadCount()
.then((data) => badgeStore.setBadge(OA_MENU_CODE_MENTION, data?.count ?? 0))
.catch(() => badgeStore.setBadge(OA_MENU_CODE_MENTION, 0))
)
}
await Promise.all(jobs)
}