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