1
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
const stats = require("../service/biz_api_stats_service");
|
||||
const crud = require("../service/biz_admin_crud");
|
||||
|
||||
|
||||
const baseModel = require("../../middleware/baseModel");
|
||||
const { find_page } = require("../service/biz_query_helpers");
|
||||
|
||||
module.exports = {
|
||||
/** 按用户查询调用统计 */
|
||||
@@ -36,10 +35,10 @@ module.exports = {
|
||||
ctx.success(data);
|
||||
},
|
||||
|
||||
/** 调用日志分页列表(复用通用 CRUD) */
|
||||
/** 调用日志分页列表 */
|
||||
"POST /biz_api_call_log/page": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
const data = await crud.page("biz_api_call_log", body);
|
||||
ctx.success({ rows: data.rows, count: data.count });
|
||||
const { count, rows } = await find_page(baseModel.biz_api_call_log, "biz_api_call_log", body);
|
||||
ctx.success({ rows, count });
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
const crud = require("../service/biz_admin_crud");
|
||||
|
||||
const baseModel = require("../../middleware/baseModel");
|
||||
const { find_page, find_for_export } = require("../service/biz_query_helpers");
|
||||
|
||||
module.exports = {
|
||||
"POST /biz_audit_log/page": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
|
||||
|
||||
const data = await crud.page("biz_audit_log", body);
|
||||
ctx.success({ rows: data.rows, count: data.count });
|
||||
const { count, rows } = await find_page(baseModel.biz_audit_log, "biz_audit_log", body);
|
||||
ctx.success({ rows, count });
|
||||
},
|
||||
"POST /biz_audit_log/export": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
const res = await crud.exportCsv("biz_audit_log", body);
|
||||
const res = await find_for_export(baseModel.biz_audit_log, "biz_audit_log", body);
|
||||
ctx.success(res);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
const crud = require("../service/biz_admin_crud");
|
||||
const baseModel = require("../../middleware/baseModel");
|
||||
const { find_page, find_for_export, normalize_for_write } = require("../service/biz_query_helpers");
|
||||
const audit = require("../service/biz_audit_service");
|
||||
|
||||
module.exports = {
|
||||
"POST /biz_plan/page": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
const data = await crud.page("biz_plan", body);
|
||||
ctx.success({ rows: data.rows, count: data.count });
|
||||
const { count, rows } = await find_page(baseModel.biz_plan, "biz_plan", body);
|
||||
ctx.success({ rows, count });
|
||||
},
|
||||
"POST /biz_plan/add": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
const row = await crud.add("biz_plan", body);
|
||||
const payload = normalize_for_write(baseModel.biz_plan, body, { for_create: true });
|
||||
const row = await baseModel.biz_plan.create(payload);
|
||||
await audit.logAudit({
|
||||
admin_user_id: audit.pickAdminId(ctx),
|
||||
action: "biz_plan.add",
|
||||
@@ -22,7 +23,11 @@ module.exports = {
|
||||
},
|
||||
"POST /biz_plan/edit": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
await crud.edit("biz_plan", body);
|
||||
const id = body.id;
|
||||
if (id === undefined || id === null || id === "") throw new Error("缺少 id");
|
||||
const payload = normalize_for_write(baseModel.biz_plan, body, { for_create: false });
|
||||
delete payload.id;
|
||||
await baseModel.biz_plan.update(payload, { where: { id } });
|
||||
await audit.logAudit({
|
||||
admin_user_id: audit.pickAdminId(ctx),
|
||||
action: "biz_plan.edit",
|
||||
@@ -33,22 +38,29 @@ module.exports = {
|
||||
},
|
||||
"POST /biz_plan/del": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
await crud.del("biz_plan", body);
|
||||
const id = body.id !== undefined ? body.id : body;
|
||||
if (id === undefined || id === null || id === "") throw new Error("缺少 id");
|
||||
await baseModel.biz_plan.destroy({ where: { id } });
|
||||
await audit.logAudit({
|
||||
admin_user_id: audit.pickAdminId(ctx),
|
||||
action: "biz_plan.del",
|
||||
resource_type: "biz_plan",
|
||||
resource_id: body.id,
|
||||
resource_id: id,
|
||||
});
|
||||
ctx.success({});
|
||||
},
|
||||
"GET /biz_plan/detail": async (ctx) => {
|
||||
const q = ctx.query || {};
|
||||
const row = await crud.detail("biz_plan", { id: q.id || q.ID });
|
||||
const id = q.id || q.ID;
|
||||
if (id === undefined || id === null || id === "") throw new Error("缺少 id");
|
||||
const row = await baseModel.biz_plan.findByPk(id);
|
||||
ctx.success(row);
|
||||
},
|
||||
"GET /biz_plan/all": async (ctx) => {
|
||||
const rows = await crud.all("biz_plan");
|
||||
const rows = await baseModel.biz_plan.findAll({
|
||||
limit: 2000,
|
||||
order: [["id", "DESC"]],
|
||||
});
|
||||
ctx.success(rows);
|
||||
},
|
||||
"POST /biz_plan/toggle": async (ctx) => {
|
||||
@@ -70,7 +82,7 @@ module.exports = {
|
||||
},
|
||||
"POST /biz_plan/export": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
const res = await crud.exportCsv("biz_plan", body);
|
||||
const res = await find_for_export(baseModel.biz_plan, "biz_plan", body);
|
||||
ctx.success(res);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,18 +1,19 @@
|
||||
const crud = require("../service/biz_admin_crud");
|
||||
|
||||
const baseModel = require("../../middleware/baseModel");
|
||||
const { find_page, find_for_export } = require("../service/biz_query_helpers");
|
||||
const logic = require("../service/biz_subscription_logic");
|
||||
const audit = require("../service/biz_audit_service");
|
||||
|
||||
module.exports = {
|
||||
"POST /biz_subscription/page": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
const data = await crud.page("biz_subscription", body);
|
||||
ctx.success({ rows: data.rows, count: data.count });
|
||||
const { count, rows } = await find_page(baseModel.biz_subscription, "biz_subscription", body);
|
||||
ctx.success({ rows, count });
|
||||
},
|
||||
"GET /biz_subscription/detail": async (ctx) => {
|
||||
const q = ctx.query || {};
|
||||
const row = await crud.detail("biz_subscription", { id: q.id || q.ID });
|
||||
const id = q.id || q.ID;
|
||||
if (id === undefined || id === null || id === "") throw new Error("缺少 id");
|
||||
const row = await baseModel.biz_subscription.findByPk(id);
|
||||
ctx.success(row);
|
||||
},
|
||||
"GET /biz_subscription/by_user": async (ctx) => {
|
||||
@@ -74,7 +75,7 @@ module.exports = {
|
||||
},
|
||||
"POST /biz_subscription/export": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
const res = await crud.exportCsv("biz_subscription", body);
|
||||
const res = await find_for_export(baseModel.biz_subscription, "biz_subscription", body);
|
||||
ctx.success(res);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
const crud = require("../service/biz_admin_crud");
|
||||
|
||||
const baseModel = require("../../middleware/baseModel");
|
||||
const { find_page, find_for_export } = require("../service/biz_query_helpers");
|
||||
const tokenLogic = require("../service/biz_token_logic");
|
||||
const audit = require("../service/biz_audit_service");
|
||||
|
||||
module.exports = {
|
||||
"POST /biz_token/page": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
const data = await crud.page("biz_api_token", body);
|
||||
ctx.success({ rows: data.rows, count: data.count });
|
||||
const { count, rows } = await find_page(baseModel.biz_api_token, "biz_api_token", body);
|
||||
ctx.success({ rows, count });
|
||||
},
|
||||
"POST /biz_token/create": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
@@ -44,7 +44,7 @@ module.exports = {
|
||||
},
|
||||
"POST /biz_token/export": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
const res = await crud.exportCsv("biz_api_token", body);
|
||||
const res = await find_for_export(baseModel.biz_api_token, "biz_api_token", body);
|
||||
ctx.success(res);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,35 +1,44 @@
|
||||
const crud = require("../service/biz_admin_crud");
|
||||
|
||||
const baseModel = require("../../middleware/baseModel");
|
||||
const { find_page, find_for_export, normalize_for_write } = require("../service/biz_query_helpers");
|
||||
|
||||
module.exports = {
|
||||
"POST /biz_usage/page": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
const data = await crud.page("biz_usage_monthly", body);
|
||||
ctx.success({ rows: data.rows, count: data.count });
|
||||
const { count, rows } = await find_page(baseModel.biz_usage_monthly, "biz_usage_monthly", body);
|
||||
ctx.success({ rows, count });
|
||||
},
|
||||
"POST /biz_usage/add": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
const row = await crud.add("biz_usage_monthly", body);
|
||||
const payload = normalize_for_write(baseModel.biz_usage_monthly, body, { for_create: true });
|
||||
const row = await baseModel.biz_usage_monthly.create(payload);
|
||||
ctx.success(row);
|
||||
},
|
||||
"POST /biz_usage/edit": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
await crud.edit("biz_usage_monthly", body);
|
||||
const id = body.id;
|
||||
if (id === undefined || id === null || id === "") throw new Error("缺少 id");
|
||||
const payload = normalize_for_write(baseModel.biz_usage_monthly, body, { for_create: false });
|
||||
delete payload.id;
|
||||
await baseModel.biz_usage_monthly.update(payload, { where: { id } });
|
||||
ctx.success({});
|
||||
},
|
||||
"POST /biz_usage/del": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
await crud.del("biz_usage_monthly", body);
|
||||
const id = body.id !== undefined ? body.id : body;
|
||||
if (id === undefined || id === null || id === "") throw new Error("缺少 id");
|
||||
await baseModel.biz_usage_monthly.destroy({ where: { id } });
|
||||
ctx.success({});
|
||||
},
|
||||
"GET /biz_usage/detail": async (ctx) => {
|
||||
const q = ctx.query || {};
|
||||
const row = await crud.detail("biz_usage_monthly", { id: q.id || q.ID });
|
||||
const id = q.id || q.ID;
|
||||
if (id === undefined || id === null || id === "") throw new Error("缺少 id");
|
||||
const row = await baseModel.biz_usage_monthly.findByPk(id);
|
||||
ctx.success(row);
|
||||
},
|
||||
"POST /biz_usage/export": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
const res = await crud.exportCsv("biz_usage_monthly", body);
|
||||
const res = await find_for_export(baseModel.biz_usage_monthly, "biz_usage_monthly", body);
|
||||
ctx.success(res);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
const Sequelize = require("sequelize");
|
||||
const crud = require("../service/biz_admin_crud");
|
||||
|
||||
const { find_for_export, normalize_for_write, build_search_where } = require("../service/biz_query_helpers");
|
||||
const baseModel = require("../../middleware/baseModel");
|
||||
const tokenLogic = require("../service/biz_token_logic");
|
||||
const audit = require("../service/biz_audit_service");
|
||||
@@ -15,7 +14,7 @@ module.exports = {
|
||||
const pageSize = parseInt(pageOption.pageSize, 10) || 20;
|
||||
const offset = (pageNum - 1) * pageSize;
|
||||
const model = baseModel.biz_user;
|
||||
const where = crud.buildSearchWhere(model, seachOption);
|
||||
const where = build_search_where(model, seachOption);
|
||||
const { count, rows } = await model.findAndCountAll({
|
||||
where,
|
||||
offset,
|
||||
@@ -25,7 +24,7 @@ module.exports = {
|
||||
include: [
|
||||
[
|
||||
Sequelize.literal(
|
||||
`(SELECT COUNT(*) FROM biz_api_token WHERE biz_api_token.user_id = biz_user.id)`
|
||||
`(SELECT COUNT(*) FROM biz_api_tokens WHERE biz_api_tokens.user_id = biz_user.id)`
|
||||
),
|
||||
"token_count",
|
||||
],
|
||||
@@ -36,7 +35,8 @@ module.exports = {
|
||||
},
|
||||
"POST /biz_user/add": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
const row = await crud.add("biz_user", body);
|
||||
const payload = normalize_for_write(baseModel.biz_user, body, { for_create: true });
|
||||
const row = await baseModel.biz_user.create(payload);
|
||||
await audit.logAudit({
|
||||
admin_user_id: audit.pickAdminId(ctx),
|
||||
biz_user_id: row.id,
|
||||
@@ -49,7 +49,11 @@ module.exports = {
|
||||
},
|
||||
"POST /biz_user/edit": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
await crud.edit("biz_user", body);
|
||||
const id = body.id;
|
||||
if (id === undefined || id === null || id === "") throw new Error("缺少 id");
|
||||
const payload = normalize_for_write(baseModel.biz_user, body, { for_create: false });
|
||||
delete payload.id;
|
||||
await baseModel.biz_user.update(payload, { where: { id } });
|
||||
await audit.logAudit({
|
||||
admin_user_id: audit.pickAdminId(ctx),
|
||||
biz_user_id: body.id,
|
||||
@@ -61,7 +65,9 @@ module.exports = {
|
||||
},
|
||||
"POST /biz_user/del": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
await crud.del("biz_user", body);
|
||||
const id = body.id !== undefined ? body.id : body;
|
||||
if (id === undefined || id === null || id === "") throw new Error("缺少 id");
|
||||
await baseModel.biz_user.destroy({ where: { id } });
|
||||
await audit.logAudit({
|
||||
admin_user_id: audit.pickAdminId(ctx),
|
||||
biz_user_id: body.id,
|
||||
@@ -74,7 +80,8 @@ module.exports = {
|
||||
"GET /biz_user/detail": async (ctx) => {
|
||||
const q = ctx.query || {};
|
||||
const id = q.id || q.ID;
|
||||
const user = await crud.detail("biz_user", { id });
|
||||
if (id === undefined || id === null || id === "") throw new Error("缺少 id");
|
||||
const user = await baseModel.biz_user.findByPk(id);
|
||||
if (!user) {
|
||||
return ctx.fail("用户不存在");
|
||||
}
|
||||
@@ -100,7 +107,10 @@ module.exports = {
|
||||
});
|
||||
},
|
||||
"GET /biz_user/all": async (ctx) => {
|
||||
const rows = await crud.all("biz_user");
|
||||
const rows = await baseModel.biz_user.findAll({
|
||||
limit: 2000,
|
||||
order: [["id", "DESC"]],
|
||||
});
|
||||
ctx.success(rows);
|
||||
},
|
||||
"POST /biz_user/disable": async (ctx) => {
|
||||
@@ -133,7 +143,7 @@ module.exports = {
|
||||
},
|
||||
"POST /biz_user/export": async (ctx) => {
|
||||
const body = ctx.getBody();
|
||||
const res = await crud.exportCsv("biz_user", body);
|
||||
const res = await find_for_export(baseModel.biz_user, "biz_user", body);
|
||||
ctx.success(res);
|
||||
},
|
||||
"POST /biz_user/revoke_all_tokens": async (ctx) => {
|
||||
|
||||
@@ -1,196 +0,0 @@
|
||||
/**
|
||||
* 订阅模块通用 CRUD(与 admin 约定 POST /{model}/page|add|edit|del ,GET /{model}/detail|all)
|
||||
*/
|
||||
const baseModel = require("../../middleware/baseModel");
|
||||
const { op } = baseModel;
|
||||
|
||||
|
||||
|
||||
function getModel(modelName) {
|
||||
const m = baseModel[modelName];
|
||||
if (!m) {
|
||||
throw new Error(`模型不存在: ${modelName}`);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
function normalizeForWrite(model, data, { forCreate } = {}) {
|
||||
const attrs = model.rawAttributes;
|
||||
const out = {};
|
||||
for (const k of Object.keys(data || {})) {
|
||||
if (!attrs[k]) continue;
|
||||
let v = data[k];
|
||||
if (v === "") {
|
||||
if (k === "id" && forCreate) continue;
|
||||
if (k.endsWith("_id") || k === "id") {
|
||||
v = null;
|
||||
} else if (attrs[k].allowNull) {
|
||||
v = null;
|
||||
}
|
||||
}
|
||||
if (k === "enabled_features" && typeof v === "string" && v.trim() !== "") {
|
||||
try {
|
||||
v = JSON.parse(v);
|
||||
} catch (e) {
|
||||
/* 保持原字符串,由 Sequelize 或 DB 报错 */
|
||||
}
|
||||
}
|
||||
out[k] = v;
|
||||
}
|
||||
if (forCreate && out.id !== undefined && (out.id === "" || out.id === null)) {
|
||||
delete out.id;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function buildSearchWhere(model, seachOption) {
|
||||
const key = seachOption && seachOption.key;
|
||||
const raw = seachOption && seachOption.value;
|
||||
if (!key || raw === undefined || raw === null) return {};
|
||||
const str = String(raw).trim();
|
||||
if (str === "") return {};
|
||||
|
||||
const attr = model.rawAttributes[key];
|
||||
if (!attr) {
|
||||
return { [key]: { [op.like]: `%${str}%` } };
|
||||
}
|
||||
|
||||
const typeKey = attr.type && attr.type.key;
|
||||
|
||||
if (typeKey === "BOOLEAN") {
|
||||
if (str === "true" || str === "1" || str === "是") return { [key]: true };
|
||||
if (str === "false" || str === "0" || str === "否") return { [key]: false };
|
||||
return {};
|
||||
}
|
||||
|
||||
if (typeKey === "ENUM") {
|
||||
return { [key]: str };
|
||||
}
|
||||
|
||||
if (
|
||||
typeKey === "INTEGER" ||
|
||||
typeKey === "BIGINT" ||
|
||||
typeKey === "FLOAT" ||
|
||||
typeKey === "DOUBLE" ||
|
||||
typeKey === "DECIMAL"
|
||||
) {
|
||||
const n = Number(str);
|
||||
if (!Number.isNaN(n)) return { [key]: n };
|
||||
return {};
|
||||
}
|
||||
|
||||
if (typeKey === "DATE" || typeKey === "DATEONLY") {
|
||||
return { [key]: str };
|
||||
}
|
||||
|
||||
return { [key]: { [op.like]: `%${str}%` } };
|
||||
}
|
||||
|
||||
/** 模型未声明但表中存在的列(列表/导出需要带出) */
|
||||
function extraListAttributes(modelName, model) {
|
||||
if (modelName === "biz_audit_log") {
|
||||
const tn = model.tableName;
|
||||
return {
|
||||
attributes: {
|
||||
include: [[model.sequelize.col(`${tn}.created_at`), "created_at"]],
|
||||
},
|
||||
};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
async function page(modelName, body) {
|
||||
const model = getModel(modelName);
|
||||
const param = body.param || body;
|
||||
const pageOption = param.pageOption || {};
|
||||
const seachOption = param.seachOption || {};
|
||||
|
||||
const pageNum = parseInt(pageOption.page, 10) || 1;
|
||||
const pageSize = parseInt(pageOption.pageSize, 10) || 20;
|
||||
const offset = (pageNum - 1) * pageSize;
|
||||
|
||||
const where = buildSearchWhere(model, seachOption);
|
||||
|
||||
const { count, rows } = await model.findAndCountAll({
|
||||
where,
|
||||
offset,
|
||||
limit: pageSize,
|
||||
order: [["id", "DESC"]],
|
||||
...extraListAttributes(modelName, model),
|
||||
});
|
||||
|
||||
return { rows, count };
|
||||
}
|
||||
|
||||
async function add(modelName, body) {
|
||||
const model = getModel(modelName);
|
||||
const payload = normalizeForWrite(model, body, { forCreate: true });
|
||||
const row = await model.create(payload);
|
||||
return row;
|
||||
}
|
||||
|
||||
async function edit(modelName, body) {
|
||||
const model = getModel(modelName);
|
||||
const id = body.id;
|
||||
if (id === undefined || id === null || id === "") {
|
||||
throw new Error("缺少 id");
|
||||
}
|
||||
const payload = normalizeForWrite(model, body, { forCreate: false });
|
||||
delete payload.id;
|
||||
await model.update(payload, { where: { id } });
|
||||
return {};
|
||||
}
|
||||
|
||||
async function del(modelName, body) {
|
||||
const model = getModel(modelName);
|
||||
const id = body.id !== undefined ? body.id : body;
|
||||
if (id === undefined || id === null || id === "") {
|
||||
throw new Error("缺少 id");
|
||||
}
|
||||
await model.destroy({ where: { id } });
|
||||
return {};
|
||||
}
|
||||
|
||||
async function detail(modelName, query) {
|
||||
const model = getModel(modelName);
|
||||
const id = query && (query.id || query.ID);
|
||||
if (id === undefined || id === null || id === "") {
|
||||
throw new Error("缺少 id");
|
||||
}
|
||||
const row = await model.findByPk(id, extraListAttributes(modelName, model));
|
||||
return row;
|
||||
}
|
||||
|
||||
async function all(modelName) {
|
||||
const model = getModel(modelName);
|
||||
const rows = await model.findAll({
|
||||
limit: 2000,
|
||||
order: [["id", "DESC"]],
|
||||
...extraListAttributes(modelName, model),
|
||||
});
|
||||
return rows;
|
||||
}
|
||||
|
||||
async function exportCsv(modelName, body) {
|
||||
const model = getModel(modelName);
|
||||
const param = body.param || body;
|
||||
const where = buildSearchWhere(model, param.seachOption || {});
|
||||
const rows = await model.findAll({
|
||||
where,
|
||||
limit: 10000,
|
||||
order: [["id", "DESC"]],
|
||||
...extraListAttributes(modelName, model),
|
||||
});
|
||||
return { rows };
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
page,
|
||||
add,
|
||||
edit,
|
||||
del,
|
||||
detail,
|
||||
all,
|
||||
exportCsv,
|
||||
buildSearchWhere,
|
||||
};
|
||||
128
api/service/biz_query_helpers.js
Normal file
128
api/service/biz_query_helpers.js
Normal file
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* 列表筛选、写入字段裁剪、审计日志列表补列(供 admin 控制器直接配合 baseModel 使用)
|
||||
*/
|
||||
const Sequelize = require("sequelize");
|
||||
const { Op } = Sequelize;
|
||||
|
||||
function build_search_where(model, seach_option) {
|
||||
const key = seach_option && seach_option.key;
|
||||
const raw = seach_option && seach_option.value;
|
||||
if (!key || raw === undefined || raw === null) return {};
|
||||
const str = String(raw).trim();
|
||||
if (str === "") return {};
|
||||
|
||||
const attr = model.rawAttributes[key];
|
||||
if (!attr) {
|
||||
return { [key]: { [Op.like]: `%${str}%` } };
|
||||
}
|
||||
|
||||
const type_key = attr.type && attr.type.key;
|
||||
|
||||
if (type_key === "BOOLEAN") {
|
||||
if (str === "true" || str === "1" || str === "是") return { [key]: true };
|
||||
if (str === "false" || str === "0" || str === "否") return { [key]: false };
|
||||
return {};
|
||||
}
|
||||
|
||||
if (type_key === "ENUM") {
|
||||
return { [key]: str };
|
||||
}
|
||||
|
||||
if (
|
||||
type_key === "INTEGER" ||
|
||||
type_key === "BIGINT" ||
|
||||
type_key === "FLOAT" ||
|
||||
type_key === "DOUBLE" ||
|
||||
type_key === "DECIMAL"
|
||||
) {
|
||||
const n = Number(str);
|
||||
if (!Number.isNaN(n)) return { [key]: n };
|
||||
return {};
|
||||
}
|
||||
|
||||
if (type_key === "DATE" || type_key === "DATEONLY") {
|
||||
return { [key]: str };
|
||||
}
|
||||
|
||||
return { [key]: { [Op.like]: `%${str}%` } };
|
||||
}
|
||||
|
||||
function normalize_for_write(model, data, { for_create } = {}) {
|
||||
const attrs = model.rawAttributes;
|
||||
const out = {};
|
||||
for (const k of Object.keys(data || {})) {
|
||||
if (!attrs[k]) continue;
|
||||
let v = data[k];
|
||||
if (v === "") {
|
||||
if (k === "id" && for_create) continue;
|
||||
if (k.endsWith("_id") || k === "id") {
|
||||
v = null;
|
||||
} else if (attrs[k].allowNull) {
|
||||
v = null;
|
||||
}
|
||||
}
|
||||
if (k === "enabled_features" && typeof v === "string" && v.trim() !== "") {
|
||||
try {
|
||||
v = JSON.parse(v);
|
||||
} catch (e) {
|
||||
/* 保持原字符串 */
|
||||
}
|
||||
}
|
||||
out[k] = v;
|
||||
}
|
||||
if (for_create && out.id !== undefined && (out.id === "" || out.id === null)) {
|
||||
delete out.id;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function list_query_extra(model_name, model) {
|
||||
if (model_name === "biz_audit_log") {
|
||||
const tn = model.tableName;
|
||||
return {
|
||||
attributes: {
|
||||
include: [[model.sequelize.col(`${tn}.created_at`), "created_at"]],
|
||||
},
|
||||
};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
async function find_page(model, model_name, body, extra_find_options = {}) {
|
||||
const param = body.param || body;
|
||||
const page_option = param.pageOption || {};
|
||||
const seach_option = param.seachOption || {};
|
||||
const page_num = parseInt(page_option.page, 10) || 1;
|
||||
const page_size = parseInt(page_option.pageSize, 10) || 20;
|
||||
const offset = (page_num - 1) * page_size;
|
||||
const where = build_search_where(model, seach_option);
|
||||
return model.findAndCountAll({
|
||||
where,
|
||||
offset,
|
||||
limit: page_size,
|
||||
order: [["id", "DESC"]],
|
||||
...list_query_extra(model_name, model),
|
||||
...extra_find_options,
|
||||
});
|
||||
}
|
||||
|
||||
async function find_for_export(model, model_name, body, extra_find_options = {}) {
|
||||
const param = body.param || body;
|
||||
const where = build_search_where(model, param.seachOption || {});
|
||||
const rows = await model.findAll({
|
||||
where,
|
||||
limit: 10000,
|
||||
order: [["id", "DESC"]],
|
||||
...list_query_extra(model_name, model),
|
||||
...extra_find_options,
|
||||
});
|
||||
return { rows };
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
build_search_where,
|
||||
normalize_for_write,
|
||||
list_query_extra,
|
||||
find_page,
|
||||
find_for_export,
|
||||
};
|
||||
Reference in New Issue
Block a user