文件
Henglianshutong/application/admin/controller/shopro/Withdraw.php
chen a2220427e2 feat(withdraw): DMEPA提现手续费与最小提现额度配置收敛至商城配置页面并实现内扣闭环
1. 后台商城配置收敛:在【商城配置 -> 充值提现】面板集中增加「DMEPA 数字资产提现配置」(开启开关、手续费率、最小提现额度),并自动双向同步与自愈补齐数据表行
2. 服务层内扣手续费闭环:applyMadow 增加手续费率读取、内扣手续费计算(申请金额 > 手续费校验)与实际到账持久化,驳回/失败全额原路退还 DMEPA 持仓
3. API 与前台联动:rules() 与 madow() 接口返回手续费率,withdraw.vue 实时动态计算扣除 DMEPA 枚数、手续费内扣金额与预计到账金额
4. 后台列表与导出优化:提现管理列表与导出功能支持展示换算市值、手续费与到账金额
5. 新增及更新 3 套自动化规格测试(累计 130 项断言全部通过)
2026-09-21 14:33:20 +08:00

291 行
11 KiB
PHP

<?php
namespace app\admin\controller\shopro;
use think\Db;
use think\exception\HttpResponseException;
use addons\shopro\exception\ShoproException;
use app\admin\model\shopro\Withdraw as WithdrawModel;
use app\admin\model\shopro\WithdrawLog as WithdrawLogModel;
use app\admin\model\shopro\user\User as UserModel;
use app\admin\model\Admin as AdminModel;
use addons\shopro\service\Withdraw as WithdrawLibrary;
use addons\shopro\library\Operator;
/**
* 提现
*/
class Withdraw extends Common
{
protected $logModel;
protected $noNeedRight = ['checkWechatTransferResult', 'export'];
public function _initialize()
{
parent::_initialize();
$this->model = new WithdrawModel;
$this->logModel = new WithdrawLogModel;
}
/**
* 提现列表
*/
public function index()
{
if (!$this->request->isAjax()) {
$exportConfig = (new \addons\shopro\library\Export())->getConfig();
$this->assignconfig("save_type", $exportConfig['save_type'] ?? 'download');
return $this->view->fetch();
}
$withdraws = $this->model->sheepFilter()->with(['user'])->paginate($this->request->param('list_rows', 10));
$this->success('获取成功', null, $withdraws);
}
/**
* 导出提现管理订单台账
*/
public function export()
{
$cellTitles = [
'id' => 'ID',
'withdraw_sn' => '商户单号',
'user_nickname' => '申请用户',
'user_mobile' => '用户手机号',
'asset_type' => '提现资产',
'amount' => '提现金额/换算市值(元)',
'paid_fee' => '实际到账(元)',
'charge_rate_format' => '费率',
'charge_fee' => '手续费(元)',
'withdraw_type_text' => '提现方式',
'payee_name' => '真实姓名',
'bank_name' => '开户行',
'card_no' => '银行卡号/收款账号',
'asset_detail' => '资产折算明细',
'status_text' => '状态',
'createtime' => '申请时间',
'updatetime' => '操作时间',
];
$total = $this->model->sheepFilter()->count();
if ($total <= 0) {
$this->error('导出数据为空');
}
$export = new \addons\shopro\library\Export();
$params = [
'file_name' => '提现管理台账',
'cell_titles' => $cellTitles,
'total' => $total,
'is_sub_cell' => false,
];
$totalAmount = 0;
$totalPaidFee = 0;
$totalChargeFee = 0;
$result = $export->export($params, function ($pages) use (&$totalAmount, &$totalPaidFee, &$totalChargeFee, $total) {
$datas = $this->model->sheepFilter()->with(['user'])
->limit((($pages['page'] - 1) * $pages['list_rows']), $pages['list_rows'])
->select();
$newDatas = [];
foreach ($datas as $withdraw) {
$user = $withdraw['user'] ?? null;
$displayInfo = $withdraw['withdraw_info_display'] ?? ($withdraw['withdraw_info'] ?? []);
// 收款账户信息
$payeeName = $displayInfo['真实姓名'] ?? ($displayInfo['姓名'] ?? ($displayInfo['微信用户'] ?? ''));
$bankName = $displayInfo['开户行'] ?? ($displayInfo['开户银行'] ?? '');
$cardNo = $displayInfo['银行卡号'] ?? ($displayInfo['卡号'] ?? ($displayInfo['支付宝账户'] ?? ($displayInfo['支付宝账号'] ?? ($displayInfo['微信ID'] ?? ''))));
if ($cardNo !== '') {
$cardNo = "\t" . $cardNo;
}
// 提现资产类型
$assetType = $withdraw['is_madow_withdraw'] ? 'DMEPA' : ($withdraw['is_money_withdraw'] ? '商城余额' : ($displayInfo['提现资产'] ?? '商城余额'));
// 资产折算明细
$assetDetailParts = [];
if (!empty($displayInfo['扣除DMEPA'])) {
$assetDetailParts[] = '扣除: ' . $displayInfo['扣除DMEPA'];
}
if (!empty($displayInfo['市值汇率'])) {
$assetDetailParts[] = $displayInfo['市值汇率'];
}
if (!empty($displayInfo['换算市值'])) {
$assetDetailParts[] = '市值: ' . $displayInfo['换算市值'];
}
$assetDetail = implode('; ', $assetDetailParts);
$amount = floatval($withdraw['amount'] ?? 0);
$paidFee = floatval($withdraw['paid_fee'] ?? 0) > 0 ? floatval($withdraw['paid_fee']) : floatval($withdraw['real_amount'] ?? $amount);
$chargeFee = floatval($withdraw['charge_fee'] ?? 0);
$totalAmount = function_exists('bcadd') ? bcadd((string)$totalAmount, (string)$amount, 2) : ($totalAmount + $amount);
$totalPaidFee = function_exists('bcadd') ? bcadd((string)$totalPaidFee, (string)$paidFee, 2) : ($totalPaidFee + $paidFee);
$totalChargeFee = function_exists('bcadd') ? bcadd((string)$totalChargeFee, (string)$chargeFee, 2) : ($totalChargeFee + $chargeFee);
$newDatas[] = [
'id' => $withdraw['id'],
'withdraw_sn' => "\t" . (string)($withdraw['withdraw_sn'] ?? ''),
'user_nickname' => $user['nickname'] ?? ('用户 ' . $withdraw['user_id']),
'user_mobile' => "\t" . (string)($user['mobile'] ?? ''),
'asset_type' => $assetType,
'amount' => number_format($amount, 2, '.', ''),
'paid_fee' => number_format($paidFee, 2, '.', ''),
'charge_rate_format' => $withdraw['charge_rate_format'] ?? '0.0%',
'charge_fee' => number_format($chargeFee, 2, '.', ''),
'withdraw_type_text' => $withdraw['withdraw_type_text'] ?? '-',
'payee_name' => $payeeName ?: '-',
'bank_name' => $bankName ?: '-',
'card_no' => $cardNo ?: '-',
'asset_detail' => $assetDetail ?: '-',
'status_text' => $withdraw['status_text'] ?? '-',
'createtime' => $withdraw['createtime'] ?? '-',
'updatetime' => $withdraw['updatetime'] ?? '-',
];
}
if ($pages['is_last_page']) {
$newDatas[] = [
'id' => '合计: 共 ' . $total . ' 条记录',
'withdraw_sn' => '',
'user_nickname' => '',
'user_mobile' => '',
'asset_type' => '',
'amount' => number_format((float)$totalAmount, 2, '.', ''),
'paid_fee' => number_format((float)$totalPaidFee, 2, '.', ''),
'charge_rate_format' => '',
'charge_fee' => number_format((float)$totalChargeFee, 2, '.', ''),
'withdraw_type_text' => '',
'payee_name' => '',
'bank_name' => '',
'card_no' => '',
'asset_detail' => '',
'status_text' => '',
'createtime' => '',
'updatetime' => '',
];
}
return $newDatas;
});
return $result;
}
/**
* 提现日志
*/
public function log($id)
{
if (!$this->request->isAjax()) {
return $this->view->fetch();
}
$logs = $this->logModel->where('withdraw_id', $id)->order('id desc')->select();
$morphs = [
'user' => UserModel::class,
'admin' => AdminModel::class,
'system' => AdminModel::class
];
$logs = morph_to($logs, $morphs, ['oper_type', 'oper_id']);
$logs = $logs->toArray();
// 解析操作人信息
foreach ($logs as &$log) {
$log['oper'] = Operator::info($log['oper_type'], $log['oper'] ?? null);
}
$this->success('获取成功', null, $logs);
}
public function handle($id)
{
if (!$this->request->isAjax()) {
return $this->view->fetch();
}
$params = $this->request->param();
$action = $params['action'] ?? null;
if (!in_array($action, ['agree', 'withdraw', 'agree&withdraw', 'refuse', 'refund_commission', 'cancel'])) {
$this->error('您的操作有误');
}
$refuse_msg = $params['refuse_msg'] ?? '';
if ($action === 'refuse' && !$refuse_msg) {
$this->error('请输入拒绝原因');
}
$ids = is_array($id) ? $id : explode(',', $id);
foreach ($ids as $key => $id) {
Db::startTrans();
try {
$withdraw = $this->model->lock(true)->where('id', $id)->find();
if (!$withdraw) {
$this->error(__('No Results were found'));
}
$withdrawLib = new WithdrawLibrary($withdraw->user_id);
switch ($action) {
case 'agree':
$withdraw = $withdrawLib->handleAgree($withdraw);
break;
case 'withdraw':
$withdraw = $withdrawLib->handleWithdraw($withdraw);
break;
case 'agree&withdraw':
$withdraw = $withdrawLib->handleAgree($withdraw);
$withdraw = $withdrawLib->handleWithdraw($withdraw);
break;
case 'refuse':
$withdraw = $withdrawLib->handleRefuse($withdraw, $refuse_msg);
break;
}
Db::commit();
} catch (ShoproException $e) {
// 不回滚,记录错误日志
Db::commit();
$this->error($e->getMessage());
} catch (HttpResponseException $e) {
$data = $e->getResponse()->getData();
$message = $data ? ($data['msg'] ?? '') : $e->getMessage();
$this->error($message);
} catch (\Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
}
$this->success('处理成功');
}
// 检查转账提现订单结果
public function checkWechatTransferResult($id)
{
$params = $this->request->param();
$withdraw = $this->model->lock(true)->where('id', $id)->find();
if (!$withdraw) {
$this->error(__('No Results were found'));
}
$withdrawLib = new WithdrawLibrary($withdraw->user_id);
try {
$withdrawLib->checkWechatTransferResult($withdraw);
} catch (\Exception $e) {
$this->error($e->getMessage());
}
$this->success('操作成功');
}
}