love_php/app/Http/Controllers/Admin/EarningController.php
2026-04-02 09:20:51 +08:00

812 lines
43 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Jobs\SendEasySms;
use App\Models\EarningAccount;
use App\Models\EarningRule;
use App\Models\EarningUser;
use App\Models\PayOrder;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Jobs\SendTemplateMsg;
use App\Models\EarningWithdraw;
use App\Models\Live\Anchor;
use App\Models\MEarningTransfers;
use App\Models\MEarningwithdraws;
use App\Models\Message;
use App\Models\Server\MEarningAccount;
use App\Models\Server\MerchantAccount;
use App\Models\Server\MerchantUser;
use App\Services\LiveAlipayService;
use App\Utils\Messenger;
use Exception;
use Illuminate\Support\Facades\DB;
use Log;
class EarningController extends Controller
{
/**
* 收益规则列表
* @param Request $request
* @return JsonResponse|string
*/
public function rules(Request $request)
{
$rules = EarningRule::paginate();
return $this->success('ok', $rules);
}
/**
* 修改收益规则比例
* @param Request $request
* @param $id //收益id
* @return JsonResponse|string
*/
public function updateRule(Request $request, $id)
{
$rule = EarningRule::find($id);
if (empty($rule)) return $this->failure("修改失败,收益规则不存在");
$ratio = $request->input('ratio');
if ($request->has('ratio') && $ratio != $rule->ratio) {
$rule->ratio = $ratio;
}
$first_sharer = $request->input('first_sharer');
if ($request->has('first_sharer') && $first_sharer != $rule->first_sharer) {
$rule->first_sharer = $first_sharer;
}
$last_sharer = $request->input('last_sharer');
if ($request->has('last_sharer') && $ratio != $rule->last_sharer) {
$rule->last_sharer = $last_sharer;
}
$other_sharer = $request->input('other_sharer');
if ($request->has('other_sharer') && $other_sharer != $rule->other_sharer) {
$rule->other_sharer = $other_sharer;
}
$forzen_time = $request->input('forzen_time');
if ($request->has('forzen_time') && $forzen_time != $rule->forzen_time) {
$rule->forzen_time = $forzen_time;
}
$rule->save();
return $this->success('ok', $rule);
}
/**
* 收益账号列表
* @param Request $request
* @return JsonResponse|string
*/
public function accounts(Request $request)
{
$accounts = EarningAccount::with('user:id,nickname,photo,mobile');
$keyword = $request->input('keyword');
if ($keyword) {
$keyword = trim($keyword);
$accounts = $accounts->whereHas('user', function ($sql) use($keyword) {
$sql->where('mobile', 'like', '%'.$keyword.'%')
->orWhere('nickname', 'like', '%'.$keyword.'%');
});
}
$accounts = $accounts->where('user_id', '<>', 0)->orderBy('id', 'desc')->paginate();
return $this->success('ok', $accounts);
}
public function accountEarnings(Request $request, $id)
{
$account = EarningAccount::find($id);
if (empty($account)) return $this->failure('收益账号不存在');
$earnings = $account->earnings()->with('order:id,goods')->orderBy('id', 'desc')->paginate();
return $this->success('ok', compact('account', 'earnings'));
}
public function accountWithdraws(Request $request, $id)
{
$account = EarningAccount::find($id);
if (empty($account)) return $this->failure('收益账号不存在');
$withdraws = $account->withdraws()->orderBy('id', 'desc')->paginate();
return $this->success('ok', compact('account', 'withdraws'));
}
public function withdraws(Request $request)
{
$withdraws = EarningWithdraw::with("user");
$status = $request->input('status');
if ($status) {
$withdraws = $withdraws->where('status',$status);
}
$keyword = $request->input('keyword');
if ($keyword) {
$withdraws = $withdraws->where(function ($sql) use($keyword) {
$sql->where(function ($query) use($keyword){
$query->where('name', 'like', '%'.$keyword.'%')
->orWhere('account', 'like', '%'.$keyword.'%');
})->orwhereHas("user", function($query) use($keyword) {
$query->where('nickname', 'like', '%'.$keyword.'%');
});
});
}else {
$withdraws = $withdraws->whereHas('user');
}
$withdraws = $withdraws->orderByDesc('id')->paginate();
foreach ($withdraws as $withdraw) {
$withdraw->name = $withdraw->name?:$withdraw->user->nickname;
}
return $this->success('ok', $withdraws);
}
public function auditWithdraws(Request $request, $id)
{
Log::info("审核商家提现");
try {
$log = EarningWithdraw::find($id);
if (empty($log)) throw new Exception("提现记录不存在");
if ($log->status == 'canceled' || ($log->status == 'finished' && empty($log->err_msg))) return $this->failure("该提现记录已审核");
//获取该用户的收益账号
$account = $log->earningAccount;
if (empty($account)) throw new Exception("提现账号不存在");
$balance = $account->balance;//账户余额
$frozen_withdraw = $account->frozen_withdraw;//账户冻结提现金额
//如果当前用户冻结提现金额 小于 本次提现金额
if($frozen_withdraw < $log->value) throw new Exception('当前账户异常,提现金额大于实际金额');
$user = $log->user;
if($log->way == 'alipay'){
$transfer_way = '支付宝';
}elseif($log->way == 'weixin'){
$transfer_way = '微信';
}elseif($log->way == 'bank'){
$transfer_way = '银行卡';
}
DB::beginTransaction();
$status = $request->input('status');
$admin_id = auth()->id();
if ($status == 'canceled') {
$err_msg = $request->input('err_msg');
if (empty($err_msg)) return $this->failure("请输入拒绝理由");
EarningWithdraw::where('id',$id)->update(['status'=>$status, 'err_msg'=>$err_msg, 'admin_id'=>$admin_id, 'audit_at'=>date('Y-m-d H:i:s')]);
$account->increment('balance', $log->value);
$account->decrement("frozen_withdraw", $log->value);
DB::commit();
return $this->success('ok');
}
$data = [
'remark'=>"提现到账",
'payee_account'=>$log->account,
'payee_real_name'=>$log->name,
'amount'=>$log->real_value,
'out_biz_no'=>$log->trade_no,
];
Log::info($data);
switch ($log->way) {
case 'alipay':
$ali = new LiveAlipayService();
if($log->alipay_id){//通过支付宝id打款
$data['payee_account'] = $log->alipay_id;
$result = $ali->UserTransferAccount($data);
}else{//通过支付宝账号和名字打款
$result = $ali->platTransferAccount($data);
}
break;
case "weixin":
$result = \WechatService::officialUserTransferV2($log->trade_no,$log->account,$log->real_value * 100,$data['remark']);
Log::info($result);
break;
}
$err_msg = null;
if (is_array($result)) {
$err_msg = isset($result['msg']) ? $result['msg'] : $result['err_code_des'];
}else {
$account->increment('withdrawl', $log->value);
$account->decrement("frozen_withdraw", $log->value);
}
EarningWithdraw::where('id',$id)->update(['status'=>$status, 'err_msg'=>$err_msg, 'admin_id'=>$admin_id, 'audit_at'=>date('Y-m-d H:i:s')]);
//todo 通知
DB::commit();
return $this->success('审核完成');
}catch (Exception $e) {
DB::rollBack();
$this->getError($e);
return $this->failure();
}
}
public function orderEarnings(Request $request)
{
$earnings = DB::table('earnings')->selectRaw("*, CASE sharer
WHEN 'last_sharer' THEN 1
WHEN 'other_sharer' THEN 2
WHEN 'first_sharer' THEN 3
ELSE ''
END share_order, CASE sharer
WHEN 'last_sharer' THEN '促成'
WHEN 'other_sharer' THEN '间接'
WHEN 'first_sharer' THEN '首邀'
ELSE ''
END sharer")
->orderBy('order_id', 'desc')
->orderBy('share_order', 'asc')
->orderBy('sub_ratio', 'desc');
$orders = DB::table('orders')->where('pay_status', 'PAID');
$orders = $orders->leftJoin('users', 'orders.user_id', '=', 'users.id')
->selectRaw('ufutx_orders.trade_no, ufutx_users.mobile as pay_user_mobile, ufutx_orders.type, type_id, ufutx_orders.id as orders_id, ufutx_users.nickname as pay_user_name,ufutx_users.photo as pay_user_pic, ufutx_orders.goods as title');
$keyword = $request->input('keyword');
if ($keyword) {
$keyword = trim($keyword);
$orders = $orders->where(function($sql) use($keyword) {
$sql->where('users.nickname', 'like', '%'.$keyword.'%')
->orWhere('users.mobile', 'like', '%'.$keyword.'%')
->orWhere('orders.goods', 'like', '%'.$keyword.'%');
});
}
$users = DB::table('users')->selectRaw('id as user_id, nickname as sharer_user_name, photo as sharer_user_pic, mobile as sharer_user_mobile');
$earnings = $earnings->joinSub($orders, 'ufutx_o', function($join) {
$join->on('earnings.order_id', '=','o.orders_id');
})->leftJoinSub($users, 'ufutx_u', function($join) {
$join->on('earnings.user_id', '=','u.user_id');
})->paginate();
foreach ($earnings as $earning) {
$earning->order_price = PayOrder::where('trade_no', $earning->trade_no)->value('cash');
}
return $this->success('ok', $earnings);
}
//提现审核列表
public function withdrawalLogs(Request $request){
$keyword = $request->keyword;
$type = $request->type ??'saas';
$has_withdraw = 0;
$start_time = date('Y-m-d 00:00:00');
$end_time = date('Y-m-d 23:59:59');
$status = $request->status??'freezing'; //freezing未审核 finished审核通过 canceled审核失败
if($type == 'saas'){//saas 商家提现列表
$logs = MEarningwithdraws::with('anchor:id,m_id,name,mobile,pic','admin:id,nickname,name,mobile,circle_avatar,app_avatar')->where('status',$status)->where('m_user_id',0)->where('created_at','>','2022-02-24 00:00:00');
if($keyword){
$keyword = trim($keyword);
$logs->whereHas('anchor',function($sql) use($keyword){
$sql->where('name','like','%'.$keyword.'%')
->orWhere('mobile','like','%'.$keyword.'%');
});
}
}elseif($type == 'h5'){//福恋h5用户提现列表
$logs = EarningWithdraw::with('user:id,name,nickname,mobile,circle_avatar,app_avatar','admin:id,nickname,name,mobile,circle_avatar,app_avatar')->where('status',$status)->where('created_at','>','2022-02-24 00:00:00');
if($keyword){
$keyword = trim($keyword);
$logs->whereHas('user',function($sql) use($keyword){
$sql->where('name','like','%'.$keyword.'%')
->orWhere('mobile','like','%'.$keyword.'%')
->orWhere('nickname','like','%'.$keyword.'%');
});
}
}else{//saas H5用户提现列表
$logs = MEarningwithdraws::with('user:id,nickname,mobile,pic as avatar','admin:id,nickname,name,mobile,circle_avatar,app_avatar')->where('status',$status)->where('m_user_id','<>',0)->where('created_at','>','2022-02-24 00:00:00');
if($keyword){
$keyword = trim($keyword);
$logs->whereHas('user',function($sql) use($keyword){
$sql->where('nickname','like','%'.$keyword.'%')
->orWhere('mobile','like','%'.$keyword.'%');
});
}
}
$logs = $logs->orderBy('id','desc')->paginate();
foreach ($logs as $key => $log) {
if($log->m_id){//saas端提现
$has_withdraw = MEarningwithdraws::where(['m_id'=>$log->m_id,'m_user_id'=>$log->m_user_id,'status'=>'finished'])->whereBetWeen('created_at',[$start_time,$end_time])->sum('value');
}elseif($log->user_id){//h5提现
$has_withdraw = EarningWithdraw::where(['user_id'=>$log->user_id,'status'=>'finished'])->whereBetWeen('created_at',[$start_time,$end_time])->sum('value');
}
$log->has_withdraw = $has_withdraw;//用户当日提现金额
}
return $this->success('ok',$logs);
}
/**
* saas提现审核
* *
* @param Request $request
* @param $id
* @return JsonResponse|string
*/
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);
$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;
$url = '';
DB::beginTransaction();
switch ($status){
case 'finished': //审核通过
if($log->way == 'alipay'){
$ali = new LiveAlipayService();
if($log->alipay_id){//通过支付宝id打款
$result = $ali->UserTransferAccount($data);
}else{//通过支付宝账号和名字打款
$result = $ali->platTransferAccount($data);
}
}elseif($log->way == 'weixin'){
$result = \WechatService::officialUserTransferV2($log->trade_no,$log->account,$log->real_value * 100,'提现已到账');
}elseif($log->way == 'bank'){
$result = \WechatService::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('审核失败,请联系开发人员');
}
}
//福恋h5提现审核
public function auditWithdrawH5(Request $request,$id){
try {
$admin_id = auth()->id();
$log = EarningWithdraw::where('id',$id)->where('status','freezing')->first();
if(empty($log)) return $this->failure('该记录不存在或已被其他管理员审核');
//获取该用户的收益账号
$account = EarningAccount::where('user_id',$log->user_id)->first();
$balance = $account->balance;//账户余额
$frozen_withdraw = $account->frozen_withdraw;//账户冻结提现金额
//如果当前用户冻结提现金额 小于 本次提现金额
if($frozen_withdraw < $log->value) return $this->failure('当前账户异常,联系开发人员处理');
$user = User::where('id',$log->user_id)->first();
$openid = $user->wechat ? $user->wechat->official_openid : '';
$transfer_way = $log->way == 'alipay' ? '支付宝' : '微信';//转账方式
$status = $request->status;
if(!in_array($status,['finished','canceled','manual'])) throw new \Exception("提供审核参数有误",1);
$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;
$ali = new LiveAlipayService();
$url = '';
$alipay_real_name = empty($data['payee_real_name']) ? $data['alipay_id'] :$data['payee_real_name'];
DB::beginTransaction();
if($status == 'finished'){//审核通过
//todo 打款 发送通知
if($log->way == 'alipay'){
if($log->alipay_id){//通过支付宝id打款
$result = $ali->UserTransferAccount($data);
}else{//通过支付宝账号和名字打款
$result = $ali->platTransferAccount($data);
}
}elseif($log->way == 'weixin'){
$result = \WechatService::officialUserTransferV2($log->trade_no,$log->account,$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 = '福恋h5用户' . $alipay_real_name . ' 提现' . $log->value . '元失败,原因:' . $err_msg;
$this->sentMessage($mobile,$message);
//模板通知 邓智锋
$data['touser'] = ['oPC_2vnSECnYp5p--uaq3rca3Ry0', 'oPC_2vpJd34uN2E1tTsFbf8Lhlcs'];
$data['template_id'] = 'AqwVt0liVmQfzfnX3ZGvmVOdOh62nkCbhlOUI0NVQGs';
$data['url'] = $url;
$data['data'] = [
'first' => '福恋h5用户:' . $alipay_real_name . '提现失败',
'keyword1' => $log->value . '元',
'keyword2' => $transfer_way,
'keyword3' => $err_msg,
'remark' => '点击查看提现记录',
];
SendTemplateMsg::dispatch($data)->onQueue('template_message');
//短信通知
$message = $user->name.',提现'.$log->value.'元失败,具体原因请查看你的后台系统的提现反馈。';
$this->sentMessage($user->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 = $user->name.',提现金额'.$log->real_value.'元已到账,请在'.$transfer_way.'查收。';
$this->sentMessage($user->mobile,$message);
}
}elseif($status == '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]);
//todo 短信通知 公众号通知拒绝理由
$data['touser'] = $openid;
$data['template_id'] = 'AqwVt0liVmQfzfnX3ZGvmVOdOh62nkCbhlOUI0NVQGs';
$data['url'] = $url;
$data['data'] = [
'first' => '用户' . $alipay_real_name . '提现失败',
'keyword1' => $log->real_value . '元',
'keyword2' => $transfer_way,
'keyword3' => $reason,
'remark' => '账号信息存在异常',
];
$message = $user->name.',提现'.$log->real_value.'元失败,失败原因:'.$reason.',具体原因请查看你的后台系统的提现反馈。';
$this->sentMessage($user->mobile,$message);
SendTemplateMsg::dispatch($data)->onQueue('template_message');
}elseif($status == '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' => '商户:' . $alipay_real_name . '提现人工审核通过,处理方式:手工打款,请即时处理',
'keyword1' => $alipay_real_name,
'keyword2' => date('Y-m-d H:i:s'),
'keyword3' => $log->real_value . '元',
'keyword4' => '手动打款处理',
'remark' => '提现人工审核通过,处理方式:手工打款',
];
SendTemplateMsg::dispatch($data)->onQueue('template_message');
}
DB::commit();
return $this->success('审核完成');
} catch (\Exception $e) {
DB::rollBack();
$this->getError($e);
return $this->failure('审核失败,请联系开发人员');
}
}
/**
* saas h5 用户提现审核
* *
* @param Request $request
* @param $id
* @return JsonResponse|string
*/
public function auditWithdrawSaasH5(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',$log->m_user_id)->first();
$balance = $account->balance;//账户余额
$frozen_withdraw = $account->frozen_withdraw;//账户冻结提现金额
//如果当前用户冻结提现金额 小于 本次提现金额
if($frozen_withdraw < $log->value) return $this->failure('当前账户异常,联系开发人员处理');
$merchant_user = MerchantUser::where('id',$log->m_user_id)->first();
$status = $request->status;
if($log->way == 'alipay'){
$transfer_way = '支付宝';
}elseif($log->way == 'weixin'){
$transfer_way = '微信';
}elseif($log->way == 'bank'){
$transfer_way = '银行卡';
}
if(!in_array($status,['finished','canceled','manual'])) throw new \Exception("提供审核参数有误",1);
$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;
$ali = new LiveAlipayService();
$url = '';
$alipay_real_name = empty($data['payee_real_name']) ? $log->alipay_id :$data['payee_real_name'];
DB::beginTransaction();
switch ($status){
case 'finished': //审核通过
if($log->way == 'alipay'){
if($log->alipay_id){//通过支付宝id打款
$result = $ali->UserTransferAccount($data);
}else{//通过支付宝账号和名字打款
$result = $ali->platTransferAccount($data);
}
}elseif($log->way == 'weixin'){
$result = \WechatService::officialUserTransferV2($log->trade_no,$log->account,$log->real_value * 100,'提现已到账');
}
//\Log::info($result);
//判断打款是否成功
if(is_array($result)){//失败
$err_msg = isset($result['msg']) ? $result['msg'] : $result['err_code_des'];
//\Log::info('打款失败');
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 = 's端用户' . $alipay_real_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' => 's端用户' . $alipay_real_name . '提现失败',
'keyword1' => $log->real_value . '元',
'keyword2' => $transfer_way,
'keyword3' => $err_msg,
'remark' => '点击查看提现记录',
];
SendTemplateMsg::dispatch($data)->onQueue('template_message');
//短信通知
$message = $log->name.',提现'.$log->real_value.'元失败,具体原因请查看你的后台系统的提现反馈。';
$this->sentMessage($merchant_user->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'] = $merchant_user->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 = $log->name.',提现金额'.$log->real_value.'元已到账,请在'.$transfer_way.'查收。';
$this->sentMessage($merchant_user->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]);
//todo 短信通知 公众号通知拒绝理由
$data['touser'] = $merchant_user->openid;
$data['template_id'] = 'AqwVt0liVmQfzfnX3ZGvmVOdOh62nkCbhlOUI0NVQGs';
$data['url'] = $url;
$data['data'] = [
'first' => '商户' . $alipay_real_name . '提现失败',
'keyword1' => $log->real_value . '元',
'keyword2' => $transfer_way,
'keyword3' => $reason,
'remark' => '账号信息存在异常',
];
$message = $log->name.',提现'.$log->real_value.'元失败,失败原因:'.$reason.',具体原因请查看你的后台系统的提现反馈。';
$this->sentMessage($merchant_user->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' => 's端用户:' . $log->name . '提现人工审核通过,处理方式:手工打款,请即时处理',
'keyword1' => $log->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('审核失败,请联系开发人员');
}
}
public function earningUsers(Request $request)
{
try {
$users = EarningUser::with('user:id,nickname,name,mobile,photo,app_avatar');
$keyword = $request->input('keyword');
if ($keyword) {
$users = $users->whereHas('user', function ($sql) use($keyword) {
$sql->where('name', 'like', '%'.$keyword.'%')
->orWhere("nickname", 'like', '%'.$keyword.'%')
->orWhere('mobile', 'like', '%'.$keyword.'%');
});
}
$users = $users->orderByDesc('id')->paginate();
return $this->success('ok', $users);
}catch (Exception $e) {
$this->getError($e);
return $this->failure("获取信息失败,请联系开发人员");
}
}
public function addEarningUser(Request $request)
{
try {
$user_id = $request->input('user_id');
if (empty($user_id)) throw new Exception("缺少用户id参数");
$user = User::find($user_id);
$euser = EarningUser::where('user_id', $user_id)->first();
if (empty($euser)) {
$euser = EarningUser::create(['user_id'=>$user_id]);
//短信通知
SendEasySms::dispatch(['mobile'=>$user->mobile, 'message'=>'恭喜你,已成为福恋合作老师,邀请新朋友注册产生消费,将会获得对应比例的收益,快邀请身边的单身朋友加入吧'])->onQueue('love');
}
return $this->success('ok', $euser);
}catch (Exception $e) {
$this->getError($e);
return $this->failure("增加收益用户失败,请联系开发人员");
}
}
public function deleteEarningUser(Request $request)
{
try {
$user_id = $request->input('user_id');
if (empty($user_id)) throw new Exception("缺少用户id参数");
EarningUser::where('user_id', $user_id)->delete();
return $this->success('ok');
}catch (Exception $e) {
$this->getError($e);
return $this->failure("删除收益用户失败,请联系开发人员");
}
}
}