文件
yjyf/server/application/common/library/AdminRechargeService.php
MiAn 5dd04b2f24 feat: 新增后台人工充值功能及支付结果页
- 新增后台人工充值模块,支持上传支付凭证
- 新增支付结果查询接口及页面
- 新增订单支付成功跳转结果页逻辑
- 更新钱包流水类型标签区分充值方式
- 新增插件路由 PHP 8 兼容错误报告设置
2026-11-19 16:22:14 +08:00

54 行
2.5 KiB
PHP

<?php
namespace app\common\library;
use think\Db;
class AdminRechargeService
{
public static function credit($userId, $adminId, $amount, $voucher, $requestId)
{
$amount = trim((string)$amount);
$voucher = trim((string)$voucher);
if (!preg_match('/^(?:0|[1-9]\d{0,7})(?:\.\d{1,2})?$/D', $amount) || bccomp($amount, '0', 2) <= 0) {
throw new \RuntimeException('请输入大于零且最多两位小数的充值金额');
}
if (!preg_match('/^[a-f0-9]{32}$/D', (string)$requestId)) {
throw new \RuntimeException('充值请求无效,请重新打开页面');
}
if (!$voucher || strlen($voucher) > 255 || !Db::name('attachment')->where('url', $voucher)->where('admin_id', (int)$adminId)->where('category', 'recharge')->where('mimetype', 'in', ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/bmp'])->find()) {
throw new \RuntimeException('请上传本次支付凭证图片');
}
$amount = bcadd($amount, '0', 2);
Db::startTrans();
try {
$user = Db::name('user')->where('id', (int)$userId)->lock(true)->find();
if (!$user) {
throw new \RuntimeException('充值用户不存在');
}
if (Db::name('admin_recharge')->where('request_id', $requestId)->find()) {
throw new \RuntimeException('该充值请求已提交,请勿重复充值');
}
$balance = bcadd((string)$user['money'], $amount, 2);
if (bccomp($balance, '99999999.99', 2) > 0) {
throw new \RuntimeException('充值后余额超出允许范围');
}
$now = time();
Db::name('user')->where('id', (int)$userId)->update(['money' => $balance, 'updatetime' => $now]);
$logId = Db::name('wallet_log')->insertGetId([
'user_type' => 'user', 'user_id' => (int)$userId, 'type' => 'recharge',
'amount' => $amount, 'balance' => $balance, 'order_id' => 0,
'remark' => '后台人工充值', 'createtime' => $now,
]);
Db::name('admin_recharge')->insert([
'wallet_log_id' => $logId, 'admin_id' => (int)$adminId,
'voucher' => $voucher, 'request_id' => $requestId, 'createtime' => $now,
]);
Db::commit();
return ['wallet_log_id' => $logId, 'balance' => $balance];
} catch (\Throwable $e) {
Db::rollback();
throw $e;
}
}
}