文件
fenleixinxi/application/api/controller/AdminFinance.php
linda a09a0a10be feat(api): 新增小程序用户端与后台管理端 v1 业务接口(62 端点)
一、基础设施
- route.php:58 条路由覆盖 /api/v1/**;:id 用 \d+ 约束、长路径优先
- api/controller/V1.php:响应适配为 {code,message,data}(FastAdmin 原生 {code,msg,time,data} 不兼容);
  Authorization Bearer 鉴权;统一分页;业务公共 userInfo/ext/flow/notify/adminLog
- admin/command/FcInstall.php:19 张 fa_fc_* 表 + 种子数据,幂等可重跑

二、控制器 17 个
- 用户端 Auth/Home/Post/Member/Distribution/Message/Setting
- 后台端 AdminDashboard/AdminMerchant/AdminMember/AdminPost/AdminComment/
  AdminDist/AdminOrder/AdminFinance/AdminRbac/AdminSetting

三、契约一致性
- 字段与小程序 common/mock/data.js 逐字段对齐,Mock 与真实接口可对拍
- 实现 BR-01~BR-12,错误文案与前端 mock 完全一致
- 详情联系方式强制脱敏,付费/会员方可解锁

四、验证
- tools/verify-api.mjs:63 端点契约 + 12 条业务规则 + 8 步双端闭环
- docs/05-后端实现规范.md
2026-10-15 14:36:26 +08:00

130 行
4.0 KiB
PHP

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace app\api\controller;
use think\Db;
/**
* 后台管理端 · 财务管理(Module-ADM-FINANCE)
* 路由:
* GET /api/v1/admin/finance/flows AF1 资金流水
* GET /api/v1/admin/finance/report AF2 财务报表
*/
class AdminFinance extends V1
{
/**
* AF1 资金流水
* GET /api/v1/admin/finance/flows 入参 {type?,page,size}
* → 分页 {list:[{id,flowNo,type,scene,amount,balance,createdAt}],page,size,total,hasMore}
*/
public function flows()
{
list($page, $size) = $this->pager();
$type = trim((string)$this->input('type', ''));
$query = Db::name('fc_finance_flow');
if ($type !== '') {
$query->where('type', $type);
}
$total = $query->count();
$rows = $query->order('createtime desc')->page($page, $size)->select();
$list = [];
foreach ($rows as $row) {
$list[] = [
'id' => (int)$row['id'],
'flowNo' => (string)$row['flow_no'],
'type' => (string)$row['type'],
'scene' => (string)$row['scene'],
'amount' => (float)$row['amount'],
'balance' => (float)$row['balance'],
'createdAt' => $this->iso($row['createtime']),
];
}
$this->ok($this->paginate($list, $total, $page, $size));
}
/**
* AF2 财务报表
* GET /api/v1/admin/finance/report 入参 {period: day|month|year}
* → {period, rows:[{period,postIncome,memberIncome,rechargeIncome,withdraw,net}]}
*/
public function report()
{
$period = trim((string)$this->input('period', 'day'));
if (!in_array($period, ['day', 'month', 'year'], true)) {
$period = 'day';
}
$fmtMap = ['day' => '%Y-%m-%d', 'month' => '%Y-%m', 'year' => '%Y'];
$fmt = $fmtMap[$period];
$post = $this->periodOrderSum('post', $fmt);
$member = $this->periodOrderSum('member', $fmt);
$recharge = $this->periodOrderSum('recharge', $fmt);
$withdraw = $this->periodWithdrawSum($fmt);
$keys = array_unique(array_merge(
array_keys($post),
array_keys($member),
array_keys($recharge),
array_keys($withdraw)
));
sort($keys);
$rows = [];
foreach ($keys as $k) {
$p = isset($post[$k]) ? (float)$post[$k] : 0;
$m = isset($member[$k]) ? (float)$member[$k] : 0;
$r = isset($recharge[$k]) ? (float)$recharge[$k] : 0;
$w = isset($withdraw[$k]) ? (float)$withdraw[$k] : 0;
$rows[] = [
'period' => (string)$k,
'postIncome' => $p,
'memberIncome' => $m,
'rechargeIncome' => $r,
'withdraw' => $w,
'net' => round($p + $m + $r - $w, 2),
];
}
$this->ok(['period' => $period, 'rows' => $rows]);
}
/** 按周期聚合某类已支付订单金额,返回 [period => total] */
protected function periodOrderSum($type, $fmt)
{
$rows = Db::name('fc_order')
->where('status', 'paid')
->where('type', $type)
->field("FROM_UNIXTIME(createtime, '{$fmt}') AS period, SUM(amount) AS total")
->group('period')
->select();
$map = [];
foreach ($rows as $row) {
$map[$row['period']] = (float)$row['total'];
}
return $map;
}
/** 按周期聚合已打款提现金额,返回 [period => total] */
protected function periodWithdrawSum($fmt)
{
$rows = Db::name('fc_withdraw')
->where('status', 'paid')
->whereNotNull('paytime')
->field("FROM_UNIXTIME(paytime, '{$fmt}') AS period, SUM(amount) AS total")
->group('period')
->select();
$map = [];
foreach ($rows as $row) {
$map[$row['period']] = (float)$row['total'];
}
return $map;
}
}