keyword; $merchant_id = $request->account_id; $type = $request->type; $sharer = $request->sharer; $type_id = $request->type_id; $result = MEarning::/*where('m_id',$merchant_id)->*/ wherenotin('type', ['system', 'reward']) ->whereHas('order', function ($sql) use ($merchant_id) { $sql->where('merchant_id', $merchant_id); }) ->when($type, function ($query) use ($type) { $query->where('type', $type); }) ->when($sharer, function ($query) use ($sharer) { $query->where('sharer', $sharer); }) ->when($type_id, function ($query) use ($type_id, $type) { if ($type_id) { if ($type == 'activity' || $type == 'service') $type = 'community'; $ids = TouristOrder::where('type', $type) ->where('type_id', $type_id) ->where('pay_status', '<>', 0) ->pluck('id'); $query->wherein('m_order_id', $ids); } }) ->when($keyword, function ($query) use ($keyword) { $keyword = trim($keyword); $query->whereHas('order.mUser', function ($sql) use ($keyword) { $sql->where('nickname', 'like', '%' . $keyword . '%'); }) ->orWhereHas('order', function ($sql) use ($keyword) { $sql->where('mobile', 'like', '%' . $keyword . '%') ->orWhere('desc', 'like', '%' . $keyword . '%'); }); }) ->where('type', '<>', 'enter') ->orderBy('id', 'desc') ->paginate(); foreach ($result as $key => $value) { if ($value->user) { $value->sharer_user_name = $value->user->nickname ?? '匿名用户'; $value->sharer_user_pic = $value->user->pic ?? User::DefaultAvatar; $value->sharer_user_mobile = $value->user->mobile ?? ''; } else { $user = MerchantAccount::where('id', $value->m_id) ->first(); $value->sharer_user_name = $user->anchorV2->name ?? '匿名用户'; $value->sharer_user_pic = $user->anchorV2->pic ?? User::DefaultAvatar; $value->sharer_user_mobile = $user->mobile ?? ''; } $value->pay_user_mobile = $value->order->mobile ?? ''; $payUser = 0; if ($value->order) { $payUser = MerchantUser::where('id', $value->order->account_id) ->first(); } $value->pay_user_mobile = $value->order->mobile ?? ''; $value->title = $value->order->desc ?? ''; $value->order_price = $value->order->price ?? 0; // $value->pay_user_name =$payUser->nickname??'匿名用户'; // $value->pay_user_pic =$payUser->pic??User::DefaultAvatar; $value->pay_user_name = !empty($payUser) ? $payUser->nickname : '匿名用户'; $value->pay_user_pic = !empty($payUser) ? $payUser->pic : User::DefaultAvatar; switch ($value->sharer) { case 'first_sharer': $value->sharer = '首邀'; break; case 'other_sharer': $value->sharer = '间接'; break; case 'last_sharer': $value->sharer = '促成'; break; case 'merchant': $value->sharer = '商家'; break; case 'channel': $value->sharer = '渠道'; break; default: break; } unset($value->user); unset($value->order); } return $this->success('ok', $result); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } public function earnings(Request $request) { try { $type = $request->type; $merchant_id = $request->account_id; if ($type == 'activity' || $type == 'service') { $type = 'community'; } $service_type = 0; if ($request->type_id) { $merchant_service = MerchantService::where('type', $type) ->where('type_id', $request->type_id) ->first(); if ($merchant_service && $merchant_service->service_type == 1) { $service_type = 1; } else { $service_type = 0; } } $earnings = DB::table('m_earnings') ->selectRaw( "*, CASE sharer WHEN 'merchant' THEN 1 WHEN 'last_sharer' THEN 2 WHEN 'other_sharer' THEN 3 WHEN 'first_sharer' THEN 4 WHEN 'channel' THEN 5 WHEN 'first_line' THEN 6 WHEN 'second_line' THEN 7 WHEN 'agent_merchant' THEN 8 ELSE '' END share_order, CASE sharer WHEN 'merchant' THEN '商家' WHEN 'last_sharer' THEN '促成' WHEN 'other_sharer' THEN '间接' WHEN 'first_sharer' THEN '首邀' WHEN 'channel' THEN '渠道' WHEN 'first_line' THEN '直接' WHEN 'second_line' THEN '间接' WHEN 'agent_merchant' THEN '佣金' ELSE '' END sharer" ) ->where('m_id', $merchant_id) ->whereNull("deleted_at") ->orderBy('m_order_id', 'desc') ->orderBy('share_order', 'asc') ->orderBy('sub_ratio', 'desc'); $orders = DB::table('tourist_orders') ->when($type, function ($query) use ($type) { $query->where('type', $type); }) ->when($request->type_id, function ($query) use ($request, $service_type) { $query->where('type_id', $request->type_id); if ($service_type) { $query->where('merchant_id', $request->account_id); } }) ->whereIn('pay_status', [1, 4]) ->whereNotNull('merchant_id') ->whereNotNull('account_id'); if (!$request->type_id) { $orders = $orders->where('merchant_id', $request->account_id); } $orders = $orders->leftJoin('merchant_users', 'tourist_orders.account_id', '=', 'merchant_users.id') ->selectRaw('price as order_price, ufutx_tourist_orders.mobile as pay_user_mobile, type, type_id, account_id, ufutx_tourist_orders.id as order_id, nickname as pay_user_name,ufutx_tourist_orders.goods, ufutx_merchant_users.pic as pay_user_pic, ufutx_merchant_users.id as user_id, ufutx_tourist_orders.desc as title'); $keyword = $request->input('keyword'); if ($keyword) { $keyword = trim($keyword); $orders = $orders->where(function ($sql) use ($keyword) { $sql->where('merchant_users.nickname', 'like', '%' . $keyword . '%') ->orWhere('tourist_orders.mobile', 'like', '%' . $keyword . '%') ->orWhere('tourist_orders.desc', 'like', '%' . $keyword . '%'); }); } $earnings = $earnings->joinSub($orders, 'ufutx_o', function ($join) { $join->on('m_earnings.m_order_id', '=', 'o.order_id'); }) ->paginate(16); //商家服务id $service_ids = CommunityActivity::where('merchant_id', $request->account_id) ->where('class', 'many')->pluck('id') ->toArray();; foreach ($earnings as $earning) { if ($earning->m_user_id) { $user = MerchantUser::find($earning->m_user_id); $earning->sharer_user_name = $user->nickname ?? '匿名用户'; $earning->sharer_user_pic = $user->pic ?? User::DefaultAvatar; $earning->sharer_user_mobile = $user->mobile ?? ''; } else { $user = MerchantAccount::where('id', $earning->m_id)->first(); $earning->sharer_user_name = $user->anchorV2->name ?? '匿名用户'; $earning->sharer_user_pic = $user->anchorV2->pic ?? User::DefaultAvatar; $earning->sharer_user_mobile = $user->mobile ?? ''; } if ($earning->type == 'community') { if (in_array($earning->type_id, $service_ids)) { $earning->type = 'service'; } else { $earning->type = 'activity'; } } } return $this->success('ok', $earnings); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } public function advanceEarnings(Request $request) { try { $type = $request->type; $merchant_id = $request->account_id; if ($type == 'activity' || $type == 'service') { $type = 'community'; } $earnings = DB::table('m_advance_earnings') ->selectRaw( "*, CASE sharer WHEN 'merchant' THEN 1 WHEN 'last_sharer' THEN 2 WHEN 'other_sharer' THEN 3 WHEN 'first_sharer' THEN 4 WHEN 'channel' THEN 5 ELSE '' END share_order, CASE sharer WHEN 'merchant' THEN '商家' WHEN 'last_sharer' THEN '促成' WHEN 'other_sharer' THEN '间接' WHEN 'first_sharer' THEN '首邀' WHEN 'channel' THEN '渠道' ELSE '' END sharer" ) ->where('m_id', $merchant_id) ->whereNull('deleted_at') ->orderBy('m_order_id', 'desc') ->orderBy('share_order', 'asc') ->orderBy('sub_ratio', 'desc'); $orders = DB::table('tourist_orders') ->when($type, function ($query) use ($type) { $query->where('type', $type); }) ->when($request->type_id, function ($query) use ($request) { $query->where('type_id', $request->type_id); }) ->whereIn('pay_status', [1, 4]) ->whereNotNull('merchant_id') ->whereNotNull('account_id') ->where('merchant_id', $request->account_id); $orders = $orders->leftJoin('merchant_users', 'tourist_orders.account_id', '=', 'merchant_users.id') ->selectRaw('price as order_price, ufutx_tourist_orders.mobile as pay_user_mobile, type, type_id, account_id, ufutx_tourist_orders.id as order_id, nickname as pay_user_name,ufutx_tourist_orders.goods, ufutx_merchant_users.pic as pay_user_pic, ufutx_merchant_users.id as user_id, ufutx_tourist_orders.desc as title'); $keyword = $request->input('keyword'); if ($keyword) { $keyword = trim($keyword); $orders = $orders->where(function ($sql) use ($keyword) { $sql->where('merchant_users.nickname', 'like', '%' . $keyword . '%') ->orWhere('tourist_orders.mobile', 'like', '%' . $keyword . '%') ->orWhere('tourist_orders.desc', 'like', '%' . $keyword . '%'); }); } $earnings = $earnings->joinSub($orders, 'ufutx_o', function ($join) { $join->on('m_advance_earnings.m_order_id', '=', 'o.order_id'); }) ->paginate(); //商家服务id $service_ids = CommunityActivity::where('merchant_id', $request->account_id)->where('class', 'many')->pluck('id')->toArray(); foreach ($earnings as $earning) { if ($earning->m_user_id) { $user = MerchantUser::find($earning->m_user_id); $earning->sharer_user_name = $user->nickname ?? '匿名用户'; $earning->sharer_user_pic = $user->pic ?? User::DefaultAvatar; $earning->sharer_user_mobile = $user->mobile ?? ''; } else { $user = MerchantAccount::where('id', $earning->m_id)->first(); $earning->sharer_user_name = $user->anchorV2->name ?? '匿名用户'; $earning->sharer_user_pic = $user->anchorV2->pic ?? User::DefaultAvatar; $earning->sharer_user_mobile = $user->mobile ?? ''; } if ($earning->type == 'community') { if (in_array($earning->type_id, $service_ids)) { $earning->type = 'service'; } else { $earning->type = 'activity'; } } } return $this->success('ok', $earnings); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 分成详情 * * * @param Request $request * @return JsonResponse|string */ public function earningDetail(Request $request) { try { $id = $request->id; $MEarning = MEarning::where('id', $id) ->first(); $MEarning->sharer_user_name = $MEarning->user->nickname ?? '匿名用户'; $MEarning->sharer_user_pic = $MEarning->user->pic ?? User::DefaultAvatar; $MEarning->sharer_user_mobile = $MEarning->user->mobile ?? ''; $MEarning->pay_user_pic = User::DefaultAvatar; $MEarning->pay_user_mobile = $MEarning->order->mobile ?? ''; $payUser = MerchantUser::where('id', $MEarning->order->account_id) ->first(); $MEarning->pay_user_mobile = $MEarning->order->mobile ?? ''; $MEarning->title = $MEarning->order->desc ?? ''; $MEarning->pay_user_name = $payUser->nickname ?? '匿名用户'; unset($MEarning->order); unset($MEarning->user); return $this->success('ok', $MEarning); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 提现记录 * @param Request $request * @return JsonResponse|string */ public function MyWithdraws(Request $request) { try { $merchant_id = $request->account_id; $result = MEarningwithdraws::where('m_id', $merchant_id) ->where('m_user_id', 0) ->paginate(); return $this->success('ok', $result); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } // 收益规则 public function earningRule(Request $request) { try { $merchant_id = $request->account_id; $rules = $request->rules; foreach ($rules as $key => $value) { 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%"); $result = MEarningRules::where('m_id', $merchant_id) ->where('type_id', 0) ->where('name', $key) ->first(); if (!$result) { MEarningRules::create([ 'ratio' => $value['ratio'], 'forzen_time' => $value['forzen_time'], 'm_id' => $merchant_id, 'name' => $key, ]); } else { $result->ratio = $value['ratio']; $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]); } return $this->success('ok'); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 获取所有的收益规则 * @param Request $request * @return JsonResponse|string */ public function getAllEarningRule(Request $request) { try { $service_arr = ['service', 'activity', 'course', 'consult', 'shop', 'evaluate']; $merchant_id = $request->account_id; $rules = MEarningRules:: where('m_id', $merchant_id) ->where('type_id', 0) ->get(); $result = $rules->toarray(); if (!$result) { foreach ($service_arr as $service) { $data[$service]['ratio'] = 0.3; $data[$service]['forzen_time'] = 1; $data[$service]['first_sharer'] = 0.3; $data[$service]['other_sharer'] = 0.2; $data[$service]['last_sharer'] = 0.5; $data[$service]['is_first_sharer'] = 1; $data[$service]['is_other_sharer'] = 1; $data[$service]['is_last_sharer'] = 1; } return $this->success('ok', $data); } $data = []; foreach ($rules as $key => $value) { $data[$value->name]['ratio'] = $value->ratio ?? 0.3; $data[$value->name]['forzen_time'] = $value->forzen_time ?? 1; $data[$value->name]['first_sharer'] = $value->first_sharer; $data[$value->name]['other_sharer'] = $value->other_sharer; $data[$value->name]['last_sharer'] = $value->last_sharer; $data[$value->name]['is_first_sharer'] = $value->is_first_sharer; $data[$value->name]['is_other_sharer'] = $value->is_other_sharer; $data[$value->name]['is_last_sharer'] = $value->is_last_sharer; } return $this->success('ok', $data); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } public function earningRules(Request $request) { try { $m_id = $request->account_id; $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(); if (empty($rules)) { $rules = []; $services = MerchantAccount::EARNINGSERVICE; foreach ($services as $service) { $rules[$service]['ratio'] = 0; $rules[$service]['forzen_time'] = 1; } } break; case "grade": $rules = MEarningGrade::where('m_id', $m_id)->orderBy('amount', 'asc')->get(); break; case 'agent': break; default: break; } return $this->success('ok', compact('earning_rule', 'rules')); }catch (Exception $e) { $this->getError($e); return $this->failure(); } } /** * 商家选择分成规则 * @param Request $request * @return JsonResponse|string */ public function updateEarningRule(Request $request) { try { $m_id = $request->account_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) { $this->getError($e); return $this->failure(); } } /** * 创建等级收益规则 * @param Request $request * @return JsonResponse|string */ public function storeGradeRule(Request $request) { try { $m_id = $request->account_id; $title = $request->input('title'); 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]); return $this->success('ok', $grade); }catch (Exception $e) { $this->getError($e); return $this->failure(); } } /** * 修改等级收益规则 * @param Request $request * @param $grade_id * @return JsonResponse|string */ 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){ $grade->title = $request->title; } 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("操作失败,已设置同价位的规则"); $grade->amount = $request->amount; } if ($request->has('ratio') && $request->ratio != $grade->ratio){ $grade->ratio = $request->ratio; } 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) { $this->getError($e); return $this->failure(); } } /** * 等级规则详情 * @param Request $request * @param $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("操作失败,该收益规则不能存在"); return $this->success('ok', $grade); } catch (Exception $e) { $this->getError($e); return $this->failure(); } } /** * 删除等级收益规则 * @param Request $request * @param $grade_id * @return JsonResponse|string */ public function deleteGradeRule(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("操作失败,该收益规则不能存在"); $result = $grade->delete(); return $this->success('ok', $result); } catch (Exception $e) { $this->getError($e); return $this->failure(); } } /** * 获取某个类型的收益规则 * @param Request $request * @return JsonResponse|string */ public function getTypeEarningRule(Request $request) { try { $type = $request->type; $merchant_id = $request->account_id; $mEarningRules = MEarningRules::select('name', 'ratio', 'forzen_time') ->where('m_id', $merchant_id) ->where('name', $type) ->where('type_id', 0) ->first(); return $this->success('ok', $mEarningRules); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 获取提现账号 * @param Request $request * @return JsonResponse|string */ public function GetTransferAccount(Request $request) { try { $merchant_id = $request->account_id; $account = MEarningAccount::where('m_id', $merchant_id) ->where('m_user_id', 0) ->first(); $result = MEarningTransfers::where('m_id', $merchant_id) ->where('m_user_id', 0) ->paginate(); foreach ($result as $key => $value) { //可提现金额 $can_cash_out_amount = 0; if ($account) { $can_cash_out_amount = $account->balance; } $value->can_cash_out_amount = $can_cash_out_amount; if ($value->way == 'bank') { $value->nick_name = $value->company_name; } } return $this->success('ok', $result); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 新增提现账号 * @param Request $request * @return JsonResponse|string */ public function AddTransferAccount(Request $request) { try { //todo 验证用户支付宝帐号 $merchant_id = $request->account_id; $merchant_admin_id = $request->merchant_admin_id; if ($merchant_admin_id) { return $this->failure('当前用户非商户创建者,不能新增提现帐号'); } $status = MEarningTransfers::where('m_id', $request->account_id) ->where('m_user_id', 0) ->exists(); $MEarningTransfers = new MEarningTransfers(); $MEarningTransfers->way = $request->way; $MEarningTransfers->m_id = $merchant_id; $MEarningTransfers->account = $request->account; $MEarningTransfers->name = $request->name; $MEarningTransfers->bank_name = $request->bank_name; $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('暂不支持该银行'); } $MEarningTransfers->in_use = $status ? 0 : 1; $MEarningTransfers->save(); return $this->success('ok', $MEarningTransfers); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 更新提现账号 * @param Request $request * @return JsonResponse|string */ public function updateTransferAccount(Request $request) { try { $id = $request->id; $merchant_id = $request->account_id; $merchant = MerchantAccount::where('id', $merchant_id) ->first(); if (!$merchant) { return $this->failure('当前用户非商户创建者,不能新增提现帐号'); } $MEarningTransfers = MEarningTransfers::where('id', $id) ->where('m_id', $merchant_id)->where('m_user_id', 0) ->first(); if (!$MEarningTransfers) { return $this->failure('该记录不存在或已删除'); } 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('暂不支持该银行'); } $MEarningTransfers->way = $request->way ?? $MEarningTransfers->way; $MEarningTransfers->m_id = $merchant_id ?? $MEarningTransfers->merchant_id; $MEarningTransfers->account = $request->account ?? $MEarningTransfers->account; $MEarningTransfers->name = $request->name ?? $MEarningTransfers->name; $MEarningTransfers->in_use = $request->in_use ?? $MEarningTransfers->in_use; $MEarningTransfers->bank_name = $request->bank_name ?? $MEarningTransfers->bank_name; $MEarningTransfers->company_name = $request->company_name ?? $MEarningTransfers->company_name; $MEarningTransfers->code = $request->code ?? $MEarningTransfers->code; $MEarningTransfers->bank_code = $request->bank_code ?? $MEarningTransfers->bank_code; $MEarningTransfers->save(); //如果设置为已选中,更新其他账户数据 if ($request->in_use == 1) { MEarningTransfers::where('m_id', $merchant_id) ->where('m_user_id', 0) ->wherenotin('id', [$id]) ->where('in_use', 1) ->update(['in_use' => 0]); } return $this->success('ok', $MEarningTransfers); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 扫码获取微信信息 * @param Request $request * @return \Illuminate\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ public function BindWechat(Request $request) { try { $code = $request->code; //获取access_token $appid = 'wxd33d710f3ff66dc4'; $secret = 'd2e370ef0f7c1e7a2e00bf1ddbfdd8b6'; $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" . $secret . "&code=" . $code . "&grant_type=authorization_code"; $result = $this->http_request($url); $merchant_id = $request->merchant_id ?? 0; $url = 'https://love.ufutx.com/pu_admin/#/earningsMGT/withdrawalAccount'; if ($result && isset($result['access_token'])) { $auth_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $result['access_token'] . '&openid=' . $result['openid']; $result = $this->http_request($auth_url); $MEarningTransfers = MEarningTransfers::where('m_id', $merchant_id) ->where('way', 'weixin') ->where('account', $result['openid']) ->first(); if ($MEarningTransfers) return redirect($url); $MEarningTransfers = new MEarningTransfers(); $MEarningTransfers->m_id = $merchant_id; $MEarningTransfers->m_user_id = 0; $MEarningTransfers->way = 'weixin'; $MEarningTransfers->account = $result['openid']; // $MEarningTransfers->name = $result['nickname']; $MEarningTransfers->nick_name = $result['nickname']; $MEarningTransfers->pic = $result['headimgurl'] ?? User::DefaultAvatar; $MEarningTransfers->save(); } return redirect($url); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * HTTP请求(支持HTTP/HTTPS,支持GET/POST) * @param $url * @param null $data * @return mixed|string */ private function http_request($url, $data = null) { try { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)) { curl_setopt($curl, CURLOPT_POST, TRUE); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); $output = curl_exec($curl); curl_close($curl); return json_decode($output, true); } catch (\Exception $e) { return ''; } } /** * 删除提现账号 * @param Request $request * @return JsonResponse|string */ public function deleteTransferAccount(Request $request) { try { $id = $request->id; $merchant_id = $request->account_id; $MEarningTransfers = MEarningTransfers::where('id', $id) ->where('m_id', $merchant_id) ->where('m_user_id', 0) ->first(); if (!$MEarningTransfers) { return $this->failure('该记录不存在或已删除'); } $MEarningTransfers->delete(); return $this->success('ok'); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 提现详情 * @param Request $request * @return JsonResponse|string */ public function withdrawInfo(Request $request) { try { $merchant_id = $request->account_id; $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){ $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')); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 收益列表 v2 * @param Request $request * @return JsonResponse|string */ public function incomeList(Request $request) { try { $merchant_id = $request->account_id; $keyword = $request->keyword; $nopage = $request->nopage; $start_time = $request->start_time; $end_time = $request->end_time; $data = []; $data['can_cash_out_amount'] = 0; $data['can_withdraw_total'] = 0; $data['used_withdraw_total'] = 0; $data['frozen_withdraw'] = 0; $result = MEarningAccount::where('m_id', $merchant_id) ->where('m_user_id', 0) ->first(); if ($result) { //已提现 $data['used_withdraw_total'] = $result->withdrawl ?? 0; // 可提现收益 $data['can_cash_out_amount'] = $result->balance ?? 0; //总收益 $data['can_withdraw_total'] = $result->total_value ?? 0; $data['frozen_withdraw'] = $result->frozen_withdraw ?? 0; } // $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 . '%') // ->orWhere('mobile', 'like', '%' . $keyword . '%'); // }); // } // // $result = TouristOrder::with('mEarning') // // if($start_time){ // $result = $result->where('created_at','>=',$start_time.' 00:00:01'); // } // if($end_time){ // $result = $result->where('created_at','<=',$end_time.' 23:59:59'); // } $result = 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); }) ->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'); // $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); // if ($request->has('keyword')) { $result = $result->where(function ($query) use ($keyword) { $query->where('name', 'like', '%' . $keyword . '%') ->orWhere('mobile', 'like', '%' . $keyword . '%'); }); } // if($start_time){ // $result = $result->where('tourist_orders.created_at','>=',$start_time.' 00:00:01'); // } // if($end_time){ // $result = $result->where('tourist_orders.created_at','<=',$end_time.' 23:59:59'); // } // $result = $result->where('m_earnings.m_id',$merchant_id) // ->where('tourist_orders.pay_status', '<>', 0) // ->orderBy('tourist_orders.id', 'desc'); // if($nopage){ $result = $result->get(); }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; $value->can_withdraw_count = $value->price; if ($can_withdraw_count >= 0) $value->can_withdraw_count = $can_withdraw_count; if ($value->type == 'community') { $activity = CommunityActivity::withTrashed() ->where('id', $value->type_id) ->first(); if ($activity->class == 'one') { $goods = CommunityActivity::withTrashed() ->where('id', $value->type_id) ->where('class', 'one') ->value('title'); $value->goods = '购买了活动《' . $goods . "》"; } } if ($value->type == 'community') { $service = CommunityActivity::withTrashed() ->where('id', $value->type_id) ->first(); if ($service->class == 'many') { $goods = CommunityActivity::withTrashed() ->where('id', $value->type_id) ->where('class', 'many')->value('title'); $value->goods = '购买了服务《' . $goods . "》"; } } if ($value->type == 'course') { $goods = Course::withTrashed() ->where('id', $value->type_id) ->value('title'); $value->goods = '购买了课程《' . $goods . "》"; } if ($value->type == 'shop') { $goods = MerchantShop::withTrashed() ->where('id', $value->type_id) ->value('title'); $addres = ShopRecive::where('order_id', $value->id) ->value('address'); $value->goods = '购买了商品《' . $goods . "》"; $value->address = $addres; } if ($value->type == 'system') { $value->goods = '系统转账'; } if ($value->type == 'consult') { $goods = Consultation::withTrashed() ->where('id', $value->type_id) ->value('title'); $value->goods = '购买了咨询《' . $goods . "》"; } if ($value->type == 'member') { $goods = SaasMemberLevel::where('id', $value->type_id) ->value('level_title'); $value->goods = '购买了VIP《' . $goods . "》"; } if ($value->type == 'reward_info') { $user = MerchantUser::where('id', $value->account_id) ->first(); $goods = MerchantInformation::withTrashed() ->where('id', $value->type_id) ->value('title'); $value->goods = '赞赏了资讯《' . $goods . "》"; $value->name = $user->nickname ?? '匿名用户'; $value->mobile = $user->mobile ?? ''; } if ($value->type == 'reward_activity') { $user = MerchantUser::where('id', $value->account_id) ->first(); $goods = CommunityActivity::withTrashed() ->where('id', $value->type_id) ->value('title'); $value->goods = '赞赏了活动《' . $goods . "》"; $value->name = $user->nickname ?? '匿名用户'; $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 :'匿名用户'; $value->mobile = $anchor ? $anchor->mobile : ''; $value->goods = '商家'.$anchor->name.'发起转账'; } } $data['orders'] = $result; // $data['poundage'] = Redis::get('withdrawal_poundage') ?? 2; $data['poundage'] = MerchantAccount::where('id',$request->account_id)->value('poundage'); return $this->success('ok', $data); } catch (\Exception $e) { $this->getError($e); return $this->failure($e->getMessage()); } } /** * 收入列表导出 * @param Request $request * @return JsonResponse|BinaryFileResponse */ public function OrderEarningExport(Request $request){ try { $start_time = $request->start_time; $end_time = $request->end_time; if(!$start_time){ $start_time = Carbon::now()->addMonth(-1)->toDateString(); } 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); }) ->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', '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; $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); $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']); $order_list[$key]['mobile'] = $anchor ? $anchor->mobile : $order_list[$key]['mobile']; $order_list[$key]['goods'] = '商家'.($anchor->name??'').'发起转账'; } } $file_name = '系统统计收款表.xls'; if ($start_time && $end_time) { $file_name = '系统统计收款表-' . $start_time . '-' . $end_time . '.xls'; } else if ($start_time && !$end_time) { $file_name = '系统统计收款表-' . $start_time . '-' . date('Y:m:d') . '.xls'; } elseif (!$start_time && $end_time) { $file_name = '系统统计收款表-' . $end_time . '.xls'; } //return Excel::download(new TouristOrdersEarningExport($order_list), $file_name); $list = new TouristOrdersEarningExport($order_list); return $this->success('ok',$list->collection_a()); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } // 过滤掉emoji表情 function filterEmoji($str){ $str = preg_replace_callback('/./u', function (array $match) { return strlen($match[0]) >= 4 ? '' : $match[0]; }, $str); return $str; } /** * 提现 * //1.綁定支付宝或微信 * //2.选择提现到支付宝或微信 * //3.成功 或 失败 * //3.1 成功: 增加提现记录 发送通知 * //3.2 失败: 发送通知 * @param Request $request * @return JsonResponse|string */ public function transferToAccount(Request $request) { try { $account_id = $request->account_id; if ($account_id == 32) { return $this->failure('提现功能维护中,请稍后再试!'); } $account = MerchantAccount::where('id', $account_id) ->first(); if (empty($account)) { return $this->failure('提现失败,请联系开发人员'); } $openid = $account->openid; $cashout_num = $request->cashout_num;//需要提现的金额 $earning_account = MEarningAccount::where('m_id', $account_id) ->where('m_user_id', 0) ->first(); if (!is_numeric($cashout_num) || $cashout_num <= 0) { return $this->failure('请输入正确的提现金额'); } if ($cashout_num < 1) { return $this->failure('单笔提现最低金额为1元'); } $poundage = Redis::get('withdrawal_poundage') ?? 2; //提现手续费百分比 $poundage = $cashout_num * ($poundage / 100) >= 0.01 ? $cashout_num * ($poundage / 100) : 0.01;//收取手续费费用 不足1分 按一分处理 $poundage = floor($poundage * 100) / 100; $actual_received = $cashout_num - $poundage;//实际到账金额 if (!$earning_account || $cashout_num > $earning_account->balance) { return $this->failure('提现金额不能大于可以提现的总金额!'); } $cash_out_type = $request->input('cash_out_type'); if (empty($cash_out_type)) { return $this->failure('请选择您需要提现的方式'); } $anchor_name = $earning_account->anchor?$earning_account->anchor->name:''; $trade_no = \CommonUtilsService::getTradeNO(); if ($cash_out_type == 'wechat') { //微信提现 //判断是否绑定微信 if (empty($openid)) return $this->failure('绑定微信账号失败'); //绑定完成 提现 $desc = "<$anchor_name>提现-微信"; // $wechat = new WechatService($this->sms); // $app_id = config('wechat.app_id'); $result = \WechatService::officialUserTransferV2($trade_no, $openid, $actual_received * 100, $desc);//成功返回result = null if ($result) { return $this->failure($result['err_code_des']); } else { $anchor = Anchor::where('openid', $account->openid) ->first(); MEarningwithdraws::create([ 'm_id' => $request->account_id, 'm_user_id' => 0, 'way' => 'weixin', 'value' => $cashout_num, 'real_value' => $actual_received, 'account' => $account->openid, 'name' => $anchor->name ?? '未获取', 'trade_no' => $trade_no, 'status' => 'finished', ]); $earning_account->balance = $earning_account->balance - $cashout_num; $earning_account->withdrawl = $earning_account->withdrawl + $cashout_num; $earning_account->save(); return $this->success('ok'); } } else {//支付宝提现 //判断是否绑定支付宝 $transfer_id = $request->transfer_id; $transfer = MEarningTransfers::where('id', $transfer_id) ->first(); if (!$transfer) return $this->failure('提现账户数据有误'); $data = []; $desc = "<$anchor_name>提现-支付宝"; $data['remark'] = $desc; //提现备注 $data['out_biz_no'] = $trade_no; $data['amount'] = $actual_received; if ($transfer->alipay_id) { $data['payee_account'] = $alipay_account = $transfer->alipay_id;//支付宝账号 $alipay_real_name = $transfer->nick_name; //支付宝昵称 $ali = new LiveAlipayService(); $result = $ali->UserTransferAccount($data); } else { $data['payee_account'] = $alipay_account = $transfer->account;//支付宝账号 $data['payee_real_name'] = $alipay_real_name = $transfer->name;//支付宝绑定姓名 $ali = new LiveAlipayService(); $result = $ali->platTransferAccount($data); } $url = ''; if (is_array($result)) { // 短信通知 $mobile = '15707534403'; $message = '商户' . $alipay_real_name . ' 提现' . $cashout_num . '元失败,原因:' . $result['msg']; $this->sentMessage($mobile, $message); //模板通知 邓智锋 $data['touser'] = ['oPC_2vnSECnYp5p--uaq3rca3Ry0', 'oPC_2vpJd34uN2E1tTsFbf8Lhlcs']; $data['template_id'] = 'AqwVt0liVmQfzfnX3ZGvmVOdOh62nkCbhlOUI0NVQGs'; $data['url'] = $url; $data['data'] = [ 'first' => '商户' . $alipay_real_name . '提现失败', 'keyword1' => $cashout_num . '元', 'keyword2' => '支付宝', 'keyword3' => $result['msg'], 'remark' => '点击查看提现记录', ]; $fianl = MEarningwithdraws::create([ 'm_id' => $request->account_id, 'm_user_id' => 0, 'way' => 'alipay', 'value' => $cashout_num, 'real_value' => $actual_received, 'account' => $alipay_account, 'name' => $alipay_real_name, 'trade_no' => $trade_no, 'status' => 'canceled', 'err_msg' => $result['msg'], ]); SendTemplateMsg::dispatch($data)->onQueue('template_message'); $content = '失败原因:' . $result['msg']; SaasNotice::addRecord($request->account_id, 0, 'withdraw', $fianl->id, $content, 1); if ($result['msg'] == '余额不足,建议尽快充值。后续可登录电脑端支付宝,自主设置余额预警提醒功能。') { $fianl->status = 'freezing'; $fianl->save(); return $this->success('ok', ['status' => 0]); } else { return $this->failure($result['msg'], ['status' => 2]); } //return $this->failure($result['msg']); } else {//提现成功 $fianl = MEarningwithdraws::create([ 'm_id' => $request->account_id, 'm_user_id' => 0, 'way' => 'alipay', 'value' => $cashout_num, 'real_value' => $actual_received, 'account' => $alipay_account, 'name' => $alipay_real_name, 'trade_no' => $trade_no, 'status' => 'finished', ]); //增加提现通知记录 $content = '已成功提现' . $actual_received . '元到支付宝余额,请在你的提现支付宝查收'; SaasNotice::addRecord($request->account_id, 0, 'withdraw', $fianl->id, $content, 1); $earning_account->balance = $earning_account->balance - $cashout_num; $earning_account->withdrawl = $earning_account->withdrawl + $cashout_num; $earning_account->save(); //模板通知 $data['touser'] = $openid; $data['template_id'] = 'aV4ic7jr5bOlf55CgR0jmMsFYyhdRAiVmqqXEjnUqQU'; $data['url'] = $url; $data['data'] = [ 'first' => '提现已到账,请在支付宝查收', 'keyword1' => $actual_received . '元', 'keyword2' => date('Y-m-d H:i:s'), 'keyword3' => '提现到支付宝余额', 'remark' => '感谢您的的使用', ]; SendTemplateMsg::dispatch($data)->onQueue('template_message'); } return $this->success('ok', ['status' => 1]); } } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 提现列表 * @param Request $request * @return JsonResponse|string */ public function cashOutLogs(Request $request) { try { $start_time = $request->start_time; $end_time = $request->end_time; $merchant_id = $request->account_id; $nopage = $request->nopage; $result = MEarningwithdraws::whereIn('status', ['finished', 'freezing', 'canceled']) // ->select('id', 'way', 'real_value', 'value', 'status', 'created_at', 'err_msg', 'name', 'account') ->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'); }) ->when($end_time, function ($query) use ($end_time) { $query->where('created_at', '<=', $end_time.' 23:59:59'); }) ->orderBy('id', 'desc'); if($nopage){ $result = $result->get(); }else{ $result = $result ->paginate(); } foreach ($result as $key => $value) { $value->poundage = number_format($value->value - $value->real_value, 2) ?? 0; $value->actual_received = floatval($value->real_value); $value->amount = floatval($value->value); $value->type = $value->way; 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{ $value->pic = null; } if($value->way == 'bank'){ $value->bankName = DB::table('banks')->where('code',$value->bank_code)->value('name'); }else{ $value->bankName = null; } } return $this->success('ok', $result); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 移动端后台收益列表 * @param Request $request * @return JsonResponse|string */ public function phoneIncomeList(Request $request) { try { $merchant_id = $request->account_id; $data = []; // $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') // ->paginate(); $result = Db::table('m_earnings') ->join('tourist_orders', 'm_earnings.m_order_id', '=', 'tourist_orders.id') // ->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' ]) ->where('m_earnings.m_user_id', 0) ->where('m_earnings.m_id',$merchant_id) ->where('tourist_orders.pay_status', '<>', 0) ->orderBy('tourist_orders.id', 'desc') ->paginate(); foreach ($result as $key => $order) { $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; $order->can_withdraw_count = $order->price; if ($can_withdraw_count > 0) $order->can_withdraw_count = $can_withdraw_count; if ($order->type == 'community') { $activity = CommunityActivity::withTrashed() ->where('id', $order->type_id) ->first(); if ($activity->class == 'one') { $goods = CommunityActivity::withTrashed() ->where('id', $order->type_id) ->where('class', 'one') ->value('title'); $text = $order->created_at . ' 购买了 《' . $goods . '》'; $order->text = $text; } } if ($order->type == 'community') { $service = CommunityActivity::withTrashed() ->where('id', $order->type_id) ->first(); if ($service->class == 'many') { $goods = CommunityActivity::withTrashed() ->where('id', $order->type_id) ->where('class', 'many') ->value('title'); $text = $order->created_at . ' 购买了 《' . $goods . '》'; $order->text = $text; } } if ($order->type == 'course') { $goods = Course::withTrashed() ->where('id', $order->type_id) ->value('title'); $text = $order->created_at . ' 购买了 《' . $goods . '》'; $order->text = $text; } if ($order->type == 'shop') { $goods = MerchantShop::withTrashed() ->where('id', $order->type_id) ->value('title'); $addres = ShopRecive::where('order_id', $order->id) ->value('address'); $text = $order->created_at . ' 购买了 《' . $goods . '》'; $order->text = $text; $order->address = $addres; } if ($order->type == 'consult') { $goods = Consultation::withTrashed() ->where('id', $order->type_id) ->value('title'); $order->text = $order->created_at . ' 购买了 《' . $goods . '》'; } if ($order->type == 'reward_info') { $goods = MerchantInformation::withTrashed() ->where('id', $order->type_id) ->value('title'); $order->text = '赞赏了资讯《' . $goods . "》"; // $order->name = $user->nickname??'匿名用户'; $order->mobile = $user->mobile ?? ''; } if ($order->type == 'reward_activity') { $goods = CommunityActivity::withTrashed() ->where('id', $order->type_id) ->value('title'); $order->text = '赞赏了活动《' . $goods . "》"; // $order->name = $user->nickname??'匿名用户'; $order->mobile = $user->mobile ?? ''; } if ($order->type == 'member') { $goods = SaasMemberLevel::where('id', $order->type_id)->value('level_title'); $order->text = $order->created_at . ' 购买了 《' . $goods . '》'; } $order->avatar = $user->pic ?? User::DefaultAvatar; $name = $user->nickname ?? '匿名用户'; if ($order->type == 'system') { $order->text = '系统转账'; $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.'元'; $order->avatar = $anchor ? $anchor->pic : User::DefaultAvatar; } $order->name = $name; //unset($order->mUser); } return $this->success('ok', $result); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 分成详情 * @param Request $request * @param $order_id * @return JsonResponse|string */ public function earningData(Request $request, $order_id) { try { $order = TouristOrder::with('mEarning', 'mEarning.user')->where('id', $order_id)->first(); if (empty($order)) { return $this->failure('未查询到该订单'); } if ($order->type == 'community') {//服务 $good = CommunityActivity::withTrashed()->where('id', $order->type_id)->first(); $order->title = $good->title; $order->pic = $good->pic; } elseif ($order->type == 'course') { $good = Course::withTrashed()->where('id', $order->type_id)->first(); $order->title = $good->title; $order->pic = $good->thumb; } elseif ($order->type == 'shop') { $good = MerchantShop::withTrashed()->where('id', $order->type_id)->first(); $order->title = $good->title; $order->pic = $good->icon; } elseif ($order->type == 'consult') { $good = Consultation::withTrashed()->where('id', $order->type_id)->first(); $order->title = $good->title; $order->pic = $good->pic; } elseif ($order->type == 'reward_info') { $good = MerchantInformation::withTrashed()->where('id', $order->type_id)->first(); $order->title = $good->title; $order->pic = $good->pic; } elseif ($order->type == 'reward_activity') { $good = CommunityActivity::withTrashed()->where('id', $order->type_id)->first(); $order->title = $good->title; $order->pic = $good->pic; } $order->income_amount = MEarning::where('m_order_id', $order_id)->where('m_user_id', 0)->sum('value') ?? 0.00; if ($order->mEarning) { foreach ($order->mEarning as $key => $value) { if ($value->sharer == 'first_sharer') { $value->sharer = '首邀'; } if ($value->sharer == 'last_sharer') { $value->sharer = '促成'; } if ($value->sharer == 'other_sharer') { $value->sharer = '间接'; } if ($value->sharer == 'merchant') { $value->sharer = '商家'; } if ($value->sharer == 'channel') { $value->sharer = '渠道'; } if ($value->user) { $value->chat_way = $value->user ? $value->user->mobile : '未获取';//联系方式 $value->share_name = $value->user ? $value->user->nickname : '匿名用户'; } else { $user = MerchantAccount::where('id', $value->m_id)->first(); $value->chat_way = $user->anchorV2->mobile ?? '未获取';//联系方式 $value->share_name = $user->anchorV2->name ?? '匿名用户'; } unset($value->user); } } return $this->success('ok', $order); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 提交提现申请 * * * @param Request $request * @return JsonResponse|string */ public function withdrawApply(Request $request) { try { $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); //用户当前提现账号 $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); //提现多少钱 $cashout_num = $request->cashout_num; 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->balance < $cashout_num) return $this->failure('提现金额不能大于可以提现的总金额!'); $trade_no = \CommonUtilsService::getTradeNO(); // $poundage = Redis::get('withdrawal_poundage') ?? 2; //提现手续费百分比 $poundage = $merchant->poundage; $poundage = $cashout_num * ($poundage / 100) >= 0.01 ? $cashout_num * ($poundage / 100) : 0.01;//收取手续费费用 不足1分 按一分处理 $poundage = floor($poundage * 100) / 100; $actual_received = $cashout_num - $poundage;//实际到账金额 //处理冻结金额 DB::beginTransaction(); $account->balance = $account->balance - $cashout_num; $account->frozen_withdraw = $account->frozen_withdraw + $cashout_num; $insert = ['m_id' => $transfer->m_id, 'm_user_id' => $transfer->m_user_id, 'way' => $transfer->way, 'real_value' => $actual_received, 'value' => $cashout_num, 'trade_no' => $trade_no, 'status' => 'freezing', 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')]; if ($transfer->way == 'alipay') {//支付宝提现 if ($transfer->alipay_id) { $insert['alipay_id'] = $transfer->alipay_id; } else { $insert['account'] = $transfer->account; $insert['name'] = $transfer->name; } } elseif ($transfer->way == 'weixin') {//微信提现 $insert['account'] = $transfer->account; $insert['name'] = $transfer->nick_name; } elseif ($transfer->way == 'bank') { $insert['account'] = $transfer->account; $insert['name'] = $transfer->name; $insert['bank_code'] = $transfer->bank_code; } //发票凭证 if($request->invoice && !empty($request->invoice)){ $insert['invoice'] = $request->invoice; } $account->save(); MEarningwithdraws::insert($insert); DB::commit(); $data['touser'] = ['oPC_2vudVLVHj2U7dNinr2IEDHR4', 'oPC_2vuTj7YRgUzQQY7PlSJVLBBc']; $data['template_id'] = 'OwXPF2dKEjPQUoGyzH944ATsJ6SgxpZ8kzB-KVVxanY'; $data['url'] = ''; $data['data'] = [ 'first' => '商户:' . $anchor->name . '刚刚提交了提现申请,请即时处理', 'keyword1' => $cashout_num . '元', 'keyword2' => '提现申请提交', 'keyword3' => '请求提交时间' . date('Y-m-d H:i'), 'keyword4' => '福恋后台审核', 'remark' => '提现申请提交', ]; SendTemplateMsg::dispatch($data)->onQueue('template_message'); $openid = $merchant->openid; if (empty($openid)) $openid = $anchor->openid; $data['touser'] = $openid; $data['template_id'] = 'OwXPF2dKEjPQUoGyzH944ATsJ6SgxpZ8kzB-KVVxanY'; $data['url'] = ''; $data['data'] = [ 'first' => '你已申请提现,正在审核中', 'keyword1' => $anchor->name, 'keyword2' => date('Y-m-d H:i:s'), 'keyword3' => $actual_received . '元', 'keyword4' => $transfer->way == 'alipay' ? '支付宝' : '微信零钱', 'remark' => '审核结果请留意服务通知', ]; SendTemplateMsg::dispatch($data)->onQueue('template_message'); //短信通知 $message = $anchor->name . ',你已申请提现,正在审核中,审核结果请留意手机短信,请耐心等待。'; $this->sentMessage($merchant->mobile, $message); return $this->success('ok'); } catch (\Exception $e) { DB::rollBack(); $this->getError($e); return $this->failure('服务器休息,请稍后再试'); } } //授权码 public function authQrcode(Request $request) { $m_id = $request->account_id; $admin_id = $request->merchant_admin_id; 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); return $this->success('ok', $share_qrcode); } //添加对公转账 public function addPublicAccount(Request $request) { if ($request->merchant_admin_id) { return $this->failure('管理员没有权限'); } try { $m_id = $request->account_id; $account = $request->account; $bank_name = $request->bank_name; $company_name = $request->company_name; $code = $request->code; MEarningTransfers::create([ 'm_id' => $m_id, 'm_user_id' => 0, 'way' => 'bank', 'account' => $account, 'bank_name' => $bank_name, 'company_name' => $company_name, 'code' => $code, ]); return $this->success('ok'); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } public function banks(Request $request) { try { $keyword = trim($request->keyword); $banks = DB::table('banks')->select('name', 'code'); if ($keyword) { $banks = $banks->where('name', 'like', "%$keyword%"); } $banks = $banks->get(); return $this->success('ok', $banks); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * m_earning表添加type_id字段并填充数据 * @param Request $request */ public function syncEarningTypeId(Request $request) { $earnings_ids = MEarning::all(); foreach ($earnings_ids as $earning) { if ($earning->type == 'activity' || $earning->type == 'service') { $type = 'community'; } else { $type = $earning->type; } $type_id = TouristOrder::where('id', $earning->m_order_id) ->where('type', $type) ->value('type_id'); if ($type_id) { $earning->type_id = $type_id; $earning->save(); } } return $this->success('ok'); } //商户转账给商户或用户 public function transferAccounts(Request $request){ try { 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('请填写转账原因'); //输入手机号和姓名,根据手机号优先转给商家,未找到对应用户/商家,转账失败 $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('未查询到需要转账的用户,请确认后重新发起转账'); //当前商家收益账号 $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.'元'); DB::beginTransaction(); $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', 'type' => 'transfer', 'pay_status' => 4, 'trade_no' => $trade_no, 'withdrawal_radio' => 100, 'name' => $name, 'mobile' => $mobile, 'desc' => $remark, ]); 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); $order->merchant_id = $transfer_object->id; $order->account_id = null; }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); $order->merchant_id = null; $order->account_id = $transfer_object->id; } 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); MerchantTransferLog::insert($insert); DB::commit(); return $this->success('ok'); } catch (\Exception $e) { DB::rollBack(); $this->getError($e); return $this->failure(); } } //转账记录 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 = $logs->orderBy('id','desc')->paginate(); return $this->success('ok',$logs); } catch (\Exception $e) { $this->getError($e); return $this->failure(); } } //下载转账 public function merchantTransferExport(Request $request){ try { $start_time = $request->start_time; $end_time = $request->end_time; if(!$start_time){ $start_time = Carbon::now()->addMonth(-1)->toDateString(); } 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(); $total = 0; foreach ($logs as $log) { $total += $log->amount; $log->total = $total; } return $this->success("ok", $logs); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } }