100 lines
3.3 KiB
TypeScript
100 lines
3.3 KiB
TypeScript
import {
|
||
fetchMineProductionWorkOrderTabStepCountsDeduped,
|
||
fetchProductionWorkOrderTabStepCountsDeduped,
|
||
sumProductionWorkOrderTabStepCounts
|
||
} from '@/api/production-work-order'
|
||
import {
|
||
PRODUCTION_MENU_CODE_MY_TASK,
|
||
PRODUCTION_MENU_CODE_WORK_ORDER,
|
||
useProductionMenuBadgeStore
|
||
} from '@/store/modules/production-menu-badge'
|
||
import { useMenuStore } from '@/store/modules/menu'
|
||
import type { AppRouteRecord } from '@/types/router'
|
||
import { PRODUCTION_WORK_ORDER_VISIBLE_TABS } from '@/utils/production-work-order-tabs'
|
||
|
||
const VISIBLE_TAB_KEYS = PRODUCTION_WORK_ORDER_VISIBLE_TABS
|
||
.filter((tab) => tab.key !== 'all')
|
||
.map((tab) => tab.key)
|
||
|
||
/** 解析菜单项对应的生产角标 menuCode(兼容后端菜单未带 menuCode) */
|
||
export function resolveProductionMenuBadgeCode(item: AppRouteRecord): string | undefined {
|
||
const code = (item.meta.menuCode || '').trim()
|
||
if (code === PRODUCTION_MENU_CODE_WORK_ORDER || code === PRODUCTION_MENU_CODE_MY_TASK) {
|
||
return code
|
||
}
|
||
const name = String(item.name || '')
|
||
if (name === 'ProductionWorkOrder') return PRODUCTION_MENU_CODE_WORK_ORDER
|
||
if (name === 'ProductionMyProductionTask') return PRODUCTION_MENU_CODE_MY_TASK
|
||
const path = String(item.path || '')
|
||
if (path.endsWith('/production-work-order') || path === 'production-work-order') {
|
||
return PRODUCTION_MENU_CODE_WORK_ORDER
|
||
}
|
||
if (path.endsWith('/my-production-task') || path === 'my-production-task') {
|
||
return PRODUCTION_MENU_CODE_MY_TASK
|
||
}
|
||
return undefined
|
||
}
|
||
|
||
/** 从当前侧栏菜单树收集需展示角标的生产菜单 code */
|
||
export function collectVisibleProductionMenuBadgeCodes(menuList: AppRouteRecord[]): string[] {
|
||
const codes = new Set<string>()
|
||
const walk = (items: AppRouteRecord[]) => {
|
||
for (const item of items) {
|
||
const code = resolveProductionMenuBadgeCode(item)
|
||
if (code) codes.add(code)
|
||
if (item.children?.length) walk(item.children)
|
||
}
|
||
}
|
||
walk(menuList || [])
|
||
return [...codes]
|
||
}
|
||
|
||
/** 刷新侧栏「生产工单 / 我的生产任务」Tab 角标汇总 */
|
||
export async function refreshProductionMenuBadges() {
|
||
const menuStore = useMenuStore()
|
||
const badgeStore = useProductionMenuBadgeStore()
|
||
const visibleCodes = collectVisibleProductionMenuBadgeCodes(menuStore.menuList)
|
||
|
||
const tasks: Promise<void>[] = []
|
||
|
||
if (visibleCodes.includes(PRODUCTION_MENU_CODE_WORK_ORDER)) {
|
||
tasks.push(
|
||
fetchProductionWorkOrderTabStepCountsDeduped()
|
||
.then((data) =>
|
||
badgeStore.setBadge(
|
||
PRODUCTION_MENU_CODE_WORK_ORDER,
|
||
sumProductionWorkOrderTabStepCounts(data, VISIBLE_TAB_KEYS)
|
||
)
|
||
)
|
||
.catch(() =>
|
||
badgeStore.setBadge(PRODUCTION_MENU_CODE_WORK_ORDER, {
|
||
overdueIncomplete: 0,
|
||
futureIncomplete: 0
|
||
})
|
||
)
|
||
)
|
||
}
|
||
|
||
if (visibleCodes.includes(PRODUCTION_MENU_CODE_MY_TASK)) {
|
||
tasks.push(
|
||
fetchMineProductionWorkOrderTabStepCountsDeduped()
|
||
.then((data) =>
|
||
badgeStore.setBadge(
|
||
PRODUCTION_MENU_CODE_MY_TASK,
|
||
sumProductionWorkOrderTabStepCounts(data, VISIBLE_TAB_KEYS)
|
||
)
|
||
)
|
||
.catch(() =>
|
||
badgeStore.setBadge(PRODUCTION_MENU_CODE_MY_TASK, {
|
||
overdueIncomplete: 0,
|
||
futureIncomplete: 0
|
||
})
|
||
)
|
||
)
|
||
}
|
||
|
||
if (!tasks.length) return
|
||
|
||
await Promise.all(tasks)
|
||
}
|