import request from '@/utils/request' const v = '1.0' // ============================================================ // 类型定义(匹配实际 API 响应字段) // ============================================================ /** API 应用列表项 */ export interface ApiAppItem { id: number app_name: string warehouse_id: number self_app_key: string self_app_secret: string // 列表返回脱敏后的 Secret(如 "ce4f07a1****") self_ip_whitelist: string[] | null callback_url: string | null /** 承接的出站回调 method 列表(空/未勾选=不承接任何回调) */ callback_functions?: string[] | null interface_type_id: number rate_limit: number sign_method: string is_enabled: number // 1-启用, 0-停用 created_at: string updated_at: string } /** API 应用详情(含完整 Secret — 仅创建/重新生成时返回) */ export interface ApiAppDetail extends ApiAppItem { // 创建/重置密钥时 self_app_secret 为完整 64 位 hex(顶层字段) } /** create / detail / update / regenerateSecret 的通用扁平响应 */ export interface ApiAppFlatResponse { flag: string code: number message: string id: number app_name: string warehouse_id: number self_app_key: string self_app_secret: string // 创建/重置时是完整密钥,列表/详情时脱敏 self_ip_whitelist: string[] | null callback_url: string | null /** 承接的出站回调 method 列表(create/update 回显;detail 亦返回) */ callback_functions?: string[] | null interface_type_id: number rate_limit: number sign_method: string is_enabled: number created_at: string updated_at: string timestamp: number } /** 列表查询参数 */ export interface ApiAppListParams { page?: number pageSize?: number app_name?: string is_enabled?: number } /** 列表响应(list 和 pagination 在顶层,非 data 内嵌套) */ export interface ApiAppListResponse { flag: string code: number message: string list: ApiAppItem[] pagination: { page: number pageSize: number total: number totalPages: number hasNext: boolean hasPrevious: boolean } } /** 创建参数(sign_method 固定为 md5,无需传参) */ export interface ApiAppCreateParams { app_name: string interface_type_id?: number self_ip_whitelist?: string[] callback_url?: string /** * 承接的出站回调 method 列表,仅允许注册表(apiApp.listCallbackFunctions)中的方法 * (如 notify.reply);空数组/不传 = 不承接任何回调 */ callback_functions?: string[] rate_limit?: number } /** 更新参数(sign_method 固定为 md5,不允许修改) */ export interface ApiAppUpdateParams { id: number app_name?: string self_ip_whitelist?: string[] callback_url?: string /** * 承接的出站回调 method 列表,仅允许注册表中的方法; * **空数组 = 清空承接(不承接任何回调)** */ callback_functions?: string[] rate_limit?: number is_enabled?: number } // ============================================================ // API 方法(响应中 list/pagination 在顶层) // ============================================================ /** 查询 API 应用列表(分页) */ export function apiAppList( params: ApiAppListParams = {} ): Promise { return request({ url: '/system', method: 'post', params: { method: 'apiApp.list', v, timestamp: Date.now() }, data: params, isSystemApi: true } as any) } /** 查询单个 API 应用详情 */ export function apiAppDetail( id: number ): Promise { return request({ url: '/system', method: 'post', params: { method: 'apiApp.detail', v, timestamp: Date.now() }, data: { id }, isSystemApi: true } as any) } /** 创建 API 应用 */ export function apiAppCreate( data: ApiAppCreateParams ): Promise { return request({ url: '/system', method: 'post', params: { method: 'apiApp.create', v, timestamp: Date.now() }, data, isSystemApi: true } as any) } /** 更新 API 应用 */ export function apiAppUpdate( data: ApiAppUpdateParams ): Promise { return request({ url: '/system', method: 'post', params: { method: 'apiApp.update', v, timestamp: Date.now() }, data, isSystemApi: true } as any) } /** 停用 API 应用(软删除) */ export function apiAppDelete( id: number ): Promise<{ flag: string; code: number; message: string }> { return request({ url: '/system', method: 'post', params: { method: 'apiApp.delete', v, timestamp: Date.now() }, data: { id }, isSystemApi: true } as any) } /** 重新生成 AppSecret(响应为扁平结构,self_app_secret 为完整密钥) */ export function apiAppRegenerateSecret( id: number ): Promise { return request({ url: '/system', method: 'post', params: { method: 'apiApp.regenerateSecret', v, timestamp: Date.now() }, data: { id }, isSystemApi: true } as any) } // ============================================================ // 出站回调 method 注册表(apiApp.listCallbackFunctions) // ============================================================ /** 注册表条目:兼容后端返回「字符串数组」或「对象数组」两种形态 */ export type ApiAppCallbackFunction = | string | { /** 回调 method,如 notify.reply */ method: string /** 展示名称(可选) */ title?: string /** 说明(可选) */ description?: string [key: string]: unknown } /** 列表响应(list/total 在顶层,与 apiApp.list 口径一致) */ export interface ApiAppListCallbackFunctionsResponse { flag?: string code: number message?: string list: ApiAppCallbackFunction[] total: number timestamp?: number } /** * 拉取出站回调 method 注册表(代码注册,供管理端多选列表使用) * * 应用勾选后由服务端按 method 自动路由出站回调;入参无。 */ export function apiAppListCallbackFunctions(): Promise { return request({ url: '/system', method: 'post', params: { method: 'apiApp.listCallbackFunctions', v, timestamp: Date.now() }, data: {}, isSystemApi: true } as any) }