给所有请求都加上jwt,隐藏生成jwt的secret(放到.env中),隐藏app-secret(放在pm2运行配置文件中,后续直接读取环境配置即可)

This commit is contained in:
2025-10-17 15:28:22 +08:00
parent 9ec6d30573
commit 59706b70d0
70 changed files with 2279 additions and 688 deletions
+49 -27
View File
@@ -68,9 +68,10 @@ function extractApiData<T>(responseData: unknown): T | null {
/**
* 获取评查点分组列表
* @param token JWT token (可选)
* @returns 评查点分组列表
*/
export async function getRuleGroups(): Promise<{data: RuleGroup[]; error?: never} | {data?: never; error: string; status?: number}> {
export async function getRuleGroups(token?: string): Promise<{data: RuleGroup[]; error?: never} | {data?: never; error: string; status?: number}> {
try {
const params: PostgrestParams = {
select: `
@@ -84,7 +85,8 @@ export async function getRuleGroups(): Promise<{data: RuleGroup[]; error?: never
`,
filter: {
'pid': 'eq.0'
}
},
token
};
const response = await postgrestGet<{code: number; msg: string; data: Array<{
@@ -138,9 +140,10 @@ export async function getRuleGroups(): Promise<{data: RuleGroup[]; error?: never
/**
* 获取指定分组的子分组
* @param parentId 父分组ID
* @param token JWT token (可选)
* @returns 子分组列表
*/
export async function getChildGroups(parentId: string): Promise<{data: RuleGroup[]; error?: never} | {data?: never; error: string; status?: number}> {
export async function getChildGroups(parentId: string, token?: string): Promise<{data: RuleGroup[]; error?: never} | {data?: never; error: string; status?: number}> {
try {
// 1. 获取子分组
const childGroupsParams: PostgrestParams = {
@@ -154,7 +157,8 @@ export async function getChildGroups(parentId: string): Promise<{data: RuleGroup
`,
filter: {
'pid': `eq.${parentId}`
}
},
token
};
const childGroupsResponse = await postgrestGet<{code: number; msg: string; data: Array<{
@@ -179,7 +183,8 @@ export async function getChildGroups(parentId: string): Promise<{data: RuleGroup
select: 'id',
filter: {
'evaluation_point_groups_id': `eq.${group.id}`
}
},
token
};
const ruleCountResponse = await postgrestGet<ApiResponse<Array<{id: number}>>>('evaluation_points', ruleCountParams);
@@ -203,7 +208,8 @@ export async function getChildGroups(parentId: string): Promise<{data: RuleGroup
select: 'id',
filter: {
'evaluation_point_groups_id': `eq.${group.id}`
}
},
token
};
const ruleCountResponse = await postgrestGet<ApiResponse<Array<{id: number}>>>('evaluation_points', ruleCountParams);
@@ -234,9 +240,10 @@ export async function getChildGroups(parentId: string): Promise<{data: RuleGroup
/**
* 获取所有评查点分组(包括一级和二级)
* @param token JWT token (可选)
* @returns 完整的评查点分组列表
*/
export async function getAllRuleGroups(): Promise<{data: RuleGroup[]; error?: never} | {data?: never; error: string; status?: number}> {
export async function getAllRuleGroups(token?: string): Promise<{data: RuleGroup[]; error?: never} | {data?: never; error: string; status?: number}> {
try {
// 1. 获取所有分组
const allGroupsParams: PostgrestParams = {
@@ -245,7 +252,8 @@ export async function getAllRuleGroups(): Promise<{data: RuleGroup[]; error?: ne
pid,
name,
is_enabled
`
`,
token
};
const allGroupsResponse = await postgrestGet<{code: number; msg: string; data: Array<{
@@ -292,7 +300,8 @@ export async function getAllRuleGroups(): Promise<{data: RuleGroup[]; error?: ne
select: 'id',
filter: {
'evaluation_point_groups_id': `eq.${child.id}`
}
},
token
};
const ruleCountResponse = await postgrestGet<ApiResponse<Array<{id: number}>>>('evaluation_points', ruleCountParams);
@@ -316,9 +325,10 @@ export async function getAllRuleGroups(): Promise<{data: RuleGroup[]; error?: ne
/**
* 获取单个评查点分组详情
* @param id 分组ID
* @param token JWT token (可选)
* @returns 分组详情
*/
export async function getRuleGroup(id: string): Promise<{data: RuleGroup; error?: never} | {data?: never; error: string; status?: number}> {
export async function getRuleGroup(id: string, token?: string): Promise<{data: RuleGroup; error?: never} | {data?: never; error: string; status?: number}> {
try {
if (!id) {
return { error: '分组ID不能为空', status: 400 };
@@ -336,7 +346,8 @@ export async function getRuleGroup(id: string): Promise<{data: RuleGroup; error?
`,
filter: {
'id': `eq.${id}`
}
},
token
};
const response = await postgrestGet<{code: number; msg: string; data: Array<{
@@ -389,7 +400,8 @@ export async function getRuleGroup(id: string): Promise<{data: RuleGroup; error?
select: 'id',
filter: {
'evaluation_point_groups_id': `eq.${group.id}`
}
},
token
};
const ruleCountResponse = await postgrestGet<ApiResponse<Array<{id: number}>>>('evaluation_points', ruleCountParams);
@@ -412,9 +424,10 @@ export async function getRuleGroup(id: string): Promise<{data: RuleGroup; error?
/**
* 创建评查点分组
* @param groupData 分组数据
* @param token JWT token (可选)
* @returns 创建的分组
*/
export async function createRuleGroup(groupData: RuleGroupCreateUpdateDto): Promise<{data: RuleGroup; error?: never} | {data?: never; error: string; status?: number}> {
export async function createRuleGroup(groupData: RuleGroupCreateUpdateDto, token?: string): Promise<{data: RuleGroup; error?: never} | {data?: never; error: string; status?: number}> {
try {
// 验证必填字段
if (!groupData.name || !groupData.code) {
@@ -447,7 +460,8 @@ export async function createRuleGroup(groupData: RuleGroupCreateUpdateDto): Prom
// 直接发送到 PostgreSQL 表
const response = await postgrestPost<ApiResponse<ApiRuleGroup> | ApiRuleGroup, ApiRuleGroup>(
'evaluation_point_groups', // 表名
apiGroup
apiGroup,
token
);
if (response.error) {
@@ -490,15 +504,17 @@ export async function createRuleGroup(groupData: RuleGroupCreateUpdateDto): Prom
* 更新评查点分组
* @param id 分组ID
* @param data 更新的分组数据
* @param token JWT token (可选)
* @returns 更新后的分组
*/
export async function updateRuleGroup(id: string, data: RuleGroupCreateUpdateDto): Promise<{data: RuleGroup; error?: never} | {data?: never; error: string; status?: number}> {
export async function updateRuleGroup(id: string, data: RuleGroupCreateUpdateDto, token?: string): Promise<{data: RuleGroup; error?: never} | {data?: never; error: string; status?: number}> {
try {
// 使用新的filters参数
const response = await postgrestPut<ApiResponse<RuleGroup> | RuleGroup, RuleGroupCreateUpdateDto>(
'evaluation_point_groups',
data,
{ id }
{ id },
token
);
if (response.error) {
@@ -524,12 +540,13 @@ export async function updateRuleGroup(id: string, data: RuleGroupCreateUpdateDto
/**
* 删除评查点分组
* @param id 分组ID
* @param token JWT token (可选)
* @returns 删除结果
*/
export async function deleteRuleGroup(id: string): Promise<{success: boolean; error?: string}> {
export async function deleteRuleGroup(id: string, token?: string): Promise<{success: boolean; error?: string}> {
try {
// 1. 首先获取分组信息,判断是一级还是二级分组
const groupResponse = await getRuleGroup(id);
const groupResponse = await getRuleGroup(id, token);
if (groupResponse.error) {
return { success: false, error: groupResponse.error };
}
@@ -542,7 +559,7 @@ export async function deleteRuleGroup(id: string): Promise<{success: boolean; er
// 2. 如果是一级分组,需要先删除所有子分组
if (group.pid === '0') {
// 获取所有子分组
const childGroupsResponse = await getChildGroups(id);
const childGroupsResponse = await getChildGroups(id, token);
if (childGroupsResponse.error) {
return { success: false, error: childGroupsResponse.error };
}
@@ -551,7 +568,7 @@ export async function deleteRuleGroup(id: string): Promise<{success: boolean; er
// 遍历删除每个子分组
for (const childGroup of childGroups) {
const deleteChildResult = await deleteChildGroup(childGroup.id);
const deleteChildResult = await deleteChildGroup(childGroup.id, token);
if (!deleteChildResult.success) {
return deleteChildResult;
}
@@ -559,7 +576,7 @@ export async function deleteRuleGroup(id: string): Promise<{success: boolean; er
}
// 3. 删除分组下的所有评查点
const deletePointsResult = await deleteEvaluationPointsByGroupId(id);
const deletePointsResult = await deleteEvaluationPointsByGroupId(id, token);
if (!deletePointsResult.success) {
return deletePointsResult;
}
@@ -568,7 +585,8 @@ export async function deleteRuleGroup(id: string): Promise<{success: boolean; er
const response = await postgrestDelete<ApiResponse<{id: number}>>('evaluation_point_groups', {
filter: {
'id': `eq.${id}`
}
},
token
});
if (response.error) {
@@ -588,12 +606,13 @@ export async function deleteRuleGroup(id: string): Promise<{success: boolean; er
/**
* 删除子分组及其相关数据
* @param id 子分组ID
* @param token JWT token (可选)
* @returns 删除结果
*/
async function deleteChildGroup(id: string): Promise<{success: boolean; error?: string}> {
async function deleteChildGroup(id: string, token?: string): Promise<{success: boolean; error?: string}> {
try {
// 1. 删除子分组下的所有评查点
const deletePointsResult = await deleteEvaluationPointsByGroupId(id);
const deletePointsResult = await deleteEvaluationPointsByGroupId(id, token);
if (!deletePointsResult.success) {
return deletePointsResult;
}
@@ -602,7 +621,8 @@ async function deleteChildGroup(id: string): Promise<{success: boolean; error?:
const response = await postgrestDelete<ApiResponse<{id: number}>>('evaluation_point_groups', {
filter: {
'id': `eq.${id}`
}
},
token
});
if (response.error) {
@@ -622,14 +642,16 @@ async function deleteChildGroup(id: string): Promise<{success: boolean; error?:
/**
* 删除指定分组下的所有评查点
* @param groupId 分组ID
* @param token JWT token (可选)
* @returns 删除结果
*/
async function deleteEvaluationPointsByGroupId(groupId: string): Promise<{success: boolean; error?: string}> {
async function deleteEvaluationPointsByGroupId(groupId: string, token?: string): Promise<{success: boolean; error?: string}> {
try {
const response = await postgrestDelete<ApiResponse<{id: number}>>('evaluation_points', {
filter: {
'evaluation_point_groups_id': `eq.${groupId}`
}
},
token
});
if (response.error) {