提交
This commit is contained in:
1 parent
f9676f8759
commit
725774ef0c
155 files changed
+50675
-5242
No files matched your search
@@ -0,0 +1,103 @@
|
||||
import { attachmentApi, type BxId } from '@/api/bx'
|
||||
import { ElMessage } from 'element-plus'
|
||||
|
||||
export type BxAttachmentItem = {
|
||||
id: number | string
|
||||
fileName: string
|
||||
fileType?: string
|
||||
fileSize?: number
|
||||
previewUrl?: string
|
||||
_uploading?: boolean
|
||||
_existing?: boolean
|
||||
}
|
||||
|
||||
const IMAGE_EXT = /\.(png|jpe?g|gif|webp|bmp|svg)$/i
|
||||
const VIDEO_EXT = /\.(mp4|webm|ogg|mov|avi)$/i
|
||||
|
||||
export function isBxImageFile(file: { fileName?: string; fileType?: string }) {
|
||||
const name = String(file.fileName || '').toLowerCase()
|
||||
const type = String(file.fileType || '').toLowerCase()
|
||||
return type.startsWith('image/') || IMAGE_EXT.test(name)
|
||||
}
|
||||
|
||||
export function isBxVideoFile(file: { fileName?: string; fileType?: string }) {
|
||||
const name = String(file.fileName || '').toLowerCase()
|
||||
const type = String(file.fileType || '').toLowerCase()
|
||||
return type.startsWith('video/') || VIDEO_EXT.test(name)
|
||||
}
|
||||
|
||||
export function canBxPreview(file: BxAttachmentItem) {
|
||||
if (file._uploading) return false
|
||||
return isBxImageFile(file) || isBxVideoFile(file)
|
||||
}
|
||||
|
||||
export async function fillBxAttachmentPreviews(files: BxAttachmentItem[]) {
|
||||
const tasks = files
|
||||
.filter((file) => isBxImageFile(file) && !file.previewUrl)
|
||||
.map(async (file) => {
|
||||
try {
|
||||
const res: any = await attachmentApi.preview(file.id)
|
||||
file.previewUrl = res.data?.url || ''
|
||||
} catch {
|
||||
/* preview optional */
|
||||
}
|
||||
})
|
||||
await Promise.all(tasks)
|
||||
}
|
||||
|
||||
export async function loadBxAttachments(bizType: string, bizId: BxId): Promise<BxAttachmentItem[]> {
|
||||
const res: any = await attachmentApi.listByBiz(bizType, bizId)
|
||||
const files: BxAttachmentItem[] = (res.data || []).map((item: any) => ({
|
||||
id: item.id,
|
||||
fileName: item.fileName,
|
||||
fileType: item.fileType || '',
|
||||
fileSize: item.fileSize,
|
||||
previewUrl: '',
|
||||
_existing: true
|
||||
}))
|
||||
await fillBxAttachmentPreviews(files)
|
||||
return files
|
||||
}
|
||||
|
||||
export async function downloadBxAttachment(file: { id: number | string; fileName: string }) {
|
||||
try {
|
||||
const res: any = await attachmentApi.download(file.id)
|
||||
const blob = new Blob([res], { type: 'application/octet-stream' })
|
||||
const url = URL.createObjectURL(blob)
|
||||
const anchor = document.createElement('a')
|
||||
anchor.href = url
|
||||
anchor.download = file.fileName
|
||||
anchor.click()
|
||||
URL.revokeObjectURL(url)
|
||||
} catch (error: any) {
|
||||
ElMessage.error(error?.message || '下载失败')
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export async function previewBxAttachment(file: BxAttachmentItem) {
|
||||
if (isBxVideoFile(file)) {
|
||||
try {
|
||||
const res: any = await attachmentApi.preview(file.id)
|
||||
return { type: 'video' as const, url: res.data?.url || '', fileName: file.fileName }
|
||||
} catch (error: any) {
|
||||
ElMessage.error(error?.message || '预览失败')
|
||||
throw error
|
||||
}
|
||||
}
|
||||
if (isBxImageFile(file)) {
|
||||
if (file.previewUrl) {
|
||||
return { type: 'image' as const, url: file.previewUrl, fileName: file.fileName }
|
||||
}
|
||||
try {
|
||||
const res: any = await attachmentApi.preview(file.id)
|
||||
file.previewUrl = res.data?.url || ''
|
||||
return { type: 'image' as const, url: file.previewUrl, fileName: file.fileName }
|
||||
} catch (error: any) {
|
||||
ElMessage.error(error?.message || '预览失败')
|
||||
throw error
|
||||
}
|
||||
}
|
||||
await downloadBxAttachment(file)
|
||||
return null
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { pinyin } from 'pinyin-pro'
|
||||
|
||||
/** 递归收集树上已有项目编码(含子节点) */
|
||||
export function flattenExpenseItemCodes(nodes: unknown[]): string[] {
|
||||
const out: string[] = []
|
||||
const walk = (arr: unknown[]) => {
|
||||
for (const n of arr) {
|
||||
const row = n as { itemCode?: string; children?: unknown[] }
|
||||
if (row.itemCode != null && String(row.itemCode).trim() !== '') {
|
||||
out.push(String(row.itemCode).trim())
|
||||
}
|
||||
if (Array.isArray(row.children) && row.children.length) {
|
||||
walk(row.children)
|
||||
}
|
||||
}
|
||||
}
|
||||
walk(nodes)
|
||||
return out
|
||||
}
|
||||
|
||||
/** 项目名称 → 拼音首字母缩写(大写、字母数字) */
|
||||
export function itemNameToCodeBase(name: string): string {
|
||||
const s = name.trim()
|
||||
if (!s) return ''
|
||||
const py = pinyin(s, {
|
||||
pattern: 'first',
|
||||
toneType: 'none',
|
||||
type: 'string',
|
||||
separator: ''
|
||||
})
|
||||
return py
|
||||
.toUpperCase()
|
||||
.replace(/[^A-Z0-9]/g, '')
|
||||
.slice(0, 32)
|
||||
}
|
||||
|
||||
/** 在已有编码集合中分配不重复编码:优先 base,冲突则 base01、base02… */
|
||||
export function allocateItemCode(base: string, existingCodes: string[]): string {
|
||||
const upperExisting = new Set(existingCodes.map((c) => c.toUpperCase()))
|
||||
let b = base.toUpperCase().replace(/[^A-Z0-9]/g, '')
|
||||
if (!b) b = 'XM'
|
||||
if (!upperExisting.has(b)) return b
|
||||
for (let n = 1; n <= 999; n++) {
|
||||
const candidate = `${b}${String(n).padStart(2, '0')}`
|
||||
if (!upperExisting.has(candidate)) return candidate
|
||||
}
|
||||
return `${b}${Date.now().toString().slice(-6)}`
|
||||
}
|
||||
|
||||
/** 根据当前费用项目树生成建议的项目编码 */
|
||||
export function suggestExpenseItemCode(itemName: string, treeNodes: unknown[]): string {
|
||||
if (!itemName.trim()) return ''
|
||||
const base = itemNameToCodeBase(itemName)
|
||||
const existing = flattenExpenseItemCodes(treeNodes)
|
||||
return allocateItemCode(base || 'XM', existing)
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
export function statusLabel(status: number | string | null | undefined): string {
|
||||
if (status == null) return '-'
|
||||
const map: Record<string | number, string> = {
|
||||
1: '启用',
|
||||
0: '禁用',
|
||||
draft: '待提交',
|
||||
pending: '审批中',
|
||||
approved: '审批通过',
|
||||
rejected: '审批驳回',
|
||||
issued: '已发放',
|
||||
paid: '已发放',
|
||||
offset: '已冲抵',
|
||||
processed: '已处理',
|
||||
}
|
||||
return map[status] ?? String(status)
|
||||
}
|
||||
|
||||
export function statusTone(status: number | string | null | undefined): string {
|
||||
if (status == null) return 'info'
|
||||
const map: Record<string | number, string> = {
|
||||
1: 'done',
|
||||
0: 'info',
|
||||
draft: 'info',
|
||||
pending: 'warning',
|
||||
approved: 'done',
|
||||
rejected: 'error',
|
||||
issued: 'done',
|
||||
paid: 'done',
|
||||
offset: 'done',
|
||||
processed: 'done',
|
||||
}
|
||||
return map[status] ?? 'info'
|
||||
}
|
||||
|
||||
/** bx 状态 → Element Plus Tag type(对齐 OpenSpec 语义色) */
|
||||
export function statusTagType(
|
||||
status: number | string | null | undefined
|
||||
): 'success' | 'warning' | 'danger' | 'info' | 'primary' {
|
||||
const tone = statusTone(status)
|
||||
const map: Record<string, 'success' | 'warning' | 'danger' | 'info' | 'primary'> = {
|
||||
done: 'success',
|
||||
error: 'danger',
|
||||
warning: 'warning',
|
||||
info: 'info',
|
||||
processing: 'primary',
|
||||
}
|
||||
return map[tone] ?? 'info'
|
||||
}
|
||||
|
||||
export function menuTypeLabel(type: number): string {
|
||||
const map: Record<number, string> = { 1: '目录', 2: '菜单', 3: '按钮' }
|
||||
return map[type] ?? String(type)
|
||||
}
|
||||
|
||||
function formatMoneyNumber(value: unknown): string {
|
||||
const n = Number(value)
|
||||
if (!Number.isFinite(n)) return '0.00'
|
||||
return n.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 })
|
||||
}
|
||||
|
||||
/** 金额展示:¥ + 固定两位小数(500 → ¥500.00) */
|
||||
export function formatMoney(value: unknown): string {
|
||||
return `¥${formatMoneyNumber(value)}`
|
||||
}
|
||||
|
||||
/** 带 ¥ 前缀的金额展示(与 formatMoney 一致,保留别名便于语义区分) */
|
||||
export function formatMoneyYuan(value: unknown): string {
|
||||
return formatMoney(value)
|
||||
}
|
||||
|
||||
/** 列表合计行金额展示 */
|
||||
export function formatSumMoney(n: number): string {
|
||||
return formatMoney(n)
|
||||
}
|
||||
|
||||
/** el-table-column 金额列 formatter */
|
||||
export function moneyTableFormatter(_row: unknown, _column: unknown, cellValue: unknown): string {
|
||||
return formatMoney(cellValue)
|
||||
}
|
||||
|
||||
/** 列表「当前节点」是否有可展示文案(空/null/空白串视为无) */
|
||||
export function hasWorkflowNode(value: unknown): boolean {
|
||||
if (value == null) return false
|
||||
return String(value).trim() !== ''
|
||||
}
|
||||
|
||||
const DRAFT_STATUS_SET = new Set(['draft', '待提交', '未提交'])
|
||||
const COMPLETED_NODE_STATUS_SET = new Set([
|
||||
'approved',
|
||||
'issued',
|
||||
'paid',
|
||||
'offset',
|
||||
'processed',
|
||||
'审批通过',
|
||||
'已发放',
|
||||
'已冲抵',
|
||||
'已处理',
|
||||
])
|
||||
|
||||
/** 当前节点展示文案:无节点名时按单据状态回退(如未提交、已完成) */
|
||||
export function workflowNodeLabel(currentNode: unknown, status: unknown): string | null {
|
||||
if (hasWorkflowNode(currentNode)) return String(currentNode).trim()
|
||||
const s = String(status ?? '').trim()
|
||||
if (!s) return null
|
||||
if (DRAFT_STATUS_SET.has(s)) return '未提交'
|
||||
if (COMPLETED_NODE_STATUS_SET.has(s)) return '已完成'
|
||||
if (s === 'rejected' || s === '审批驳回') return '已驳回'
|
||||
return null
|
||||
}
|
||||
|
||||
/** 当前节点标签语义色 */
|
||||
export function workflowNodeTone(currentNode: unknown, status: unknown): string {
|
||||
if (hasWorkflowNode(currentNode)) return statusTone(currentNode)
|
||||
const label = workflowNodeLabel(currentNode, status)
|
||||
if (label === '未提交') return 'info'
|
||||
if (label === '已完成') return 'done'
|
||||
if (label === '已驳回') return 'error'
|
||||
return 'info'
|
||||
}
|
||||
|
||||
export function shouldShowWorkflowNodeTag(currentNode: unknown, status: unknown): boolean {
|
||||
return workflowNodeLabel(currentNode, status) != null
|
||||
}
|
||||
|
||||
/** 当前节点 → Element Plus Tag type */
|
||||
export function workflowNodeTagType(
|
||||
currentNode: unknown,
|
||||
status: unknown
|
||||
): 'success' | 'warning' | 'danger' | 'info' | 'primary' {
|
||||
const tone = workflowNodeTone(currentNode, status)
|
||||
const map: Record<string, 'success' | 'warning' | 'danger' | 'info' | 'primary'> = {
|
||||
done: 'success',
|
||||
error: 'danger',
|
||||
warning: 'warning',
|
||||
info: 'info',
|
||||
processing: 'primary',
|
||||
}
|
||||
return map[tone] ?? 'info'
|
||||
}
|
||||
|
||||
/** 详情键值区等纯文本展示 */
|
||||
export function workflowNodeDisplay(currentNode: unknown, status: unknown): string {
|
||||
return workflowNodeLabel(currentNode, status) ?? '-'
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* bx 报销模块 HTTP 客户端
|
||||
* 协议:code === 0、msg、data.rows(与门户 request code===200 分离)
|
||||
*/
|
||||
import axios, { type AxiosRequestConfig, type InternalAxiosRequestConfig } from 'axios'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { useUserStore } from '@/store/modules/user'
|
||||
|
||||
export interface BxApiResponse<T = unknown> {
|
||||
code: number
|
||||
msg: string
|
||||
data?: T
|
||||
}
|
||||
|
||||
const bxAxios = axios.create({
|
||||
baseURL: '/api',
|
||||
timeout: 60000,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
})
|
||||
|
||||
bxAxios.interceptors.request.use((config: InternalAxiosRequestConfig) => {
|
||||
const token = useUserStore().accessToken
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
return config
|
||||
})
|
||||
|
||||
bxAxios.interceptors.response.use(
|
||||
async (res) => {
|
||||
if (res.config.responseType === 'blob') {
|
||||
const contentType = String(res.headers['content-type'] || '')
|
||||
if (contentType.includes('application/json')) {
|
||||
const text = await (res.data as Blob).text()
|
||||
try {
|
||||
const json = JSON.parse(text) as BxApiResponse
|
||||
if (json.code !== 0) {
|
||||
ElMessage.error(json.msg || '请求失败')
|
||||
return Promise.reject(new Error(json.msg))
|
||||
}
|
||||
} catch {
|
||||
/* 非 JSON blob 直出 */
|
||||
}
|
||||
}
|
||||
return res.data
|
||||
}
|
||||
|
||||
const body = res.data as BxApiResponse
|
||||
if (body?.code !== undefined && body.code !== 0) {
|
||||
ElMessage.error(body.msg || '请求失败')
|
||||
return Promise.reject(new Error(body.msg || '请求失败'))
|
||||
}
|
||||
return body
|
||||
},
|
||||
async (err) => {
|
||||
if (err.response?.status === 401) {
|
||||
const userStore = useUserStore()
|
||||
await userStore.logOut()
|
||||
} else {
|
||||
const detail = err.response?.data?.detail || err.response?.data?.msg
|
||||
ElMessage.error(detail || err.message || '网络异常')
|
||||
}
|
||||
return Promise.reject(err)
|
||||
}
|
||||
)
|
||||
|
||||
function request<T = unknown>(config: AxiosRequestConfig) {
|
||||
return bxAxios.request<any, BxApiResponse<T>>(config)
|
||||
}
|
||||
|
||||
const bxRequest = {
|
||||
get<T = unknown>(url: string, config?: AxiosRequestConfig) {
|
||||
return request<T>({ ...config, url, method: 'GET' })
|
||||
},
|
||||
post<T = unknown>(url: string, data?: unknown, config?: AxiosRequestConfig) {
|
||||
return request<T>({ ...config, url, method: 'POST', data })
|
||||
},
|
||||
put<T = unknown>(url: string, data?: unknown, config?: AxiosRequestConfig) {
|
||||
return request<T>({ ...config, url, method: 'PUT', data })
|
||||
},
|
||||
delete<T = unknown>(url: string, config?: AxiosRequestConfig) {
|
||||
return request<T>({ ...config, url, method: 'DELETE' })
|
||||
}
|
||||
}
|
||||
|
||||
export default bxRequest
|
||||
Reference in new issue
Block a user