diff --git a/api/controller_admin/biz_api_stats.js b/api/controller_admin/biz_api_stats.js index 4c3fba5..e448d64 100644 --- a/api/controller_admin/biz_api_stats.js +++ b/api/controller_admin/biz_api_stats.js @@ -1,20 +1,12 @@ const stats = require("../service/biz_api_stats_service"); const crud = require("../service/biz_admin_crud"); -function getRequestBody(ctx) { - if (ctx.request && ctx.request.body && Object.keys(ctx.request.body).length > 0) { - return ctx.request.body; - } - if (typeof ctx.getBody === "function") { - return ctx.getBody() || {}; - } - return {}; -} + module.exports = { /** 按用户查询调用统计 */ "POST /biz_api_stats/by_user": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const { user_id, start_date, end_date } = body; if (!user_id) { ctx.fail("缺少 user_id"); @@ -26,7 +18,7 @@ module.exports = { /** 按接口路径查询调用统计 */ "POST /biz_api_stats/by_api": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const { api_path, start_date, end_date } = body; if (!api_path) { ctx.fail("缺少 api_path"); @@ -38,7 +30,7 @@ module.exports = { /** 综合统计面板 */ "POST /biz_api_stats/summary": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const { start_date, end_date, top_limit } = body; const data = await stats.getSummary(start_date, end_date, top_limit || 10); ctx.success(data); @@ -46,7 +38,7 @@ module.exports = { /** 调用日志分页列表(复用通用 CRUD) */ "POST /biz_api_call_log/page": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const data = await crud.page("biz_api_call_log", body); ctx.success({ rows: data.rows, count: data.count }); }, diff --git a/api/controller_admin/biz_audit_log.js b/api/controller_admin/biz_audit_log.js index e48212b..689f122 100644 --- a/api/controller_admin/biz_audit_log.js +++ b/api/controller_admin/biz_audit_log.js @@ -1,14 +1,16 @@ const crud = require("../service/biz_admin_crud"); -const { getRequestBody } = crud; + module.exports = { "POST /biz_audit_log/page": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); + + const data = await crud.page("biz_audit_log", body); ctx.success({ rows: data.rows, count: data.count }); }, "POST /biz_audit_log/export": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const res = await crud.exportCsv("biz_audit_log", body); ctx.success(res); }, diff --git a/api/controller_admin/biz_payment.js b/api/controller_admin/biz_payment.js index 731a88f..aba6131 100644 --- a/api/controller_admin/biz_payment.js +++ b/api/controller_admin/biz_payment.js @@ -1,19 +1,11 @@ const logic = require("../service/biz_subscription_logic"); const audit = require("../service/biz_audit_service"); -function getRequestBody(ctx) { - if (ctx.request && ctx.request.body && Object.keys(ctx.request.body).length > 0) { - return ctx.request.body; - } - if (typeof ctx.getBody === "function") { - return ctx.getBody() || {}; - } - return {}; -} + module.exports = { "POST /biz_payment/confirm-offline": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const row = await logic.confirmOfflinePayment(body); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), @@ -25,7 +17,7 @@ module.exports = { ctx.success(row); }, "POST /biz_payment/confirm-link": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const row = await logic.confirmLinkPayment(body); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), diff --git a/api/controller_admin/biz_plan.js b/api/controller_admin/biz_plan.js index c0082bf..c8c7e05 100644 --- a/api/controller_admin/biz_plan.js +++ b/api/controller_admin/biz_plan.js @@ -1,16 +1,16 @@ const crud = require("../service/biz_admin_crud"); -const { getRequestBody } = crud; + const baseModel = require("../../middleware/baseModel"); const audit = require("../service/biz_audit_service"); module.exports = { "POST /biz_plan/page": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const data = await crud.page("biz_plan", body); ctx.success({ rows: data.rows, count: data.count }); }, "POST /biz_plan/add": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const row = await crud.add("biz_plan", body); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), @@ -22,7 +22,7 @@ module.exports = { ctx.success(row); }, "POST /biz_plan/edit": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); await crud.edit("biz_plan", body); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), @@ -33,7 +33,7 @@ module.exports = { ctx.success({}); }, "POST /biz_plan/del": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); await crud.del("biz_plan", body); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), @@ -53,7 +53,7 @@ module.exports = { ctx.success(rows); }, "POST /biz_plan/toggle": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const id = body.id; if (id == null) return ctx.fail("缺少 id"); const row = await baseModel.biz_plan.findByPk(id); @@ -70,7 +70,7 @@ module.exports = { ctx.success({ status: next }); }, "POST /biz_plan/export": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const res = await crud.exportCsv("biz_plan", body); ctx.success(res); }, diff --git a/api/controller_admin/biz_subscription.js b/api/controller_admin/biz_subscription.js index fcce818..d603dc3 100644 --- a/api/controller_admin/biz_subscription.js +++ b/api/controller_admin/biz_subscription.js @@ -1,12 +1,12 @@ const crud = require("../service/biz_admin_crud"); -const { getRequestBody } = crud; + const baseModel = require("../../middleware/baseModel"); const logic = require("../service/biz_subscription_logic"); const audit = require("../service/biz_audit_service"); module.exports = { "POST /biz_subscription/page": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const data = await crud.page("biz_subscription", body); ctx.success({ rows: data.rows, count: data.count }); }, @@ -26,7 +26,7 @@ module.exports = { ctx.success(rows); }, "POST /biz_subscription/open": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const row = await logic.openSubscription(body); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), @@ -39,7 +39,7 @@ module.exports = { ctx.success(row); }, "POST /biz_subscription/upgrade": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const row = await logic.upgradeSubscription(body); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), @@ -51,7 +51,7 @@ module.exports = { ctx.success(row); }, "POST /biz_subscription/renew": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const row = await logic.renewSubscription(body); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), @@ -62,7 +62,7 @@ module.exports = { ctx.success(row); }, "POST /biz_subscription/cancel": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const row = await logic.cancelSubscription(body); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), @@ -73,7 +73,7 @@ module.exports = { ctx.success(row); }, "POST /biz_subscription/export": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const res = await crud.exportCsv("biz_subscription", body); ctx.success(res); }, diff --git a/api/controller_admin/biz_token.js b/api/controller_admin/biz_token.js index 897c21d..479a4c1 100644 --- a/api/controller_admin/biz_token.js +++ b/api/controller_admin/biz_token.js @@ -1,16 +1,16 @@ const crud = require("../service/biz_admin_crud"); -const { getRequestBody } = crud; + const tokenLogic = require("../service/biz_token_logic"); const audit = require("../service/biz_audit_service"); module.exports = { "POST /biz_token/page": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const data = await crud.page("biz_api_token", body); ctx.success({ rows: data.rows, count: data.count }); }, "POST /biz_token/create": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const result = await tokenLogic.createToken(body); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), @@ -31,7 +31,7 @@ module.exports = { }); }, "POST /biz_token/revoke": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const row = await tokenLogic.revokeToken(body); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), @@ -43,7 +43,7 @@ module.exports = { ctx.success({ id: row.id, status: row.status }); }, "POST /biz_token/export": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const res = await crud.exportCsv("biz_api_token", body); ctx.success(res); }, diff --git a/api/controller_admin/biz_usage.js b/api/controller_admin/biz_usage.js index 89ef512..1082a5a 100644 --- a/api/controller_admin/biz_usage.js +++ b/api/controller_admin/biz_usage.js @@ -1,24 +1,24 @@ const crud = require("../service/biz_admin_crud"); -const { getRequestBody } = crud; + module.exports = { "POST /biz_usage/page": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const data = await crud.page("biz_usage_monthly", body); ctx.success({ rows: data.rows, count: data.count }); }, "POST /biz_usage/add": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const row = await crud.add("biz_usage_monthly", body); ctx.success(row); }, "POST /biz_usage/edit": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); await crud.edit("biz_usage_monthly", body); ctx.success({}); }, "POST /biz_usage/del": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); await crud.del("biz_usage_monthly", body); ctx.success({}); }, @@ -28,7 +28,7 @@ module.exports = { ctx.success(row); }, "POST /biz_usage/export": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const res = await crud.exportCsv("biz_usage_monthly", body); ctx.success(res); }, diff --git a/api/controller_admin/biz_user.js b/api/controller_admin/biz_user.js index 5261b88..0a559f5 100644 --- a/api/controller_admin/biz_user.js +++ b/api/controller_admin/biz_user.js @@ -1,17 +1,17 @@ const crud = require("../service/biz_admin_crud"); -const { getRequestBody } = 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 = getRequestBody(ctx); + const body = ctx.getBody(); const data = await crud.page("biz_user", body); ctx.success({ rows: data.rows, count: data.count }); }, "POST /biz_user/add": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const row = await crud.add("biz_user", body); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), @@ -24,7 +24,7 @@ module.exports = { ctx.success(row); }, "POST /biz_user/edit": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); await crud.edit("biz_user", body); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), @@ -36,7 +36,7 @@ module.exports = { ctx.success({}); }, "POST /biz_user/del": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); await crud.del("biz_user", body); await audit.logAudit({ admin_user_id: audit.pickAdminId(ctx), @@ -73,7 +73,7 @@ module.exports = { ctx.success(rows); }, "POST /biz_user/disable": async (ctx) => { - const body = getRequestBody(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 } }); @@ -87,7 +87,7 @@ module.exports = { ctx.success({}); }, "POST /biz_user/enable": async (ctx) => { - const body = getRequestBody(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 } }); @@ -101,12 +101,12 @@ module.exports = { ctx.success({}); }, "POST /biz_user/export": async (ctx) => { - const body = getRequestBody(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 = getRequestBody(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); diff --git a/api/controller_custom/proxy_api.js b/api/controller_custom/proxy_api.js index ac2dcea..0c51ce3 100644 --- a/api/controller_custom/proxy_api.js +++ b/api/controller_custom/proxy_api.js @@ -2,15 +2,7 @@ const swagger = require("../../_docs/swagger.json"); const auth = require("../service/biz_auth_verify"); const proxy = require("../service/biz_proxy_service"); -function getRequestBody(ctx) { - if (ctx.request && ctx.request.body && Object.keys(ctx.request.body).length > 0) { - return ctx.request.body; - } - if (typeof ctx.getBody === "function") { - return ctx.getBody() || {}; - } - return {}; -} + /** * 从请求中提取 Token @@ -69,7 +61,7 @@ function buildProxyRoutes() { api_path: path, method: method.toUpperCase(), query, - body: getRequestBody(ctx), + body: ctx.getBody(), headers: ctx.headers || {}, auth_ctx: authResult.context, }); diff --git a/api/controller_front/auth_verify.js b/api/controller_front/auth_verify.js index 651c9e7..4fa5a6c 100644 --- a/api/controller_front/auth_verify.js +++ b/api/controller_front/auth_verify.js @@ -1,18 +1,10 @@ const auth = require("../service/biz_auth_verify"); -function getRequestBody(ctx) { - if (ctx.request && ctx.request.body && Object.keys(ctx.request.body).length > 0) { - return ctx.request.body; - } - if (typeof ctx.getBody === "function") { - return ctx.getBody() || {}; - } - return {}; -} + module.exports = { "POST /auth/verify": async (ctx) => { - const body = getRequestBody(ctx); + const body = ctx.getBody(); const result = await auth.verifyRequest(body); ctx.success(result); }, diff --git a/api/service/biz_admin_crud.js b/api/service/biz_admin_crud.js index 88dc6d2..d2d679d 100644 --- a/api/service/biz_admin_crud.js +++ b/api/service/biz_admin_crud.js @@ -4,15 +4,7 @@ const baseModel = require("../../middleware/baseModel"); const { op } = baseModel; -function getRequestBody(ctx) { - if (ctx.request && ctx.request.body && Object.keys(ctx.request.body).length > 0) { - return ctx.request.body; - } - if (typeof ctx.getBody === "function") { - return ctx.getBody() || {}; - } - return {}; -} + function getModel(modelName) { const m = baseModel[modelName];