118 lines
4.1 KiB
JavaScript
118 lines
4.1 KiB
JavaScript
import { post } from "./http";
|
|
|
|
/**
|
|
* @template T
|
|
* @typedef {object} ApiResult
|
|
* @property {boolean} success 前端根据 code 补充的成功标记。
|
|
* @property {number} code 后端业务状态码,0 表示成功。
|
|
* @property {string} message 响应消息。
|
|
* @property {T} data 响应数据。
|
|
*/
|
|
|
|
/**
|
|
* 机构用户登录。
|
|
* @param {string} orgId 机构码。
|
|
* @param {string} userId 用户 ID。
|
|
* @param {string} password 密码。
|
|
* @returns {Promise<ApiResult<{token: string, orgid: string, user: {id: string, name: string}}>>} 登录响应。
|
|
*/
|
|
export function loginApi(orgId, userId, password) {
|
|
return post("/auth/login", {
|
|
orgid: orgId,
|
|
userid: userId,
|
|
password,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 按表或视图加载数据。
|
|
* @param {string} viewName 表名或视图名,必填。
|
|
* @param {string} [searchCondition] 查询条件,不包含 WHERE。
|
|
* @param {string} [orderBy] 排序字段,可带 ASC 或 DESC。
|
|
* @param {string[]} [searchColumns] 需要返回的字段列表,省略时返回全部字段。
|
|
* @returns {Promise<ApiResult<object[]>>} 查询响应。
|
|
*/
|
|
export function loadDataApi(viewName, searchCondition, orderBy, searchColumns) {
|
|
return post("/data/loaddata", {
|
|
view_name: viewName,
|
|
search_condition: searchCondition,
|
|
order_by: orderBy,
|
|
search_columns: searchColumns,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 执行只读 SQL 查询。
|
|
* @param {string} sql 完整的 SELECT 或 WITH 查询语句。
|
|
* @returns {Promise<ApiResult<object[]>>} 查询响应。
|
|
*/
|
|
export function loadDataBySqlApi(sql) {
|
|
return post("/data/loaddatabysql", { sql });
|
|
}
|
|
|
|
/**
|
|
* 分页加载表或视图数据。
|
|
* @param {object} options 分页查询参数。
|
|
* @param {string} options.viewName 表名或视图名,必填。
|
|
* @param {string} options.orderBy 排序字段,必填,可用逗号分隔多个字段。
|
|
* @param {string[]} [options.searchColumns] 需要返回的字段列表。
|
|
* @param {string} [options.searchCondition] 查询条件,不包含 WHERE。
|
|
* @param {number} [options.pageNo=1] 页码,从 1 开始。
|
|
* @param {number} [options.pageSize=20] 每页记录数。
|
|
* @returns {Promise<ApiResult<{rows: object[], row_count: number, total: number, page_no: number, page_size: number}>>} 分页响应。
|
|
*/
|
|
export function pageDataApi({
|
|
viewName,
|
|
orderBy,
|
|
searchColumns,
|
|
searchCondition,
|
|
pageNo = 1,
|
|
pageSize = 20,
|
|
}) {
|
|
return post("/data/page", {
|
|
view_name: viewName,
|
|
search_columns: searchColumns,
|
|
search_condition: searchCondition,
|
|
order_by: orderBy,
|
|
page_no: pageNo,
|
|
page_size: pageSize,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 在同一事务中保存一个或多个表的数据变更。
|
|
* @param {{table: string, key_field: string, inserts?: object[], updates?: object[], deletes?: object[]}[]} requests 保存配置,不能为空数组。
|
|
* @returns {Promise<ApiResult<null>>} 保存响应。
|
|
*/
|
|
export function saveObjectApi(requests) {
|
|
return post("/data/saveobjt", requests);
|
|
}
|
|
|
|
/**
|
|
* 获取下一个雪花 ID;传入 count 可批量获取指定数量的 ID(返回数组)。
|
|
* @param {number} [count=1] 需要获取的 ID 数量,缺省 1。
|
|
* @returns {Promise<ApiResult<string|string[]>>} count 为 1 时 data 为单个 ID 字符串,大于 1 时为 ID 字符串数组。
|
|
*/
|
|
export function nextIdApi(count = 1) {
|
|
return post("/data/nextid", { count });
|
|
}
|
|
|
|
/**
|
|
* 按模块规则原子获取业务编号;count 大于 1 时一次返回多个编号。
|
|
* @param {string} moduleId 数据模块 ID。
|
|
* @param {number} [count=1] 需要获取的编号数量。
|
|
* @returns {Promise<ApiResult<string|string[]>>} 业务编号或编号数组。
|
|
*/
|
|
export function nextCodeApi(moduleId, count = 1) {
|
|
return post("/data/nextcode", { moduleId: String(moduleId), count });
|
|
}
|
|
|
|
/**
|
|
* 读取表或视图的列结构,用于同步字段等场景。
|
|
* @param {string} table 表名或视图名,必填,后端会校验标识符。
|
|
* @returns {Promise<ApiResult<{name: string, type: string, nullable: boolean, size: number}[]>} 列结构响应。
|
|
*/
|
|
export function describeApi(table) {
|
|
return post("/data/describe", { table });
|
|
}
|