给所有请求都加上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
+33 -14
View File
@@ -58,6 +58,7 @@ export interface TemplateSearchParams {
pageSize?: number;
sortBy?: string;
sortOrder?: 'asc' | 'desc';
token?: string; // JWT token
}
export interface SearchResult {
@@ -70,12 +71,14 @@ export interface SearchResult {
/**
* 获取所有合同分类
* @param jwt JWT token (可选)
*/
export async function getContractCategories() {
export async function getContractCategories(jwt?: string) {
try {
const params: PostgrestParams = {
select: '*',
order: 'sort_order.asc,name.asc'
order: 'sort_order.asc,name.asc',
token: jwt
};
const response = await postgrestGet<ContractCategory[]>('contract_categories', params);
@@ -98,13 +101,15 @@ export async function getContractCategories() {
/**
* 获取所有合同分类及其模板数量(使用聚合查询)
* @param jwt JWT token (可选)
*/
export async function getContractCategoriesWithCount() {
export async function getContractCategoriesWithCount(jwt?: string) {
try {
// 获取所有分类
const categoriesResponse = await postgrestGet<ContractCategory[]>('contract_categories', {
select: '*',
order: 'sort_order.asc,name.asc'
order: 'sort_order.asc,name.asc',
token: jwt
});
if (categoriesResponse.error) {
@@ -120,7 +125,8 @@ export async function getContractCategoriesWithCount() {
// 简化方案:获取该分类下的所有模板ID,然后计算数量
const countResponse = await postgrestGet<{ id: number }[]>('contract_templates', {
select: 'id',
filter: { 'category_id': `eq.${category.id}` }
filter: { 'category_id': `eq.${category.id}` },
token: jwt
});
let templateCount = 0;
@@ -172,7 +178,8 @@ export async function getContractTemplates(searchParams: TemplateSearchParams =
page = 1,
pageSize = 6,
sortBy = 'updated_at',
sortOrder = 'desc'
sortOrder = 'desc',
token
} = searchParams;
// 构建查询参数
@@ -180,7 +187,8 @@ export async function getContractTemplates(searchParams: TemplateSearchParams =
select: 'id,template_code,title,category_id,description,file_path,file_format,is_featured,created_at,updated_at,pdf_file_path,category:contract_categories(id,name,icon,description)',
limit: pageSize,
offset: (page - 1) * pageSize,
order: `${sortBy}.${sortOrder}`
order: `${sortBy}.${sortOrder}`,
token
};
// 构建过滤条件
@@ -207,7 +215,8 @@ export async function getContractTemplates(searchParams: TemplateSearchParams =
try {
const categoryResponse = await postgrestGet<ContractCategory[]>('contract_categories', {
select: 'id',
filter: { 'name': `ilike.*${cleanKeyword}*` }
filter: { 'name': `ilike.*${cleanKeyword}*` },
token
});
if (categoryResponse.data) {
@@ -237,7 +246,8 @@ export async function getContractTemplates(searchParams: TemplateSearchParams =
if (category && !category_id) {
const categoryResponse = await postgrestGet<ContractCategory[]>('contract_categories', {
select: 'id',
filter: { 'name': `eq.${category}` }
filter: { 'name': `eq.${category}` },
token
});
if (categoryResponse.data) {
@@ -265,7 +275,8 @@ export async function getContractTemplates(searchParams: TemplateSearchParams =
const countParams: PostgrestParams = {
select: 'id',
filter: params.filter,
or: params.or
or: params.or,
token
};
const countResponse = await postgrestGet<{ id: number }[]>('contract_templates', countParams);
@@ -295,12 +306,15 @@ export async function getContractTemplates(searchParams: TemplateSearchParams =
/**
* 根据ID获取单个合同模板
* @param id 模板ID
* @param jwt JWT token (可选)
*/
export async function getContractTemplate(id: string | number) {
export async function getContractTemplate(id: string | number, jwt?: string) {
try {
const params: PostgrestParams = {
select: 'id,template_code,title,category_id,description,file_path,file_format,is_featured,created_at,updated_at,pdf_file_path,category:contract_categories(id,name,icon,description)',
filter: { 'id': `eq.${id}` }
filter: { 'id': `eq.${id}` },
token: jwt
};
const response = await postgrestGet<ContractTemplate[]>('contract_templates', params);
@@ -327,14 +341,17 @@ export async function getContractTemplate(id: string | number) {
/**
* 获取推荐模板
* @param limit 数量限制
* @param jwt JWT token (可选)
*/
export async function getFeaturedTemplates(limit: number = 6) {
export async function getFeaturedTemplates(limit: number = 6, jwt?: string) {
try {
const params: PostgrestParams = {
select: 'id,template_code,title,category_id,description,file_path,file_format,is_featured,created_at,updated_at,pdf_file_path,category:contract_categories(id,name,icon,description)',
filter: { 'is_featured': 'eq.true' },
order: 'updated_at.desc',
limit
limit,
token: jwt
};
const response = await postgrestGet<ContractTemplate[]>('contract_templates', params);
@@ -357,6 +374,8 @@ export async function getFeaturedTemplates(limit: number = 6) {
/**
* 搜索合同模板(智能搜索)
* @param query 搜索关键词
* @param filters 过滤条件
*/
export async function searchContractTemplates(
query: string,