This commit is contained in:
张成
2026-04-01 10:53:26 +08:00
parent c2205188d1
commit 03c5579c86
11 changed files with 211 additions and 580 deletions

View File

@@ -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 });
},
};

View File

@@ -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);
},
};

View File

@@ -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);
},
};

View File

@@ -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);
},
};

View File

@@ -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);
},
};

View File

@@ -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);
},
};

View File

@@ -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) => {