cce3a01de1
Co-authored-by: OuYingJun1024 <1034284404@qq.com> Reviewed-on: #49 Co-authored-by: ouzhou <ouzhou@wonderable.ai> Co-committed-by: ouzhou <ouzhou@wonderable.ai>
2.5 KiB
2.5 KiB
POST /admin/api/users/{user_id}/coins — 手动增减/设值金币(带审计)
所属:Admin·用户 组(前缀
/admin/api/users) | 鉴权:Bearer admin_token(角色:finance,super_admin恒通过) | ← 返回 API 索引
入参
- 路径:
user_id(int) - application/json:
字段 类型 必填 说明 modestring ✗ delta(默认)=增减 /set=设为指定值amountint ✓ delta模式:金币变动(正=增加,负=扣减,不可为 0);set模式:目标金币值(须 ≥ 0)reasonstring ✓ 操作原因,1–128 字(必填,入审计与流水备注)
出参
响应 200:OkResponse = { "ok": true }
错误码
400(delta)amount == 0(detail: "amount 不能为 0");或负数扣减后余额会变负(detail: "扣减后金币为负(当前余额 N)")400(set)目标值为负(detail: "目标金币值不能为负");或目标值等于当前余额(detail: "当前金币已为 N,无需调整")401未带/无效/过期 admin token、管理员被禁用(头带WWW-Authenticate: Bearer)403角色不足(需finance或super_admin)404用户不存在(detail: "用户不存在")422缺amount/reason、reason长度不在 1–128、mode非delta/set、字段类型不合法 /user_id非整数
说明
- 金币 = 个数(非现金);本接口只动金币余额,不涉及现金(
*_cents)。 - set 模式:读当前余额算出差值
delta = target - 当前余额,再复用同一套写入逻辑(故只写一笔差值流水)。目标值须 ≥ 0;差值为 0(已等于目标)直接拒绝。 - 扣减保护:实际写入的
delta < 0时若扣减后金币余额 < 0 直接拒绝(运营误操作保护);set 模式目标值 ≥ 0 天然不会扣成负。 - 金币变动写流水 coin_transaction:
biz_type实际差值为正记admin_grant、为负记admin_deduct(set 模式同理,不新增流水类型),remark = admin:<reason>(截断至 128 字)。 - 写操作记审计 admin_audit_log:
action = user.coins.grant,target_type = user,target_id = user_id,detail = {amount(=实际差值), balance_after, reason};set 模式额外带{mode:"set", target, before}。并记录操作 IP。 - 金币变动 + 审计在同一事务原子提交(改钱必留痕)。
- 关联用户表 user;金币账户 coin_account;金币流水 coin_transaction。