update common.ts

This commit is contained in:
jingrow 2026-03-11 19:08:37 +08:00
parent 1e13b10c49
commit 496efd8d31

View File

@ -1,9 +1,9 @@
import axios from 'axios' import axios from 'axios'
import { t } from '@/shared/i18n' import { t } from '@/shared/i18n'
// 统一使用相对路径,通过 Vite 代理转发到后端 // Use relative paths, proxy to backend via Vite
// 通用的 API 调用函数 - 兼容 Jingrow 的 jingrow.call 风格 // Generic API call function - compatible with Jingrow's jingrow.call style
export const api = { export const api = {
call: async (method: string, args?: Record<string, any>): Promise<any> => { call: async (method: string, args?: Record<string, any>): Promise<any> => {
try { try {
@ -39,7 +39,7 @@ export const deleteRecords = async (pagetype: string, names: string[]): Promise<
let succeeded = 0 let succeeded = 0
let failed = 0 let failed = 0
// 直接调用 jingrow.client.delete RPC 接口,避免 REST API 层包装 // Call jingrow.client.delete RPC interface directly, avoid REST API wrapper
for (const name of names) { for (const name of names) {
try { try {
await api.call('jingrow.client.delete', { await api.call('jingrow.client.delete', {
@ -61,7 +61,7 @@ export const deleteRecords = async (pagetype: string, names: string[]): Promise<
} }
} }
// 创建记录的通用函数 // Generic function to create a record
export const createRecord = async (pagetype: string, data: Record<string, any>): Promise<{ success: boolean; data?: any; message?: string }> => { export const createRecord = async (pagetype: string, data: Record<string, any>): Promise<{ success: boolean; data?: any; message?: string }> => {
try { try {
const response = await axios.post( const response = await axios.post(
@ -70,11 +70,11 @@ export const createRecord = async (pagetype: string, data: Record<string, any>):
const message = response.data?.message const message = response.data?.message
return { success: true, data: message || response.data, message: 'Created successfully' } return { success: true, data: message || response.data, message: 'Created successfully' }
} catch (error: any) { } catch (error: any) {
return { success: false, message: error.response?.data?.message || error.message || '创建失败' } return { success: false, message: error.response?.data?.message || error.message || t('Failed to create record') }
} }
} }
// 更新记录的通用函数 // Generic function to update a record
export const updateRecord = async (pagetype: string, name: string, data: Record<string, any>): Promise<{ success: boolean; data?: any; message?: string }> => { export const updateRecord = async (pagetype: string, name: string, data: Record<string, any>): Promise<{ success: boolean; data?: any; message?: string }> => {
try { try {
const response = await axios.put( const response = await axios.put(
@ -83,44 +83,44 @@ export const updateRecord = async (pagetype: string, name: string, data: Record<
const message = response.data?.message const message = response.data?.message
return { success: true, data: message || response.data, message: 'Updated successfully' } return { success: true, data: message || response.data, message: 'Updated successfully' }
} catch (error: any) { } catch (error: any) {
return { success: false, message: error.response?.data?.message || error.message || '更新失败' } return { success: false, message: error.response?.data?.message || error.message || t('Failed to update record') }
} }
} }
// 获取记录的通用函数 // Generic function to get a record
export const getRecord = async (pagetype: string, name: string): Promise<{ success: boolean; data?: any; message?: string }> => { export const getRecord = async (pagetype: string, name: string): Promise<{ success: boolean; data?: any; message?: string }> => {
try { try {
const response = await axios.get( const response = await axios.get(
`/api/data/${pagetype}/${name}`) `/api/data/${pagetype}/${name}`)
return { success: true, data: response.data.data } return { success: true, data: response.data.data }
} catch (error: any) { } catch (error: any) {
return { success: false, message: error.response?.data?.message || error.message || '获取失败' } return { success: false, message: error.response?.data?.message || error.message || t('Failed to get record') }
} }
} }
// 获取带__onload数据的记录会调用文档的onload方法 // Get record with __onload data (calls document's onload method)
// 用于需要__onload数据的场景如User pagetype的all_modules // Used for scenarios needing __onload data, like User pagetype's all_modules
export const getRecordWithOnload = async (pagetype: string, name: string): Promise<{ success: boolean; data?: any; message?: string }> => { export const getRecordWithOnload = async (pagetype: string, name: string): Promise<{ success: boolean; data?: any; message?: string }> => {
try { try {
// 使用getdoc RPC方法它会调用onload方法并返回__onload数据 // Use getdoc RPC method, which calls onload method and returns __onload data
const result = await api.call('jingrow.desk.form.load.getdoc', { pagetype, name }) const result = await api.call('jingrow.desk.form.load.getdoc', { pagetype, name })
// 兼容两种响应格式: // Compatible with two response formats:
// SaaS 直接返回 { docs: [pg], docinfo: {...} } // SaaS returns { docs: [pg], docinfo: {...} } directly
// jlocal 返回 { message: { docs: [pg], docinfo: {...} } } // jlocal returns { message: { docs: [pg], docinfo: {...} } }
const docData = result.docs ? result : (result.message || {}) const docData = result.docs ? result : (result.message || {})
const pg = docData.docs?.[0] const pg = docData.docs?.[0]
if (pg) { if (pg) {
return { success: true, data: pg } return { success: true, data: pg }
} else { } else {
return { success: false, message: '文档不存在' } return { success: false, message: t('Document not found') }
} }
} catch (error: any) { } catch (error: any) {
return { success: false, message: error.message || '获取失败' } return { success: false, message: error.message || t('Failed to get record') }
} }
} }
// 获取PageType元数据的专用函数 - 通过 REST API v2 获取完整元数据 // Get PageType metadata - via REST API v2
export const getPageTypeMeta = async (pagetype: string): Promise<{ success: boolean; data?: any; message?: string }> => { export const getPageTypeMeta = async (pagetype: string): Promise<{ success: boolean; data?: any; message?: string }> => {
try { try {
const response = await axios.get( const response = await axios.get(
@ -131,17 +131,17 @@ export const getPageTypeMeta = async (pagetype: string): Promise<{ success: bool
if (meta) { if (meta) {
return { success: true, data: meta } return { success: true, data: meta }
} else { } else {
return { success: false, message: '获取元数据失败' } return { success: false, message: t('Failed to get metadata') }
} }
} catch (error: any) { } catch (error: any) {
return { return {
success: false, success: false,
message: error.response?.data?.message || error.message || '获取元数据失败' message: error.response?.data?.message || error.message || t('Failed to get metadata')
} }
} }
} }
// 获取记录的附件列表 // Get record attachments list
export const getRecordAttachments = async (pagetype: string, name: string): Promise<{ success: boolean; data?: any[]; message?: string }> => { export const getRecordAttachments = async (pagetype: string, name: string): Promise<{ success: boolean; data?: any[]; message?: string }> => {
try { try {
const response = await axios.get( const response = await axios.get(
@ -158,11 +158,11 @@ export const getRecordAttachments = async (pagetype: string, name: string): Prom
) )
return { success: true, data: response.data.data || [] } return { success: true, data: response.data.data || [] }
} catch (error: any) { } catch (error: any) {
return { success: false, message: error.response?.data?.message || error.message || '获取附件失败' } return { success: false, message: error.response?.data?.message || error.message || t('Failed to get attachments') }
} }
} }
// 上传附件 // Upload attachment
export const uploadAttachment = async ( export const uploadAttachment = async (
file: File, file: File,
pagetype: string, pagetype: string,
@ -191,23 +191,23 @@ export const uploadAttachment = async (
} catch (error: any) { } catch (error: any) {
return { return {
success: false, success: false,
message: error.response?.data?.message || error.message || '上传附件失败' message: error.response?.data?.message || error.message || t('Failed to upload attachment')
} }
} }
} }
// 删除附件 // Delete attachment
export const deleteAttachment = async (attachmentName: string): Promise<{ success: boolean; message?: string }> => { export const deleteAttachment = async (attachmentName: string): Promise<{ success: boolean; message?: string }> => {
try { try {
await axios.delete( await axios.delete(
`/api/data/File/${attachmentName}`) `/api/data/File/${attachmentName}`)
return { success: true, message: '附件删除成功' } return { success: true, message: t('Attachment deleted successfully') }
} catch (error: any) { } catch (error: any) {
return { success: false, message: error.response?.data?.message || error.message || '删除附件失败' } return { success: false, message: error.response?.data?.message || error.message || t('Failed to delete attachment') }
} }
} }
// 获取 Workspace 配置 // Get Workspace config
export const getWorkspace = async (name: string): Promise<{ success: boolean; data?: any; message?: string }> => { export const getWorkspace = async (name: string): Promise<{ success: boolean; data?: any; message?: string }> => {
return getRecord('Workspace', name) return getRecord('Workspace', name)
} }
@ -239,7 +239,7 @@ export const saveWorkspaceLayout = async (
} }
} }
// 获取记录总数的通用函数 // Generic function to get record count
export const getCount = async (pagetype: string): Promise<{ success: boolean; count?: number; message?: string }> => { export const getCount = async (pagetype: string): Promise<{ success: boolean; count?: number; message?: string }> => {
try { try {
const response = await axios.get( const response = await axios.get(
@ -257,12 +257,12 @@ export const getCount = async (pagetype: string): Promise<{ success: boolean; co
return { return {
success: false, success: false,
count: 0, count: 0,
message: error.response?.data?.message || error.message || '获取记录总数失败' message: error.response?.data?.message || error.message || t('Failed to get record count')
} }
} }
} }
// 获取Local Job总数的专用函数 - 通过专用端点获取 // Get Local Job count - via dedicated endpoint
export const getLocalJobCount = async (): Promise<{ success: boolean; count?: number; message?: string }> => { export const getLocalJobCount = async (): Promise<{ success: boolean; count?: number; message?: string }> => {
try { try {
const response = await axios.get( const response = await axios.get(
@ -272,18 +272,18 @@ export const getLocalJobCount = async (): Promise<{ success: boolean; count?: nu
return { return {
success: true, success: true,
count: count, count: count,
message: '获取Local Job总数成功' message: t('Local job count retrieved successfully')
} }
} catch (error: any) { } catch (error: any) {
return { return {
success: false, success: false,
count: 0, count: 0,
message: error.response?.data?.detail || error.response?.data?.message || error.message || '获取Local Job总数失败' message: error.response?.data?.detail || error.response?.data?.message || error.message || t('Failed to get local job count')
} }
} }
} }
// 获取记录列表的通用函数 // Generic function to get records list
export const getRecords = async (pagetype: string, filters: any[] = [], fields: string[] = [], orderBy: string = 'modified desc', limitStart: number = 0, limitPageLength: number = 20): Promise<{ success: boolean; data?: any[]; total?: number; message?: string }> => { export const getRecords = async (pagetype: string, filters: any[] = [], fields: string[] = [], orderBy: string = 'modified desc', limitStart: number = 0, limitPageLength: number = 20): Promise<{ success: boolean; data?: any[]; total?: number; message?: string }> => {
try { try {
const response = await axios.get( const response = await axios.get(
@ -313,12 +313,12 @@ export const getRecords = async (pagetype: string, filters: any[] = [], fields:
} catch (error: any) { } catch (error: any) {
return { return {
success: false, success: false,
message: error.response?.data?.message || error.message || '获取记录列表失败' message: error.response?.data?.message || error.message || t('Failed to get records list')
} }
} }
} }
// 上传文件到 Jingrow 服务器 // Upload file to Jingrow server
export const uploadFileToJingrow = async ( export const uploadFileToJingrow = async (
file: File, file: File,
attachedToPagetype?: string, attachedToPagetype?: string,
@ -361,21 +361,21 @@ export const uploadFileToJingrow = async (
file_name: result.message.file_name file_name: result.message.file_name
} }
} else { } else {
return { success: false, error: 'API响应格式错误' } return { success: false, error: t('Invalid API response format') }
} }
} else { } else {
return { success: false, error: `API请求失败 (HTTP ${response.status}): ${response.statusText}` } return { success: false, error: `API request failed (HTTP ${response.status}): ${response.statusText}` }
} }
} catch (error: any) { } catch (error: any) {
return { return {
success: false, success: false,
error: error.response?.data?.message || error.message || '上传文件失败' error: error.response?.data?.message || error.message || t('Failed to upload file')
} }
} }
} }
// 搜索记录(支持翻译搜索)- 使用后端search_widget API // Search records (supports translation search) - uses backend search_widget API
// 对于标记了translated_pagetype的PageType后端会自动处理翻译搜索 // For PageTypes marked with translated_pagetype, backend automatically handles translation search
export const searchWidget = async ( export const searchWidget = async (
pagetype: string, pagetype: string,
txt: string, txt: string,
@ -394,7 +394,7 @@ export const searchWidget = async (
txt: txt || '', txt: txt || '',
page_length: options?.page_length || 20, page_length: options?.page_length || 20,
start: options?.start || 0, start: options?.start || 0,
as_dict: 1 // 返回字典格式,而不是数组格式 as_dict: 1 // Return dict format instead of array format
} }
if (options?.searchfield) { if (options?.searchfield) {
@ -426,7 +426,7 @@ export const searchWidget = async (
} catch (error: any) { } catch (error: any) {
return { return {
success: false, success: false,
message: error.response?.data?.message || error.message || '搜索失败' message: error.response?.data?.message || error.message || t('Search failed')
} }
} }
} }