fix: 优化提示词模板的创建者的显示
This commit is contained in:
+17
-33
@@ -27,6 +27,7 @@ export interface PromptTemplateUI {
|
||||
status: 'active' | 'inactive' | 'system';
|
||||
version: string;
|
||||
created_by: number;
|
||||
created_by_username?: string; // 创建者用户名
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
@@ -91,10 +92,10 @@ function mapStatusToAPI(status: string): number {
|
||||
|
||||
/**
|
||||
* 将数据库模板转换为UI模板
|
||||
* @param template 数据库模板
|
||||
* @param template 数据库模板(可能包含关联的用户信息)
|
||||
* @returns UI模板
|
||||
*/
|
||||
export function convertToUITemplate(template: PromptTemplate): PromptTemplateUI {
|
||||
export function convertToUITemplate(template: PromptTemplate & { sso_users?: { username: string } }): PromptTemplateUI {
|
||||
return {
|
||||
id: template.id ? template.id.toString() : '',
|
||||
template_name: template.template_name,
|
||||
@@ -105,6 +106,7 @@ export function convertToUITemplate(template: PromptTemplate): PromptTemplateUI
|
||||
status: mapStatusToUI(template.status),
|
||||
version: template.version,
|
||||
created_by: template.created_by,
|
||||
created_by_username: template.sso_users?.username, // 从关联的用户信息中提取用户名
|
||||
created_at: formatDate(template.created_at),
|
||||
updated_at: formatDate(template.updated_at)
|
||||
};
|
||||
@@ -127,21 +129,9 @@ export async function getPromptTemplates(searchParams: PromptSearchParams = {},
|
||||
const page = searchParams.page || 1;
|
||||
const pageSize = searchParams.pageSize || 10;
|
||||
|
||||
// 构建查询参数
|
||||
// 构建查询参数,包含对 sso_users 表的左连接
|
||||
const params: PostgrestParams = {
|
||||
select: `
|
||||
id,
|
||||
template_name,
|
||||
template_type,
|
||||
description,
|
||||
template_content,
|
||||
variables,
|
||||
status,
|
||||
version,
|
||||
created_by,
|
||||
created_at,
|
||||
updated_at
|
||||
`,
|
||||
select: `id,template_name,template_type,description,template_content,variables,status,version,created_by,created_at,updated_at,sso_users!created_by(username)`,
|
||||
order: 'updated_at.desc',
|
||||
headers: {
|
||||
'Prefer': 'count=exact'
|
||||
@@ -242,19 +232,7 @@ export async function getPromptTemplate(id: string, frontendJWT?: string): Promi
|
||||
}
|
||||
|
||||
const params: PostgrestParams = {
|
||||
select: `
|
||||
id,
|
||||
template_name,
|
||||
template_type,
|
||||
description,
|
||||
template_content,
|
||||
variables,
|
||||
status,
|
||||
version,
|
||||
created_by,
|
||||
created_at,
|
||||
updated_at
|
||||
`,
|
||||
select: `id,template_name,template_type,description,template_content,variables,status,version,created_by,created_at,updated_at,sso_users!created_by(username)`,
|
||||
filter: {
|
||||
'id': `eq.${id}`
|
||||
},
|
||||
@@ -286,10 +264,11 @@ export async function getPromptTemplate(id: string, frontendJWT?: string): Promi
|
||||
/**
|
||||
* 创建提示词模板
|
||||
* @param template 提示词模板数据
|
||||
* @param userId 当前用户ID
|
||||
* @param frontendJWT JWT token (可选)
|
||||
* @returns 创建的提示词模板
|
||||
*/
|
||||
export async function createPromptTemplate(template: Partial<PromptTemplateUI>, frontendJWT?: string): Promise<{
|
||||
export async function createPromptTemplate(template: Partial<PromptTemplateUI>, userId: number, frontendJWT?: string): Promise<{
|
||||
data?: PromptTemplateUI;
|
||||
error?: string;
|
||||
status?: number;
|
||||
@@ -299,7 +278,12 @@ export async function createPromptTemplate(template: Partial<PromptTemplateUI>,
|
||||
if (!template.template_name || !template.template_type || !template.template_content) {
|
||||
return { error: '模板名称、类型和内容不能为空', status: 400 };
|
||||
}
|
||||
|
||||
|
||||
// 验证用户ID
|
||||
if (!userId) {
|
||||
return { error: '用户ID不能为空', status: 400 };
|
||||
}
|
||||
|
||||
// 准备变量数据
|
||||
let variablesData: Record<string, string> = {};
|
||||
if (typeof template.variables === 'string') {
|
||||
@@ -311,7 +295,7 @@ export async function createPromptTemplate(template: Partial<PromptTemplateUI>,
|
||||
} else if (template.variables) {
|
||||
variablesData = template.variables;
|
||||
}
|
||||
|
||||
|
||||
// 准备API数据
|
||||
const apiTemplate: Partial<PromptTemplate> = {
|
||||
template_name: template.template_name,
|
||||
@@ -321,7 +305,7 @@ export async function createPromptTemplate(template: Partial<PromptTemplateUI>,
|
||||
variables: variablesData,
|
||||
status: mapStatusToAPI(template.status || 'active'),
|
||||
version: template.version || 'v1.0',
|
||||
created_by: 1 // 固定创建者为1
|
||||
created_by: userId // 使用当前登录用户ID
|
||||
};
|
||||
|
||||
if(apiTemplate){
|
||||
|
||||
@@ -72,9 +72,9 @@ const portConfigs: Record<string, Partial<ApiConfig>> = {
|
||||
// 主要
|
||||
// 梅州
|
||||
'51703': {
|
||||
baseUrl: 'http://nas.7bm.co:8073',
|
||||
documentUrl: 'http://nas.7bm.co:8073/docauditai/',
|
||||
uploadUrl: 'http://nas.7bm.co:8073/admin/documents'
|
||||
baseUrl: 'http://172.16.0.58:8073',
|
||||
documentUrl: 'http://172.16.0.58:8073/docauditai/',
|
||||
uploadUrl: 'http://172.16.0.58:8073/admin/documents'
|
||||
// baseUrl: 'http://nas.7bm.co:8873',
|
||||
// documentUrl: 'http://nas.7bm.co:8873/docauditai/',
|
||||
// uploadUrl: 'http://nas.7bm.co:8873/admin/documents'
|
||||
|
||||
@@ -334,7 +334,8 @@ export default function PromptsIndex() {
|
||||
key: "created_by",
|
||||
width: "100px",
|
||||
render: (_: unknown, record: PromptTemplateUI) => (
|
||||
<span className="text-secondary">用户 {record.created_by}</span>
|
||||
// <span className="text-secondary">用户 {record.created_by}</span>
|
||||
<span className="text-secondary">{record.created_by_username}</span>
|
||||
)
|
||||
},
|
||||
{
|
||||
|
||||
@@ -109,8 +109,9 @@ export async function loader({ request }: LoaderFunctionArgs) {
|
||||
// Action函数 - 处理表单提交
|
||||
export async function action({ request }: ActionFunctionArgs) {
|
||||
const { getUserSession } = await import("~/api/login/auth.server");
|
||||
const { frontendJWT } = await getUserSession(request);
|
||||
const { userInfo, frontendJWT } = await getUserSession(request);
|
||||
|
||||
const userId = userInfo.get('user_id')
|
||||
const formData = await request.formData();
|
||||
const id = formData.get("id") as string;
|
||||
const template_name = formData.get("template_name") as string;
|
||||
@@ -168,7 +169,7 @@ export async function action({ request }: ActionFunctionArgs) {
|
||||
result = await updatePromptTemplate(id, apiTemplate, frontendJWT);
|
||||
} else {
|
||||
// 创建模板
|
||||
result = await createPromptTemplate(apiTemplate, frontendJWT);
|
||||
result = await createPromptTemplate(apiTemplate, userId, frontendJWT);
|
||||
}
|
||||
|
||||
if (result.error) {
|
||||
|
||||
Reference in New Issue
Block a user