562 lines
16 KiB
TypeScript
562 lines
16 KiB
TypeScript
import { postgrestGet, postgrestPut } from "../postgrest-client";
|
|
import axios from 'axios';
|
|
import { API_BASE_URL } from '../../config/api-config';
|
|
|
|
interface ResultEnvelope<T> {
|
|
code?: number;
|
|
msg?: string;
|
|
data?: T;
|
|
}
|
|
|
|
function unwrapResultEnvelope<T>(payload: unknown): T {
|
|
if (payload && typeof payload === 'object' && 'data' in (payload as ResultEnvelope<T>)) {
|
|
return ((payload as ResultEnvelope<T>).data ?? null) as T;
|
|
}
|
|
return payload as T;
|
|
}
|
|
|
|
/**
|
|
* 从不同格式的 API 响应中提取数据
|
|
* @param responseData API 响应数据
|
|
* @returns 提取后的数据或 null
|
|
*/
|
|
function extractApiData<T>(responseData: unknown): T | null {
|
|
if (!responseData) return null;
|
|
|
|
// 格式1: { code: number, msg: string, data: T }
|
|
if (typeof responseData === 'object' && responseData !== null &&
|
|
'code' in responseData &&
|
|
'data' in responseData &&
|
|
(responseData as { data: unknown }).data) {
|
|
return (responseData as { data: T }).data;
|
|
}
|
|
|
|
// 格式2: 直接是数据对象
|
|
return responseData as T;
|
|
}
|
|
|
|
/**
|
|
* 提出意见的请求参数接口
|
|
*/
|
|
export interface SubmitOpinionRequest {
|
|
reviewPointResultId: string | number;
|
|
documentId: string | number;
|
|
evaluationPointId: number | null; // 必须是数字ID
|
|
auditOpinion: string;
|
|
deductionScore: number;
|
|
}
|
|
|
|
/**
|
|
* 提出意见的响应接口
|
|
*/
|
|
export interface SubmitOpinionResponse {
|
|
success: boolean;
|
|
message: string;
|
|
data?: {
|
|
id: string | number;
|
|
created_at: string;
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 交叉评查意见数据接口
|
|
*/
|
|
export interface CrossCheckingOpinion {
|
|
proposal_id: string | number;
|
|
evaluation_point_name: string;
|
|
proposed_score: number;
|
|
reason: string;
|
|
proposer: string;
|
|
votes: Array<{ voter: string; vote_type: string }>;
|
|
agree_voters: string[];
|
|
disagree_voters: string[];
|
|
pending_voters: string[];
|
|
can_vote: boolean;
|
|
problem_message: string;
|
|
proposer_id: number;
|
|
created_at: string;
|
|
status: string;
|
|
}
|
|
|
|
/**
|
|
* API响应格式
|
|
*/
|
|
export interface ApiResponse<T> {
|
|
data?: T;
|
|
error?: string;
|
|
status?: number;
|
|
}
|
|
|
|
/**
|
|
* 安全获取JWT token
|
|
* @param jwtToken JWT token字符串
|
|
* @returns JWT token字符串
|
|
*/
|
|
async function safeGetJWT(jwtToken?: string): Promise<string> {
|
|
return jwtToken || '';
|
|
}
|
|
|
|
/**
|
|
* 检查用户是否有权确认完成文档评查
|
|
*
|
|
* 📍 API地址: GET /api/v3/cross-review/tasks/{task_id}/can-confirm
|
|
*
|
|
* @param taskId 任务ID
|
|
* @param frontendJWT JWT token
|
|
* @returns 是否有权确认完成
|
|
*/
|
|
export async function findIsProposer(taskId: string | number, userId: number | undefined, frontendJWT?: string): Promise<boolean> {
|
|
try {
|
|
if (!taskId) {
|
|
console.error('任务ID不能为空');
|
|
return false;
|
|
}
|
|
|
|
const response = await axios.get<ResultEnvelope<{ canConfirm?: boolean; can_confirm?: boolean }>>(
|
|
`${API_BASE_URL}/api/v3/cross-review/tasks/${taskId}/can-confirm`,
|
|
{
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${frontendJWT}`
|
|
}
|
|
}
|
|
);
|
|
|
|
const data = response.data?.data || response.data;
|
|
return data?.canConfirm === true || data?.can_confirm === true;
|
|
} catch (error) {
|
|
console.error('[findIsProposer] 检查权限失败:', error);
|
|
|
|
// 正确处理 axios 错误响应
|
|
if (axios.isAxiosError(error) && error.response?.data) {
|
|
console.error('[findIsProposer] 错误详情:', error.response.data);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 提交交叉评查意见
|
|
* @param opinionData 意见数据
|
|
* @param jwtToken JWT token
|
|
* @returns 提交结果
|
|
*/
|
|
export async function submitCrossCheckingOpinion(
|
|
opinionData: SubmitOpinionRequest,
|
|
jwtToken?: string,
|
|
userInfo?: { user_id: number }
|
|
): Promise<ApiResponse<SubmitOpinionResponse>> {
|
|
try {
|
|
// 获取JWT token
|
|
console.log('jwtToken', jwtToken)
|
|
const token = await safeGetJWT(jwtToken);
|
|
|
|
const requestData = {
|
|
documentId: Number(opinionData.documentId),
|
|
evaluationPointId: Number(opinionData.evaluationPointId),
|
|
deductionScore: opinionData.deductionScore,
|
|
auditOpinion: opinionData.auditOpinion,
|
|
reviewPointResultId: Number(opinionData.reviewPointResultId)
|
|
};
|
|
|
|
const response = await axios.post<ResultEnvelope<{ proposalId?: number; createdAt?: string }>>(`${API_BASE_URL}/api/v3/cross-review/proposals`, requestData, {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${token}`
|
|
}
|
|
});
|
|
const data = unwrapResultEnvelope<{ proposalId?: number; createdAt?: string }>(response.data);
|
|
|
|
return {
|
|
data: {
|
|
success: true,
|
|
message: '意见提交成功',
|
|
data: {
|
|
id: data?.proposalId,
|
|
created_at: data?.createdAt || new Date().toISOString()
|
|
}
|
|
}
|
|
};
|
|
} catch (error) {
|
|
console.error('提交交叉评查意见失败:', error);
|
|
|
|
// 正确处理 axios 错误响应
|
|
let errorMessage = '提交意见失败';
|
|
|
|
if (axios.isAxiosError(error) && error.response?.data) {
|
|
// 从 axios 错误响应中提取 msg 字段
|
|
errorMessage = error.response.data.msg || errorMessage;
|
|
} else if (error instanceof Error) {
|
|
// 处理普通 Error 对象
|
|
errorMessage = error.message || errorMessage;
|
|
}
|
|
|
|
return {
|
|
error: errorMessage,
|
|
status: axios.isAxiosError(error) ? error.response?.status || 500 : 500
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取交叉评查意见列表(支持分页)
|
|
* @param documentId 文档ID
|
|
* @param page 页码
|
|
* @param pageSize 每页大小
|
|
* @param userId 用户ID,可选,便于后端接口对接
|
|
* @param jwtToken JWT token
|
|
* @returns 意见列表和总数
|
|
*/
|
|
export async function getCrossCheckingOpinions(
|
|
documentId: string | number,
|
|
page: number = 1,
|
|
pageSize: number = 10,
|
|
userId?: number, // 可选,便于后端接口对接
|
|
jwtToken?: string // 改为jwtToken参数
|
|
): Promise<ApiResponse<{ opinions: CrossCheckingOpinion[], total: number }>> {
|
|
try {
|
|
// 获取JWT token
|
|
const token = await safeGetJWT(jwtToken);
|
|
|
|
// 如果没传userId,默认用1
|
|
const realUserId = userId ?? 1;
|
|
// 实际后端API调用,拼接API_BASE_URL
|
|
const response = await axios.get<ResultEnvelope<{
|
|
total: number;
|
|
page: number;
|
|
pageSize: number;
|
|
items: Array<{
|
|
proposalId: string | number;
|
|
evaluationPointName: string;
|
|
proposedScore: number;
|
|
reason: string;
|
|
proposer: string;
|
|
votes?: Array<{ voter: string; voteType: string }>;
|
|
agreeVoters?: string[];
|
|
disagreeVoters?: string[];
|
|
pendingVoters?: string[];
|
|
canVote?: boolean;
|
|
problemMessage?: string;
|
|
proposerId: number;
|
|
createdAt: string;
|
|
status: string;
|
|
}>;
|
|
}>>(`${API_BASE_URL}/api/v3/cross-review/documents/${documentId}/proposals`, {
|
|
params: {
|
|
page,
|
|
pageSize
|
|
},
|
|
headers: {
|
|
'Authorization': `Bearer ${token}`
|
|
}
|
|
});
|
|
|
|
const pageData = unwrapResultEnvelope<{
|
|
total: number;
|
|
page: number;
|
|
pageSize: number;
|
|
items: Array<{
|
|
proposalId: string | number;
|
|
evaluationPointName: string;
|
|
proposedScore: number;
|
|
reason: string;
|
|
proposer: string;
|
|
votes?: Array<{ voter: string; voteType: string }>;
|
|
agreeVoters?: string[];
|
|
disagreeVoters?: string[];
|
|
pendingVoters?: string[];
|
|
canVote?: boolean;
|
|
problemMessage?: string;
|
|
proposerId: number;
|
|
createdAt: string;
|
|
status: string;
|
|
}>;
|
|
}>(response.data);
|
|
|
|
// 定义后端返回的数据项类型
|
|
const opinions: CrossCheckingOpinion[] = Array.isArray(pageData?.items) ? pageData.items.map((item) => ({
|
|
proposal_id: item.proposalId,
|
|
evaluation_point_name: item.evaluationPointName,
|
|
proposed_score: item.proposedScore,
|
|
reason: item.reason,
|
|
proposer: item.proposer,
|
|
votes: (item.votes || []).map((vote) => ({ voter: vote.voter, vote_type: vote.voteType })),
|
|
agree_voters: item.agreeVoters || [],
|
|
disagree_voters: item.disagreeVoters || [],
|
|
pending_voters: item.pendingVoters || [],
|
|
can_vote: item.canVote ?? false,
|
|
problem_message: item.problemMessage || '',
|
|
proposer_id: item.proposerId,
|
|
created_at: item.createdAt,
|
|
status: item.status
|
|
})) : [];
|
|
|
|
return {
|
|
data: {
|
|
opinions,
|
|
total: pageData?.total || opinions.length
|
|
}
|
|
};
|
|
} catch (error) {
|
|
console.error('获取交叉评查意见失败:', error);
|
|
|
|
// 正确处理 axios 错误响应
|
|
let errorMessage = '获取意见列表失败';
|
|
|
|
if (axios.isAxiosError(error) && error.response?.data) {
|
|
// 从 axios 错误响应中提取 msg 字段
|
|
errorMessage = error.response.data.msg || errorMessage;
|
|
} else if (error instanceof Error) {
|
|
// 处理普通 Error 对象
|
|
errorMessage = error.message || errorMessage;
|
|
}
|
|
|
|
return {
|
|
error: errorMessage,
|
|
status: axios.isAxiosError(error) ? error.response?.status || 500 : 500
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 意见操作类型
|
|
*/
|
|
export type OpinionActionType = 'agree' | 'disagree' | 'withdraw_vote' | 'withdraw_opinion';
|
|
|
|
/**
|
|
* 投票请求参数接口
|
|
*/
|
|
export interface OpinionVoteCreate {
|
|
vote_type: 'agree' | 'disagree';
|
|
}
|
|
|
|
/**
|
|
* 意见操作请求参数
|
|
*/
|
|
export interface OpinionActionRequest {
|
|
opinionId: string | number;
|
|
action: OpinionActionType;
|
|
}
|
|
|
|
/**
|
|
* 执行意见操作(赞同、反对、撤销投票、撤销意见)
|
|
* @param actionData 操作数据
|
|
* @param jwtToken JWT token
|
|
* @returns 操作结果
|
|
*/
|
|
export async function performOpinionAction(
|
|
actionData: OpinionActionRequest,
|
|
jwtToken?: string,
|
|
userInfo?: { user_id: number }
|
|
): Promise<ApiResponse<{ success: boolean; message: string }>> {
|
|
try {
|
|
const token = await safeGetJWT(jwtToken);
|
|
|
|
let message = '';
|
|
let endpoint = '';
|
|
let requestBody: Record<string, unknown> = {};
|
|
|
|
switch (actionData.action) {
|
|
case 'agree':
|
|
message = '已赞同该意见';
|
|
endpoint = `${API_BASE_URL}/api/v3/cross-review/proposals/${actionData.opinionId}/votes`;
|
|
requestBody = { voteType: 'agree' };
|
|
break;
|
|
case 'disagree':
|
|
message = '已反对该意见';
|
|
endpoint = `${API_BASE_URL}/api/v3/cross-review/proposals/${actionData.opinionId}/votes`;
|
|
requestBody = { voteType: 'disagree' };
|
|
break;
|
|
case 'withdraw_vote':
|
|
message = '已撤销投票';
|
|
endpoint = `${API_BASE_URL}/api/v3/cross-review/proposals/${actionData.opinionId}/votes`;
|
|
requestBody = { voteType: 'cancel' };
|
|
break;
|
|
case 'withdraw_opinion':
|
|
message = '已撤销意见';
|
|
endpoint = `${API_BASE_URL}/api/v3/cross-review/proposals/${actionData.opinionId}`;
|
|
requestBody = {};
|
|
break;
|
|
default:
|
|
throw new Error('无效的操作类型');
|
|
}
|
|
|
|
await (actionData.action === 'withdraw_opinion'
|
|
? await axios.delete(endpoint, {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${token}`
|
|
}
|
|
})
|
|
: await axios.post(endpoint, requestBody, {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${token}`
|
|
}
|
|
}));
|
|
|
|
return {
|
|
data: {
|
|
success: true,
|
|
message: message
|
|
}
|
|
};
|
|
} catch (error) {
|
|
console.error('执行意见操作失败:', error);
|
|
|
|
// 正确处理 axios 错误响应
|
|
let errorMessage = '操作失败';
|
|
|
|
if (axios.isAxiosError(error) && error.response?.data) {
|
|
// 从 axios 错误响应中提取 msg 字段
|
|
errorMessage = error.response.data.msg || errorMessage;
|
|
} else if (error instanceof Error) {
|
|
// 处理普通 Error 对象
|
|
errorMessage = error.message || errorMessage;
|
|
}
|
|
|
|
return {
|
|
error: errorMessage,
|
|
status: axios.isAxiosError(error) ? error.response?.status || 500 : 500
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 完成评查(确认文档审核完成)
|
|
* @param taskId 任务ID
|
|
* @param documentId 文档ID
|
|
* @param frontendJWT JWT token
|
|
* @returns 完成评查结果
|
|
*
|
|
* 📍 API地址: POST /api/v3/cross-review/tasks/{task_id}/documents/{document_id}/complete
|
|
*/
|
|
export async function confirmReviewResults(
|
|
taskId: string | number,
|
|
documentId: string | number,
|
|
frontendJWT?: string
|
|
): Promise<{data?: unknown, error?: string, status?: number}> {
|
|
try {
|
|
if (!taskId) {
|
|
return { error: '任务ID不能为空', status: 400 };
|
|
}
|
|
if (!documentId) {
|
|
return { error: '文档ID不能为空', status: 400 };
|
|
}
|
|
|
|
const response = await axios.post<ResultEnvelope<{
|
|
taskId?: number;
|
|
documentId?: number;
|
|
taskStatus?: string;
|
|
auditStatus?: number;
|
|
}>>(
|
|
`${API_BASE_URL}/api/v3/cross-review/tasks/${taskId}/documents/${documentId}/complete`,
|
|
{}, // 无需请求体
|
|
{
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${frontendJWT}`
|
|
}
|
|
}
|
|
);
|
|
|
|
const data = response.data?.data || response.data;
|
|
|
|
if (data) {
|
|
return {
|
|
data: {
|
|
task_id: data.taskId || taskId,
|
|
document_id: data.documentId || documentId,
|
|
task_status: data.taskStatus,
|
|
audit_status: data.auditStatus,
|
|
message: '文档评查已完成'
|
|
}
|
|
};
|
|
}
|
|
|
|
// 数据为空或格式不正确
|
|
console.error('❌ [confirmReviewResults] API响应数据异常:', data);
|
|
return {
|
|
error: '确认文档审核失败',
|
|
status: 500
|
|
};
|
|
|
|
} catch (error) {
|
|
console.error('完成评查失败:', error);
|
|
|
|
// 正确处理 axios 错误响应
|
|
let errorMessage = '完成评查失败';
|
|
|
|
if (axios.isAxiosError(error) && error.response?.data) {
|
|
// 从 axios 错误响应中提取 detail 或 msg 字段
|
|
errorMessage = error.response.data.detail || error.response.data.msg || errorMessage;
|
|
} else if (error instanceof Error) {
|
|
errorMessage = error.message || errorMessage;
|
|
}
|
|
|
|
return {
|
|
error: errorMessage,
|
|
status: axios.isAxiosError(error) ? error.response?.status || 500 : 500
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
// 点击完成评查按钮后,调用接口,检查文档下提案是否存在未投票用户
|
|
export async function checkProposalVotes(
|
|
documentId: string | number,
|
|
jwtToken?: string
|
|
): Promise<{data?: unknown, error?: string, status?: number}> {
|
|
try {
|
|
// 获取JWT token
|
|
const token = await safeGetJWT(jwtToken);
|
|
|
|
const response = await axios.get<ResultEnvelope<{
|
|
hasPendingVotes?: boolean;
|
|
pendingProposals?: Array<{ evaluationPointName: string; pendingVotersNum: number }>;
|
|
}>>(`${API_BASE_URL}/api/v3/cross-review/documents/${documentId}/pending-votes`, {
|
|
headers: {
|
|
'Authorization': `Bearer ${token}`
|
|
}
|
|
});
|
|
|
|
const data = unwrapResultEnvelope<{
|
|
hasPendingVotes?: boolean;
|
|
pendingProposals?: Array<{ evaluationPointName: string; pendingVotersNum: number }>;
|
|
}>(response.data);
|
|
|
|
return {
|
|
data: {
|
|
success: true,
|
|
message: '检查成功',
|
|
data: {
|
|
pending_proposals: (data?.pendingProposals || []).map((item) => ({
|
|
evaluation_point_name: item.evaluationPointName,
|
|
pending_voters_num: item.pendingVotersNum
|
|
}))
|
|
}
|
|
}
|
|
};
|
|
} catch (error) {
|
|
console.error('检查失败:', error);
|
|
|
|
// 正确处理 axios 错误响应
|
|
let errorMessage = '检查失败';
|
|
|
|
if (axios.isAxiosError(error) && error.response?.data) {
|
|
// 从 axios 错误响应中提取 msg 字段
|
|
errorMessage = error.response.data.msg || errorMessage;
|
|
} else if (error instanceof Error) {
|
|
// 处理普通 Error 对象
|
|
errorMessage = error.message || errorMessage;
|
|
}
|
|
|
|
return {
|
|
error: errorMessage,
|
|
status: axios.isAxiosError(error) ? error.response?.status || 500 : 500
|
|
};
|
|
}
|
|
}
|