Files
workspace/code/fms/fms-vue/src/services/api.js
T
2026-07-26 22:52:21 +08:00

146 lines
5.2 KiB
JavaScript

import { get, 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,
});
}
/**
* 分页查询登录日志(只读列表)。
* 通过通用分页接口加载 s_log_login 表,支持按账号(模糊)、登录结果、登录时间范围筛选。
* @param {object} options
* @param {string} [options.account] 账号关键字
* @param {0|1} [options.result] 登录结果:1 成功 / 0 失败
* @param {[Dayjs, Dayjs]} [options.range] 登录时间范围(antdv RangePicker 的 dayjs 值)
* @param {number} [options.pageNo=1]
* @param {number} [options.pageSize=20]
* @returns {Promise<ApiResult<{rows: object[], total: number}>>} 分页响应。
*/
export function pageLoginLogApi({ account, result, range, pageNo = 1, pageSize = 20 } = {}) {
const conditions = [];
if (account && account.trim()) {
const safe = account.trim().replace(/'/g, "''");
conditions.push(`b_account LIKE N'%${safe}%'`);
}
if (result === 1 || result === 0) {
conditions.push(`b_login_result = ${result}`);
}
if (Array.isArray(range) && range.length === 2 && range[0] && range[1]) {
const fmt = (v) =>
v && typeof v.format === "function"
? v.format("YYYY-MM-DD")
: new Date(v).toISOString().slice(0, 10);
conditions.push(`b_login_time >= '${fmt(range[0])} 00:00:00'`);
conditions.push(`b_login_time <= '${fmt(range[1])} 23:59:59'`);
}
const searchCondition = conditions.length ? conditions.join(" AND ") : null;
return pageDataApi({
viewName: "s_log_login",
orderBy: "b_login_time DESC",
searchCondition,
pageNo,
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 get("/data/nextid", { params: { count } });
}
/**
* 读取表或视图的列结构,用于同步字段等场景。
* @param {string} table 表名或视图名,必填,后端会校验标识符。
* @returns {Promise<ApiResult<{name: string, type: string, nullable: boolean, size: number}[]>} 列结构响应。
*/
export function describeApi(table) {
return post("/data/describe", { table });
}