| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593 |
- <?php
- namespace App\Http\Controllers\Game;
- use App\Facade\TableName;
- use App\Game\GlobalUserInfo;
- use App\Game\Services\OuroGameService;
- use App\Http\Controllers\Controller;
- use App\Http\helper\NumConfig;
- use App\Models\Order;
- use App\Services\OrderServices;
- use App\Utility\SetNXLock;
- use App\Util;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\App;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Redis;
- class PayRechargeController extends Controller
- {
- /**
- * 设置用户语言环境
- */
- private function setUserLocale($request)
- {
- $user = $request->user();
- if ($user) {
- $locale = GlobalUserInfo::getLocaleByUserID($user->UserID, 'en');
- App::setLocale($locale);
- }
- }
- // 充值记录
- public function orderList(Request $request)
- {
- $user_id = (int)$request->globalUser->UserID;//$request->get('user_id', 1);
- $page = $request->get('page', 1);
- $pageSize = $request->get('pageSize', 7);
- $redisKey = 'PayRecharge_orderList_'.$user_id;
- if (!SetNXLock::getExclusiveLock($redisKey)) {
- return apiReturnFail(['web.withdraw.try_again_later','Tente novamente mais tarde']);
- }
- $where[] = ['user_id', $user_id];
- $where[] = ['pay_status', 1];
- $cacheTime = 60 * rand(1, 2);
- // $list = cache()->remember($user_id . '_order_list', $cacheTime, function () use ($where, $pageSize) {
- //
- // return Order::where($where)
- // ->orderBy('finished_at', 'desc')
- // ->selectRaw('amount,payment_code,order_sn as payment_sn,finished_at,created_at,pay_status')
- // ->paginate(15,['*'],'page',1);
- // });
- $list = Order::query()->where($where)
- ->orderBy('finished_at', 'desc')
- ->selectRaw('amount,payment_code,order_sn as payment_sn,finished_at,created_at,pay_status,type')
- ->paginate($pageSize);
- // ->paginate(15,['*'],'page',1);
- foreach ($list as &$val) {
- $val->amount = number_format($val->amount, 2, '.', '');
- }
- SetNXLock::release($redisKey);
- return apiReturnSuc($list);
- }
- // 首充
- public function firstPay(Request $request)
- {
- $user = $request->user();
- $user_recharge = DB::table(TableName::QPAccountsDB() . 'YN_VIPAccount')
- ->where('UserID', $user->UserID)
- ->value('Recharge') ?: 0;
- if($user_recharge)return apiReturnSuc();
- $firstPayGift = DB::table('agent.dbo.recharge_gift')
- ->where('gift_id', 301)->first();
- if(!$firstPayGift) return apiReturnFail();
- $names = DB::table('agent.dbo.admin_configs')
- ->where([
- 'type' => 'pay_method',
- 'status' => 1,
- ])
- ->selectRaw('id, name, status, new_pay_type as type')
- ->orderByDesc('sort')->get()->toArray();
- $list = DB::table('agent.dbo.recharge_gear')
- ->select('id','money','favorable_price','give')
- ->orderBy('money', 'asc')->where('status', 1)->get();
- $gear = Util::filterGearByDevice(\GuzzleHttp\json_encode($names));
- foreach ($list as &$val) {
- $val->favorable_price = $val->favorable_price + $val->give;
- $val->gear = $gear;
- $val->recommend = 0;
- if($val->money == $firstPayGift->recommend){
- $val->recommend = 1;
- }
- }
- return apiReturnSuc(compact('list', 'firstPayGift'));
- }
- // 首充礼包(带倒计时逻辑)
- public function firstPayGift(Request $request)
- {
- $user = $request->user();
-
- $this->setUserLocale($request);
-
- // 获取礼包配置
- $giftConfig = DB::table('agent.dbo.recharge_gift')
- ->where('gift_id', 301)
- ->first();
-
- if (!$giftConfig) {
- return apiReturnFail(['web.gift.config_not_exists', __('web.gift.config_not_exists')]);
- }
-
- // 检查用户是否购买了首充礼包
- $giftRecord = DB::table('agent.dbo.first_pay_gift_records')
- ->where('user_id', $user->UserID)
- ->first();
-
- // ========== 未购买礼包:返回充值列表 + 礼包配置 + 倒计时 ==========
- if (!$giftRecord) {
- // 获取充值档位列表
- $list = DB::table('agent.dbo.recharge_gear')
- ->select('money', 'favorable_price', 'give', 'gear')
- ->orderBy('money', 'asc')
- ->where('status', 1)
- ->where('in_shop', 1)
- ->get();
-
- foreach ($list as &$val) {
- $val->favorable_price = $val->favorable_price + $val->give;
- $val->recommend = 0;
- if ($val->money == $giftConfig->recommend) {
- $val->recommend = 1;
- }
- if (!empty($val->gear)) {
- $val->gear = Util::filterGearByDevice($val->gear);
- }
- }
-
- // ========== 处理倒计时逻辑(仅未购买时) ==========
- $redisKey = "first_pay_gift_timer_{$user->UserID}";
- $currentTime = time();
- $timeLeft = 0;
-
- $timerData = Redis::get($redisKey);
-
- if ($timerData) {
- $timerData = json_decode($timerData, true);
- $firstRequestTime = $timerData['first_request_time'];
- $phase = $timerData['phase'];
-
- // 阶段1:valid_h倒计时
- if ($phase === 'first') {
- $expireTime = $firstRequestTime + ($giftConfig->valid_h * 3600);
-
- if ($currentTime >= $expireTime) {
- $expireDate = date('Y-m-d', $expireTime);
- $currentDate = date('Y-m-d', $currentTime);
-
- if ($expireDate !== $currentDate) {
- // 进入阶段2
- $nextDayStart = time();
- $newExpireTime = $nextDayStart + ($giftConfig->valid_h_2 * 3600);
-
- $newTimerData = [
- 'first_request_time' => $firstRequestTime,
- 'phase' => 'daily',
- 'current_cycle_start' => $nextDayStart,
- 'last_expire_time' => $newExpireTime
- ];
- Redis::set($redisKey, json_encode($newTimerData));
-
- $timeLeft = max(0, $newExpireTime - $currentTime);
- }
- } else {
- $timeLeft = $expireTime - $currentTime;
- }
- }
- // 阶段2:每日循环
- elseif ($phase === 'daily') {
- $currentCycleStart = $timerData['current_cycle_start'];
- $expireTime = $currentCycleStart + ($giftConfig->valid_h_2 * 3600);
-
- if ($currentTime >= $expireTime) {
- $expireDate = date('Y-m-d', $expireTime);
- $currentDate = date('Y-m-d', $currentTime);
-
- if ($expireDate !== $currentDate) {
- $todayStart = time();
- $newExpireTime = $todayStart + ($giftConfig->valid_h_2 * 3600);
-
- $newTimerData = [
- 'first_request_time' => $firstRequestTime,
- 'phase' => 'daily',
- 'current_cycle_start' => $todayStart,
- 'last_expire_time' => $newExpireTime
- ];
- Redis::set($redisKey, json_encode($newTimerData));
-
- $timeLeft = max(0, $newExpireTime - $currentTime);
- }
- } else {
- $timeLeft = $expireTime - $currentTime;
- }
- }
- } else {
- // 首次请求,初始化倒计时
- $expireTime = $currentTime + ($giftConfig->valid_h * 3600);
- $timerData = [
- 'first_request_time' => $currentTime,
- 'phase' => 'first',
- 'last_expire_time' => $expireTime
- ];
- Redis::set($redisKey, json_encode($timerData));
- $timeLeft = $giftConfig->valid_h * 3600;
- }
-
- return apiReturnSuc([
- 'has_purchased' => false, // 未购买标记
- 'list' => $list, // 充值档位列表
- 'gift_info' => [
- 'gift_id' => $giftConfig->gift_id,
- 'gift_name' => $giftConfig->gift_name,
- 'total_bonus' => $giftConfig->total_bonus,
- 'bonus_instantly' => $giftConfig->bonus_instantly,
- 'day_rewards' => $giftConfig->day_rewards ? json_decode($giftConfig->day_rewards) : null,
- 'betting_bonus' => $giftConfig->betting_bonus ? json_decode($giftConfig->betting_bonus) : null,
- 'betting_task' => $giftConfig->betting_task ? json_decode($giftConfig->betting_task) : null,
- ],
- 'time_left' => $timeLeft, // ✅ 倒计时(未购买时展示)
- 'expire_at' => time() + $timeLeft // ✅ 过期时间戳
- ]);
- }
-
- // ========== 已购买礼包:返回任务进度(无倒计时) ==========
-
- // 获取用户当前累计下注次数(从RecordUserGameCount表)
- $currentTotalBetCount = DB::table(TableName::QPRecordDB() . 'RecordUserGameCount')
- ->where('UserID', $user->UserID)
- ->sum('Cnt') ?? 0;
-
- // 获取用户当前累计下注金额(从统计表,用于下注任务)
- $userStats = DB::table('QPRecordDB.dbo.RecordUserTotalStatistics')
- ->where('UserID', $user->UserID)
- ->first();
-
- $currentTotalBetAmount = $userStats->TotalBet ?? 0;
-
- // 更新下注进度到礼包记录(betting_current_bet存储下注次数,用于下注奖励)
- DB::table('agent.dbo.first_pay_gift_records')
- ->where('user_id', $user->UserID)
- ->update([
- 'betting_current_bet' => $currentTotalBetCount,
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
-
- // 重新获取最新记录
- $giftRecord = DB::table('agent.dbo.first_pay_gift_records')
- ->where('user_id', $user->UserID)
- ->first();
-
- // 解析任务数据
- $dayRewardsData = $giftRecord->day_rewards_data ? json_decode($giftRecord->day_rewards_data, true) : null;
- $bettingBonusData = $giftRecord->betting_bonus_data ? json_decode($giftRecord->betting_bonus_data, true) : null;
- $bettingTaskData = $giftRecord->betting_task_data ? json_decode($giftRecord->betting_task_data, true) : null;
-
- // 计算当前是购买后的第几天(所有任务都需要用到)
- $purchaseDate = date('Y-m-d', strtotime($giftRecord->created_at));
- $today = date('Y-m-d');
- $daysPassed = floor((strtotime($today) - strtotime($purchaseDate)) / 86400);
-
- // 构建返回数据
- $giftInfo = [
- 'gift_id' => $giftRecord->gift_id,
- 'gift_name' => $giftRecord->gift_name,
- 'total_bonus' => (float)$giftRecord->total_bonus,
- 'bonus_instantly' => (float)$giftRecord->bonus_instantly,
- 'pay_amount' => (float)$giftRecord->pay_amount,
- ];
-
- // 每日奖励进度
- if ($dayRewardsData) {
-
- $startDay = $dayRewardsData['start_day'] ?? 1;
- $bonusDay = $dayRewardsData['bonus_day'] ?? 0;
- $claimedDays = $giftRecord->day_rewards_claimed;
- // $startDay = $startDay-1;
- // 判断每日奖励状态
- // 0=不可领取, 1=可领取, 2=已领取, 3=过期
- $dayRewardStatus = 0; // 默认不可领取
-
- // 检查是否过期(购买后7天)
- if ($daysPassed >= 7) {
- $dayRewardStatus = 3; // 过期
- } elseif ($daysPassed >= $startDay-1) {
- // 计算今天是第几个奖励日(从起始天数开始)
- $rewardIndex = $daysPassed - ($startDay-1);
-
- if ($rewardIndex < $bonusDay) {
- // 在奖励期内
- if ($claimedDays > $rewardIndex) {
- $dayRewardStatus = 2; // 已领取
- } else {
- // 即便前几天漏领,后续奖励仍可继续领取(漏掉的视为作废)
- $dayRewardStatus = 1; // 可领取
- }
- } else {
- // 超过奖励天数
- $dayRewardStatus = 2; // 已领取完
- }
- }
-
- $giftInfo['day_rewards'] = [
- 'total_bonus' => (float)$giftRecord->day_rewards_total,
- 'bonus_day' => $bonusDay,
- 'start_day' => $startDay,
- 'bonus' => $dayRewardsData['bonus'] ?? [],
- 'claimed_days' => $claimedDays,
- 'progress' => $claimedDays . '/' . $bonusDay,
- 'status' => $dayRewardStatus, // 0=不可领取, 1=可领取, 2=已领取, 3=过期
- 'status_text' => [
- __('web.gift.status_cannot_claim'),
- __('web.gift.status_can_claim'),
- __('web.gift.status_claimed'),
- __('web.gift.status_expired')
- ][$dayRewardStatus] ?? __('web.gift.status_unknown')
- ];
- }
-
- // 下注奖励进度(使用下注次数)
- if ($bettingBonusData) {
- $perBet = $bettingBonusData['per_bet'] ?? 100; // 每次下注次数要求
- $perBetBonus = $bettingBonusData['per_bet_bonus'] ?? 2; // 每次奖励金额(不是百分比)
- $currentBetCount = $giftRecord->betting_current_bet; // 当前累计下注次数
- $totalBonusLimit = $giftRecord->betting_bonus_total; // 总奖励上限
- $claimedAmount = $giftRecord->betting_bonus_claimed; // 已领取金额
- $remainingBonus = $totalBonusLimit - $claimedAmount; // 剩余可领取金额
-
- // 每次可领取的金额(直接使用配置的值,不是百分比计算)
- $perReward = (float)$perBetBonus;
-
- // 根据当前下注次数计算已达成的次数
- $completedTimes = floor($currentBetCount / $perBet);
-
- // 已领取的次数
- $claimedTimes = $claimedAmount > 0 ? floor($claimedAmount / $perReward) : 0;
-
- // 当前可领取次数(但不能超过剩余总额)
- $canClaimTimes = $completedTimes - $claimedTimes;
- $maxCanClaimTimes = floor($remainingBonus / $perReward); // 剩余总额最多可领次数
- $canClaimTimes = min($canClaimTimes, $maxCanClaimTimes);
-
- // 当前可领取金额
- $canClaimAmount = min($canClaimTimes * $perReward, $remainingBonus);
-
- // 计算下次领取需要的下注次数
- $nextClaimBet = 0;
- if ($remainingBonus > 0 && $canClaimTimes == 0) {
- // 还有剩余奖励但当前不能领取,计算还需下注多少次
- $nextClaimBet = ($claimedTimes + 1) * $perBet - $currentBetCount;
- }
-
- // 判断下注奖励状态
- // 0=不可领取(可领取金额为0), 1=可领取, 2=已领取(所有奖励都领完), 3=过期
- $bettingBonusStatus = 0; // 默认不可领取
-
- // 检查是否过期(购买后7天)
- if ($daysPassed >= 7) {
- $bettingBonusStatus = 3; // 过期
- } elseif ($remainingBonus <= 0) {
- $bettingBonusStatus = 2; // 已领取完
- } elseif ($canClaimAmount > 0) {
- $bettingBonusStatus = 1; // 可领取
- } else {
- $bettingBonusStatus = 0; // 不可领取(下注次数不足)
- }
-
- $giftInfo['betting_bonus'] = [
- 'total_bonus' => (float)$totalBonusLimit, // 总奖励上限
- 'per_bet' => $perBet, // 每次下注次数要求
- 'per_bet_bonus' => $perReward, // 每次奖励金额(实际金额)
- // 'per_reward' => $perReward, // 每次奖励金额(保持兼容)
- 'current_bet' => (float)$currentBetCount, // 当前累计下注次数
- 'claimed_amount' => (float)$claimedAmount, // 已领取金额
- 'remaining_bonus' => (float)$remainingBonus, // 剩余可领金额
- 'can_claim_amount' => (float)$canClaimAmount, // 当前可领取金额
- 'can_claim_times' => max(0, $canClaimTimes), // 当前可领取次数
- 'next_claim_bet' => max(0, $nextClaimBet), // 下次领取还需下注次数
- 'progress' => round($currentBetCount, 0) . '/' . round($claimedTimes * $perBet + $perBet, 0), // 当前进度/下次领取目标(次数)
- 'status' => $bettingBonusStatus, // 0=不可领取, 1=可领取, 2=已领取, 3=过期
- 'status_text' => [
- __('web.gift.status_cannot_claim'),
- __('web.gift.status_can_claim'),
- __('web.gift.status_claimed'),
- __('web.gift.status_expired')
- ][$bettingBonusStatus] ?? __('web.gift.status_unknown')
- ];
- }
-
- // 下注任务进度(使用下注金额)
- if ($bettingTaskData) {
- $betPayTimes = $bettingTaskData['bet_pay_times'] ?? 60;
- $requiredBet = $giftRecord->pay_amount * $betPayTimes;
- $currentProgress = $currentTotalBetAmount / NumConfig::NUM_VALUE; // 使用下注金额,不是次数
- $taskCompleted = $currentProgress >= $requiredBet;
- $isClaimed = $giftRecord->betting_task_claimed == 1;
-
- // 判断下注任务状态
- // 0=不可领取, 1=可领取, 2=已领取, 3=过期
- $bettingTaskStatus = 0; // 默认不可领取
-
- // 检查是否过期(购买后7天)
- if ($daysPassed >= 7) {
- $bettingTaskStatus = 3; // 过期
- } elseif ($isClaimed) {
- $bettingTaskStatus = 2; // 已领取
- } elseif ($taskCompleted) {
- $bettingTaskStatus = 1; // 可领取(任务完成且未领取)
- } else {
- $bettingTaskStatus = 0; // 不可领取(任务未完成)
- }
-
- $giftInfo['betting_task'] = [
- 'total_bonus' => (float)$giftRecord->betting_task_total,
- 'bet_pay_times' => $betPayTimes,
- 'required_bet' => $requiredBet, // 需要下注的金额
- 'current_bet' => (float)$currentProgress, // 当前累计下注
- 'progress' => round($currentProgress, 2) . '/' . $requiredBet,
- 'status' => $bettingTaskStatus, // 0=不可领取, 1=可领取, 2=已领取, 3=过期
- 'status_text' => [
- __('web.gift.status_cannot_claim'),
- __('web.gift.status_can_claim'),
- __('web.gift.status_claimed'),
- __('web.gift.status_expired')
- ][$bettingTaskStatus] ?? __('web.gift.status_unknown')
- ];
- }
- $giftInfo['expired'] = strtotime($giftRecord->created_at)+86400*7;
- return apiReturnSuc([
- 'has_purchased' => true, // 已购买标记
- 'gift_info' => $giftInfo
- // ✅ 已购买用户不返回倒计时
- ]);
- }
- /**
- * 领取首充礼包奖励
- */
- public function claimFirstPayGiftReward(Request $request)
- {
- $user = $request->user();
- $userId = $user->UserID;
- $rewardType = $request->input('reward_type'); // 'day_reward', 'betting_bonus', 'betting_task'
- $this->setUserLocale($request);
-
- // 获取礼包记录
- $giftRecord = DB::table('agent.dbo.first_pay_gift_records')
- ->where('user_id', $userId)
- ->first();
-
- if (!$giftRecord) {
- return apiReturnFail(['web.gift.not_purchased', __('web.gift.not_purchased')]);
- }
-
- $rewardAmount = 0;
-
- try {
- // 每日奖励领取
- if ($rewardType === 'day_reward') {
- $dayRewardsData = json_decode($giftRecord->day_rewards_data, true);
- if (!$dayRewardsData) {
- return apiReturnFail(['web.gift.no_day_rewards_config', __('web.gift.no_day_rewards_config')]);
- }
-
- $bonusDay = $dayRewardsData['bonus_day'] ?? 0;
- $startDay = $dayRewardsData['start_day'] ?? 1;
- $bonusArray = $dayRewardsData['bonus'] ?? [];
- $claimedDays = $giftRecord->day_rewards_claimed;
-
- // 检查是否还有可领取的天数
- if ($claimedDays >= $bonusDay) {
- return apiReturnFail(['web.gift.day_rewards_all_claimed', __('web.gift.day_rewards_all_claimed')]);
- }
-
- // 计算当前是购买后的第几天
- $purchaseDate = date('Y-m-d', strtotime($giftRecord->created_at));
- $currentDate = date('Y-m-d');
- $daysPassed = floor((strtotime($currentDate) - strtotime($purchaseDate)) / 86400);
-
- // 检查是否到了可以领取的天数(从第start_day天开始)
- if ($daysPassed < $startDay-1) {
- return apiReturnFail(['web.gift.day_rewards_not_time', str_replace(':day', $startDay, __('web.gift.day_rewards_not_time'))]);
- }
-
- // 计算今天应该领取第几天的奖励
- // 例如:start_day=2,今天是第3天,应该领取第1个奖励(索引0)
- $todayRewardIndex = $daysPassed - ($startDay-1);
-
- // 检查是否超过奖励天数
- if ($todayRewardIndex >= $bonusDay) {
- return apiReturnFail(['web.gift.day_rewards_expired', __('web.gift.day_rewards_expired')]);
- }
-
- // 检查今天是否已领取
- $lastClaimDate = $giftRecord->day_last_claim_date;
- if ($lastClaimDate && $lastClaimDate === $currentDate) {
- return apiReturnFail(['web.gift.day_rewards_claimed_today', __('web.gift.day_rewards_claimed_today')]);
- }
-
- // 检查是否跳过了某些天(过期不补领)
- // 如果今天应该领第5天的奖励,但用户只领了3天,那就直接领第5天的
- if ($todayRewardIndex > $claimedDays) {
- // 跳过了一些天,更新已领取天数为今天的索引
- $claimedDays = $todayRewardIndex;
- }
-
- // 计算今天可领取的奖励(使用todayRewardIndex作为索引)
- $todayBonusPercent = $bonusArray[$todayRewardIndex] ?? 0;
- $rewardAmount = round($giftRecord->pay_amount * $todayBonusPercent / 100, 2);
-
- // 更新记录
- DB::table('agent.dbo.first_pay_gift_records')
- ->where('user_id', $userId)
- ->update([
- 'day_rewards_claimed' => $todayRewardIndex + 1, // 已领取到第几天(索引+1)
- 'day_last_claim_date' => $currentDate,
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- }
- // 下注奖励领取(使用下注次数)
- elseif ($rewardType === 'betting_bonus') {
- $bettingBonusData = json_decode($giftRecord->betting_bonus_data, true);
- if (!$bettingBonusData) {
- return apiReturnFail(['web.gift.no_betting_bonus_config', __('web.gift.no_betting_bonus_config')]);
- }
-
- // 检查是否过期(购买后7天)
- $purchaseDate = date('Y-m-d', strtotime($giftRecord->created_at));
- $currentDate = date('Y-m-d');
- $daysPassed = floor((strtotime($currentDate) - strtotime($purchaseDate)) / 86400);
- if ($daysPassed >= 7) {
- return apiReturnFail(['web.gift.betting_bonus_expired', __('web.gift.betting_bonus_expired')]);
- }
-
- // 获取最新的下注次数
- $currentBetCount = DB::table(TableName::QPRecordDB() . 'RecordUserGameCount')
- ->where('UserID', $userId)
- ->sum('Cnt') ?? 0;
-
- $perBet = $bettingBonusData['per_bet'] ?? 100; // 每次下注次数要求
- $perBetBonus = $bettingBonusData['per_bet_bonus'] ?? 2; // 每次奖励金额(不是百分比)
- $totalBonusLimit = $giftRecord->betting_bonus_total; // 总奖励上限
- $claimedAmount = $giftRecord->betting_bonus_claimed; // 已领取金额
- $remainingBonus = $totalBonusLimit - $claimedAmount; // 剩余可领金额
-
- // 检查是否还有剩余奖励
- if ($remainingBonus <= 0) {
- return apiReturnFail(['web.gift.betting_bonus_all_claimed', __('web.gift.betting_bonus_all_claimed')]);
- }
-
- // 每次可领取的金额(直接使用配置的值,不是百分比计算)
- $perReward = (float)$perBetBonus;
-
- // 根据当前下注次数计算已达成的次数
- $completedTimes = floor($currentBetCount / $perBet);
-
- // 已领取的次数
- $claimedTimes = $claimedAmount > 0 ? floor($claimedAmount / $perReward) : 0;
-
- // 可领取次数(下注达成的次数 - 已领取次数)
- $canClaimTimes = $completedTimes - $claimedTimes;
-
- // 检查是否可以领取
- if ($canClaimTimes <= 0) {
- $neededBets = (($claimedTimes + 1) * $perBet - $currentBetCount);
- return apiReturnFail(['web.gift.betting_bonus_insufficient', str_replace(':times', $neededBets, __('web.gift.betting_bonus_insufficient'))]);
- }
-
- // ✅ 一次性领取所有已达成的奖励(但不能超过剩余总额)
- $totalCanClaimAmount = $canClaimTimes * $perReward; // 理论可领取总额
- $rewardAmount = min($totalCanClaimAmount, $remainingBonus); // 实际领取金额(不超过剩余总额)
- $actualClaimTimes = floor($rewardAmount / $perReward); // 实际领取次数
-
- // 更新记录(同时更新下注次数)
- DB::table('agent.dbo.first_pay_gift_records')
- ->where('user_id', $userId)
- ->update([
- 'betting_bonus_claimed' => $giftRecord->betting_bonus_claimed + $rewardAmount,
- 'betting_current_bet' => $currentBetCount, // 更新最新下注次数
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
-
- \Log::info('下注奖励领取', [
- 'user_id' => $userId,
- 'can_claim_times' => $canClaimTimes,
- 'actual_claim_times' => $actualClaimTimes,
- 'reward_amount' => $rewardAmount
- ]);
- }
- // 下注任务领取(使用下注金额)
- elseif ($rewardType === 'betting_task') {
- if ($giftRecord->betting_task_claimed == 1) {
- return apiReturnFail(['web.gift.betting_task_claimed', __('web.gift.betting_task_claimed')]);
- }
-
- $bettingTaskData = json_decode($giftRecord->betting_task_data, true);
- if (!$bettingTaskData) {
- return apiReturnFail(['web.gift.no_betting_task_config', __('web.gift.no_betting_task_config')]);
- }
-
- // 检查是否过期(购买后7天)
- $purchaseDate = date('Y-m-d', strtotime($giftRecord->created_at));
- $currentDate = date('Y-m-d');
- $daysPassed = floor((strtotime($currentDate) - strtotime($purchaseDate)) / 86400);
- if ($daysPassed >= 7) {
- return apiReturnFail(['web.gift.betting_task_expired', __('web.gift.betting_task_expired')]);
- }
-
- // 获取最新的下注金额
- $userStats = DB::table('QPRecordDB.dbo.RecordUserTotalStatistics')
- ->where('UserID', $userId)
- ->first();
-
- $currentBetAmount = ($userStats->TotalBet ?? 0) / NumConfig::NUM_VALUE;
-
- $betPayTimes = $bettingTaskData['bet_pay_times'] ?? 60;
- $requiredBet = $giftRecord->pay_amount * $betPayTimes;
-
- if ($currentBetAmount < $requiredBet) {
- return apiReturnFail(['web.gift.betting_task_insufficient', str_replace(':amount', $requiredBet, __('web.gift.betting_task_insufficient'))]);
- }
-
- $rewardAmount = $giftRecord->betting_task_total;
-
- // 更新记录
- DB::table('agent.dbo.first_pay_gift_records')
- ->where('user_id', $userId)
- ->update([
- 'betting_task_claimed' => 1,
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- }
- else {
- return apiReturnFail(['web.gift.invalid_reward_type', __('web.gift.invalid_reward_type')]);
- }
-
- // 添加奖励到用户账户
- if ($rewardAmount > 0) {
- OuroGameService::AddScore($userId, $rewardAmount * NumConfig::NUM_VALUE, 52, true); // 52=首充礼包奖励
-
- \Log::info('首充礼包奖励领取', [
- 'user_id' => $userId,
- 'reward_type' => $rewardType,
- 'amount' => $rewardAmount
- ]);
- }
-
- return apiReturnSuc([
- 'amount' => $rewardAmount,
- 'message' => str_replace(':amount', $rewardAmount, __('web.gift.claim_success'))
- ]);
-
- } catch (\Exception $e) {
- \Log::error('首充礼包奖励领取失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage()
- ]);
- return apiReturnFail(['web.gift.claim_failed', str_replace(':error', $e->getMessage(), __('web.gift.claim_failed'))]);
- }
- }
- /**
- * 获取首充礼包数据(包含充值档位列表)
- * @param $firstPayGift 礼包配置
- * @param $timeLeft 剩余秒数(已经是用户实际剩余时间)
- */
- private function getFirstPayGiftData($firstPayGift, $timeLeft)
- {
- // 获取支付方式
- // $names = DB::table('agent.dbo.admin_configs')
- // ->where([
- // 'type' => 'pay_method',
- // 'status' => 1,
- // ])
- // ->selectRaw('id, name, status, new_pay_type as type')
- // ->orderByDesc('sort')
- // ->get()
- // ->toArray();
- // 获取充值档位
- $list = DB::table('agent.dbo.recharge_gear')
- ->select('money', 'favorable_price', 'give','gear')
- ->orderBy('money', 'asc')
- ->where('status', 1)
- ->where('in_shop', 1)
- ->get();
- // $gear = \GuzzleHttp\json_encode($names);
- foreach ($list as &$val) {
- $val->favorable_price = $val->favorable_price + $val->give;
- // $val->gear = $gear;
- $val->recommend = 0;
- if ($val->money == $firstPayGift->recommend) {
- $val->recommend = 1;
- }
- if (!empty($val->gear)) {
- $val->gear = Util::filterGearByDevice($val->gear);
- }
- }
- return apiReturnSuc([
- 'list' => $list,
- 'gift_info' => [
- 'gift_id' => $firstPayGift->gift_id,
- 'gift_name' => $firstPayGift->gift_name,
- 'total_bonus' => $firstPayGift->total_bonus,
- 'bonus_instantly' => $firstPayGift->bonus_instantly,
- 'day_rewards' => $firstPayGift->day_rewards?json_decode($firstPayGift->day_rewards):'',
- 'betting_bonus' => $firstPayGift->betting_bonus?json_decode($firstPayGift->betting_bonus):'',
- 'betting_task' => $firstPayGift->betting_task?json_decode($firstPayGift->betting_task):'',
- ],
- 'time_left' => $timeLeft, // 剩余秒数
- 'expire_at' => time() + $timeLeft // 过期时间戳
- ]);
- }
- public function firstPayMulti(Request $request)
- {
- $user = LoginController::checkLogin($request);
- if($user){
- if(env('CONFIG_24680_NFTD_99',0)==0)if($user->Channel==99)return apiReturnFail();
- $fpkey='Firstpay_'.$user->UserID;
- if(Redis::exists($fpkey)){
- $data=Redis::get($fpkey);
- $data=json_decode($data,true);
- $data['timeleft']=86400-(time()-$data['buytime']);
- return apiReturnSuc(['leftitem'=>$data]);
- }
- $user_recharge = DB::table(TableName::QPAccountsDB() . 'YN_VIPAccount')
- ->where('UserID', $user->UserID)
- ->value('Recharge') ?: 0;
- if($user_recharge)return apiReturnFail();
- }
- $items=DB::table('agent.dbo.recharge_gear')
- ->where('status',1)
- ->where('second_give','>',0)
- ->where('first_pay','>=', 1)
- ->select('gift_id','money','give','favorable_price','second_give')->get()->each(function($item){
- $item->id=28;
- })->toArray();
- $default=count($items)-1;
- return apiReturnSuc(compact('items','default'));
- }
- // 破产礼包
- public function bankruptcyGift(Request $request)
- {
- $user = $request->user();
- $this->setUserLocale($request);
-
- // 判断用户是否充值
- $user_recharge = DB::table(TableName::QPAccountsDB() . 'YN_VIPAccount')
- ->where('UserID', $user->UserID)
- ->value('Recharge') ?: 0;
-
- if ($user_recharge <= 0) {
- return apiReturnFail(['web.gift.user_not_recharged', __('web.gift.user_not_recharged')]);
- }
-
- // 获取所有破产礼包配置 (gift_id=302)
- $bankruptcyGifts = DB::table('agent.dbo.recharge_gift')
- ->where('gift_id', 302)
- ->get();
-
- if ($bankruptcyGifts->isEmpty()) {
- return apiReturnFail(['web.gift.bankruptcy_gift_not_exists', __('web.gift.bankruptcy_gift_not_exists')]);
- }
- $result = [];
-
- // 遍历每条礼包配置,根据 recommend 关联 recharge_gear
- foreach ($bankruptcyGifts as $gift) {
- $gear = DB::table('agent.dbo.recharge_gear')
- ->select('money', 'favorable_price', 'gear')
- ->where('money', $gift->recommend)
- ->where('status', 1)
- ->first();
-
- if ($gear) {
- $gear->gift_id = $gift->gift_id;
- $gear->total_bonus = $gift->total_bonus;
- $gear->bonus = $gift->total_bonus - 100;
- if (!empty($gear->gear)) {
- $gear->gear = Util::filterGearByDevice($gear->gear);
- }
- $result[] = $gear;
- }
- }
- // 按 money 排序
- usort($result, function($a, $b) {
- return $a->money <=> $b->money;
- });
- return apiReturnSuc($result);
- }
- // 每日首充礼包
- public function dailyFirstRechargeGift(Request $request)
- {
- $user = $request->user();
- $this->setUserLocale($request);
-
- // 检查用户今日是否已充值(完成任意档位充值后入口消失)
- $todayStart = date('Y-m-d') . ' 00:00:00';
- $todayEnd = date('Y-m-d') . ' 23:59:59';
-
- $todayRecharge = DB::table('agent.dbo.order')
- ->where('user_id', $user->UserID)
- ->where('pay_status', 1)
- ->where('GiftsID', 303)
- ->where('pay_at', '>=', $todayStart)
- ->where('pay_at', '<=', $todayEnd)
- ->first();
-
- // 如果今日已充值,返回入口消失标记
- if ($todayRecharge) {
- return apiReturnSuc([
- 'has_recharged_today' => true,
- 'message' => __('web.gift.recharged_today')
- ]);
- }
-
- // 获取每日首充礼包配置 (gift_id=303)
- $dailyGifts = DB::table('agent.dbo.recharge_gift')
- ->where('gift_id', 303)
- ->get();
-
- if ($dailyGifts->isEmpty()) {
- return apiReturnFail(['web.gift.daily_first_recharge_not_exists', __('web.gift.daily_first_recharge_not_exists')]);
- }
- $result = [];
-
- // 遍历每条礼包配置,根据 recommend 关联 recharge_gear
- foreach ($dailyGifts as $gift) {
- $gear = DB::table('agent.dbo.recharge_gear')
- ->select('money', 'favorable_price', 'gear', 'give')
- ->where('money', $gift->recommend)
- ->where('status', 1)
- ->first();
-
- if ($gear) {
- $gear->gift_id = $gift->gift_id;
- $gear->total_bonus = $gift->total_bonus;
- $gear->bonus = $gift->total_bonus - 100;
- $gear->recommend = $gift->first_pay?1:0;
- if (!empty($gear->gear)) {
- $gear->gear = Util::filterGearByDevice($gear->gear);
- }
- $result[] = $gear;
- }
- }
- // 按 money 排序
- usort($result, function($a, $b) {
- return $a->money <=> $b->money;
- });
- return apiReturnSuc([
- 'has_recharged_today' => false,
- 'list' => $result
- ]);
- }
- // 每日礼包(三档充值)
- public function dailyGift(Request $request)
- {
- $user = $request->user();
- $this->setUserLocale($request);
-
- // 获取每日礼包配置 (gift_id=304),应该有3档
- $dailyGifts = DB::table('agent.dbo.recharge_gift')
- ->where('gift_id', 304)
- ->orderBy('recommend', 'asc')
- ->get();
-
- if ($dailyGifts->isEmpty()) {
- return apiReturnFail(['web.gift.daily_gift_not_exists', __('web.gift.daily_gift_not_exists')]);
- }
-
- if ($dailyGifts->count() > 3) {
- // 如果配置超过3档,只取前3档
- $dailyGifts = $dailyGifts->take(3);
- }
-
- // 检查用户今日每档的充值状态
- $todayStart = date('Y-m-d') . ' 00:00:00';
- $todayEnd = date('Y-m-d') . ' 23:59:59';
-
- $todayRecharges = DB::table('agent.dbo.order')
- ->where('user_id', $user->UserID)
- ->where('GiftsID', 304)
- ->where('pay_status', 1)
- ->where('pay_at', '>=', $todayStart)
- ->where('pay_at', '<=', $todayEnd)
- ->pluck('amount')
- ->toArray();
-
- $result = [];
- $completedCount = 0;
-
- // 遍历每档配置
- foreach ($dailyGifts as $index => $gift) {
- $gear = DB::table('agent.dbo.recharge_gear')
- ->select('money', 'favorable_price', 'gear', 'give')
- ->where('money', $gift->recommend)
- ->where('status', 1)
- ->first();
-
- if ($gear) {
- // 检查该档位今日是否已充值(使用浮点数比较,允许小数点精度差异)
- $isCompleted = false;
- foreach ($todayRecharges as $rechargeAmount) {
- if (abs($rechargeAmount/100 - $gift->recommend) < 0.01) {
- $isCompleted = true;
- break;
- }
- }
- if ($isCompleted) {
- $completedCount++;
- }
-
- $gear->gift_id = $gift->gift_id;
- $gear->total_bonus = $gift->total_bonus;
- $gear->bonus = $gift->total_bonus - 100;
- $gear->is_completed = $isCompleted; // 是否已完成
- $gear->gear_index = $index + 1; // 档位序号(1,2,3)
- $gear->recommend = $gift->first_pay?1:0;
-
- if (!empty($gear->gear)) {
- $gear->gear = Util::filterGearByDevice($gear->gear);
- }
- $result[] = $gear;
- }
- }
-
- // 获取额外奖励金额(从任意一档的 task_bonus 字段获取,task_bonus 是数字字段)
- $taskBonus = null;
- $firstGift = $dailyGifts->first();
- if ($firstGift && $firstGift->task_bonus !== null) {
- $taskBonus = round((float)$firstGift->task_bonus, 2);
- }
-
- // 检查是否可以领取额外奖励(三档都完成)
- $canClaimBonus = ($completedCount >= 3 && $taskBonus > 0);
-
- // 检查是否已领取过额外奖励
- $hasClaimedBonus = false;
- if ($canClaimBonus) {
- $redisKey = "daily_gift_bonus_claimed_{$user->UserID}_" . date('Y-m-d');
- $hasClaimedBonus = Redis::exists($redisKey);
- }
-
- return apiReturnSuc([
- 'list' => $result,
- 'completed_count' => $completedCount,
- 'total_count' => count($result),
- 'can_claim_bonus' => $canClaimBonus && !$hasClaimedBonus,
- 'has_claimed_bonus' => $hasClaimedBonus,
- 'task_bonus' => $taskBonus
- ]);
- }
- // 领取每日礼包三档完成后的额外奖励
- public function claimDailyGiftBonus(Request $request)
- {
- $user = $request->user();
- $userId = $user->UserID;
- $this->setUserLocale($request);
-
- // 检查三档是否都已完成
- $todayStart = date('Y-m-d') . ' 00:00:00';
- $todayEnd = date('Y-m-d') . ' 23:59:59';
-
- // 获取每日礼包配置
- $dailyGifts = DB::table('agent.dbo.recharge_gift')
- ->where('gift_id', 304)
- ->orderBy('recommend', 'asc')
- ->get()
- ->take(3);
-
- if ($dailyGifts->isEmpty()) {
- return apiReturnFail(['web.gift.daily_gift_not_exists', __('web.gift.daily_gift_not_exists')]);
- }
-
- // 检查每档今日是否都已充值
- $todayRecharges = DB::table('agent.dbo.order')
- ->where('user_id', $userId)
- ->where('GiftsID', 304)
- ->where('pay_status', 1)
- ->where('pay_at', '>=', $todayStart)
- ->where('pay_at', '<=', $todayEnd)
- ->pluck('amount')
- ->toArray();
-
- $completedCount = 0;
- foreach ($dailyGifts as $gift) {
- // 检查该档位是否已充值(使用浮点数比较)
- $isCompleted = false;
- foreach ($todayRecharges as $rechargeAmount) {
- if (abs($rechargeAmount/100 - $gift->recommend) < 0.01) {
- $isCompleted = true;
- break;
- }
- }
- if ($isCompleted) {
- $completedCount++;
- }
- }
-
- if ($completedCount < 3) {
- return apiReturnFail(['web.gift.three_tiers_not_completed', __('web.gift.three_tiers_not_completed')]);
- }
-
- // 检查是否已领取过
- $redisKey = "daily_gift_bonus_claimed_{$userId}_" . date('Y-m-d');
- if (Redis::exists($redisKey)) {
- return apiReturnFail(['web.gift.bonus_already_claimed_today', __('web.gift.bonus_already_claimed_today')]);
- }
-
- // 获取奖励金额(从任意一档的 task_bonus 获取,task_bonus 是数字字段)
- $firstGift = $dailyGifts->first();
- $rewardAmount = 0;
-
- if ($firstGift && $firstGift->task_bonus !== null) {
- $rewardAmount = round((float)$firstGift->task_bonus, 2);
- }
-
- if ($rewardAmount <= 0) {
- return apiReturnFail(['web.gift.bonus_config_not_exists', __('web.gift.bonus_config_not_exists')]);
- }
-
- try {
- // 发放奖励
- OuroGameService::AddScore($userId, $rewardAmount * NumConfig::NUM_VALUE, 52, true); // 52=礼包奖励
-
- // 标记已领取
- Redis::setex($redisKey, 86400, 1); // 24小时过期
-
- \Log::info('每日礼包额外奖励领取成功', [
- 'user_id' => $userId,
- 'reward_amount' => $rewardAmount
- ]);
-
- return apiReturnSuc([
- 'amount' => $rewardAmount,
- 'message' => str_replace(':amount', $rewardAmount, __('web.gift.claim_success'))
- ]);
-
- } catch (\Exception $e) {
- \Log::error('每日礼包额外奖励领取失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage()
- ]);
- return apiReturnFail(['web.gift.claim_failed', str_replace(':error', $e->getMessage(), __('web.gift.claim_failed'))]);
- }
- }
- public function getSecondGive(Request $request)
- {
- $user = $request->user();
- $fpkey='Firstpay_'.$user->UserID;
- if(Redis::exists($fpkey)){
- $data=Redis::get($fpkey);
- $data=json_decode($data,true);
- $data['timeleft']=86400-(time()-$data['buytime']);
- if($data['timeleft']<=0) {
- Redis::del($fpkey);
- //加钱
- if($data['second_give']){
- $czReason=$data['czReason'];
- $cjReason=$data['cjReason'];
- [$OrgScore,$NowScore]=OuroGameService::AddScore($user->UserID,$data['second_give']*NumConfig::NUM_VALUE,$cjReason);
- //更新二次领钱记录
- DB::table(TableName::agent() . 'guide_payment')->where('UserID',$user->UserID)->update([
- 'GetSecondTime' => now(),
- 'SecondScoreNum'=>$data['second_give']*NumConfig::NUM_VALUE
- ]);
- return apiReturnSuc(compact('OrgScore','NowScore'));
- }
- return apiReturnSuc();
- }
- }
- return apiReturnFail(['web.withdraw.try_again_later','Tente novamente mais tarde']);
- }
- /**
- * 连续未充值 VIP 礼包(gift_id=305)—— 检查条件和返回档位
- */
- public function vipInactiveGift(Request $request)
- {
- $user = $request->user();
- $this->setUserLocale($request);
- $userId = $user->UserID;
- // 1. VIP 判断:是否有过充值(YN_VIPAccount.Recharge > 0)
- $userRecharge = DB::table(TableName::QPAccountsDB() . 'YN_VIPAccount')
- ->where('UserID', $userId)
- ->value('Recharge') ?: 0;
-
- if ($userRecharge <= 0) {
- // 不满足条件:不是 VIP 用户
- return apiReturnSuc([
- 'status' => 0,
- 'message' => __('web.gift.vip_inactive_not_vip')
- ]);
- }
- // 2. 检查最近一次充值时间,计算连续未充值天数
- $lastOrder = DB::table('agent.dbo.order')
- ->where('user_id', $userId)
- ->where('pay_status', 1)
- ->where('GiftsID','<>', 305)
- ->orderBy('pay_at', 'desc')
- ->first();
- if (!$lastOrder) {
- return apiReturnSuc([
- 'status' => 0,
- 'message' => __('web.gift.vip_inactive_not_vip')
- ]);
- }
- $lastDate = date('Y-m-d', strtotime($lastOrder->pay_at));
- $today = date('Y-m-d');
- $diffDays = (strtotime($today) - strtotime($lastDate)) / 86400;
- // 连续未充值天数:如果最后一次充值是昨天,则未充值天数为0;如果是前天,则为1;以此类推
- // 需要 >= 3 天,即最后一次充值至少是3天前
- $inactiveDays = max(0, (int)$diffDays - 1);
- if ($inactiveDays < 3) {
- // 不满足条件:连续未充值天数不足
- return apiReturnSuc([
- 'status' => 0,
- 'message' => __('web.gift.vip_inactive_days_insufficient'),
- 'inactive_days' => $inactiveDays
- ]);
- }
- // 3. 计算总充值与平均单笔(从 RecordUserTotalStatistics)
- $stat = DB::table(TableName::QPRecordDB() . 'RecordUserTotalStatistics')
- ->where('UserID', $userId)
- ->select('Recharge', 'RechargeTimes')
- ->first();
- $totalRecharge = (float)($stat->Recharge ?? 0) / NumConfig::NUM_VALUE; // 转换为元
- $rechargeTimes = (int)($stat->RechargeTimes ?? 0);
- if ($totalRecharge <= 0 || $rechargeTimes <= 0) {
- return apiReturnSuc([
- 'status' => 0,
- 'message' => __('web.gift.vip_inactive_not_vip')
- ]);
- }
- $avgRecharge = $totalRecharge / $rechargeTimes;
- // 4. 按规则选择礼包金额
- $amount = null;
- if ($totalRecharge < 50) {
- $amount = $avgRecharge < 15 ? 9.99 : 19.99;
- } else {
- if ($avgRecharge < 15) {
- $amount = 19.99;
- } elseif ($avgRecharge >= 15 && $avgRecharge < 20) {
- $amount = 29.99;
- } else {
- $amount = 39.99;
- }
- }
- // 5. 检查是否已经买过 305 礼包
- $hasBought = DB::table('agent.dbo.order')
- ->where('user_id', $userId)
- ->where('GiftsID', 305)
- ->where('pay_status', 1)
- ->exists();
- // 6. 查询 7 日礼包记录状态
- $record = DB::table('agent.dbo.inactive_vip_gift_records')
- ->where('user_id', $userId)
- ->where('gift_id', 305)
- ->orderBy('id', 'desc')
- ->first();
- /*
- * status 定义:
- * 0 不满足条件
- * 1 满足条件未充值
- * 2 满足条件已充值,奖励未全部领取
- * 3 满足条件已充值,奖励全部领取
- * 4 满足条件已充值,7天礼包已过期
- */
- $status = 1;
- $timeLeft = 0; // 倒计时剩余秒数
-
- if ($hasBought) {
- if (!$record) {
- $status = 2;
- } else {
- // 检查 7 天礼包是否已过期
- if (strtotime($record->expired_at) < time()) {
- $status = 4; // 7天礼包已过期
- } else {
- // 检查 7 天是否全部领完:claimed_days_mask 的低 7 位全部为 1
- $allClaimedMask = (1 << 7) - 1; // 0b1111111
- $status = (($record->claimed_days_mask & $allClaimedMask) == $allClaimedMask) ? 3 : 2;
- }
- }
- } else {
- // 满足条件但未充值:检查24小时倒计时(每天重置)
- $today = date('Y-m-d');
- $timerKey = "vip_inactive_gift_timer_{$userId}_{$today}";
- $timerData = Redis::get($timerKey);
-
- if ($timerData) {
- $timerData = json_decode($timerData, true);
- $expireTime = $timerData['expire_time'] ?? 0;
- $timeLeft = max(0, $expireTime - time());
-
- if ($timeLeft <= 0) {
- // 今天的倒计时已结束,不再显示礼包
- $status = 0;
- }
- } else {
- // 今天首次检测到满足条件,初始化24小时倒计时
- // 从当前时间开始,24小时后过期
- $expireTime = time() + 86400; // 24小时后
- $ttl = 86400 + 3600; // Redis过期时间设为25小时,确保跨天也能获取
-
- Redis::setex($timerKey, $ttl, json_encode([
- 'expire_time' => $expireTime,
- 'created_at' => time(),
- 'date' => $today
- ]));
- $timeLeft = 86400;
- }
- }
- $payload = [
- 'status' => $status,
- 'inactive_days' => $inactiveDays,
- // 'total_recharge' => $totalRecharge,
- // 'avg_recharge' => round($avgRecharge, 2),
- 'time_left' => $timeLeft,
- 'expire_at' => $timeLeft > 0 ? (time() + $timeLeft) : null,
- 'message' => $status == 1 ? __('web.gift.vip_inactive_can_recharge') :
- ($status == 2 ? __('web.gift.vip_inactive_claimed_partial') :
- ($status == 3 ? __('web.gift.vip_inactive_claimed_all') :
- __('web.gift.vip_inactive_seven_days_expired')))
- ];
- // status=1 时返回充值档位信息(参考 bankruptcyGift),gift_id 固定 305,加总奖励百分比
- if ($status == 1 && $amount !== null) {
- $sevenPercent = 140 + max(0, $inactiveDays - 3) * 10;
- $sevenPercent = min($sevenPercent, 200);
- $totalRewardPercent = 120 + $sevenPercent; // 120% 立即 + 7日礼包%
- $gear = DB::table('agent.dbo.recharge_gear')
- ->select('money', 'favorable_price', 'gear', 'give')
- ->where('money', $amount)
- ->where('status', 1)
- ->first();
- $favorablePrice = round($amount * 120 / 100, 2); // 305 固定 120% 立即到账
- $give = $favorablePrice - $amount;
- if ($gear) {
- $gear->gift_id = 305;
- $gear->favorable_price = $favorablePrice;
- $gear->give = $give;
- $gear->total_bonus = $totalRewardPercent;
- $gear->bonus = $totalRewardPercent;
- $gear->total_reward_percent = $totalRewardPercent;
- $gear->recommend = 1;
- if (!empty($gear->gear)) {
- $gear->gear = Util::filterGearByDevice($gear->gear);
- }
- $payload['list'] = [$gear];
- } else {
- $payload['list'] = [(object)[
- 'money' => $amount,
- 'favorable_price' => $favorablePrice,
- 'give' => $give,
- 'gear' => null,
- 'gift_id' => 305,
- 'total_bonus' => $totalRewardPercent,
- 'bonus' => $totalRewardPercent,
- 'total_reward_percent' => $totalRewardPercent,
- 'recommend' => 1,
- ]];
- }
- $payload['amount'] = $amount;
- $payload['total_reward_percent'] = $totalRewardPercent;
- $payload['extra_reward'] = round($amount*$totalRewardPercent/100);
- $payload['total_reward'] = round($amount*$totalRewardPercent/100)+$amount;
- } else {
- $payload['amount'] = $amount;
- }
- return apiReturnSuc($payload);
- }
- /**
- * 连续未充值 VIP 礼包(gift_id=305)—— 获取 7 日礼包信息
- */
- public function vipInactiveGiftSevenDays(Request $request)
- {
- $user = $request->user();
- $this->setUserLocale($request);
- $userId = $user->UserID;
- // 获取 7 日礼包记录
- $record = DB::table('agent.dbo.inactive_vip_gift_records')
- ->where('user_id', $userId)
- ->where('gift_id', 305)
- ->orderBy('id', 'desc')
- ->first();
- if (!$record) {
- return apiReturnFail(['web.gift.vip_inactive_no_record', __('web.gift.vip_inactive_no_record')]);
- }
- // 计算从充值完成后的第几天(从第1天开始,即充值的次日)
- // 如果created_at是2024-01-01,今天是2024-01-02,daysPassed=1,表示可以领取第1天的奖励
- $createdDate = date('Y-m-d', strtotime($record->created_at));
- $today = date('Y-m-d');
- $daysPassed = floor((strtotime($today) - strtotime($createdDate)) / 86400);
- // daysPassed = 0 表示充值当天(还不能领取),1 表示充值的次日(可以领取第1天),以此类推
- // 检查是否已过期(超过7天)
- $expiredAt = strtotime($record->expired_at);
- $isExpired = time() > $expiredAt;
- $perDayAmount = (float)$record->per_day_amount;
- $claimedMask = (int)$record->claimed_days_mask;
- // 构建每一天的状态
- $days = [];
- for ($day = 1; $day <= 7; $day++) {
- $dayIndex = $day - 1; // 位索引:day1对应bit0
- $isClaimed = ($claimedMask & (1 << $dayIndex)) > 0;
- // 计算这一天对应的日期(充值后第day天)
- $targetDate = date('Ymd', strtotime($record->created_at . ' +' . $day . ' days'));
- // 判断状态
- // 0=不可领取, 1=可领取, 2=已领取, 3=过期
- $dayStatus = 0;
- $betAmount = 0; // 初始化
- // 第4-7天需要查询流水信息(无论状态如何,都要展示进度)
- if ($day >= 4) {
- // var_dump($userId,$day,$targetDate);
- $todayBet = DB::table('QPRecordDB.dbo.RecordUserDataStatisticsNew')
- ->where('UserID', $userId)
- ->where('DateID', $targetDate)
- ->value('TotalBet') ?? 0;
- $betAmount = $todayBet / NumConfig::NUM_VALUE; // 转换为元
- }
- if ($isClaimed) {
- $dayStatus = 2; // 已领取
- } elseif ($daysPassed > $day) {
- // 已过这一天但未领取:过期
- // 例如:今天是第4天(daysPassed=4),但第3天的奖励还没领取,则第3天过期
- $dayStatus = 3; // 过期
- } elseif ($daysPassed < $day) {
- // 还没到这一天:第day天的奖励需要在充值后的第day天才能领取
- // 例如:第1天奖励需要daysPassed >= 1(充值的次日)才能领取
- $dayStatus = 0; // 不可领取
- } elseif ($isExpired || $daysPassed >= 7) {
- // 整体过期(超过7天)时,未领取的奖励都过期
- $dayStatus = 3; // 过期
- } else {
- // 到了这一天,判断是否可以领取
- if ($day <= 3) {
- // 前3天:直接领取
- $dayStatus = 1; // 可领取
- } else {
- // 第4-7天:需要当天游戏流水 >= 100(流水信息已在上面查询)
- if ($betAmount >= 100) {
- $dayStatus = 1; // 可领取
- } else {
- $dayStatus = 0; // 不可领取(流水不足)
- }
- }
- }
- // 计算进度条数据(仅第4-7天)
- $betProgress = null;
- if ($day >= 4) {
- $requiredBet = 100;
- $currentBetValue = round($betAmount, 2);
- $progressPercent = $requiredBet > 0 ? min(100, round(($currentBetValue / $requiredBet) * 100, 2)) : 0;
-
- $betProgress = [
- 'current' => $currentBetValue, // 当前流水
- 'required' => $requiredBet, // 需要流水
- 'progress_percent' => $progressPercent, // 进度百分比(0-100)
- 'is_completed' => $currentBetValue >= $requiredBet // 是否完成
- ];
- }
- $days[] = [
- 'day' => $day,
- 'amount' => $perDayAmount,
- 'status' => $dayStatus,
- 'status_text' => [
- __('web.gift.status_cannot_claim'),
- __('web.gift.status_can_claim'),
- __('web.gift.status_claimed'),
- __('web.gift.status_expired')
- ][$dayStatus] ?? __('web.gift.status_unknown'),
- 'target_date' => $targetDate,
- 'bet_progress' => $betProgress // 第4-7天的流水进度信息
- ];
- }
- // 检查是否有倒计时(未充值时的24小时倒计时)
- $timerKey = "vip_inactive_gift_timer_{$userId}";
- $timerData = Redis::get($timerKey);
- $timeLeft = 0;
- if ($timerData) {
- $timerData = json_decode($timerData, true);
- $expireTime = $timerData['expire_time'] ?? 0;
- $timeLeft = max(0, $expireTime - time());
- }
- return apiReturnSuc([
- 'record' => [
- 'pay_amount' => (float)$record->pay_amount,
- 'total_percent' => (float)$record->total_percent,
- 'seven_days_percent' => (float)$record->seven_days_percent,
- 'per_day_amount' => $perDayAmount,
- 'inactive_days' => (int)$record->inactive_days,
- 'created_at' => $record->created_at,
- 'expired_at' => $record->expired_at
- ],
- 'days' => $days,
- 'current_day' => min($daysPassed + 1, 7), // 当前是第几天
- 'is_expired' => $isExpired,
- 'time_left' => $timeLeft, // 未充值时的倒计时(秒)
- 'expire_at' => $expiredAt
- ]);
- }
- /**
- * 连续未充值 VIP 礼包(gift_id=305)—— 领取某日奖励
- */
- public function claimVipInactiveGiftDayReward(Request $request)
- {
- $user = $request->user();
- $this->setUserLocale($request);
- $userId = $user->UserID;
- $day = (int)$request->input('day', 0);
- if ($day < 1 || $day > 7) {
- return apiReturnFail(['web.gift.invalid_reward_type', __('web.gift.invalid_reward_type')]);
- }
- // 获取 7 日礼包记录
- $record = DB::table('agent.dbo.inactive_vip_gift_records')
- ->where('user_id', $userId)
- ->where('gift_id', 305)
- ->orderBy('id', 'desc')
- ->first();
- if (!$record) {
- return apiReturnFail(['web.gift.vip_inactive_no_record', __('web.gift.vip_inactive_no_record')]);
- }
- // 检查是否已过期
- if (strtotime($record->expired_at) < time()) {
- return apiReturnFail(['web.gift.vip_inactive_expired', __('web.gift.vip_inactive_expired')]);
- }
- // 检查是否已领取
- $claimedMask = (int)$record->claimed_days_mask;
- $dayIndex = $day - 1;
- if ($claimedMask & (1 << $dayIndex)) {
- return apiReturnFail(['web.gift.vip_inactive_day_claimed', str_replace(':day', $day, __('web.gift.vip_inactive_day_claimed'))]);
- }
- // 计算从充值完成后的第几天
- $createdDate = date('Y-m-d', strtotime($record->created_at));
- $today = date('Y-m-d');
- $daysPassed = floor((strtotime($today) - strtotime($createdDate)) / 86400);
- // 检查是否到了这一天:第day天的奖励只能在充值后的第day天领取(daysPassed == day)
- if ($daysPassed < $day) {
- return apiReturnFail(['web.gift.vip_inactive_day_not_time', str_replace(':day', $day, __('web.gift.vip_inactive_day_not_time'))]);
- }
-
- // 检查是否已过期:如果已经过了这一天(daysPassed > day),则不能领取
- if ($daysPassed > $day) {
- return apiReturnFail(['web.gift.vip_inactive_day_expired', str_replace(':day', $day, __('web.gift.vip_inactive_day_expired'))]);
- }
- // 第4-7天需要检查当天游戏流水 >= 100
- if ($day >= 4) {
- $targetDate = date('Ymd', strtotime($record->created_at . ' +' . $day . ' days'));
- $todayBet = DB::table('QPRecordDB.dbo.RecordUserDataStatisticsNew')
- ->where('UserID', $userId)
- ->where('DataID', $targetDate)
- ->value('TotalBet') ?? 0;
- $betAmount = $todayBet / NumConfig::NUM_VALUE; // 转换为元
- if ($betAmount < 100) {
- return apiReturnFail(['web.gift.vip_inactive_bet_insufficient', str_replace(':amount', 100, __('web.gift.vip_inactive_bet_insufficient'))]);
- }
- }
- $amount = (float)$record->per_day_amount;
- try {
- // 发放奖励
- OuroGameService::AddScore($userId, $amount * NumConfig::NUM_VALUE, 52, true); // 52=礼包奖励
- // 更新位标记
- $newMask = $claimedMask | (1 << $dayIndex);
- DB::connection('write')->table('agent.dbo.inactive_vip_gift_records')
- ->where('id', $record->id)
- ->update([
- 'claimed_days_mask' => $newMask,
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- \Log::info('连续未充值VIP礼包奖励领取成功', [
- 'user_id' => $userId,
- 'day' => $day,
- 'amount' => $amount
- ]);
- return apiReturnSuc([
- 'amount' => $amount,
- 'day' => $day,
- 'message' => str_replace(':amount', $amount, __('web.gift.claim_success'))
- ]);
- } catch (\Exception $e) {
- \Log::error('连续未充值VIP礼包奖励领取失败', [
- 'user_id' => $userId,
- 'day' => $day,
- 'error' => $e->getMessage()
- ]);
- return apiReturnFail(['web.gift.claim_failed', str_replace(':error', $e->getMessage(), __('web.gift.claim_failed'))]);
- }
- }
- }
|