unis-oa/frontend/src/utils/oa.ts

196 lines
8.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* Small helpers shared by the OA views: status vocabularies, distinguished-name
* rendering and toast-driven action wrappers.
*/
import { useAppStore } from '@/stores/app'
import { useTabsStore } from '@/stores/tabs'
import { viewReadsOfJob } from '@/api/work'
/**
* 打开这张单据,就算把抄送给我的那一阅看过了(服务端口径见 mark_reads_for_work)。
*
* 抄送能从首页待办中心、右上角通知、统一待办三处点进来,指望每一处都记得调一次
* 「标记已阅」迟早漏一处,结果是那一行一直红着。口径收在打开单据这一处。
* 失败不吭声:这只是顺手记一笔,不该在人家看单据的时候弹一条红出来。
*
* 改完要重数的是待办那一颗角标:只需阅的那几条当场就归进已阅、离开了取件箱。
*/
export function markReadsOpened(job: string): void {
if (!job) return
viewReadsOfJob(job)
.then((res) => {
if (res?.count) void useAppStore().refreshBadgeCounts()
})
.catch(() => undefined)
}
/**
* 一张单被人办了一步:哪几屏上的数当场旧了。
*
* 与上面那句 markReadsOpened 同一种"顺手记一笔":指望每一处动作之后都记得去刷那几个列表,
* 迟早漏一处,而漏掉的那一处显示的正是刚被自己办掉的那一行——人看见的就是"我点了没反应"。
* 判据只写在这一处:会随一张单动起来的就是这几屏。
*
* 这里只记一笔账,不直接发请求:真正重取的是那一屏自己(composables/useStaleRevalidate
* 与那两个列表加载器)。所以没开着那些页签时这一句是免费的,而正摆着的那一屏当场悄悄重取,
* 窄屏也重取——这本账的存在就是为了窄屏那一档刻意不做的"每次切回来都重拉"。
*/
export function markListsStale(): void {
useTabsStore().markStale(STALE_AFTER_DOC_ACTION)
}
/**
* 会随一张单动起来的那几屏:待办少一行、已办多一行、我发起的换了状态、草稿箱那份没了、
* 工作台钉着的那几个数也不再是那几个数。待阅那一屏不在这份里:办一步不改谁该阅。
*/
const STALE_AFTER_DOC_ACTION = ['/todo', '/done', '/created', '/draft', '/workbench']
/**
* 一批批量动作的账单:待办与待阅两个列表共用这一个形状。
*
* 后端那两口子(``/task/view/batch`` 与 ``/read/view/batch``)回的是同一组数——成了几条、
* 早就是这状态的几条、几条没动成、被上限留下的几条——界面上那套话也理应是同一套。
* 两边各写一遍的结局是措辞与分寸迟早分家,同一件事在两个列表里听起来像两件事。
*/
export interface BatchOutcome {
/** 真被这次动作改动的条数 */
marked: number
/** 待阅那一路顺带处理掉的条数(写进处理意见的那种) */
handled?: number
/** 勾到时已经是目标状态的:办完了,不是失败 */
already?: number
/** 需要额外条件(处理意见)而这次没给,原样没动的 */
skipped?: number
/** 真没办成的(不存在 / 不是本人名下) */
failed?: number
/** 超出单次上限、一条都没碰的 */
truncated?: number
/** 逐条原因,第一条会跟在提示里说给人听 */
errors?: { id: string; message: string }[]
}
/**
* 把一批的账单念成几句人话,并按轻重弹出去。
*
* 三条规矩,都是被真实反馈磨出来的:
*
* 1. ``already`` / ``skipped`` / ``truncated`` 各说一句,但都不算失败。列表页被页签缓存住时,
* 刚在单据页里看过的那一条还挂在勾里,那是办完了;需处理的没写意见是「这一条还得你自己
* 表个态」;超限那几条更是一条都没碰——混进失败里,人就以为这一批出了错,回头把整批重勾。
* 2. 只报条数不报原因的只有前三种;``failed`` 一定把第一条原因带出来,那两种常见情形
* (不是本人名下 / 这条已被别人办掉)一句就能说清,让人去猜是最坏的收尾。
* 3. 一句都没得说时(一批全是早已办完的)也不静默:至少留一句「没有需要登记的」,
* 否则点了钮屏幕上什么都不动,人只当这一批没生效。
*
* ``say`` 由调用方给:同一个数在待办上叫「已标记 N 条」、在待阅上叫「已登记 N 条」,
* 这两句话的分寸归界面管,这里只管按轻重排队与弹出去。
*/
export function reportBatchOutcome(
result: BatchOutcome,
say: {
done: (result: BatchOutcome) => string
already?: (count: number) => string
skipped?: (count: number) => string
truncated: (count: number) => string
failed: (count: number, reason: string) => string
}
): void {
const appStore = useAppStore()
const already = Number(result.already || 0)
const skipped = Number(result.skipped || 0)
const truncated = Number(result.truncated || 0)
const failed = Number(result.failed || 0)
if (!result.marked && !skipped && !failed && !already && !truncated) {
appStore.info(say.done(result))
} else if (result.marked || (result.handled || 0) > 0) {
appStore.success(say.done(result))
}
if (skipped && say.skipped) appStore.warning(say.skipped(skipped))
if (already && say.already) appStore.warning(say.already(already))
if (truncated) appStore.warning(say.truncated(truncated))
if (failed) {
const reason = String((result.errors || [])[0]?.message || '')
appStore.error(say.failed(failed, reason))
}
}
export type BadgeVariant = 'primary' | 'success' | 'warning' | 'danger' | 'gray' | 'purple'
const WORK_STATUS: Record<string, { label: string; variant: BadgeVariant }> = {
running: { label: '处理中', variant: 'primary' },
handling: { label: '处理中', variant: 'primary' },
completed: { label: '已办结', variant: 'success' },
terminated: { label: '已终止', variant: 'danger' },
canceled: { label: '已作废', variant: 'gray' },
draft: { label: '草稿', variant: 'gray' }
}
const PROCESSING_TYPE: Record<string, string> = {
normal: '正常处理',
auto: '自动完成',
transfer: '转办',
addBefore: '加签接收',
addAfter: '加签处理',
move: '随改道结束',
goback: '退回',
terminate: '终止',
reset: '撤回',
// 不是环节上的表态:抄送 / 转发「需处理」的那条,本人点了「处理」
cc: '抄送处理'
}
export function workStatusLabel(status: string): string {
return WORK_STATUS[status]?.label || status || '--'
}
export function workStatusVariant(status: string): BadgeVariant {
return WORK_STATUS[status]?.variant || 'gray'
}
export function processingTypeLabel(type: string): string {
return PROCESSING_TYPE[type] || type || '正常处理'
}
export function badgeClass(variant: BadgeVariant): string {
return `badge badge-${variant}`
}
export function errorMessage(error: unknown): string {
if (!error) return '操作失败'
if (typeof error === 'string') return error
const candidate = error as { message?: string; data?: { message?: string } }
return candidate.message || candidate.data?.message || '操作失败'
}
/**
* Wraps a mutating API call: toasts the outcome and resolves to null on failure
* so a view can keep its handlers to a couple of lines.
*/
export async function runAction<T>(
action: () => Promise<T>,
options: { success?: string; failure?: string } = {}
): Promise<T | null> {
const appStore = useAppStore()
try {
const value = await action()
if (options.success) appStore.success(options.success)
return value
} catch (error) {
appStore.error(options.failure || errorMessage(error))
return null
}
}
/** `YYYY-MM-DDTHH:mm` friendly value for `<input type="datetime-local">`. */
export function nowLocalInput(): string {
const now = new Date()
const pad = (value: number) => String(value).padStart(2, '0')
return `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}T${pad(now.getHours())}:${pad(
now.getMinutes()
)}`
}
export function todayInput(): string {
return nowLocalInput().slice(0, 10)
}