Files
wenzi/frontend/admin/src/services/approval.ts

254 lines
6.3 KiB
TypeScript
Raw Normal View History

/**
* - API交互
*/
import type { AdminRole } from '../auth/roles'
export interface ApprovalFlow {
id: number
flowCode: string
flowName: string
moduleCode: string
description?: string
status: number
createdAt: string
updatedAt?: string
}
export interface ApprovalRecord {
id: number
flowId: number
bizType: string
bizId: string
title: string
applicantId: number
applicantName: string
currentStatus: string
currentNodeId: number
currentNodeName: string
applyReason?: string
applyAttachments?: string
createdAt: string
updatedAt?: string
}
export interface ApprovalNode {
id: number
flowId: number
nodeName: string
nodeType: 'start' | 'approver' | 'condition' | 'cc' | 'end'
approverType: 'user' | 'role' | 'department_leader'
approverIds?: string
approverRoles?: string
condition?: string
timeout: number
action: string
sortOrder: number
}
export interface ApprovalHistory {
id: number
recordId: number
nodeId: number
nodeName: string
action: 'submit' | 'approve' | 'reject' | 'transfer' | 'callback'
operatorId: number
operatorName: string
comment?: string
attachments?: string
createdAt: string
}
export interface CreateFlowRequest {
flowCode: string
flowName: string
moduleCode: string
description?: string
}
export interface UpdateFlowRequest extends Partial<CreateFlowRequest> {
id: number
status?: number
}
export interface ApiResponse<T> {
code: number
data: T
message?: string
}
/**
*
*/
class ApprovalService {
private baseUrl = '/api'
/**
*
*/
async getFlows(): Promise<ApprovalFlow[]> {
const response = await fetch(`${this.baseUrl}/approval/flows`, {
credentials: 'include'
})
const result = await response.json() as ApiResponse<ApprovalFlow[]>
if (result.code !== 200) {
throw new Error(result.message || '获取审批流失败')
}
return result.data
}
/**
*
*/
async getFlowById(id: number): Promise<ApprovalFlow | null> {
const response = await fetch(`${this.baseUrl}/approval/flows/${id}`, {
credentials: 'include'
})
const result = await response.json() as ApiResponse<ApprovalFlow>
if (result.code !== 200) {
return null
}
return result.data
}
/**
*
*/
async createFlow(data: CreateFlowRequest): Promise<number> {
const response = await fetch(`${this.baseUrl}/approval/flows`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify(data)
})
const result = await response.json() as ApiResponse<number>
if (result.code !== 200) {
throw new Error(result.message || '创建审批流失败')
}
return result.data
}
/**
*
*/
async updateFlow(data: UpdateFlowRequest): Promise<void> {
const response = await fetch(`${this.baseUrl}/approval/flows/${data.id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify(data)
})
const result = await response.json() as ApiResponse<void>
if (result.code !== 200) {
throw new Error(result.message || '更新审批流失败')
}
}
/**
*
*/
async deleteFlow(id: number): Promise<void> {
const response = await fetch(`${this.baseUrl}/approval/flows/${id}`, {
method: 'DELETE',
credentials: 'include'
})
const result = await response.json() as ApiResponse<void>
if (result.code !== 200) {
throw new Error(result.message || '删除审批流失败')
}
}
/**
*
*/
async getPendingApprovals(): Promise<ApprovalRecord[]> {
const response = await fetch(`${this.baseUrl}/approval/pending`, {
credentials: 'include'
})
const result = await response.json() as ApiResponse<ApprovalRecord[]>
if (result.code !== 200) {
throw new Error(result.message || '获取待审批列表失败')
}
return result.data
}
/**
*
*/
async getApprovedList(): Promise<ApprovalRecord[]> {
const response = await fetch(`${this.baseUrl}/approval/approved`, {
credentials: 'include'
})
const result = await response.json() as ApiResponse<ApprovalRecord[]>
if (result.code !== 200) {
throw new Error(result.message || '获取已审批列表失败')
}
return result.data
}
/**
*
*/
async getMyApplications(): Promise<ApprovalRecord[]> {
const response = await fetch(`${this.baseUrl}/approval/my`, {
credentials: 'include'
})
const result = await response.json() as ApiResponse<ApprovalRecord[]>
if (result.code !== 200) {
throw new Error(result.message || '获取我发起的审批失败')
}
return result.data
}
/**
*
*/
async approve(data: {
recordId: number
action: 'approve' | 'reject' | 'transfer'
comment?: string
}): Promise<void> {
const response = await fetch(`${this.baseUrl}/approval/handle`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify(data)
})
const result = await response.json() as ApiResponse<void>
if (result.code !== 200) {
throw new Error(result.message || '审批操作失败')
}
}
/**
*
*/
async getRecordById(id: number): Promise<ApprovalRecord | null> {
const response = await fetch(`${this.baseUrl}/approval/records/${id}`, {
credentials: 'include'
})
const result = await response.json() as ApiResponse<ApprovalRecord>
if (result.code !== 200) {
return null
}
return result.data
}
/**
*
*/
async getApprovalHistory(recordId: number): Promise<ApprovalHistory[]> {
const response = await fetch(`${this.baseUrl}/approval/records/${recordId}/history`, {
credentials: 'include'
})
const result = await response.json() as ApiResponse<ApprovalHistory[]>
if (result.code !== 200) {
throw new Error(result.message || '获取审批历史失败')
}
return result.data
}
}
export const approvalService = new ApprovalService()
export default approvalService