文件

291 行
12 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();
if (defined('ROOT_PATH') && is_file(ROOT_PATH . 'addons/shopro/helper/helper.php')) {
require_once ROOT_PATH . 'addons/shopro/helper/helper.php';
}
$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' => '操作时间',
];
try {
$total = (new WithdrawModel)->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 = (new WithdrawModel)->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 = !empty($withdraw['is_madow_withdraw']) ? 'DMEPA' : (!empty($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);
$createTime = is_numeric($withdraw['createtime'] ?? null) && intval($withdraw['createtime']) > 0
? date('Y-m-d H:i:s', intval($withdraw['createtime']))
: ($withdraw['createtime'] ?: '-');
$updateTime = is_numeric($withdraw['updatetime'] ?? null) && intval($withdraw['updatetime']) > 0
? date('Y-m-d H:i:s', intval($withdraw['updatetime']))
: ($withdraw['updatetime'] ?: '-');
$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' => $createTime,
'updatetime' => $updateTime,
];
}
if ($pages['is_last_page']) {
$newDatas[] = [
'id' => '合计: 共 ' . $total . ' 条记录;提现总额:¥' . number_format((float)$totalAmount, 2, '.', '') . ';实际到账总额:¥' . number_format((float)$totalPaidFee, 2, '.', '') . ';手续费总额:¥' . number_format((float)$totalChargeFee, 2, '.', '')
];
}
return $newDatas;
});
$this->success('导出成功' . (isset($result['file_path']) && $result['file_path'] ? ',请在服务器: “' . $result['file_path'] . '” 查看' : ''), null, $result);
} catch (HttpResponseException $e) {
throw $e;
} catch (\Throwable $e) {
$this->error('导出处理失败: ' . $e->getMessage());
}
}
/**
* 提现日志
*/
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('操作成功');
}
}