ufutx.love.util/app/Http/Controllers/Admin/EarningController.php
2025-08-12 14:46:31 +08:00

175 lines
10 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Facades\AlipayService;
use App\Facades\WechatPayService;
use App\Http\Response\ResponseJson;
use App\Models\Live\Anchor;
use App\Models\Server\MEarningAccount;
use App\Models\Server\MEarningwithdraws;
use App\Models\Server\MerchantAccount;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class EarningController
{
use ResponseJson;
public function auditWithdraw(Request $request, $id)
{
try {
$admin_id = auth()->id();
$log = MEarningwithdraws::where('id', $id)->where('status', 'freezing')->first();
if (empty($log))
return $this->failure('该记录不存在或已被其他管理员审核');
//获取该商户的收益账号
$account = MEarningAccount::where('m_id', $log->m_id)->where('m_user_id', 0)->first();
$balance = $account->balance;//账户余额
$frozen_withdraw = $account->frozen_withdraw;//账户冻结提现金额
//如果当前用户冻结提现金额 小于 本次提现金额
if ($frozen_withdraw < $log->value)
return $this->failure('当前账户异常,联系开发人员处理');
$anchor = Anchor::where('m_id', $log->m_id)->first();
$openid = MerchantAccount::where('id', $log->m_id)->value('openid');//商家openid
if (empty($openid))
$openid = $anchor->openid;
if ($log->way == 'alipay') {
$transfer_way = '支付宝';
} elseif ($log->way == 'weixin') {
$transfer_way = '微信';
} elseif ($log->way == 'bank') {
$transfer_way = '银行卡';
}
$status = $request->status;
if (!in_array($status, ['finished', 'canceled', 'manual']))
throw new \Exception("提供审核参数有误", 1);
$url = '';
DB::beginTransaction();
switch ($status) {
case 'finished': //审核通过
if ($log->way == 'alipay') {
$data = [];
$data['remark'] = '提现已到账'; //提现备注
$data['out_biz_no'] = $log->trade_no;
$data['amount'] = $log->real_value;//实际到账金额
$data['payee_account'] = $log->account;//支付宝账号
$data['payee_real_name'] = $log->name;//支付宝绑定姓名
if ($log->alipay_id)
$data['payee_account'] = $log->alipay_id;
if ($log->alipay_id) {//通过支付宝id打款
$result = AlipayService::UserTransferAccount($data);
} else {//通过支付宝账号和名字打款
$result = AlipayService::platTransferAccount($data);
}
} elseif ($log->way == 'weixin') {
$result = WechatPayService::officialUserTransfer($log->trade_no, $log->account, $log->real_value * 100, '提现已到账');
} elseif ($log->way == 'bank') {
$result = WechatPayService::bankTransfer($log->trade_no, $log->account, $log->name, $log->bank_code, $log->real_value * 100, '打款至个人银行卡');
}
//\Log::info($result);
//判断打款是否成功
if (is_array($result)) {//失败
//\Log::info('打款失败');
$err_msg = isset($result['msg']) ? $result['msg'] : $result['err_code_des'];
return $this->failure($err_msg);
$log->update(['status' => 'canceled', 'admin_id' => $admin_id, 'audit_at' => date('Y-m-d H:i:s'), 'err_msg' => $err_msg]);
$account->update(['frozen_withdraw' => $frozen_withdraw - $log->value, 'balance' => $balance + $log->value]);
// 短信通知 邓智锋
$mobile = '15707534403';
$message = '商户' . $anchor->name . ' 提现' . $log->real_value . '元失败,原因:' . $err_msg;
$this->sentMessage($mobile, $message);
//模板通知 邓智锋
$data['touser'] = ['oPC_2vnSECnYp5p--uaq3rca3Ry0', 'oPC_2vpJd34uN2E1tTsFbf8Lhlcs'];
$data['template_id'] = 'AqwVt0liVmQfzfnX3ZGvmVOdOh62nkCbhlOUI0NVQGs';
$data['url'] = $url;
$data['data'] = [
'first' => '商户' . $anchor->name . '提现失败',
'keyword1' => $log->real_value . '元',
'keyword2' => $transfer_way,
'keyword3' => $err_msg,
'remark' => '点击查看提现记录',
];
SendTemplateMsg::dispatch($data)->onQueue('template_message');
//短信通知
$message = $anchor->name . ',提现' . $log->real_value . '元失败,具体原因请查看你的后台系统的提现反馈。';
$this->sentMessage($anchor->mobile, $message);
DB::commit();
return $this->failure($err_msg);
} else {//成功
//\Log::info('打款成功');
$log->update(['status' => 'finished', 'admin_id' => $admin_id, 'audit_at' => date('Y-m-d H:i:s')]);
//将冻结提现金额 换成已提现金额 //冻结提现金额 0
$account->withdrawl = $account->withdrawl + $log->value;
$account->frozen_withdraw = $account->frozen_withdraw - $log->value;
$account->save();
//模板通知
$data['touser'] = $openid;
$data['template_id'] = 'aV4ic7jr5bOlf55CgR0jmMsFYyhdRAiVmqqXEjnUqQU';
$data['url'] = $url;
$data['data'] = [
'first' => '提现已到账,请在' . $transfer_way . '查收',
'keyword1' => $log->real_value . '元',
'keyword2' => date('Y-m-d H:i:s'),
'keyword3' => '提现到' . $transfer_way . '余额',
'remark' => '感谢您的的使用',
];
SendTemplateMsg::dispatch($data)->onQueue('template_message');
//短信通知
$message = $anchor->name . ',提现金额' . $log->real_value . '元已到账,请在' . $transfer_way . '查收。';
$this->sentMessage($anchor->mobile, $message);
}
break;
case 'canceled': //审核拒绝
$reason = $request->reason;
if (!$reason)
return $this->failure('请输入拒绝理由');
$account->update(['frozen_withdraw' => $frozen_withdraw - $log->value, 'balance' => $balance + $log->value]);
$log->update(['status' => $status, 'admin_id' => $admin_id, 'audit_at' => date('Y-m-d H:i:s'), 'err_msg' => $request->reason]);
$data['touser'] = $openid;
$data['template_id'] = 'AqwVt0liVmQfzfnX3ZGvmVOdOh62nkCbhlOUI0NVQGs';
$data['url'] = $url;
$data['data'] = [
'first' => '商户' . $anchor->name . '提现失败',
'keyword1' => $log->real_value . '元',
'keyword2' => $transfer_way,
'keyword3' => $reason,
'remark' => '账号信息存在异常',
];
$message = $anchor->name . ',提现' . $log->real_value . '元失败,失败原因:' . $reason . ',具体原因请查看你的后台系统的提现反馈。';
$this->sentMessage($anchor->mobile, $message);
SendTemplateMsg::dispatch($data)->onQueue('template_message');
break;
case 'manual': //手工处理
//\Log::info('打款已经由人工处理');
// $log->update(['status' => 'finished', 'admin_id' => $admin_id, 'audit_at' => date('Y-m-d H:i:s'), 'way' => 'manual']);
// //将冻结提现金额 换成已提现金额 //冻结提现金额 0
// $account->withdrawl = $account->withdrawl + $log->value;
// $account->frozen_withdraw = $account->frozen_withdraw - $log->value;
// $account->save();
// //模板通知用户
// $data['touser'] = ['oPC_2vudVLVHj2U7dNinr2IEDHR4', 'oPC_2vuTj7YRgUzQQY7PlSJVLBBc'];
// $data['template_id'] = 'OwXPF2dKEjPQUoGyzH944ATsJ6SgxpZ8kzB-KVVxanY';
// $data['url'] = $url;
// $data['data'] = [
// 'first' => '商户:' . $anchor->name . '提现人工审核通过,处理方式:手工打款,请即时处理',
// 'keyword1' => $anchor->name,
// 'keyword2' => date('Y-m-d H:i:s'),
// 'keyword3' => $log->real_value . '元',
// 'keyword4' => '手动打款处理',
// 'remark' => '提现人工审核通过,处理方式:手工打款',
// ];
// SendTemplateMsg::dispatch($data)->onQueue('template_message');
break;
default:
break;
}
DB::commit();
return $this->success('审核完成');
} catch (\Exception $e) {
DB::rollBack();
$this->getError($e);
return $this->failure('审核失败,请联系开发人员');
}
}
}