feat: 完善会员资金明细与退款转账逻辑,新增支付密码组件与资金流水界面

这个提交包含在:
2026-10-24 17:55:36 +08:00
父节点 6b86177b5f
当前提交 b6c53abb84
共有 30 个文件被更改,包括 1222 次插入 和 537 次删除

查看文件

@@ -96,29 +96,38 @@ class AdminSetting extends V1
/**
* AS3 费用配置
* GET → {singlePost,freePostPerDay,memberMonth,memberQuarter,memberYear}
* POST 入参同字段 → 合并后配置;同时同步 fc_plan 价格
* POST 入参同字段 → 合并后配置
* 真源对齐:singlePost → post.singlePrice;freePostPerDay → post.freeQuota(按日);
* 月/季/年卡价格 → fc_plan(与后台「运营推广 → 会员套餐」同源,改价即时生效)
*/
public function fee()
{
if ($this->request->isPost()) {
$singlePost = (float)$this->input('singlePost', $this->cfg('fee.singlePost', 5));
$singlePost = (float)$this->input('singlePost', $this->cfg('post.singlePrice', 5));
if ($singlePost < 0) {
$this->fail('单条发布价格不能为负');
}
$freePostPerDay = (int)$this->input('freePostPerDay', $this->cfg('fee.freePostPerDay', 2));
$memberMonth = (float)$this->input('memberMonth', $this->cfg('fee.memberMonth', 19.9));
$memberQuarter = (float)$this->input('memberQuarter', $this->cfg('fee.memberQuarter', 49.9));
$memberYear = (float)$this->input('memberYear', $this->cfg('fee.memberYear', 168));
$freePostPerDay = (int)$this->input('freePostPerDay', $this->cfg('post.freeQuota', 0));
if ($freePostPerDay < 0) {
$this->fail('免费发布条数不能为负');
}
$this->setCfg('fee.singlePost', $singlePost);
$this->setCfg('fee.freePostPerDay', $freePostPerDay);
$this->setCfg('fee.memberMonth', $memberMonth);
$this->setCfg('fee.memberQuarter', $memberQuarter);
$this->setCfg('fee.memberYear', $memberYear);
$this->setCfg('post.singlePrice', $singlePost);
$this->setCfg('post.freeQuota', $freePostPerDay);
$this->setCfg('post.freeQuotaCycle', 'day');
Db::name('fc_plan')->where('plan_code', 'M1')->update(['price' => $memberMonth]);
Db::name('fc_plan')->where('plan_code', 'M3')->update(['price' => $memberQuarter]);
Db::name('fc_plan')->where('plan_code', 'M12')->update(['price' => $memberYear]);
$now = time();
foreach (['M1' => 'memberMonth', 'M3' => 'memberQuarter', 'M12' => 'memberYear'] as $code => $field) {
$raw = $this->input($field, null);
if ($raw === null || $raw === '') {
continue;
}
$price = round((float)$raw, 2);
if ($price < 0) {
$this->fail('套餐价格不能为负');
}
Db::name('fc_plan')->where('plan_code', $code)->update(['price' => $price, 'updatetime' => $now]);
}
$this->adminLog('修改费用配置');
}
@@ -334,15 +343,20 @@ class AdminSetting extends V1
];
}
/** 费用配置结构 */
/** 费用配置结构(月/季/年卡价格真源=fc_plan;发布价格/免费条数真源=post.*) */
protected function feeConfig()
{
$plans = Db::name('fc_plan')->where('enabled', 1)->column('price', 'plan_code');
$planPrice = function ($code, $default) use ($plans) {
return isset($plans[$code]) && $plans[$code] !== null && $plans[$code] !== '' ? round((float)$plans[$code], 2) : $default;
};
return [
'singlePost' => (float)$this->cfg('fee.singlePost', 5),
'freePostPerDay' => (int)$this->cfg('fee.freePostPerDay', 2),
'memberMonth' => (float)$this->cfg('fee.memberMonth', 19.9),
'memberQuarter' => (float)$this->cfg('fee.memberQuarter', 49.9),
'memberYear' => (float)$this->cfg('fee.memberYear', 168),
'singlePost' => round((float)$this->cfg('post.singlePrice', 5), 2),
'freePostPerDay' => max(0, (int)$this->cfg('post.freeQuota', 0)),
'memberMonth' => $planPrice('M1', 19.9),
'memberQuarter' => $planPrice('M3', 49.9),
'memberYear' => $planPrice('M12', 168),
];
}

查看文件

@@ -157,8 +157,12 @@ class Member extends V1
$isBalance = $payType === 'balance';
// 余额校验必须在落订单之前:否则余额不足时已插入一条 status=paid 的假订单
if ($isBalance && (float)$user['money'] < $amount) {
$this->fail('余额不足');
if ($isBalance) {
if ((float)$user['money'] < $amount) {
$this->fail('余额不足');
}
// 后台开启支付密码时,余额支付需校验
$this->verifyPayPassword($user);
}
$orderNo = $this->orderNo('MO');
@@ -206,6 +210,9 @@ class Member extends V1
$grant = $this->grantMembership(Db::name('fc_order')->where('id', $orderId)->find());
// 券核销(微信支付在 OrderFulfill 里核销)
\app\common\library\CouponService::markUsed($orderNo);
// 钱包流水(fa_user.money 减)→ 余额中心「余额明细」
$balance = round((float)Db::name('user')->where('id', (int)$user['id'])->value('money'), 2);
$this->walletFlow($user['id'], 'expense', '会员开通', $amount, $balance, $orderId, (string)$plan['name']);
}
});
} catch (\Throwable $e) {
@@ -289,7 +296,7 @@ class Member extends V1
$amount = round((float)(isset($order['amount']) ? $order['amount'] : 0), 2);
$orderId = (int)(isset($order['id']) ? $order['id'] : 0);
$this->flow('income', '会员开通', $amount, 0, $orderId);
$this->flow('income', '会员开通', $amount, 0, $orderId, '', $userId);
$this->notify(
$userId,
'system',
@@ -397,10 +404,104 @@ class Member extends V1
}
$this->ok([
'balance' => (float)$user['money'],
'coupons' => $coupons,
'couponCount' => count($coupons),
'points' => (int)$user['score'],
'balance' => (float)$user['money'],
'coupons' => $coupons,
'couponCount' => count($coupons),
'points' => (int)$user['score'],
'payPasswordEnabled' => (int)$this->cfg('pay.payPasswordEnabled', 0) === 1,
'hasPayPassword' => (string)$this->ext($user['id'])['pay_password'] !== '',
]);
}
/**
* 余额明细(余额中心交易记录)
* GET /api/v1/member/flows {page,size,type} → {list:[...],page,size,total,hasMore}
* type 筛选:recharge=充值 / expense=消费 / refund=退款,缺省=全部
* 数据源:fc_wallet_flow(用户钱包流水,仅记录 fa_user.money 增减;
* fc_finance_flow 是平台对账口径,含微信收入等非钱包变动,不在此展示)
*/
public function flows()
{
$user = $this->auth();
$type = (string)$this->input('type', '');
list($rows, $total, $page, $size) = $this->pageQuery(function ($forCount) use ($user, $type) {
// TP5.0 禁止复用同一 Query 先 count 再 select,必须走 pageQuery 重建
$q = Db::name('fc_wallet_flow')->where('user_id', $user['id']);
if ($type === 'recharge') {
// 充值:微信充值入账与后台充值
$q->where('scene', 'in', ['余额充值', '后台充值']);
} elseif ($type === 'expense') {
// 消费:会员开通 / 发布付费 / 付费查看 / 充值退款扣回等支出
$q->where('type', 'expense');
} elseif ($type === 'refund') {
// 退款:订单退款(余额原路退回)
$q->where('scene', 'like', '%退款%');
}
return $forCount ? $q : $q->order('id desc');
}, 15);
$list = [];
foreach ($rows as $row) {
$list[] = [
'flowNo' => (string)$row['flow_no'],
'type' => (string)$row['type'],
'scene' => (string)$row['scene'],
'amount' => round((float)$row['amount'], 2),
'balance' => round((float)$row['balance'], 2),
'remark' => (string)$row['remark'],
'createdAt' => !empty($row['createtime']) ? $this->iso((int)$row['createtime']) : null,
];
}
$this->ok($this->paginate($list, $total, $page, $size));
}
/**
* 设置/修改支付密码(6 位数字)
* POST /api/v1/member/pay-password/set {password, oldPassword?}
* 首次设置无需 oldPassword;已设置时必须携带正确的 oldPassword(找回走人工/后续 SMS)。
*/
public function setPayPassword()
{
$user = $this->auth();
$this->rateLimit('payswd:' . $user['id'], 10, 60);
$password = trim((string)$this->input('password', ''));
$oldPassword = trim((string)$this->input('oldPassword', ''));
if (!preg_match('/^\d{6}$/', $password)) {
$this->fail('支付密码必须是 6 位数字');
}
$ext = $this->ext($user['id']);
$hash = (string)(isset($ext['pay_password']) ? $ext['pay_password'] : '');
if ($hash !== '') {
// 修改密码:必须验证旧密码
$old = trim((string)$oldPassword);
if ($old === '') {
$this->fail('请输入原支付密码');
}
if (!preg_match('/^\d{6}$/', $old) || !password_verify($old, $hash)) {
$this->fail('原支付密码错误');
}
if ($old === $password) {
$this->fail('新密码不能与原密码相同');
}
}
// 弱密码拦截:6 位连号/同号(如 123456 / 888888)
if (preg_match('/^(\d)\1{5}$/', $password)
|| in_array($password, ['012345', '123456', '234567', '345678', '456789', '987654', '876543'], true)) {
$this->fail('支付密码过于简单,请更换');
}
Db::name('fc_user_ext')->where('user_id', $user['id'])->update([
'pay_password' => password_hash($password, PASSWORD_DEFAULT),
'updatetime' => time(),
]);
$this->ok(['hasPayPassword' => true]);
}
}

查看文件

@@ -99,7 +99,9 @@ class Order extends V1
$balance = round((float)Db::name('user')->where('id', $userId)->value('money'), 2);
Db::name('user')->where('id', $userId)->update(['updatetime' => $now]);
$this->flow('income', '余额充值', $amount, $balance, $orderId, '余额充值');
$this->flow('income', '余额充值', $amount, $balance, $orderId, '余额充值', $userId);
// 钱包流水(fa_user.money 增)→ 余额中心「余额明细」
$this->walletFlow($userId, 'income', '余额充值', $amount, $balance, $orderId, '余额充值');
$this->notify(
$userId,
'system',

查看文件

@@ -225,8 +225,12 @@ class Post extends V1
}
// 余额校验须在落库之前(按抵扣后的实付金额判断)
if ($paid && $payType === 'balance' && (float)$user['money'] < $amount) {
$this->fail('余额不足');
if ($paid && $payType === 'balance') {
if ((float)$user['money'] < $amount) {
$this->fail('余额不足');
}
// 后台开启支付密码时,余额支付需校验
$this->verifyPayPassword($user);
}
// 区域:优先 districtId,其次 area 名称,最后按坐标匹配最近区县
@@ -344,7 +348,9 @@ class Post extends V1
]);
if ($paidNow) {
$this->flow('income', '发布付费', $amount, $balanceAfter, $postId);
$this->flow('income', '发布付费', $amount, $balanceAfter, $postId, '', (int)$user['id']);
// 钱包流水(fa_user.money 减,用户口径为支出)→ 余额中心「余额明细」
$this->walletFlow($user['id'], 'expense', '发布付费', $amount, $balanceAfter, $postId, $title);
// 余额支付:核销优惠券 + 触发分销结算
CouponService::markUsed($orderNo);
@@ -514,7 +520,7 @@ class Post extends V1
}
/**
* 发布规则(后台「发布与费用配置」维护;配置缺失时回落旧 fee.* 再回落内置默认)
* 发布规则(真源:发布规则=「系统配置 → 发布配置」post.*;套餐价格/条数=「运营推广 → 会员套餐」fc_plan)
* @return array
*/
protected function publishRule()
@@ -523,13 +529,22 @@ class Post extends V1
if (!in_array($mode, ['free', 'paid'], true)) {
$mode = 'paid';
}
// 套餐价格与含发布条数唯一真源:fc_plan(按 plan_code 取,找不到时用内置默认)
$plans = Db::name('fc_plan')->where('enabled', 1)->column('price,post_quota', 'plan_code');
$planValue = function ($code, $field, $default) use ($plans) {
return isset($plans[$code][$field]) && $plans[$code][$field] !== null && $plans[$code][$field] !== ''
? $plans[$code][$field]
: $default;
};
return [
'mode' => $mode,
'singlePrice' => round((float)$this->cfg('post.singlePrice', $this->cfg('fee.singlePost', 5)), 2),
'monthPrice' => round((float)$this->cfg('post.monthPrice', $this->cfg('fee.memberMonth', 19.9)), 2),
'monthQuota' => max(0, (int)$this->cfg('post.monthQuota', 30)),
'yearPrice' => round((float)$this->cfg('post.yearPrice', $this->cfg('fee.memberYear', 168)), 2),
'yearQuota' => max(0, (int)$this->cfg('post.yearQuota', 500)),
'singlePrice' => round((float)$this->cfg('post.singlePrice', 5), 2),
'monthPrice' => round((float)$planValue('M1', 'price', 19.9), 2),
'monthQuota' => max(0, (int)$planValue('M1', 'post_quota', 30)),
'yearPrice' => round((float)$planValue('M12', 'price', 168), 2),
'yearQuota' => max(0, (int)$planValue('M12', 'post_quota', 500)),
];
}
@@ -537,9 +552,8 @@ class Post extends V1
* 免费发布条数规则(v1.0.10)
* post.freeQuota 免费发布条数(0 = 不限制,免费发布模式下「免费到底」)
* post.freeQuotaCycle 统计周期:day 每日 / month 每月 / total 累计(不重置)
* 兼容:配置项 **缺失** 时(老库未执行新版 fc:install)回落 `fee.freePostPerDay` 按日计;
* 一旦配置项存在(后台「发布与费用配置」保存过或 fc:install 种过),就以配置为准,
* 此时 `0` 表示「不限制」而不再回落旧值。
* 配置项缺失时(未执行过 fc:install)按 0 条处理;一旦配置存在即以配置为准,
* 此时 `0` 表示「不限制」。
* @return array{quota:int,cycle:string}
*/
protected function freeQuotaRule()
@@ -550,8 +564,7 @@ class Post extends V1
}
$raw = $this->cfg('post.freeQuota', null);
if ($raw === null || $raw === '') {
$legacy = max(0, (int)$this->cfg('fee.freePostPerDay', 0));
return ['quota' => $legacy, 'cycle' => $legacy > 0 ? 'day' : $cycle];
return ['quota' => 0, 'cycle' => $cycle];
}
return ['quota' => max(0, (int)$raw), 'cycle' => $cycle];
}
@@ -987,6 +1000,14 @@ class Post extends V1
'wechat' => $wechat,
];
$amount = round((float)$this->cfg('fee.payView', 2), 2);
$now = time();
$ext = $this->ext($user['id']);
$isMember = (int)$ext['is_member'] === 1
&& !empty($ext['member_expiretime']) && (int)$ext['member_expiretime'] > $now;
$quota = $this->memberFreeViewQuota();
// 已解锁直接返回(严格返回纯净扁平字段,杜绝 JSON 嵌套导致前端格式错乱)
$exist = Db::name('fc_pay_view')
->where('user_id', $user['id'])
@@ -1009,14 +1030,14 @@ class Post extends V1
$amount = round((float)$this->cfg('fee.payView', 2), 2);
$now = time();
$ext = $this->ext($user['id']);
$isMember = (int)$ext['is_member'] === 1
&& !empty($ext['member_expiretime']) && (int)$ext['member_expiretime'] > $now;
$quota = $this->memberFreeViewQuota();
$orderId = 0;
$pending = null;
// 后台开启支付密码时,余额支付需校验(会员免费额度未用完时走免费,不要求密码)
if ($payType === 'balance' && !($isMember && $quota > 0)) {
$this->verifyPayPassword($user);
}
try {
Db::transaction(function () use (&$orderId, &$pending, $isMember, $quota, $payType, $amount, $user, $id, $now) {
// 会员每日免费查看额度(当天用完后按正常付费)
@@ -1038,7 +1059,29 @@ class Post extends V1
throw new \RuntimeException('余额不足,请充值或使用微信支付');
}
$balance = (float)Db::name('user')->where('id', (int)$user['id'])->value('money');
$this->flow('expense', '付费查看', $amount, $balance, $id);
// 余额支付为「即时支付」:同步落一笔已支付订单,保证「我的订单」与微信支付口径一致
$orderNo = $this->orderNo('VO');
$orderId = Db::name('fc_order')->insertGetId([
'order_no' => $orderNo,
'user_id' => $user['id'],
'type' => 'payview',
'title' => '付费查看 #' . $id,
'amount' => $amount,
'pay_type' => 'balance',
'status' => 'paid',
'paytime' => $now,
'related_id' => $id,
'createtime' => $now,
'updatetime' => $now,
]);
$this->flow('expense', '付费查看', $amount, $balance, $id, '', (int)$user['id']);
// 钱包流水(fa_user.money 减)→ 余额中心「余额明细」
$this->walletFlow($user['id'], 'expense', '付费查看', $amount, $balance, $id, '解锁信息 #' . $id);
// 余额支付同样触发分销结算(与微信回调 OrderFulfill::handle 口径一致)
$orderRow = Db::name('fc_order')->where('id', $orderId)->find();
if ($orderRow) {
DistributionService::settleOrder($orderRow);
}
} else {
// 微信支付:只落 pending 订单(不预写解锁记录),
// 支付成功后由 /pay/notify 按 type=payview + related_id 解锁(回调可独立完成)

查看文件

@@ -46,6 +46,8 @@ class Setting extends V1
'audit' => (string)$this->cfg('wx.tplAudit', ''),
'transfer' => (string)$this->cfg('wx.tplTransfer', ''),
],
// 余额支付密码开关(开启后余额支付需验证 6 位支付密码,用户在余额中心设置)
'payPasswordEnabled' => (int)$this->cfg('pay.payPasswordEnabled', 0) === 1,
]);
}

查看文件

@@ -606,12 +606,16 @@ class V1 extends Api
return is_array($decoded) ? $decoded : [];
}
/** 记一条资金流水 */
protected function flow($type, $scene, $amount, $balance, $relatedId = 0, $remark = '')
/**
* 记一条资金流水(含归属用户,供余额中心按用户查询)
* @param int $userId 归属用户 ID(0=平台/不可归属)
*/
protected function flow($type, $scene, $amount, $balance, $relatedId = 0, $remark = '', $userId = 0)
{
static $seq = 0;
$seq++;
Db::name('fc_finance_flow')->insert([
'user_id' => (int)$userId,
'flow_no' => 'F' . date('YmdHis') . bin2hex(random_bytes(4)) . $seq,
'type' => $type,
'scene' => $scene,
@@ -623,6 +627,55 @@ class V1 extends Api
]);
}
/**
* 记一条「用户钱包流水」(fc_wallet_flow)
* 仅在 fa_user.money 发生增减时调用——余额中心的「余额明细」数据源。
* 平台对账口径(微信收入等)继续走 flow() / fc_finance_flow。
* @param int $userId 用户 ID
* @param string $type income=入账 expense=支出(用户钱包口径)
* @param string $scene 场景:余额充值/后台充值/会员开通/发布付费/付费查看/订单退款(余额)/佣金提现
* @param float $amount 变动金额(正数)
* @param float $balance 变动后钱包余额
*/
protected function walletFlow($userId, $type, $scene, $amount, $balance, $relatedId = 0, $remark = '')
{
Db::name('fc_wallet_flow')->insert([
'user_id' => (int)$userId,
'flow_no' => 'W' . date('YmdHis') . bin2hex(random_bytes(4)),
'type' => $type,
'scene' => $scene,
'amount' => $amount,
'balance' => $balance,
'related_id' => (int)$relatedId,
'remark' => $remark,
'createtime' => time(),
]);
}
/**
* 余额支付前的支付密码校验(开关关闭时直接放行)
* 规则:后台开启「支付密码」后,所有余额支付必须携带正确的 payPassword;
* 未设置过密码时提示先设置(余额中心 → 支付密码)。
*/
protected function verifyPayPassword($user)
{
if (!(int)$this->cfg('pay.payPasswordEnabled', 0)) {
return;
}
$ext = $this->ext($user['id']);
$hash = (string)(isset($ext['pay_password']) ? $ext['pay_password'] : '');
if ($hash === '') {
$this->fail('请先在「余额中心」设置支付密码');
}
$pwd = trim((string)$this->input('payPassword', ''));
if ($pwd === '') {
$this->fail('请输入支付密码');
}
if (!preg_match('/^\d{6}$/', $pwd) || !password_verify($pwd, $hash)) {
$this->fail('支付密码错误');
}
}
/** 发一条站内消息 */
protected function notify($userId, $type, $title, $content)
{