const Sequelize = require("sequelize"); const crud = require("../service/biz_admin_crud"); const baseModel = require("../../middleware/baseModel"); const tokenLogic = require("../service/biz_token_logic"); const audit = require("../service/biz_audit_service"); module.exports = { "POST /biz_user/page": async (ctx) => { const body = ctx.getBody(); 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 model = baseModel.biz_user; const where = crud.buildSearchWhere(model, seachOption); const { count, rows } = await model.findAndCountAll({ where, offset, limit: pageSize, order: [["id", "DESC"]], attributes: { include: [ [ Sequelize.literal( `(SELECT COUNT(*) FROM biz_api_tokens WHERE biz_api_tokens.user_id = biz_user.id)` ), "token_count", ], ], }, }); ctx.success({ rows, count }); }, "POST /biz_user/add": async (ctx) => { const body = ctx.getBody(); const row = await crud.add("biz_user", body); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), biz_user_id: row.id, action: "biz_user.add", resource_type: "biz_user", resource_id: row.id, detail: { name: row.name }, }); ctx.success(row); }, "POST /biz_user/edit": async (ctx) => { const body = ctx.getBody(); await crud.edit("biz_user", body); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), biz_user_id: body.id, action: "biz_user.edit", resource_type: "biz_user", resource_id: body.id, }); ctx.success({}); }, "POST /biz_user/del": async (ctx) => { const body = ctx.getBody(); await crud.del("biz_user", body); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), biz_user_id: body.id, action: "biz_user.del", resource_type: "biz_user", resource_id: body.id, }); ctx.success({}); }, "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 (!user) { return ctx.fail("用户不存在"); } const subscriptions = await baseModel.biz_subscription.findAll({ where: { user_id: id }, order: [["id", "DESC"]], limit: 10, }); const tokenCount = await baseModel.biz_api_token.count({ where: { user_id: id }, }); const tokens = await baseModel.biz_api_token.findAll({ where: { user_id: id }, order: [["id", "DESC"]], limit: 200, attributes: ["id", "user_id", "plan_id", "token_name", "status", "expire_at", "last_used_at"], }); ctx.success({ user, subscriptions, tokenCount, tokens, }); }, "GET /biz_user/all": async (ctx) => { const rows = await crud.all("biz_user"); ctx.success(rows); }, "POST /biz_user/disable": async (ctx) => { const body = ctx.getBody(); const id = body.id; if (id == null) return ctx.fail("缺少 id"); await baseModel.biz_user.update({ status: "disabled" }, { where: { id } }); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), biz_user_id: id, action: "biz_user.disable", resource_type: "biz_user", resource_id: id, }); ctx.success({}); }, "POST /biz_user/enable": async (ctx) => { const body = ctx.getBody(); const id = body.id; if (id == null) return ctx.fail("缺少 id"); await baseModel.biz_user.update({ status: "active" }, { where: { id } }); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), biz_user_id: id, action: "biz_user.enable", resource_type: "biz_user", resource_id: id, }); ctx.success({}); }, "POST /biz_user/export": async (ctx) => { const body = ctx.getBody(); const res = await crud.exportCsv("biz_user", body); ctx.success(res); }, "POST /biz_user/revoke_all_tokens": async (ctx) => { const body = ctx.getBody(); const userId = body.user_id != null ? body.user_id : body.id; if (userId == null) return ctx.fail("缺少 user_id"); const n = await tokenLogic.revokeAllForUser(userId); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), biz_user_id: userId, action: "biz_token.revoke_all", resource_type: "biz_user", resource_id: userId, detail: { affected: n }, }); ctx.success({ revoked: n }); }, };