From e7f77af788655560922f660e4e76c60c3598e542 Mon Sep 17 00:00:00 2001 From: Hankin Date: Mon, 27 Apr 2026 14:19:00 +0800 Subject: [PATCH] callback --- .../Server/Admin/EarningController.php | 343 ++++++++++-------- 1 file changed, 195 insertions(+), 148 deletions(-) diff --git a/app/Http/Controllers/Server/Admin/EarningController.php b/app/Http/Controllers/Server/Admin/EarningController.php index 0c8d7b8..0ccef5e 100644 --- a/app/Http/Controllers/Server/Admin/EarningController.php +++ b/app/Http/Controllers/Server/Admin/EarningController.php @@ -231,7 +231,8 @@ class EarningController extends Controller //商家服务id $service_ids = CommunityActivity::where('merchant_id', $request->account_id) ->where('class', 'many')->pluck('id') - ->toArray();; + ->toArray(); + ; foreach ($earnings as $earning) { if ($earning->m_user_id) { $user = MerchantUser::find($earning->m_user_id); @@ -409,8 +410,10 @@ class EarningController extends Controller if ($value['ratio'] > 1) { return $this->failure('分成利润不能大于1'); } - if (!isset($value['first_sharer']) || !isset($value['last_sharer']) || !isset($value['other_sharer'])) throw new Exception("缺少分成人员比例参数"); - if ($value['first_sharer'] + $value['last_sharer'] + $value['other_sharer'] != 1) return $this->failure("分成人员比例必须100%"); + if (!isset($value['first_sharer']) || !isset($value['last_sharer']) || !isset($value['other_sharer'])) + throw new Exception("缺少分成人员比例参数"); + if ($value['first_sharer'] + $value['last_sharer'] + $value['other_sharer'] != 1) + return $this->failure("分成人员比例必须100%"); $result = MEarningRules::where('m_id', $merchant_id) ->where('type_id', 0) ->where('name', $key) @@ -427,7 +430,7 @@ class EarningController extends Controller $result->forzen_time = $value['forzen_time']; $result->save(); } - MEarningRules::where('m_id', $merchant_id)->where('name', $key)->update(['is_first_sharer'=>$value['is_first_sharer']??1, 'is_other_sharer'=>$value['is_other_sharer']??1, 'is_last_sharer'=>$value['is_last_sharer']??1, 'first_sharer'=> $value['first_sharer']??0.3, 'last_sharer'=>$value['last_sharer']??0.5, 'other_sharer'=> $value['other_sharer']??0.2]); + MEarningRules::where('m_id', $merchant_id)->where('name', $key)->update(['is_first_sharer' => $value['is_first_sharer'] ?? 1, 'is_other_sharer' => $value['is_other_sharer'] ?? 1, 'is_last_sharer' => $value['is_last_sharer'] ?? 1, 'first_sharer' => $value['first_sharer'] ?? 0.3, 'last_sharer' => $value['last_sharer'] ?? 0.5, 'other_sharer' => $value['other_sharer'] ?? 0.2]); } return $this->success('ok'); } catch (\Exception $e) { @@ -487,7 +490,7 @@ class EarningController extends Controller { try { $m_id = $request->account_id; - $earning_rule = MerchantInfo::where('m_id', $m_id)->value('earning_rule')?:"system"; + $earning_rule = MerchantInfo::where('m_id', $m_id)->value('earning_rule') ?: "system"; switch ($earning_rule) { case 'system': $rules = MEarningRules::select('name', 'ratio', 'forzen_time')->where('m_id', $m_id)->where('type_id', 0)->get(); @@ -509,7 +512,7 @@ class EarningController extends Controller break; } return $this->success('ok', compact('earning_rule', 'rules')); - }catch (Exception $e) { + } catch (Exception $e) { $this->getError($e); return $this->failure(); } @@ -524,14 +527,14 @@ class EarningController extends Controller { try { $m_id = $request->account_id; - $info = MerchantInfo::firstOrcreate(['m_id'=>$m_id]); + $info = MerchantInfo::firstOrcreate(['m_id' => $m_id]); $earning_rule = $request->input('earning_rule'); if ($earning_rule && $request->earning_rule != $info->earning_rule) { $info->earning_rule = $request->earning_rule; } $info->save(); return $this->success('ok', $info); - }catch (Exception $e) { + } catch (Exception $e) { $this->getError($e); return $this->failure(); } @@ -547,15 +550,17 @@ class EarningController extends Controller try { $m_id = $request->account_id; $title = $request->input('title'); - if (empty($title)) return $this->failure("操作失败,请输入规则名称"); + if (empty($title)) + return $this->failure("操作失败,请输入规则名称"); $amount = $request->input('amount', 0); $ratio = $request->input('ratio', 1); $forzen_time = $request->input('forzen_time', 0); $grade = MEarningGrade::where('m_id', $m_id)->where('amount', $amount)->first(); - if ($grade) return $this->failure("操作失败,已设置同价位的规则"); - $grade = MEarningGrade::create(['m_id'=>$m_id, 'title'=>$title, 'amount'=>$amount, 'ratio'=>$ratio, 'forzen_time'=>$forzen_time]); + if ($grade) + return $this->failure("操作失败,已设置同价位的规则"); + $grade = MEarningGrade::create(['m_id' => $m_id, 'title' => $title, 'amount' => $amount, 'ratio' => $ratio, 'forzen_time' => $forzen_time]); return $this->success('ok', $grade); - }catch (Exception $e) { + } catch (Exception $e) { $this->getError($e); return $this->failure(); } @@ -567,29 +572,31 @@ class EarningController extends Controller * @param $grade_id * @return JsonResponse|string */ - public function updateGradeRule(Request $request,$grade_id) + public function updateGradeRule(Request $request, $grade_id) { try { $m_id = $request->account_id; $grade = MEarningGrade::where('m_id', $m_id)->where('id', $grade_id)->first(); - if (empty($grade)) return $this->failure("操作失败,该收益规则不能存在"); - if ($request->title && $request->title != $grade->title){ + if (empty($grade)) + return $this->failure("操作失败,该收益规则不能存在"); + if ($request->title && $request->title != $grade->title) { $grade->title = $request->title; } - if ($request->has('amount') && $request->amount != $grade->amount){ + if ($request->has('amount') && $request->amount != $grade->amount) { $own_grade = MEarningGrade::where('m_id', $m_id)->where('amount', $request->amount)->where('id', '<>', $grade->id)->first(); - if ($own_grade) return $this->failure("操作失败,已设置同价位的规则"); + if ($own_grade) + return $this->failure("操作失败,已设置同价位的规则"); $grade->amount = $request->amount; } - if ($request->has('ratio') && $request->ratio != $grade->ratio){ + if ($request->has('ratio') && $request->ratio != $grade->ratio) { $grade->ratio = $request->ratio; } - if ($request->has('forzen_time') && $request->forzen_time != $grade->forzen_time){ + if ($request->has('forzen_time') && $request->forzen_time != $grade->forzen_time) { $grade->forzen_time = $request->forzen_time; } $grade->save(); return $this->success('ok', $grade); - }catch (Exception $e) { + } catch (Exception $e) { $this->getError($e); return $this->failure(); } @@ -600,12 +607,13 @@ class EarningController extends Controller * @param Request $request * @param $grade_id */ - public function gradeRule(Request $request,$grade_id) + public function gradeRule(Request $request, $grade_id) { try { $m_id = $request->account_id; $grade = MEarningGrade::where('m_id', $m_id)->where('id', $grade_id)->first(); - if (empty($grade)) return $this->failure("操作失败,该收益规则不能存在"); + if (empty($grade)) + return $this->failure("操作失败,该收益规则不能存在"); return $this->success('ok', $grade); } catch (Exception $e) { $this->getError($e); @@ -624,7 +632,8 @@ class EarningController extends Controller try { $m_id = $request->account_id; $grade = MEarningGrade::where('m_id', $m_id)->where('id', $grade_id)->first(); - if (empty($grade)) return $this->failure("操作失败,该收益规则不能存在"); + if (empty($grade)) + return $this->failure("操作失败,该收益规则不能存在"); $result = $grade->delete(); return $this->success('ok', $result); } catch (Exception $e) { @@ -714,7 +723,8 @@ class EarningController extends Controller $MEarningTransfers->bank_code = $request->bank_code; if ($request->bank_name || $request->bank_code) { $bank = DB::table('banks')->where('name', $request->bank_name)->where('code', $request->bank_code)->first(); - if (!$bank) return $this->failure('暂不支持该银行'); + if (!$bank) + return $this->failure('暂不支持该银行'); } $MEarningTransfers->in_use = $status ? 0 : 1; $MEarningTransfers->save(); @@ -748,7 +758,8 @@ class EarningController extends Controller } if ($request->bank_name || $request->bank_code) { $bank = DB::table('banks')->where('name', $request->bank_name)->where('code', $request->bank_code)->first(); - if (!$bank) return $this->failure('暂不支持该银行'); + if (!$bank) + return $this->failure('暂不支持该银行'); } $MEarningTransfers->way = $request->way ?? $MEarningTransfers->way; $MEarningTransfers->m_id = $merchant_id ?? $MEarningTransfers->merchant_id; @@ -877,15 +888,15 @@ class EarningController extends Controller { try { $merchant_id = $request->account_id; - $transfer = MEarningTransfers::where('m_id', $merchant_id)->where('m_user_id',0)->orderBy('in_use','desc')->first(); + $transfer = MEarningTransfers::where('m_id', $merchant_id)->where('m_user_id', 0)->orderBy('in_use', 'desc')->first(); $can_cash_out_amount = 0; - $account = MEarningAccount::where('m_id',$merchant_id)->where('m_user_id',0)->first(); - if($account){ + $account = MEarningAccount::where('m_id', $merchant_id)->where('m_user_id', 0)->first(); + if ($account) { $can_cash_out_amount = $account->balance; } // $poundage = Redis::get('withdrawal_poundage') ?? 2; - $poundage = MerchantAccount::where('id',$request->account_id)->value('poundage'); - return $this->success('ok',compact('transfer','can_cash_out_amount','poundage')); + $poundage = MerchantAccount::where('id', $request->account_id)->value('poundage'); + return $this->success('ok', compact('transfer', 'can_cash_out_amount', 'poundage')); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); @@ -924,7 +935,7 @@ class EarningController extends Controller } -// $result = TouristOrder::select('id', 'name', 'mobile', 'price', 'type', 'type_id', 'account_id', 'from_openid', 'channel', 'created_at', 'goods'); + // $result = TouristOrder::select('id', 'name', 'mobile', 'price', 'type', 'type_id', 'account_id', 'from_openid', 'channel', 'created_at', 'goods'); // if ($request->has('keyword')) { // $result = $result->where(function ($query) use ($keyword) { // $query->where('name', 'like', '%' . $keyword . '%') @@ -944,22 +955,22 @@ class EarningController extends Controller ->with('mEarning.user:id,nickname,mobile')->with('mEarning.merchant:id,mobile,share_title') ->whereNull('user_coupon_id') //->whereNotIn('id',[9672,9678]) - ->where('channel','!=',4) -// ->where('merchant_id', $request->account_id) - ->whereHas("mEarning", function ($sql) use($request) { - $sql->where('m_id', $request->account_id)->where('m_user_id', 0)->where('value', ">" , 0); + ->where('channel', '!=', 4) + // ->where('merchant_id', $request->account_id) + ->whereHas("mEarning", function ($sql) use ($request) { + $sql->where('m_id', $request->account_id)->where('m_user_id', 0)->where('value', ">", 0); }) - ->whereIn('pay_status', [1,4]) + ->whereIn('pay_status', [1, 4]) ->when($start_time, function ($query) use ($start_time) { $query->where('created_at', '>=', $start_time . ' 00:00:01'); }) ->when($end_time, function ($query) use ($end_time) { $query->where('created_at', '<=', $end_time . ' 23:59:59'); }) - ->select('id', 'trade_no', 'name', 'mobile', 'price', 'created_at', 'channel', 'goods', 'type','type_id', 'merchant_id') - ->orderBy('id','desc'); + ->select('id', 'trade_no', 'name', 'mobile', 'price', 'created_at', 'channel', 'goods', 'type', 'type_id', 'merchant_id') + ->orderBy('id', 'desc'); -// $result = Db::table('m_earnings') + // $result = Db::table('m_earnings') // ->leftJoin('tourist_orders', 'm_earnings.m_order_id', '=', 'tourist_orders.id') // ->select('tourist_orders.*') // ->where('m_earnings.m_user_id', 0); @@ -970,7 +981,7 @@ class EarningController extends Controller ->orWhere('mobile', 'like', '%' . $keyword . '%'); }); } -// if($start_time){ + // if($start_time){ // $result = $result->where('tourist_orders.created_at','>=',$start_time.' 00:00:01'); // } // if($end_time){ @@ -980,19 +991,19 @@ class EarningController extends Controller // ->where('tourist_orders.pay_status', '<>', 0) // ->orderBy('tourist_orders.id', 'desc'); // - if($nopage){ + if ($nopage) { $result = $result->get(); - }else{ - $result = $result ->paginate(); + } else { + $result = $result->paginate(); } foreach ($result as $key => $value) { $value->channel = (new TouristOrder())->getChannelAttribute($value->channel); $can_withdraw_count = MEarning::where('m_order_id', $value->id) - ->where('m_id', $merchant_id) - ->where('m_user_id', 0) - ->sum('value') ?? 0; + ->where('m_id', $merchant_id) + ->where('m_user_id', 0) + ->sum('value') ?? 0; $value->can_withdraw_count = $value->price; if ($can_withdraw_count >= 0) $value->can_withdraw_count = $can_withdraw_count; @@ -1069,17 +1080,17 @@ class EarningController extends Controller $value->mobile = $user->mobile ?? ''; } if ($value->type == 'transfer') { - $earning = Mearning::where('m_order_id',$value->id)->first(); - $m_id = MerchantTransferLog::where('m_earning_id',$earning->id)->value('m_id'); - $anchor = Anchor::where('m_id',$m_id)->first(); - $value->name = $anchor ? $anchor->name :'匿名用户'; + $earning = Mearning::where('m_order_id', $value->id)->first(); + $m_id = MerchantTransferLog::where('m_earning_id', $earning->id)->value('m_id'); + $anchor = Anchor::where('m_id', $m_id)->first(); + $value->name = $anchor ? $anchor->name : '匿名用户'; $value->mobile = $anchor ? $anchor->mobile : ''; - $value->goods = '商家'.$anchor->name.'发起转账'; + $value->goods = '商家' . $anchor->name . '发起转账'; } } $data['orders'] = $result; // $data['poundage'] = Redis::get('withdrawal_poundage') ?? 2; - $data['poundage'] = MerchantAccount::where('id',$request->account_id)->value('poundage'); + $data['poundage'] = MerchantAccount::where('id', $request->account_id)->value('poundage'); return $this->success('ok', $data); } catch (\Exception $e) { $this->getError($e); @@ -1092,26 +1103,27 @@ class EarningController extends Controller * @param Request $request * @return JsonResponse|BinaryFileResponse */ - public function OrderEarningExport(Request $request){ + public function OrderEarningExport(Request $request) + { try { $start_time = $request->start_time; $end_time = $request->end_time; - if(!$start_time){ + if (!$start_time) { $start_time = Carbon::now()->addMonth(-1)->toDateString(); } - if(!$end_time){ + if (!$end_time) { $end_time = date('Y-m-d'); } $order_list = TouristOrder::with('mEarning:id,m_id,m_user_id,m_order_id,sharer,ratio,value,created_at') ->with('mEarning.user:id,nickname,mobile')->with('mEarning.merchant:id,mobile,share_title') ->whereNull('user_coupon_id') //->whereNotIn('id',[9672,9678]) - ->where('channel','!=',4) -// ->where('merchant_id', $request->account_id) - ->whereHas("mEarning", function ($sql) use($request) { - $sql->where('m_id', $request->account_id)->where('m_user_id', 0)->where('value', ">" , 0); + ->where('channel', '!=', 4) + // ->where('merchant_id', $request->account_id) + ->whereHas("mEarning", function ($sql) use ($request) { + $sql->where('m_id', $request->account_id)->where('m_user_id', 0)->where('value', ">", 0); }) - ->whereIn('pay_status', [1,4]) + ->whereIn('pay_status', [1, 4]) ->when($start_time, function ($query) use ($start_time) { $query->where('created_at', '>=', $start_time . ' 00:00:01'); }) @@ -1119,29 +1131,29 @@ class EarningController extends Controller $query->where('created_at', '<=', $end_time . ' 23:59:59'); }) ->select('id', 'trade_no', 'name', 'mobile', 'price', 'created_at', 'channel', 'goods', 'type', 'desc') - ->orderBy('id','desc') + ->orderBy('id', 'desc') ->get() ->toArray(); $total = 0.00; foreach ($order_list as $key => $value) { $can_withdraw_count = MEarning::where('m_order_id', $value['id']) - ->where('m_id', $request->account_id) - ->where('m_user_id', 0) - ->sum('value') ?? 0; + ->where('m_id', $request->account_id) + ->where('m_user_id', 0) + ->sum('value') ?? 0; $order_list[$key]['name'] = $this->filterEmoji($order_list[$key]['name']); $order_list[$key]['can_withdraw_count'] = $value['price']; if ($can_withdraw_count >= 0) { $order_list[$key]['can_withdraw_count'] = $can_withdraw_count; - $total = round($total + $can_withdraw_count,2); + $total = round($total + $can_withdraw_count, 2); $order_list[$key]['total'] = $total; } if ($value['type'] == 'transfer') { - $earning = Mearning::where('m_order_id',$value['id'])->first(); - $m_id = MerchantTransferLog::where('m_earning_id',$earning->id)->value('m_id'); - $anchor = Anchor::where('m_id',$m_id)->first(); - $order_list[$key]['name'] = $anchor ? $anchor->name :$this->filterEmoji($order_list[$key]['name']); + $earning = Mearning::where('m_order_id', $value['id'])->first(); + $m_id = MerchantTransferLog::where('m_earning_id', $earning->id)->value('m_id'); + $anchor = Anchor::where('m_id', $m_id)->first(); + $order_list[$key]['name'] = $anchor ? $anchor->name : $this->filterEmoji($order_list[$key]['name']); $order_list[$key]['mobile'] = $anchor ? $anchor->mobile : $order_list[$key]['mobile']; - $order_list[$key]['goods'] = '商家'.($anchor->name??'').'发起转账'; + $order_list[$key]['goods'] = '商家' . ($anchor->name ?? '') . '发起转账'; } } @@ -1155,7 +1167,7 @@ class EarningController extends Controller } //return Excel::download(new TouristOrdersEarningExport($order_list), $file_name); $list = new TouristOrdersEarningExport($order_list); - return $this->success('ok',$list->collection_a()); + return $this->success('ok', $list->collection_a()); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); @@ -1163,7 +1175,8 @@ class EarningController extends Controller } // 过滤掉emoji表情 - function filterEmoji($str){ + function filterEmoji($str) + { $str = preg_replace_callback('/./u', function (array $match) { return strlen($match[0]) >= 4 ? '' : $match[0]; }, $str); @@ -1214,11 +1227,12 @@ class EarningController extends Controller if (empty($cash_out_type)) { return $this->failure('请选择您需要提现的方式'); } - $anchor_name = $earning_account->anchor?$earning_account->anchor->name:''; + $anchor_name = $earning_account->anchor ? $earning_account->anchor->name : ''; $trade_no = \CommonUtilsService::getTradeNO(); if ($cash_out_type == 'wechat') { //微信提现 //判断是否绑定微信 - if (empty($openid)) return $this->failure('绑定微信账号失败'); + if (empty($openid)) + return $this->failure('绑定微信账号失败'); //绑定完成 提现 $desc = "<$anchor_name>提现-微信"; // $wechat = new WechatService($this->sms); @@ -1366,16 +1380,16 @@ class EarningController extends Controller ->where('m_id', $merchant_id) ->where('m_user_id', 0) ->when($start_time, function ($query) use ($start_time) { - $query->where('created_at', '>=', $start_time.' 00:00:01'); + $query->where('created_at', '>=', $start_time . ' 00:00:01'); }) ->when($end_time, function ($query) use ($end_time) { - $query->where('created_at', '<=', $end_time.' 23:59:59'); + $query->where('created_at', '<=', $end_time . ' 23:59:59'); }) ->orderBy('id', 'desc'); - if($nopage){ + if ($nopage) { $result = $result->get(); - }else{ - $result = $result ->paginate(); + } else { + $result = $result->paginate(); } foreach ($result as $key => $value) { $value->poundage = number_format($value->value - $value->real_value, 2) ?? 0; @@ -1385,16 +1399,20 @@ class EarningController extends Controller if ($value->err_msg && strstr($value->err_msg, '余额不足,建议尽快充值')) { $value->err_msg = '您的提现将会在24小时内处理'; } - if($value->way == 'weixin'){ - $value->pic = MEarningTransfers::where('nick_name',$value->name)->value('pic'); - }else{ + if ($value->way == 'weixin') { + $value->pic = MEarningTransfers::where('nick_name', $value->name)->value('pic'); + } else { $value->pic = null; } - if($value->way == 'bank'){ - $value->bankName = DB::table('banks')->where('code',$value->bank_code)->value('name'); - }else{ + if ($value->way == 'bank') { + $value->bankName = DB::table('banks')->where('code', $value->bank_code)->value('name'); + } else { $value->bankName = null; } + $appId = config("wechat.service_payment.app_id"); + $mchId = config("wechat.payment.mch_id"); + $value->appId = $appId; + $value->mchId = $mchId; } return $this->success('ok', $result); } catch (\Exception $e) { @@ -1413,7 +1431,7 @@ class EarningController extends Controller try { $merchant_id = $request->account_id; $data = []; -// $result = TouristOrder::select('id', 'name', 'mobile', 'price', 'type', 'type_id', 'account_id', 'merchant_id', 'created_at') + // $result = TouristOrder::select('id', 'name', 'mobile', 'price', 'type', 'type_id', 'account_id', 'merchant_id', 'created_at') // ->where('merchant_id', $merchant_id) // ->where('pay_status', '<>', 0) // ->orderBy('id', 'desc') @@ -1421,14 +1439,20 @@ class EarningController extends Controller $result = Db::table('m_earnings') ->join('tourist_orders', 'm_earnings.m_order_id', '=', 'tourist_orders.id') -// ->select('tourist_orders.*') + // ->select('tourist_orders.*') ->select([ - 'tourist_orders.id', 'tourist_orders.name', 'tourist_orders.mobile', 'tourist_orders.price', - 'tourist_orders.type', 'tourist_orders.type_id', 'tourist_orders.account_id', - 'tourist_orders.merchant_id', 'tourist_orders.created_at' + 'tourist_orders.id', + 'tourist_orders.name', + 'tourist_orders.mobile', + 'tourist_orders.price', + 'tourist_orders.type', + 'tourist_orders.type_id', + 'tourist_orders.account_id', + 'tourist_orders.merchant_id', + 'tourist_orders.created_at' ]) ->where('m_earnings.m_user_id', 0) - ->where('m_earnings.m_id',$merchant_id) + ->where('m_earnings.m_id', $merchant_id) ->where('tourist_orders.pay_status', '<>', 0) ->orderBy('tourist_orders.id', 'desc') ->paginate(); @@ -1437,9 +1461,9 @@ class EarningController extends Controller $user = MerchantUser::where('id', $order->account_id) ->first(); $can_withdraw_count = MEarning::where('m_order_id', $order->id) - ->where('m_id', $merchant_id) - ->where('m_user_id', 0) - ->sum('value') ?? 0; + ->where('m_id', $merchant_id) + ->where('m_user_id', 0) + ->sum('value') ?? 0; $order->can_withdraw_count = $order->price; if ($can_withdraw_count > 0) $order->can_withdraw_count = $can_withdraw_count; @@ -1520,12 +1544,12 @@ class EarningController extends Controller $name = $order->name; $order->avatar = User::systemLogo; } - if($order->type == 'transfer') { - $earning = Mearning::where('m_order_id',$order->id)->first(); - $m_id = MerchantTransferLog::where('m_earning_id',$earning->id)->value('m_id'); - $anchor = Anchor::where('m_id',$m_id)->first(); - $name = $anchor ? $anchor->name : '匿名用户'; - $order->text = '商家'.$anchor->name.' 向你发起转账'.$order->price.'元'; + if ($order->type == 'transfer') { + $earning = Mearning::where('m_order_id', $order->id)->first(); + $m_id = MerchantTransferLog::where('m_earning_id', $earning->id)->value('m_id'); + $anchor = Anchor::where('m_id', $m_id)->first(); + $name = $anchor ? $anchor->name : '匿名用户'; + $order->text = '商家' . $anchor->name . ' 向你发起转账' . $order->price . '元'; $order->avatar = $anchor ? $anchor->pic : User::DefaultAvatar; } $order->name = $name; @@ -1624,20 +1648,28 @@ class EarningController extends Controller $merchant_id = $request->account_id; $merchant = MerchantAccount::find($merchant_id); $anchor = Anchor::where('m_id', $merchant_id)->first(); - if (!$merchant) return $this->failure('账户信息有误,请重新登录'); - if(empty($merchant->poundage)) throw new \Exception('商家提现手续费异常.商家id='.$merchant_id); + if (!$merchant) + return $this->failure('账户信息有误,请重新登录'); + if (empty($merchant->poundage)) + throw new \Exception('商家提现手续费异常.商家id=' . $merchant_id); //用户当前提现账号 $transfer = MEarningTransfers::where('id', $request->transfer_id)->first(); - if (!$transfer) return $this->failure('提现账户数据有误'); - if($transfer->m_id != $merchant_id || $transfer->m_user_id != 0) throw new \Exception('商家提现数据有误.商家id='.$merchant_id.' transfer_id='.$transfer->id); + if (!$transfer) + return $this->failure('提现账户数据有误'); + if ($transfer->m_id != $merchant_id || $transfer->m_user_id != 0) + throw new \Exception('商家提现数据有误.商家id=' . $merchant_id . ' transfer_id=' . $transfer->id); //提现多少钱 $cashout_num = $request->cashout_num; - if (!is_numeric($cashout_num) || $cashout_num <= 0) return $this->failure('请输入正确的提现金额'); - if ($cashout_num < 1) return $this->failure('单笔提现最低金额为1元'); + if (!is_numeric($cashout_num) || $cashout_num <= 0) + return $this->failure('请输入正确的提现金额'); + if ($cashout_num < 1) + return $this->failure('单笔提现最低金额为1元'); $account = MEarningAccount::where('m_id', $merchant_id)->where('m_user_id', 0)->first(); - if(!$account) return $this->failure('你暂无提现额度'); + if (!$account) + return $this->failure('你暂无提现额度'); //查询余额足够? - if ($account->balance < $cashout_num) return $this->failure('提现金额不能大于可以提现的总金额!'); + if ($account->balance < $cashout_num) + return $this->failure('提现金额不能大于可以提现的总金额!'); $trade_no = \CommonUtilsService::getTradeNO(); // $poundage = Redis::get('withdrawal_poundage') ?? 2; //提现手续费百分比 $poundage = $merchant->poundage; @@ -1665,7 +1697,7 @@ class EarningController extends Controller $insert['bank_code'] = $transfer->bank_code; } //发票凭证 - if($request->invoice && !empty($request->invoice)){ + if ($request->invoice && !empty($request->invoice)) { $insert['invoice'] = $request->invoice; } $account->save(); @@ -1684,7 +1716,8 @@ class EarningController extends Controller ]; SendTemplateMsg::dispatch($data)->onQueue('template_message'); $openid = $merchant->openid; - if (empty($openid)) $openid = $anchor->openid; + if (empty($openid)) + $openid = $anchor->openid; $data['touser'] = $openid; $data['template_id'] = 'OwXPF2dKEjPQUoGyzH944ATsJ6SgxpZ8kzB-KVVxanY'; $data['url'] = ''; @@ -1714,7 +1747,8 @@ class EarningController extends Controller { $m_id = $request->account_id; $admin_id = $request->merchant_admin_id; - if ($admin_id) return $this->failure('管理员没有权限'); + if ($admin_id) + return $this->failure('管理员没有权限'); $jump_url = urlencode(env('APP_URL') . '/h5/#/bindWeChatPage'); $share_url = env('APP_URL') . '/api/official/live/wechat/oauth?merchant_id=' . $m_id . '&url=' . $jump_url . '&auth_state=0'; $share_qrcode = $this->getPreviewQrcode($share_url); @@ -1792,37 +1826,47 @@ class EarningController extends Controller } //商户转账给商户或用户 - public function transferAccounts(Request $request){ + public function transferAccounts(Request $request) + { try { - if($request->merchant_admin_id) return $this->failure('暂不支持管理员发起转账'); + if ($request->merchant_admin_id) + return $this->failure('暂不支持管理员发起转账'); $mobile = trim($request->mobile); $name = trim($request->name); $amount = $request->amount; $remark = trim($request->remark) ?? null; - if(!$mobile || !$name) return $this->failure('请完善手机号码或名字'); - if(!is_numeric($amount) || $amount <= 0) return $this->failure('请输入正确的转账金额'); - if(empty($remark)) return $this->failure('请填写转账原因'); + if (!$mobile || !$name) + return $this->failure('请完善手机号码或名字'); + if (!is_numeric($amount) || $amount <= 0) + return $this->failure('请输入正确的转账金额'); + if (empty($remark)) + return $this->failure('请填写转账原因'); //输入手机号和姓名,根据手机号优先转给商家,未找到对应用户/商家,转账失败 - $transfer_object = MerchantAccount::with('anchorV2')->whereHas('anchorV2')->where('mobile',$mobile)->first(); - if(!$transfer_object){ - $transfer_object = MerchantUser::where('mobile',$mobile)->first(); - }else{ - if($transfer_object->id == $request->account_id) return $this->failure('无法转账给自己'); - if($transfer_object->anchorV2->mobile != $transfer_object->mobile) throw new \Exception('接收转账商家账号异常,号码:'.$request->mobile); + $transfer_object = MerchantAccount::with('anchorV2')->whereHas('anchorV2')->where('mobile', $mobile)->first(); + if (!$transfer_object) { + $transfer_object = MerchantUser::where('mobile', $mobile)->first(); + } else { + if ($transfer_object->id == $request->account_id) + return $this->failure('无法转账给自己'); + if ($transfer_object->anchorV2->mobile != $transfer_object->mobile) + throw new \Exception('接收转账商家账号异常,号码:' . $request->mobile); } - if(!$transfer_object) return $this->failure('未查询到需要转账的用户,请确认后重新发起转账'); + if (!$transfer_object) + return $this->failure('未查询到需要转账的用户,请确认后重新发起转账'); //当前商家收益账号 $account = MerchantAccount::find($request->account_id); $m_earning_account = $account->getMEarningAccount(); - if($m_earning_account->balance <= 0) return $this->failure('余额不足'); - if($amount > $m_earning_account->balance) return $this->failure('余额不足,最多转'.$m_earning_account->balance.'元'); + if ($m_earning_account->balance <= 0) + return $this->failure('余额不足'); + if ($amount > $m_earning_account->balance) + return $this->failure('余额不足,最多转' . $m_earning_account->balance . '元'); DB::beginTransaction(); - $m_earning_account->decrement('balance',$amount); - $m_earning_account->decrement('total_value',$amount); + $m_earning_account->decrement('balance', $amount); + $m_earning_account->decrement('total_value', $amount); $trade_no = \CommonUtilsService::getTradeNO(); $order = TouristOrder::create([ 'price' => $request->amount, - 'pay_type' =>'wechat', + 'pay_type' => 'wechat', 'type' => 'transfer', 'pay_status' => 4, 'trade_no' => $trade_no, @@ -1831,28 +1875,29 @@ class EarningController extends Controller 'mobile' => $mobile, 'desc' => $remark, ]); - if($transfer_object->anchorV2){//商家 + if ($transfer_object->anchorV2) {//商家 $insert1['recive_m_id'] = $transfer_object->id; $insert1['recive_m_user_id'] = 0; $insert1['nickname'] = $transfer_object->anchorV2->name; //增加收益记录 - $result = $transfer_object->addEarning('transfer',$amount,$order->id); + $result = $transfer_object->addEarning('transfer', $amount, $order->id); $order->merchant_id = $transfer_object->id; $order->account_id = null; - }else{//用户 + } else {//用户 $insert1['recive_m_id'] = 0; $insert1['recive_m_user_id'] = $transfer_object->id; $insert1['nickname'] = $transfer_object->nickname; //增加收益记录 - $result = $transfer_object->addEarning($request->account_id,'transfer',$amount,$order->id); + $result = $transfer_object->addEarning($request->account_id, 'transfer', $amount, $order->id); $order->merchant_id = null; $order->account_id = $transfer_object->id; } - if($result === false) throw new \Exception('增加收益记录异常,号码:'.$request->mobile); + if ($result === false) + throw new \Exception('增加收益记录异常,号码:' . $request->mobile); $order->save(); //增加转账记录 - $insert2 = ['m_id'=>$request->account_id,'amount'=>$amount,'mobile'=>$mobile,'m_earning_id'=>$result->id,'name'=>$name,'remark'=>$remark,'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')]; - $insert = array_merge($insert1,$insert2); + $insert2 = ['m_id' => $request->account_id, 'amount' => $amount, 'mobile' => $mobile, 'm_earning_id' => $result->id, 'name' => $name, 'remark' => $remark, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')]; + $insert = array_merge($insert1, $insert2); MerchantTransferLog::insert($insert); DB::commit(); return $this->success('ok'); @@ -1864,36 +1909,38 @@ class EarningController extends Controller } //转账记录 - public function merchantTransferLog(Request $request){ + public function merchantTransferLog(Request $request) + { try { $keyword = trim($request->keyword); - $logs = MerchantTransferLog::where('m_id',$request->account_id); - if($keyword){ - $logs = $logs->where(function($sql) use($keyword){ - $sql->where('name','like',"%$keyword%") - ->orWhere('nickname','like',"%$keyword%") - ->orWhere('mobile','like',"%$keyword%"); + $logs = MerchantTransferLog::where('m_id', $request->account_id); + if ($keyword) { + $logs = $logs->where(function ($sql) use ($keyword) { + $sql->where('name', 'like', "%$keyword%") + ->orWhere('nickname', 'like', "%$keyword%") + ->orWhere('mobile', 'like', "%$keyword%"); }); } - $logs = $logs->orderBy('id','desc')->paginate(); - return $this->success('ok',$logs); + $logs = $logs->orderBy('id', 'desc')->paginate(); + return $this->success('ok', $logs); } catch (\Exception $e) { $this->getError($e); return $this->failure(); } } //下载转账 - public function merchantTransferExport(Request $request){ + public function merchantTransferExport(Request $request) + { try { $start_time = $request->start_time; $end_time = $request->end_time; - if(!$start_time){ + if (!$start_time) { $start_time = Carbon::now()->addMonth(-1)->toDateString(); } - if(!$end_time){ + if (!$end_time) { $end_time = date('Y-m-d'); } - $logs = MerchantTransferLog::where('m_id',$request->account_id)->whereBetween("created_at", [$start_time, $end_time])->orderBy('id','desc')->get(); + $logs = MerchantTransferLog::where('m_id', $request->account_id)->whereBetween("created_at", [$start_time, $end_time])->orderBy('id', 'desc')->get(); $total = 0; foreach ($logs as $log) { $total += $log->amount;