merchant_user_id ?? 0; $type = $request->type; $result = MEarning::where('m_user_id', $merchant_user_id)->where('m_id', $request->merchant_id); //->where('value', '>', 0); if ($type) { $result = $result->where('type', $type); } else { $result = $result->where('type', '<>', 'enter'); } $result = $result->orderBy('id', 'desc')->paginate(); foreach ($result as $key => $value) { // $value->title = $value->order->desc ?? $value->order->goods; $title = (!empty($value->order) && !empty($value->order->desc)) ? $value->order->desc:($value->order->goods??'未获取'); $time = Date('Y-m-d', $value->created_at->timestamp); if ($value->sharer == 'first_sharer') { $value->title = '你推荐注册的' . $value->nickname . ',' . $time . ' 购买了【' . $title . '】'; } elseif ($value->sharer == 'last_sharer') { $value->title = $value->nickname . '通过你的直接分享,' . $time . ' 购买了【' . $title . '】'; } elseif ($value->sharer == 'other_sharer') { $value->title = $value->nickname . '通过你的间接分享,' . $time . ' 购买了【' . $title . '】'; } if ($value->sharer == 'channel') { $channel_id = $value->order->share_channel_id ?? 0; $share_title = MerchantShareChannel::where('id', $channel_id)->value('title') ?? '渠道推广'; $value->title = $value->nickname . '通过你的渠道【' . $share_title . '】购买了【' . $title . '】'; } if (isset($type) && $type = 'enter') { $merchant = MerchantAccount::where('id', $value->m_order_id)->first(); $value->nickname = $merchant->anchorV2->name ?? ''; $value->title = '商户' . $value->nickname . '入驻,获取奖励' . $value->value . '元'; } else { $account_id = $value->order->account_id ?? 0; $pay_user = MerchantUsers::where('id', $account_id)->first(); $value->nickname = $pay_user->nickname ?? '匿名用户'; } $value->order_price = $value->order->price ?? 0; $value->pic = $pay_user->pic ?? User::DefaultAvatar; unset($value->order); } return $this->success('ok', $result); }catch (\Exception $e){ $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } public function communityEarnings(Request $request) { try { $merchant_user_id = $request->merchant_user_id ?? 0; $merchant_id = $request->merchant_id; // $merchant_user_id = 55; // $merchant_id = 1; $type = $request->input('type'); $status = $request->input('status'); $users = \DB::table('merchant_users')->selectRaw('id as order_user_id,nickname,pic'); $orders = \DB::table('tourist_orders')->leftJoinSub($users, 'ufutx_users', function($join) { $join->on('tourist_orders.account_id', '=', 'users.order_user_id'); })->where('pay_status', '<>', 0)->select(\DB::raw('ufutx_users.*'), 'id', 'desc', 'type_id','goods'); if ($type) { $earnings = \DB::table('m_earnings')->where('m_id', $merchant_id)->where('m_user_id', $merchant_user_id)->where('type', 'enter')->whereNull('deleted_at')->leftJoinSub($orders, 'ufutx_orders', function($join) { $join->on('m_order_id', '=', 'orders.id'); })->selectRaw('m_user_id,m_order_id, earning_time, sum(value) as value, created_at, 2 as status, ufutx_orders.*')->groupBy('m_order_id'); $value = \DB::table('m_earnings')->where('m_id', $merchant_id)->where('m_user_id', $merchant_user_id)->where('type', 'enter')->whereNull('deleted_at')->sum('value'); $earnings = $earnings->groupBy('m_order_id')->orderBy('created_at', 'desc')->paginate(); foreach ($earnings as $earning) { $text = "商家入驻,获得奖励".$earning->value.'元'; $earning->text = $text; } }else { $m_earnings = \DB::table('m_earnings')->where('m_id', $merchant_id)->where('m_user_id', $merchant_user_id)->where('type', '<>', 'enter')->whereNull('deleted_at')->leftJoinSub($orders, 'ufutx_orders', function($join) { $join->on('m_order_id', '=', 'orders.id'); })->selectRaw('m_user_id,m_order_id,type, earning_time, sum(value) as value, created_at, 2 as status, ufutx_orders.*')->groupBy('m_order_id'); $earnings = \DB::table('m_advance_earnings')->where('m_id', $merchant_id)->where('m_user_id', $merchant_user_id)->whereNull('deleted_at')->union($m_earnings)->leftJoinSub($orders, 'ufutx_orders', function($join) { $join->on('m_order_id', '=', 'orders.id'); })->selectRaw('m_user_id,m_order_id,type, earning_time, sum(value) as value, created_at, 1 as status, ufutx_orders.*'); $value1 = \DB::table('m_earnings')->where('m_id', $merchant_id)->where('m_user_id', $merchant_user_id)->where('type', '<>', 'enter')->whereNull('deleted_at')->sum('value'); $value2 = \DB::table('m_advance_earnings')->where('m_id', $merchant_id)->where('m_user_id', $merchant_user_id)->whereNull('deleted_at')->sum('value'); $value = $value1 + $value2; $earnings = $earnings->groupBy('m_order_id')->orderBy('created_at', 'desc')->paginate(); foreach ($earnings as $earning) { if($earning->type == 'transfer'){ $m_earning = MEarning::where('m_order_id',$earning->m_order_id)->first(); $transfer_m_id = MerchantTransferLog::where('m_earning_id',$m_earning->id)->value('m_id'); $transfer_merchant_name = Anchor::where('m_id',$transfer_m_id)->value('name'); $text = '商家'.$transfer_merchant_name.'在 '.$earning->created_at.',向你发起了一笔转账,金额:'.$earning->value.'元'; }else{ $goods_name = $earning->desc?:$earning->goods; $text = $earning->nickname."通过你的分享,".$earning->created_at.'购买了【'.$goods_name.'】'; } $earning->text = $text; } } return $this->success('ok', compact('earnings', 'value')); }catch (\Exception $e) { $this->getError($e); return $this->failure("服务器休息中,请稍后再试"); } } /** * 收益信息 * * * @param Request $request * @return JsonResponse|string */ public function EarningsInfo(Request $request) { try { $merchant_user_id = $request->merchant_user_id ?? 0; $merchant_id = $request->merchant_id; $wechatUser = session('wechat.oauth_user.new'); if ($wechatUser) { $openid = $wechatUser->getId(); } else { $openid = MerchantUsers::where('id', $merchant_user_id)->value('openid'); } $data = []; $data['total_price'] = 0; $data['can_cash_out_price'] = 0; $MEarning_account = MEarningAccount::where('m_id', $request->merchant_id)->where('m_user_id', $merchant_user_id) ->first(); if ($MEarning_account) { $data['total_price'] = $MEarning_account->total_value; $data['can_cash_out_price'] = $MEarning_account->balance; } $member = 0; if ($openid){ // $member = TouristOrder::where('from_openid', $openid)->whereIn('pay_status', [1, 4]) // ->where('merchant_id', $request->merchant_id) // ->where('pay_type', 'wechat') // ->where('price', '>', 0) // ->groupby('account_id') // ->get() // ->count(); $member = AccessRecord::where('recommender',$openid) ->where('account_id',$merchant_id) ->count(); } // 商家推广人数 $merchant_member = MEarning::where('m_id', $merchant_id)->where('m_user_id', $merchant_user_id) ->where('type', 'enter')->groupBy('m_order_id') ->count(); $data['member'] = $member; $data['merchant_member'] = $merchant_member; $account = MEarningTransfers::where('m_user_id', $merchant_user_id)->first(); $data['account'] = $account->account ?? ''; $data['name'] = $account->name ?? ''; $jump_url = urlencode(env('APP_URL') . '/pu/#/information'); $url = env('APP_URL') . '/api/official/live/wechat/FamilyAuth?merchant_id=' . $merchant_id . '&url=' . $jump_url . '&from_openid=' . $openid; $key = 'qr_code' . $merchant_id . 'Recommend' . $merchant_user_id; $qr_code = Redis::get($key); if (!$qr_code) { $qr_code = $this->getPreviewQrcode($url); Redis::setex($key, 60 * 60 * 24 * 30, $qr_code); $qr_code = Redis::get($key); } $jump_url = urlencode(env('APP_URL') . '/pu/#/recruitingTeacher'); $enter_url = env('APP_URL') . '/api/official/live/wechat/FamilyAuth?merchant_id=' . $merchant_id . '&url=' . $jump_url . '&from_openid=' . $openid; $key = 'qr_code' . $merchant_id . 'Teacher' . $merchant_user_id; $enter_qr_code = Redis::get($key); if (!$enter_qr_code) { $enter_qr_code = $this->getPreviewQrcode($enter_url); Redis::setex($key, 60 * 60 * 24 * 30, $enter_qr_code); $enter_qr_code = Redis::get($key); } $data['url'] = $url; $data['qr_code'] = $qr_code; $data['enter_url'] = $enter_url; $data['enter_qr_code'] = $enter_qr_code; $data['poundage'] = Redis::get('withdrawal_poundage') ?? 2; return $this->success('ok', $data); }catch (\Exception $e){ $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } public function earningAccount(Request $request) { try { // return $this->failure("系统维护中"); $merchant_user_id = $request->merchant_user_id ?? 0; Log::info("earningAccount $merchant_user_id"); $merchant_id = $request->merchant_id; //手续费比例百分比 $poundage = Redis::get('withdrawal_poundage') ?? 2; //可提现金额 $earning_acount = MEarningAccount::where('m_id', $merchant_id)->where('m_user_id', $merchant_user_id)->first(); if (empty($earning_acount)) { $earning_acount = MEarningAccount::firstOrCreate([ 'm_id'=>$merchant_id, 'm_user_id'=>$merchant_user_id ]); } if ($earning_acount->is_banned) throw new \Exception("账号id: {$earning_acount->id}异常"); //冻结金额 $frezzing_value = \DB::table('m_advance_earnings')->where('m_id', $merchant_id)->where('m_user_id', $merchant_user_id)->whereNull('deleted_at')->sum('value'); if($earning_acount) { //总收益 $total_value = number_format($earning_acount->total_value + $frezzing_value, 2, '.', ''); //已提现 $withdraw_value = $earning_acount->withdrawl; //可提现 $balance_value = $earning_acount->balance; } else { //总收益 $total_value = $frezzing_value; //已提现 //可提现 $withdraw_value = $balance_value = 0; } //当前提现账号 $account = MEarningTransfers::where('m_id', $merchant_id)->where('m_user_id', $merchant_user_id)->select('id','m_id','m_user_id', 'account', 'name','nick_name','pic','way','created_at','updated_at')->orderBy('updated_at','desc')->first(); return $this->success("ok", compact('poundage', 'balance_value','withdraw_value','total_value','account', 'frezzing_value')); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 提现记录 * @param Request $request * @return JsonResponse|string */ public function MyWithdraws(Request $request) { try { $merchant_id = $request->merchant_id ?? 0; $merchant_user_id = $request->merchant_user_id ?? 0; $result = MEarningwithdraws::whereIn('status', ['finished', 'freezing'])->where('m_id', $merchant_id)->where('m_user_id', $merchant_user_id)->paginate(); return $this->success('ok', $result); }catch (\Exception $e){ $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 提现信息 * @param Request $request * @return JsonResponse|string */ public function WithdrawsInfo(Request $request) { try { $result = MEarningAccount::where('m_user_id', $request->merchant_user_id)->first(); $user = MerchantUsers::where('id', $request->merchant_user_id)->first(); $result->nickname = $user->nickname; $result->pic = $user->pic ?? User::DefaultAvatar; return $this->success('ok', $result); }catch (\Exception $e){ $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 绑定支付账户 * @param Request $request * @return JsonResponse|string */ public function BindEarningAccounts(Request $request) { try { $merchant_id = $request->merchant_id; $MEarningTransfers = MEarningTransfers::where('m_id', $merchant_id)->where('m_user_id', $request->merchant_user_id)->first(); if (!$MEarningTransfers) { $MEarningTransfers = new MEarningTransfers(); $MEarningTransfers->way = $request->way; $MEarningTransfers->m_id = $merchant_id; $MEarningTransfers->m_user_id = $request->merchant_user_id; $MEarningTransfers->account = $request->account; $MEarningTransfers->name = $request->name; $MEarningTransfers->save(); } else { $MEarningTransfers->way = $request->way ?? $MEarningTransfers->way; $MEarningTransfers->account = $request->account ?? $MEarningTransfers->account; $MEarningTransfers->name = $request->name ?? $MEarningTransfers->name; $MEarningTransfers->updated_at = date('Y-m-d H:i:s'); $MEarningTransfers->save(); } return $this->success('ok', $MEarningTransfers); }catch (\Exception $e){ $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 提现 * * * @param Request $request * @return JsonResponse|string */ public function UserWithdrawal(Request $request) { try { $merchant_id = $request->merchant_id; $merchant_user_id = $request->merchant_user_id ?? 0; $account = $request->account; $way = $request->way; $alipay_real_name = $request->name; $amount = $request->amount; $data = []; // 查询用户的提现余额是否充足 $earning_accounts = MEarningAccount::where('m_user_id', $merchant_user_id)->where('m_id', $merchant_id)->first(); if (!$earning_accounts) return $this->failure('您暂无提现额度'); if ($amount < 0.1) return $this->failure('提现额度有误,请重新输入'); if ($earning_accounts->balance < $amount) return $this->failure('提现额度不足,请重新输入'); $poundage = Redis::get('withdrawal_poundage') ?? 2; //提现手续费百分比 $poundage = $amount * ($poundage / 100) >= 0.01 ? $amount * ($poundage / 100) : 0.01;//收取手续费费用 不足1分 按一分处理 $poundage = floor($poundage * 100) / 100; $actual_received = $amount - $poundage;//实际到账金额 $trade_no = \CommonUtilsService::getTradeNO(); $data['payee_account'] = $account;//支付宝账号 $data['payee_real_name'] = $alipay_real_name;//支付宝绑定姓名 $data['remark'] = '提现已到账'; //提现备注 $data['out_biz_no'] = $trade_no; $data['amount'] = $actual_received; $ali = new LiveAlipayService(); $result = $ali->platTransferAccount($data); if (is_array($result)) { // 短信通知 $mobile = '15707534403'; $message = 'S端用户' . $alipay_real_name . ' 提现' . $amount . '元失败,原因:' . $result['msg']; Message::create([ 'phone' => $mobile, 'message' => $message, 'confirmed' => 1, 'ip' => request() ? request()->ip() : '127.0.0.1', ]); Messenger::sendSMS($mobile, $message); //模板通知 邓智锋 $data['touser'] = ['oPC_2vnSECnYp5p--uaq3rca3Ry0', 'oPC_2vpJd34uN2E1tTsFbf8Lhlcs']; $data['template_id'] = 'AqwVt0liVmQfzfnX3ZGvmVOdOh62nkCbhlOUI0NVQGs'; $data['url'] = ''; $data['data'] = [ 'first' => '', 'keyword1' => $amount . '元', 'keyword2' => '支付宝', 'keyword3' => $result['msg'], 'remark' => '点击查看提现记录', ]; $withdraw = MEarningwithdraws::create([ 'm_id' => $merchant_id, 'm_user_id' => $merchant_user_id, 'way' => $way ?? 'alipay', 'value' => $amount, 'real_value' => $actual_received, 'account' => $account, 'name' => $alipay_real_name, 'trade_no' => $trade_no, 'status' => 'canceled', 'err_msg' => $result['msg'] ]); SendTemplateMsg::dispatch($data)->onQueue('template_message'); if ($result['msg'] == '余额不足,建议尽快充值。后续可登录电脑端支付宝,自主设置余额预警提醒功能。') { $withdraw->status = 'freezing'; $withdraw->save(); return $this->success('ok', ['status' => 0]); } else { return $this->failure($result['msg'], ['status' => 2]); } } else {//提现成功 MEarningwithdraws::create([ 'm_id' => $merchant_id, 'm_user_id' => $merchant_user_id, 'way' => $way ?? 'alipay', 'value' => $amount, 'real_value' => $actual_received, 'account' => $account, 'name' => $alipay_real_name, 'trade_no' => $trade_no, 'status' => 'finished', ]); $earning_accounts->balance = $earning_accounts->balance - $amount; $earning_accounts->withdrawl = $earning_accounts->withdrawl + $amount; $earning_accounts->save(); } return $this->success('ok', ['status' => 1]); }catch (\Exception $e){ $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 提交提现申请 * * * @param Request $request * @return JsonResponse|string */ public function withdrawApply(Request $request) { try { // return $this->failure("系统维护中"); $merchant_id = $request->merchant_id; $merchant_user_id = $request->merchant_user_id ?? 0; $wechatUser = session('wechat.oauth_user.new'); if ($wechatUser) { $openid = $wechatUser->getId(); } else { $openid = MerchantUsers::where('id', $merchant_user_id)->value('openid'); } $merchant_user = MerchantUser::where('id', $merchant_user_id)->first(); // if ($merchant_user->id != 221) return $this->failure("提现功能维护中,请稍后再试"); $account = $request->account; $way = $request->way ?? 'alipay'; $alipay_real_name = $request->name; //提现多少钱 $amount = $request->amount; if (!is_numeric($amount) || $amount <= 0) return $this->failure('请输入正确的提现金额'); if ($amount < 1) return $this->failure('单笔提现最低金额为1元'); $earning_account = MEarningAccount::where('m_id', $merchant_id)->where('m_user_id', $merchant_user_id)->first(); if ($earning_account->is_banned) throw new \Exception("账号id: {$earning_account->id}异常"); //查询余额足够? if ($earning_account->balance < $amount) return $this->failure('提现金额不能大于可以提现的总金额!'); $trade_no = \CommonUtilsService::getTradeNO(); $poundage = Redis::get('withdrawal_poundage') ?? 2; //提现手续费百分比 $poundage = $amount * ($poundage / 100) >= 0.01 ? $amount * ($poundage / 100) : 0.01;//收取手续费费用 不足1分 按一分处理 $poundage = floor($poundage * 100) / 100; $actual_received = $amount - $poundage;//实际到账金额 //处理冻结金额 DB::beginTransaction(); $earning_account->balance = $earning_account->balance - $amount; $earning_account->frozen_withdraw = $earning_account->frozen_withdraw + $amount; $insert = ['m_id' => $merchant_id, 'm_user_id' => $merchant_user_id, 'way' => $way, 'real_value' => $actual_received, 'value' => $amount, 'trade_no' => $trade_no, 'account' => $account, 'name' => $alipay_real_name, 'status' => 'freezing']; $earning_account->save(); $withdraw = MEarningwithdraws::create($insert); if($amount >= 500) { //通知 $data['touser'] = ['oPC_2vudVLVHj2U7dNinr2IEDHR4', 'oPC_2vuTj7YRgUzQQY7PlSJVLBBc']; $data['template_id'] = 'OwXPF2dKEjPQUoGyzH944ATsJ6SgxpZ8kzB-KVVxanY'; $data['url'] = ''; $data['data'] = [ 'first' => 's端用户:' . $alipay_real_name . '刚刚提交了提现申请,请即时处理', 'keyword1' => $amount . '元', 'keyword2' => '提现申请提交', 'keyword3' => '请求提交时间' . date('Y-m-d H:i'), 'keyword4' => '福恋后台审核', 'remark' => '提现申请提交', ]; SendTemplateMsg::dispatch($data)->onQueue('template_message'); $data['touser'] = $openid; $data['template_id'] = 'OwXPF2dKEjPQUoGyzH944ATsJ6SgxpZ8kzB-KVVxanY'; $data['url'] = ''; $data['data'] = [ 'first' => '你已申请提现,正在审核中', 'keyword1' => $alipay_real_name, 'keyword2' => date('Y-m-d H:i:s'), 'keyword3' => $actual_received . '元', 'keyword4' => '支付宝', 'remark' => '审核结果请留意服务通知', ]; SendTemplateMsg::dispatch($data)->onQueue('template_message'); //短信通知 $message = $alipay_real_name . ',你已申请提现,正在审核中,审核结果请留意手机短信,请耐心等待。'; $this->sentMessage($merchant_user->mobile, $message); }else { //直接转账 list($res, $error_msg) = $this->transferWithdraw($withdraw); //失败 if (empty($res)) return $this->success('ok', $error_msg); //修改账号状态 $earning_account->decrement('frozen_withdraw', $amount); $earning_account->increment('withdrawl', $amount); $withdraw->update(['status'=>'finished']); //成功通知 $this->sendWithdrawNotice($earning_account, $withdraw); } DB::commit(); return $this->success('ok', ['status' => 1]); } catch (\Exception $e) { DB::rollBack(); $this->getError($e); return $this->failure('服务器休息,请稍后再试'); } } public function sendWithdrawNotice($account, $withdraw) { $openid = MerchantUser::where('id', $account->m_user_id)->value('openid'); $way = ''; switch ($withdraw->way) { case 'weixin': $way = "微信"; break; case "alipay": $way = '支付宝'; break; } //模板通知 $data['touser'] = $openid; $data['template_id'] = 'aV4ic7jr5bOlf55CgR0jmMsFYyhdRAiVmqqXEjnUqQU'; $data['url'] = ''; $data['data'] = [ 'first' => '提现已到账,请在支付宝查收', 'keyword1' => $withdraw->real_value . '元', 'keyword2' => date('Y-m-d H:i:s'), 'keyword3' => $way.'提现成功', 'remark' => '感谢您的的使用', ]; SendTemplateMsg::dispatch($data)->onQueue('template_message'); } /** * 体现转账 * @param $withdraw */ public function transferWithdraw($withdraw) { $trade_no = $withdraw->trade_no; $account = $withdraw->account; $real_value = $withdraw->real_value; $anchor_name = $withdraw->anchor?$withdraw->anchor->name:''; $desc = "<$anchor_name>SAAS用户提现"; $res = true; $error_msg = ''; switch ($withdraw->way) { case 'alipay': $ali = new LiveAlipayService(); $data['payee_account'] = $account;//支付宝账号 $data['payee_real_name'] = $withdraw->name;//支付宝绑定姓名 $data['remark'] = $desc; //提现备注 $data['out_biz_no'] = $trade_no; $data['amount'] = $real_value; $result = $ali->platTransferAccount($data); if (is_array($result)) { $res = false; $error_msg = $result['msg']; } break; case 'weixin': $result = \WechatService::officialUserTransferV2($trade_no, $account, $real_value * 100, $desc);//成功返回result = null if ($result) { $res = false; $error_msg = $result['err_code_des']??''; } break; } return [$res, $error_msg]; } //授权码 public function wechatAuthQrcode(Request $request){ try { $m_id = $request->merchant_id; $m_user_id = $request->merchant_user_id; $jump_url = urlencode(env('APP_URL').'/pu/#/IncomeWithdrawal'); $share_url = env('APP_URL') . '/api/official/live/wechat/FamilyAuth?merchant_id='.$m_id.'&url='.$jump_url.'&auth_state=0&account_id='.$m_user_id; // $share_qrcode = $this->getPreviewQrcode($share_url); return $this->success('ok',$share_url); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息,请稍后再试'); } } }