feat: 新增地址选择插件及订阅消息等多项功能优化

- 新增地址选择插件,支持地图位置选择并返回地址和经纬度
- 新增微信订阅消息发送功能,支持审核结果、提现通知等场景
- 新增订单支付状态查询、提现收款人姓名保存等接口
- 新增服务区域管理,支持按省市区划分并同步到信息发布
- 新增资金流水和营收报表导出功能
- 新增可用优惠券查询、订单退款拆分处理等功能
- 优化后台角标显示、首页数据统计、UI配置下发等
- 修复订单支付并发问题、地区管理逻辑等
这个提交包含在:
2026-10-22 17:14:56 +08:00
父节点 088a08d43a
当前提交 cb12bf5fc8
共有 180 个文件被更改,包括 9695 次插入 和 1680 次删除

查看文件

@@ -3,11 +3,12 @@
namespace app\admin\controller\fc;
use app\common\controller\Backend;
use think\Db;
/**
* 同城-服务区域
* 同城-服务区域(省 / 市 / 区县平铺,区县为最终服务区域)
*
* @icon fa fa-circle-o
* @icon fa fa-map-o
*/
class Area extends Backend
{
@@ -22,16 +23,81 @@ class Area extends Backend
{
parent::_initialize();
$this->model = new \app\admin\model\FcArea;
}
/**
* 新增:保存前校验
*/
public function add()
{
$this->assignAreaOptions();
if ($this->request->isPost()) {
$this->validateArea($this->request->post('row/a'), 0);
}
return parent::add();
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
* 编辑:保存前校验
* @param string $ids
*/
public function edit($ids = null)
{
$this->assignAreaOptions();
if ($this->request->isPost()) {
$this->validateArea($this->request->post('row/a'), (int)$this->request->param('ids'));
}
return parent::edit($ids);
}
/** 省 / 市 / 区县候选值,供表单级联下拉使用 */
protected function assignAreaOptions()
{
$rows = Db::name('fc_area')
->field('province,city,district')
->order('province asc, city asc, district asc')
->select();
$this->view->assign('areaOptionsJson', json_encode($rows, JSON_UNESCAPED_UNICODE));
}
/**
* 保存前校验:省 / 市 / 区县均必填,同一组合不可重复
* @param array $params 表单数据
* @param int $id 编辑时的区域ID
*/
protected function validateArea($params, $id = 0)
{
$province = isset($params['province']) ? trim((string)$params['province']) : '';
$city = isset($params['city']) ? trim((string)$params['city']) : '';
$district = isset($params['district']) ? trim((string)$params['district']) : '';
$values = ['省份' => $province, '城市' => $city, '区县' => $district];
foreach ($values as $label => $value) {
if ($value === '') {
$this->error($label . '不能为空');
}
if (mb_strlen($value) > 32) {
$this->error($label . '不能超过 32 个字符');
}
}
$duplicate = Db::name('fc_area')
->where('province', $province)
->where('city', $city)
->where('district', $district)
->where('id', '<>', $id)
->find();
if ($duplicate) {
$this->error('该省市区组合已存在');
}
foreach (['lat' => 90, 'lng' => 180] as $field => $limit) {
if (!isset($params[$field]) || $params[$field] === '' || $params[$field] === null) {
continue;
}
if (!is_numeric($params[$field]) || (float)$params[$field] < -$limit || (float)$params[$field] > $limit) {
$this->error($field === 'lat' ? '纬度格式不正确' : '经度格式不正确');
}
}
}
}

查看文件

@@ -89,6 +89,13 @@ class Dashboard extends Backend
$membersMap[$row['day']] = (int)$row['num'];
}
/* ---------- 今日较昨日的变化(用于首页涨跌标识) ---------- */
$yesterday = date('Y-m-d', $todayStart - 86400);
$metrics['yesterdayPost'] = isset($postsMap[$yesterday]) ? $postsMap[$yesterday] : 0;
$metrics['yesterdayOrder'] = isset($ordersMap[$yesterday]) ? $ordersMap[$yesterday] : 0;
$metrics['postDelta'] = $metrics['todayPost'] - $metrics['yesterdayPost'];
$metrics['orderDelta'] = round($metrics['todayOrderAmount'] - $metrics['yesterdayOrder'], 2);
$trend = [
'dates' => $days,
'posts' => [],
@@ -115,6 +122,7 @@ class Dashboard extends Backend
$this->view->assign('metrics', $metrics);
$this->view->assign('trendRows', $trendRows);
$this->view->assign('updatedAt', date('Y-m-d H:i'));
$this->assignconfig('trend', $trend);
return $this->view->fetch();

查看文件

@@ -3,6 +3,7 @@
namespace app\admin\controller\fc;
use app\common\controller\Backend;
use think\Db;
/**
* 同城-资金流水
@@ -34,4 +35,132 @@ class Financeflow extends Backend
*/
/**
* 导出资金流水(Excel)
* GET fc/financeflow/export?type=income|expense
*/
public function export()
{
$type = trim((string)$this->request->get('type', ''));
$q = Db::name('fc_finance_flow');
if ($type !== '') {
$q->where('type', $type);
}
$rows = $q->order('createtime desc')->limit(50000)->select();
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('资金流水');
$sheet->fromArray(['流水号', '类型', '场景', '金额(元)', '账户余额(元)', '关联ID', '备注', '时间'], null, 'A1');
$r = 2;
foreach ($rows as $row) {
$sheet->fromArray([
(string)$row['flow_no'],
$row['type'] === 'income' ? '收入' : '支出',
(string)$row['scene'],
(float)$row['amount'],
(float)$row['balance'],
(int)$row['related_id'],
(string)$row['remark'],
date('Y-m-d H:i:s', (int)$row['createtime']),
], null, 'A' . $r);
$r++;
}
$this->downloadXlsx($spreadsheet, '资金流水_' . date('YmdHis'));
}
/**
* 导出营收报表(Excel)
* GET fc/financeflow/exportReport?period=day|month|year
*/
public function exportReport()
{
$period = trim((string)$this->request->get('period', 'month'));
if (!in_array($period, ['day', 'month', 'year'], true)) {
$period = 'month';
}
$fmtMap = ['day' => '%Y-%m-%d', 'month' => '%Y-%m', 'year' => '%Y'];
$fmt = $fmtMap[$period];
$post = $this->periodSum('post', $fmt);
$member = $this->periodSum('member', $fmt);
$recharge = $this->periodSum('recharge', $fmt);
$withdraw = $this->periodWithdraw($fmt);
$keys = array_unique(array_merge(array_keys($post), array_keys($member), array_keys($recharge), array_keys($withdraw)));
sort($keys);
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('营收报表');
$sheet->fromArray(['周期', '发布收入', '会员收入', '充值收入', '提现支出', '净收入'], null, 'A1');
$r = 2;
foreach ($keys as $k) {
$p = isset($post[$k]) ? (float)$post[$k] : 0;
$m = isset($member[$k]) ? (float)$member[$k] : 0;
$rc = isset($recharge[$k]) ? (float)$recharge[$k] : 0;
$w = isset($withdraw[$k]) ? (float)$withdraw[$k] : 0;
$sheet->fromArray([
(string)$k,
round($p, 2),
round($m, 2),
round($rc, 2),
round($w, 2),
round($p + $m + $rc - $w, 2),
], null, 'A' . $r);
$r++;
}
$label = ['day' => '日', 'month' => '月', 'year' => '年'][$period];
$this->downloadXlsx($spreadsheet, '营收报表_按' . $label . '_' . date('YmdHis'));
}
/** 按周期聚合某类已支付订单 */
protected function periodSum($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;
}
/** 按周期聚合已到账提现 */
protected function periodWithdraw($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;
}
/** 输出 xlsx 下载 */
protected function downloadXlsx($spreadsheet, $filename)
{
$filename = preg_replace('/[^\w\x{4e00}-\x{9fa5}\-]/u', '', $filename) . '.xlsx';
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $filename . '"');
header('Cache-Control: max-age=0');
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save('php://output');
exit;
}
}

查看文件

@@ -40,37 +40,43 @@ class Order extends Backend
public function refund()
{
$ids = $this->request->post('ids');
$amount = (float)$this->request->post('amount');
$amount = $this->request->post('amount');
$reason = $this->request->post('reason', '');
if ($amount <= 0) {
$this->error('退款金额需大于 0');
}
$ids = is_array($ids) ? $ids : explode(',', (string)$ids);
$ids = array_filter($ids);
if (!$ids) {
$this->error('请选择要退款的订单');
}
$orders = Db::name('fc_order')->where('id', 'in', $ids)->select();
if (!$orders) {
$this->error('订单不存在');
}
// 金额留空 = 按每笔订单实付金额全额退款
$amount = ($amount === '' || $amount === null || (float)$amount <= 0) ? null : (float)$amount;
$ok = 0;
$errors = [];
foreach ($orders as $order) {
Db::name('fc_order')->where('id', $order['id'])->update([
'status' => 'refunded',
'refund_amount' => $amount,
'refund_reason' => $reason,
'updatetime' => time(),
]);
Db::name('fc_message')->insert([
'user_id' => $order['user_id'],
'type' => 'system',
'title' => '订单退款',
'content' => '您的订单「' . $order['order_no'] . '」已退款' . $amount . '元' . ($reason ? ',原因:' . $reason : ''),
'isread' => 0,
'createtime' => time(),
]);
try {
\app\common\library\OrderRefund::refund((int)$order['id'], $amount, (string)$reason);
$ok++;
} catch (\Throwable $e) {
$errors[] = $order['order_no'] . ':' . $e->getMessage();
}
}
$this->success('退款成功');
if ($ok === 0) {
$this->error('退款失败:' . implode(';', $errors));
}
$msg = '退款成功 ' . $ok . ' 笔';
if ($errors) {
$msg .= ',失败 ' . count($errors) . ' 笔:' . implode(';', $errors);
}
$this->success($msg);
}
/**

查看文件

@@ -47,14 +47,20 @@ class Setting extends Backend
if ($appName === '') {
$this->error('小程序名称不能为空');
}
$logo = isset($params['logo']) ? trim((string)$params['logo']) : '';
$this->setConfig([
$values = [
'base.appName' => ['value' => $appName, 'group_key' => 'base', 'remark' => '小程序名称'],
'base.logo' => ['value' => isset($params['logo']) ? trim((string)$params['logo']) : '', 'group_key' => 'base', 'remark' => 'LOGO地址'],
'base.logo' => ['value' => $logo, 'group_key' => 'base', 'remark' => 'LOGO地址'],
'base.servicePhone' => ['value' => isset($params['servicePhone']) ? trim((string)$params['servicePhone']) : '', 'group_key' => 'base', 'remark' => '客服电话'],
'base.aboutUs' => ['value' => isset($params['aboutUs']) ? trim((string)$params['aboutUs']) : '', 'group_key' => 'base', 'remark' => '关于我们'],
'base.shareTitle' => ['value' => isset($params['shareTitle']) ? trim((string)$params['shareTitle']) : '', 'group_key' => 'base', 'remark' => '分享标题'],
]);
// 兼容旧 ui.* 键(用户端已优先读 base.*)
'ui.siteName' => ['value' => $appName, 'group_key' => 'ui', 'remark' => '站点名称'],
'ui.logo' => ['value' => $logo, 'group_key' => 'ui', 'remark' => '自定义LOGO'],
];
$this->setConfig($values);
$this->success('小程序设置已保存');
}

查看文件

@@ -67,6 +67,8 @@ class Userext extends Backend
* 说明:
* 余额存放在核心表 fa_user.money,本表(fc_user_ext)仅作业务扩展,
* 因此这里按 user_id 关联 fa_user 进行加减。
* 【注意】ids 必须是 fa_user.id(即本表的 user_id 字段),
* 不能传 fc_user_ext.id,否则会充错人(或报「未找到对应用户」)。
* 为可追溯,每次充值同时写入 fa_fc_finance_flow(资金流水)与
* fa_fc_message(站内通知),后台操作留痕由 Backend 自动记录。
*/
@@ -89,52 +91,66 @@ class Userext extends Backend
$remark = trim((string)$this->request->post('remark', ''));
$now = time();
$done = 0;
$missing = [];
foreach ($userIds as $uid) {
$user = Db::name('user')->where('id', $uid)->find();
if (!$user) {
continue;
Db::startTrans();
try {
foreach ($userIds as $uid) {
// 加行锁,避免并发充值互相覆盖余额
$user = Db::name('user')->where('id', $uid)->lock(true)->find();
if (!$user) {
$missing[] = $uid;
continue;
}
$balance = round((float)$user['money'] + $amount, 2);
Db::name('user')->where('id', $uid)->update([
'money' => $balance,
'updatetime' => $now,
]);
// 资金流水(平台口径:余额充值属于收入)
Db::name('fc_finance_flow')->insert([
'flow_no' => 'R' . date('YmdHis') . mt_rand(100, 999) . $uid,
'type' => 'income',
'scene' => '余额充值',
'amount' => $amount,
'balance' => $balance,
'related_id' => $uid,
'remark' => $remark !== '' ? $remark : ('后台充值给用户 #' . $uid),
'createtime' => $now,
]);
// 站内通知
Db::name('fc_message')->insert([
'user_id' => $uid,
'type' => 'system',
'title' => '余额充值到账',
'content' => '您的账户已充值 ¥' . number_format($amount, 2, '.', '')
. ',当前余额 ¥' . number_format($balance, 2, '.', '')
. '。' . ($remark !== '' ? '备注:' . $remark : ''),
'isread' => 0,
'readtime' => null,
'createtime' => $now,
]);
$done++;
}
$balance = round((float)$user['money'] + $amount, 2);
Db::name('user')->where('id', $uid)->update([
'money' => $balance,
'updatetime' => $now,
]);
// 资金流水(平台口径:余额充值属于收入)
Db::name('fc_finance_flow')->insert([
'flow_no' => 'R' . date('YmdHis') . mt_rand(100, 999) . $uid,
'type' => 'income',
'scene' => '余额充值',
'amount' => $amount,
'balance' => $balance,
'related_id' => $uid,
'remark' => $remark !== '' ? $remark : ('后台充值给用户 #' . $uid),
'createtime' => $now,
]);
// 站内通知
Db::name('fc_message')->insert([
'user_id' => $uid,
'type' => 'system',
'title' => '余额充值到账',
'content' => '您的账户已充值 ¥' . number_format($amount, 2, '.', '')
. ',当前余额 ¥' . number_format($balance, 2, '.', '')
. '。' . ($remark !== '' ? '备注:' . $remark : ''),
'isread' => 0,
'readtime' => null,
'createtime' => $now,
]);
$done++;
Db::commit();
} catch (\Exception $e) {
Db::rollback();
$this->error('充值失败:' . $e->getMessage());
}
if (!$done) {
$this->error('未找到对应用户');
$this->error('未找到对应用户:' . implode(',', $missing) . '(请确认传的是用户ID)');
}
$this->success('充值成功:' . $done . ' 个用户,每人 ¥' . number_format($amount, 2, '.', ''));
$msg = '充值成功:' . $done . ' 个用户,每人 ¥' . number_format($amount, 2, '.', '');
if ($missing) {
$msg .= ';已跳过不存在的用户:' . implode(',', $missing);
}
$this->success($msg);
}
/**

查看文件

@@ -57,42 +57,81 @@ class Withdraw extends Backend
if (!$withdraws) {
$this->error('提现记录不存在');
}
foreach ($withdraws as $withdraw) {
if ($op === 'pay') {
Db::name('fc_withdraw')->where('id', $withdraw['id'])->update([
'status' => 'paid',
'paytime' => time(),
'updatetime' => time(),
]);
Db::name('fc_finance_flow')->insert([
'flow_no' => 'WD' . date('YmdHis') . $withdraw['id'],
'type' => 'expense',
'scene' => '佣金提现',
'amount' => $withdraw['amount'],
'balance' => 0,
'related_id' => $withdraw['id'],
'remark' => '提现打款',
'createtime' => time(),
]);
} else {
Db::name('fc_withdraw')->where('id', $withdraw['id'])->update([
'status' => 'rejected',
'reason' => $reason,
'updatetime' => time(),
]);
$done = 0;
$pending = 0;
$failed = 0;
$errors = [];
if ($op === 'reject') {
try {
Db::transaction(function () use ($withdraws, $reason, &$done) {
foreach ($withdraws as $withdraw) {
// 行锁 + 状态校验:已处理的记录不会被二次驳回
$locked = Db::name('fc_withdraw')->where('id', $withdraw['id'])->lock(true)->find();
if (!$locked || (string)$locked['status'] !== 'auditing') {
continue;
}
$done++;
Db::name('fc_withdraw')->where('id', $locked['id'])->update([
'status' => 'rejected',
'reason' => $reason,
'updatetime' => time(),
]);
Db::name('fc_message')->insert([
'user_id' => $locked['user_id'],
'type' => 'system',
'title' => '提现驳回',
'content' => '您的提现申请「' . $locked['withdraw_no'] . '」被驳回' . ($reason ? ',原因:' . $reason : ''),
'isread' => 0,
'createtime' => time(),
]);
}
});
} catch (\Throwable $e) {
$this->error('操作失败:' . $e->getMessage());
}
Db::name('fc_message')->insert([
'user_id' => $withdraw['user_id'],
'type' => 'system',
'title' => $op === 'pay' ? '提现成功' : '提现驳回',
'content' => $op === 'pay'
? '您的提现申请「' . $withdraw['withdraw_no'] . '」已打款,金额:' . $withdraw['amount'] . '元'
: '您的提现申请「' . $withdraw['withdraw_no'] . '」被驳回' . ($reason ? ',原因:' . $reason : ''),
'isread' => 0,
'createtime' => time(),
]);
if ($done === 0) {
$this->error('选中的提现记录均已处理,请刷新后重试');
}
$this->success('驳回成功(' . $done . ' 条)');
}
$this->success($op === 'pay' ? '打款成功' : '驳回成功');
// 通过 → 逐笔发起微信商家转账(外部调用不能放在事务里)
foreach ($withdraws as $withdraw) {
if ((string)$withdraw['status'] !== 'auditing') {
continue;
}
try {
$res = \app\common\library\WxTransfer::approve((int)$withdraw['id']);
$done++;
if ($res['withdrawState'] === 'paying') {
$pending++;
} elseif ($res['withdrawState'] === 'failed') {
$failed++;
}
} catch (\Throwable $e) {
$errors[] = $withdraw['withdraw_no'] . ':' . $e->getMessage();
}
}
if ($done === 0) {
$this->error('转账失败:' . implode(';', $errors));
}
$msg = '已发起转账 ' . $done . ' 笔';
if ($pending > 0) {
$msg .= '(其中 ' . $pending . ' 笔等待用户在微信确认收款)';
}
if ($failed > 0) {
$msg .= ',' . $failed . ' 笔转账失败';
}
if ($errors) {
$msg .= ';失败 ' . count($errors) . ' 笔:' . implode(';', $errors);
}
$this->success($msg);
}