PayRechargeController.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. <?php
  2. namespace App\Http\Controllers\Game;
  3. use App\Facade\TableName;
  4. use App\Game\Services\OuroGameService;
  5. use App\Http\Controllers\Controller;
  6. use App\Http\helper\NumConfig;
  7. use App\Models\Order;
  8. use App\Services\OrderServices;
  9. use App\Utility\SetNXLock;
  10. use App\Util;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Redis;
  14. class PayRechargeController extends Controller
  15. {
  16. // 充值记录
  17. public function orderList(Request $request)
  18. {
  19. $user_id = (int)$request->globalUser->UserID;//$request->get('user_id', 1);
  20. $page = $request->get('page', 1);
  21. $pageSize = $request->get('pageSize', 7);
  22. $redisKey = 'PayRecharge_orderList_'.$user_id;
  23. if (!SetNXLock::getExclusiveLock($redisKey)) {
  24. return apiReturnFail(['web.withdraw.try_again_later','Tente novamente mais tarde']);
  25. }
  26. $where[] = ['user_id', $user_id];
  27. $where[] = ['pay_status', 1];
  28. $cacheTime = 60 * rand(1, 2);
  29. // $list = cache()->remember($user_id . '_order_list', $cacheTime, function () use ($where, $pageSize) {
  30. //
  31. // return Order::where($where)
  32. // ->orderBy('finished_at', 'desc')
  33. // ->selectRaw('amount,payment_code,order_sn as payment_sn,finished_at,created_at,pay_status')
  34. // ->paginate(15,['*'],'page',1);
  35. // });
  36. $list = Order::query()->where($where)
  37. ->orderBy('finished_at', 'desc')
  38. ->selectRaw('amount,payment_code,order_sn as payment_sn,finished_at,created_at,pay_status,type')
  39. ->paginate($pageSize);
  40. // ->paginate(15,['*'],'page',1);
  41. foreach ($list as &$val) {
  42. $val->amount = number_format($val->amount, 2, '.', '');
  43. }
  44. SetNXLock::release($redisKey);
  45. return apiReturnSuc($list);
  46. }
  47. // 首充
  48. public function firstPay(Request $request)
  49. {
  50. $user = $request->user();
  51. $user_recharge = DB::table(TableName::QPAccountsDB() . 'YN_VIPAccount')
  52. ->where('UserID', $user->UserID)
  53. ->value('Recharge') ?: 0;
  54. if($user_recharge)return apiReturnSuc();
  55. $firstPayGift = DB::table('agent.dbo.recharge_gift')
  56. ->where('gift_id', 301)->first();
  57. if(!$firstPayGift) return apiReturnFail();
  58. $names = DB::table('agent.dbo.admin_configs')
  59. ->where([
  60. 'type' => 'pay_method',
  61. 'status' => 1,
  62. ])
  63. ->selectRaw('id, name, status, new_pay_type as type')
  64. ->orderByDesc('sort')->get()->toArray();
  65. $list = DB::table('agent.dbo.recharge_gear')
  66. ->select('id','money','favorable_price','give')
  67. ->orderBy('money', 'asc')->where('status', 1)->get();
  68. $gear = Util::filterGearByDevice(\GuzzleHttp\json_encode($names));
  69. foreach ($list as &$val) {
  70. $val->favorable_price = $val->favorable_price + $val->give;
  71. $val->gear = $gear;
  72. $val->recommend = 0;
  73. if($val->money == $firstPayGift->recommend){
  74. $val->recommend = 1;
  75. }
  76. }
  77. return apiReturnSuc(compact('list', 'firstPayGift'));
  78. }
  79. // 首充礼包(带倒计时逻辑)
  80. public function firstPayGift(Request $request)
  81. {
  82. $user = $request->user();
  83. // 获取礼包配置
  84. $giftConfig = DB::table('agent.dbo.recharge_gift')
  85. ->where('gift_id', 301)
  86. ->first();
  87. if (!$giftConfig) {
  88. return apiReturnFail('礼包配置不存在');
  89. }
  90. // 检查用户是否购买了首充礼包
  91. $giftRecord = DB::table('agent.dbo.first_pay_gift_records')
  92. ->where('user_id', $user->UserID)
  93. ->first();
  94. // ========== 未购买礼包:返回充值列表 + 礼包配置 + 倒计时 ==========
  95. if (!$giftRecord) {
  96. // 获取充值档位列表
  97. $list = DB::table('agent.dbo.recharge_gear')
  98. ->select('money', 'favorable_price', 'give', 'gear')
  99. ->orderBy('money', 'asc')
  100. ->where('status', 1)
  101. ->get();
  102. foreach ($list as &$val) {
  103. $val->favorable_price = $val->favorable_price + $val->give;
  104. $val->recommend = 0;
  105. if ($val->money == $giftConfig->recommend) {
  106. $val->recommend = 1;
  107. }
  108. if (!empty($val->gear)) {
  109. $val->gear = Util::filterGearByDevice($val->gear);
  110. }
  111. }
  112. // ========== 处理倒计时逻辑(仅未购买时) ==========
  113. $redisKey = "first_pay_gift_timer_{$user->UserID}";
  114. $currentTime = time();
  115. $timeLeft = 0;
  116. $timerData = Redis::get($redisKey);
  117. if ($timerData) {
  118. $timerData = json_decode($timerData, true);
  119. $firstRequestTime = $timerData['first_request_time'];
  120. $phase = $timerData['phase'];
  121. // 阶段1:valid_h倒计时
  122. if ($phase === 'first') {
  123. $expireTime = $firstRequestTime + ($giftConfig->valid_h * 3600);
  124. if ($currentTime >= $expireTime) {
  125. $expireDate = date('Y-m-d', $expireTime);
  126. $currentDate = date('Y-m-d', $currentTime);
  127. if ($expireDate !== $currentDate) {
  128. // 进入阶段2
  129. $nextDayStart = time();
  130. $newExpireTime = $nextDayStart + ($giftConfig->valid_h_2 * 3600);
  131. $newTimerData = [
  132. 'first_request_time' => $firstRequestTime,
  133. 'phase' => 'daily',
  134. 'current_cycle_start' => $nextDayStart,
  135. 'last_expire_time' => $newExpireTime
  136. ];
  137. Redis::set($redisKey, json_encode($newTimerData));
  138. $timeLeft = max(0, $newExpireTime - $currentTime);
  139. }
  140. } else {
  141. $timeLeft = $expireTime - $currentTime;
  142. }
  143. }
  144. // 阶段2:每日循环
  145. elseif ($phase === 'daily') {
  146. $currentCycleStart = $timerData['current_cycle_start'];
  147. $expireTime = $currentCycleStart + ($giftConfig->valid_h_2 * 3600);
  148. if ($currentTime >= $expireTime) {
  149. $expireDate = date('Y-m-d', $expireTime);
  150. $currentDate = date('Y-m-d', $currentTime);
  151. if ($expireDate !== $currentDate) {
  152. $todayStart = time();
  153. $newExpireTime = $todayStart + ($giftConfig->valid_h_2 * 3600);
  154. $newTimerData = [
  155. 'first_request_time' => $firstRequestTime,
  156. 'phase' => 'daily',
  157. 'current_cycle_start' => $todayStart,
  158. 'last_expire_time' => $newExpireTime
  159. ];
  160. Redis::set($redisKey, json_encode($newTimerData));
  161. $timeLeft = max(0, $newExpireTime - $currentTime);
  162. }
  163. } else {
  164. $timeLeft = $expireTime - $currentTime;
  165. }
  166. }
  167. } else {
  168. // 首次请求,初始化倒计时
  169. $expireTime = $currentTime + ($giftConfig->valid_h * 3600);
  170. $timerData = [
  171. 'first_request_time' => $currentTime,
  172. 'phase' => 'first',
  173. 'last_expire_time' => $expireTime
  174. ];
  175. Redis::set($redisKey, json_encode($timerData));
  176. $timeLeft = $giftConfig->valid_h * 3600;
  177. }
  178. return apiReturnSuc([
  179. 'has_purchased' => false, // 未购买标记
  180. 'list' => $list, // 充值档位列表
  181. 'gift_info' => [
  182. 'gift_id' => $giftConfig->gift_id,
  183. 'gift_name' => $giftConfig->gift_name,
  184. 'total_bonus' => $giftConfig->total_bonus,
  185. 'bonus_instantly' => $giftConfig->bonus_instantly,
  186. 'day_rewards' => $giftConfig->day_rewards ? json_decode($giftConfig->day_rewards) : null,
  187. 'betting_bonus' => $giftConfig->betting_bonus ? json_decode($giftConfig->betting_bonus) : null,
  188. 'betting_task' => $giftConfig->betting_task ? json_decode($giftConfig->betting_task) : null,
  189. ],
  190. 'time_left' => $timeLeft, // ✅ 倒计时(未购买时展示)
  191. 'expire_at' => time() + $timeLeft // ✅ 过期时间戳
  192. ]);
  193. }
  194. // ========== 已购买礼包:返回任务进度(无倒计时) ==========
  195. // 获取用户当前累计下注(从统计表)
  196. $userStats = DB::table('QPRecordDB.dbo.RecordUserTotalStatistics')
  197. ->where('UserID', $user->UserID)
  198. ->first();
  199. $currentTotalBet = $userStats->TotalBet ?? 0;
  200. // 更新下注进度到礼包记录
  201. DB::table('agent.dbo.first_pay_gift_records')
  202. ->where('user_id', $user->UserID)
  203. ->update([
  204. 'betting_current_bet' => $currentTotalBet / NumConfig::NUM_VALUE,
  205. 'updated_at' => date('Y-m-d H:i:s')
  206. ]);
  207. // 重新获取最新记录
  208. $giftRecord = DB::table('agent.dbo.first_pay_gift_records')
  209. ->where('user_id', $user->UserID)
  210. ->first();
  211. // 解析任务数据
  212. $dayRewardsData = $giftRecord->day_rewards_data ? json_decode($giftRecord->day_rewards_data, true) : null;
  213. $bettingBonusData = $giftRecord->betting_bonus_data ? json_decode($giftRecord->betting_bonus_data, true) : null;
  214. $bettingTaskData = $giftRecord->betting_task_data ? json_decode($giftRecord->betting_task_data, true) : null;
  215. // 构建返回数据
  216. $giftInfo = [
  217. 'gift_id' => $giftRecord->gift_id,
  218. 'gift_name' => $giftRecord->gift_name,
  219. 'total_bonus' => (float)$giftRecord->total_bonus,
  220. 'bonus_instantly' => (float)$giftRecord->bonus_instantly,
  221. 'pay_amount' => (float)$giftRecord->pay_amount,
  222. ];
  223. // 每日奖励进度
  224. if ($dayRewardsData) {
  225. // 计算当前是购买后的第几天
  226. $purchaseDate = date('Y-m-d', strtotime($giftRecord->created_at));
  227. $today = date('Y-m-d');
  228. $daysPassed = floor((strtotime($today) - strtotime($purchaseDate)) / 86400);
  229. $startDay = $dayRewardsData['start_day'] ?? 1;
  230. $bonusDay = $dayRewardsData['bonus_day'] ?? 0;
  231. $claimedDays = $giftRecord->day_rewards_claimed;
  232. // $startDay = $startDay-1;
  233. // 判断每日奖励状态
  234. // 0=不可领取, 1=可领取, 2=已领取, 3=过期
  235. $dayRewardStatus = 0; // 默认不可领取
  236. // 检查是否过期(购买后7天)
  237. if ($daysPassed >= 7) {
  238. $dayRewardStatus = 3; // 过期
  239. } elseif ($daysPassed >= $startDay-1) {
  240. // 计算今天是第几个奖励日(从起始天数开始)
  241. $rewardIndex = $daysPassed - ($startDay-1);
  242. if ($rewardIndex < $bonusDay) {
  243. // 在奖励期内
  244. if ($claimedDays > $rewardIndex) {
  245. $dayRewardStatus = 2; // 已领取
  246. } else {
  247. // 即便前几天漏领,后续奖励仍可继续领取(漏掉的视为作废)
  248. $dayRewardStatus = 1; // 可领取
  249. }
  250. } else {
  251. // 超过奖励天数
  252. $dayRewardStatus = 2; // 已领取完
  253. }
  254. }
  255. $giftInfo['day_rewards'] = [
  256. 'total_bonus' => (float)$giftRecord->day_rewards_total,
  257. 'bonus_day' => $bonusDay,
  258. 'start_day' => $startDay,
  259. 'bonus' => $dayRewardsData['bonus'] ?? [],
  260. 'claimed_days' => $claimedDays,
  261. 'progress' => $claimedDays . '/' . $bonusDay,
  262. 'status' => $dayRewardStatus, // 0=不可领取, 1=可领取, 2=已领取, 3=过期
  263. 'status_text' => ['不可领取', '可领取', '已领取', '过期'][$dayRewardStatus] ?? '未知'
  264. ];
  265. }
  266. // 下注奖励进度
  267. if ($bettingBonusData) {
  268. $perBet = $bettingBonusData['per_bet'] ?? 100; // 每次下注要求
  269. $perBetBonus = $bettingBonusData['per_bet_bonus'] ?? 2; // 每次奖励金额(不是百分比)
  270. $currentBet = $giftRecord->betting_current_bet; // 当前累计下注
  271. $totalBonusLimit = $giftRecord->betting_bonus_total; // 总奖励上限
  272. $claimedAmount = $giftRecord->betting_bonus_claimed; // 已领取金额
  273. $remainingBonus = $totalBonusLimit - $claimedAmount; // 剩余可领取金额
  274. // 每次可领取的金额(直接使用配置的值,不是百分比计算)
  275. $perReward = (float)$perBetBonus;
  276. // 根据当前下注计算已达成的次数
  277. $completedTimes = floor($currentBet / $perBet);
  278. // 已领取的次数
  279. $claimedTimes = $claimedAmount > 0 ? floor($claimedAmount / $perReward) : 0;
  280. // 当前可领取次数(但不能超过剩余总额)
  281. $canClaimTimes = $completedTimes - $claimedTimes;
  282. $maxCanClaimTimes = floor($remainingBonus / $perReward); // 剩余总额最多可领次数
  283. $canClaimTimes = min($canClaimTimes, $maxCanClaimTimes);
  284. // 当前可领取金额
  285. $canClaimAmount = min($canClaimTimes * $perReward, $remainingBonus);
  286. // 计算下次领取需要的下注金额
  287. $nextClaimBet = 0;
  288. if ($remainingBonus > 0 && $canClaimTimes == 0) {
  289. // 还有剩余奖励但当前不能领取,计算还需下注多少
  290. $nextClaimBet = ($claimedTimes + 1) * $perBet - $currentBet;
  291. }
  292. // 判断下注奖励状态
  293. // 0=不可领取(可领取金额为0), 1=可领取, 2=已领取(所有奖励都领完), 3=过期
  294. $bettingBonusStatus = 0; // 默认不可领取
  295. // 检查是否过期(购买后7天)
  296. if ($daysPassed >= 7) {
  297. $bettingBonusStatus = 3; // 过期
  298. } elseif ($remainingBonus <= 0) {
  299. $bettingBonusStatus = 2; // 已领取完
  300. } elseif ($canClaimAmount > 0) {
  301. $bettingBonusStatus = 1; // 可领取
  302. } else {
  303. $bettingBonusStatus = 0; // 不可领取(下注不足)
  304. }
  305. $giftInfo['betting_bonus'] = [
  306. 'total_bonus' => (float)$totalBonusLimit, // 总奖励上限
  307. 'per_bet' => $perBet, // 每次下注要求
  308. 'per_bet_bonus' => $perReward, // 每次奖励金额(实际金额)
  309. // 'per_reward' => $perReward, // 每次奖励金额(保持兼容)
  310. 'current_bet' => (float)$currentBet, // 当前累计下注
  311. 'claimed_amount' => (float)$claimedAmount, // 已领取金额
  312. 'remaining_bonus' => (float)$remainingBonus, // 剩余可领金额
  313. 'can_claim_amount' => (float)$canClaimAmount, // 当前可领取金额
  314. 'can_claim_times' => max(0, $canClaimTimes), // 当前可领取次数
  315. 'next_claim_bet' => max(0, $nextClaimBet), // 下次领取还需下注
  316. 'progress' => round($currentBet, 2) . '/' . round($claimedTimes * $perBet + $perBet, 2), // 当前进度/下次领取目标
  317. 'status' => $bettingBonusStatus, // 0=不可领取, 1=可领取, 2=已领取, 3=过期
  318. 'status_text' => ['不可领取', '可领取', '已领取', '过期'][$bettingBonusStatus] ?? '未知'
  319. ];
  320. }
  321. // 下注任务进度
  322. if ($bettingTaskData) {
  323. $betPayTimes = $bettingTaskData['bet_pay_times'] ?? 60;
  324. $requiredBet = $giftRecord->pay_amount * $betPayTimes;
  325. $currentProgress = $giftRecord->betting_current_bet;
  326. $taskCompleted = $currentProgress >= $requiredBet;
  327. $isClaimed = $giftRecord->betting_task_claimed == 1;
  328. // 判断下注任务状态
  329. // 0=不可领取, 1=可领取, 2=已领取, 3=过期
  330. $bettingTaskStatus = 0; // 默认不可领取
  331. // 检查是否过期(购买后7天)
  332. if ($daysPassed >= 7) {
  333. $bettingTaskStatus = 3; // 过期
  334. } elseif ($isClaimed) {
  335. $bettingTaskStatus = 2; // 已领取
  336. } elseif ($taskCompleted) {
  337. $bettingTaskStatus = 1; // 可领取(任务完成且未领取)
  338. } else {
  339. $bettingTaskStatus = 0; // 不可领取(任务未完成)
  340. }
  341. $giftInfo['betting_task'] = [
  342. 'total_bonus' => (float)$giftRecord->betting_task_total,
  343. 'bet_pay_times' => $betPayTimes,
  344. 'required_bet' => $requiredBet, // 需要下注的金额
  345. 'current_bet' => (float)$currentProgress, // 当前累计下注
  346. 'progress' => round($currentProgress, 2) . '/' . $requiredBet,
  347. 'status' => $bettingTaskStatus, // 0=不可领取, 1=可领取, 2=已领取, 3=过期
  348. 'status_text' => ['不可领取', '可领取', '已领取', '过期'][$bettingTaskStatus] ?? '未知'
  349. ];
  350. }
  351. $giftInfo['expired'] = strtotime($giftRecord->created_at)+86400*7;
  352. return apiReturnSuc([
  353. 'has_purchased' => true, // 已购买标记
  354. 'gift_info' => $giftInfo
  355. // ✅ 已购买用户不返回倒计时
  356. ]);
  357. }
  358. /**
  359. * 领取首充礼包奖励
  360. */
  361. public function claimFirstPayGiftReward(Request $request)
  362. {
  363. $user = $request->user();
  364. $userId = $user->UserID;
  365. $rewardType = $request->input('reward_type'); // 'day_reward', 'betting_bonus', 'betting_task'
  366. // 获取礼包记录
  367. $giftRecord = DB::table('agent.dbo.first_pay_gift_records')
  368. ->where('user_id', $userId)
  369. ->first();
  370. if (!$giftRecord) {
  371. return apiReturnFail('未购买首充礼包');
  372. }
  373. $rewardAmount = 0;
  374. try {
  375. // 每日奖励领取
  376. if ($rewardType === 'day_reward') {
  377. $dayRewardsData = json_decode($giftRecord->day_rewards_data, true);
  378. if (!$dayRewardsData) {
  379. return apiReturnFail('没有每日奖励配置');
  380. }
  381. $bonusDay = $dayRewardsData['bonus_day'] ?? 0;
  382. $startDay = $dayRewardsData['start_day'] ?? 1;
  383. $bonusArray = $dayRewardsData['bonus'] ?? [];
  384. $claimedDays = $giftRecord->day_rewards_claimed;
  385. // 检查是否还有可领取的天数
  386. if ($claimedDays >= $bonusDay) {
  387. return apiReturnFail('每日奖励已全部领取');
  388. }
  389. // 计算当前是购买后的第几天
  390. $purchaseDate = date('Y-m-d', strtotime($giftRecord->created_at));
  391. $currentDate = date('Y-m-d');
  392. $daysPassed = floor((strtotime($currentDate) - strtotime($purchaseDate)) / 86400);
  393. // 检查是否到了可以领取的天数(从第start_day天开始)
  394. if ($daysPassed < $startDay-1) {
  395. return apiReturnFail('还未到可领取时间,需要从第' . $startDay . '天开始');
  396. }
  397. // 计算今天应该领取第几天的奖励
  398. // 例如:start_day=2,今天是第3天,应该领取第1个奖励(索引0)
  399. $todayRewardIndex = $daysPassed - ($startDay-1);
  400. // 检查是否超过奖励天数
  401. if ($todayRewardIndex >= $bonusDay) {
  402. return apiReturnFail('每日奖励已过期');
  403. }
  404. // 检查今天是否已领取
  405. $lastClaimDate = $giftRecord->day_last_claim_date;
  406. if ($lastClaimDate && $lastClaimDate === $currentDate) {
  407. return apiReturnFail('今天已经领取过了');
  408. }
  409. // 检查是否跳过了某些天(过期不补领)
  410. // 如果今天应该领第5天的奖励,但用户只领了3天,那就直接领第5天的
  411. if ($todayRewardIndex > $claimedDays) {
  412. // 跳过了一些天,更新已领取天数为今天的索引
  413. $claimedDays = $todayRewardIndex;
  414. }
  415. // 计算今天可领取的奖励(使用todayRewardIndex作为索引)
  416. $todayBonusPercent = $bonusArray[$todayRewardIndex] ?? 0;
  417. $rewardAmount = round($giftRecord->pay_amount * $todayBonusPercent / 100, 2);
  418. // 更新记录
  419. DB::table('agent.dbo.first_pay_gift_records')
  420. ->where('user_id', $userId)
  421. ->update([
  422. 'day_rewards_claimed' => $todayRewardIndex + 1, // 已领取到第几天(索引+1)
  423. 'day_last_claim_date' => $currentDate,
  424. 'updated_at' => date('Y-m-d H:i:s')
  425. ]);
  426. }
  427. // 下注奖励领取
  428. elseif ($rewardType === 'betting_bonus') {
  429. $bettingBonusData = json_decode($giftRecord->betting_bonus_data, true);
  430. if (!$bettingBonusData) {
  431. return apiReturnFail('没有下注奖励配置');
  432. }
  433. $perBet = $bettingBonusData['per_bet'] ?? 100; // 每次下注要求
  434. $perBetBonus = $bettingBonusData['per_bet_bonus'] ?? 2; // 每次奖励金额(不是百分比)
  435. $currentBet = $giftRecord->betting_current_bet; // 当前累计下注
  436. $totalBonusLimit = $giftRecord->betting_bonus_total; // 总奖励上限
  437. $claimedAmount = $giftRecord->betting_bonus_claimed; // 已领取金额
  438. $remainingBonus = $totalBonusLimit - $claimedAmount; // 剩余可领金额
  439. // 检查是否还有剩余奖励
  440. if ($remainingBonus <= 0) {
  441. return apiReturnFail('下注奖励已全部领取');
  442. }
  443. // 每次可领取的金额(直接使用配置的值,不是百分比计算)
  444. $perReward = (float)$perBetBonus;
  445. // 根据当前下注计算已达成的次数
  446. $completedTimes = floor($currentBet / $perBet);
  447. // 已领取的次数
  448. $claimedTimes = $claimedAmount > 0 ? floor($claimedAmount / $perReward) : 0;
  449. // 可领取次数(下注达成的次数 - 已领取次数)
  450. $canClaimTimes = $completedTimes - $claimedTimes;
  451. // 检查是否可以领取
  452. if ($canClaimTimes <= 0) {
  453. return apiReturnFail('下注金额不足,还需下注 ' . (($claimedTimes + 1) * $perBet - $currentBet) . ' 才能领取');
  454. }
  455. // ✅ 一次性领取所有已达成的奖励(但不能超过剩余总额)
  456. $totalCanClaimAmount = $canClaimTimes * $perReward; // 理论可领取总额
  457. $rewardAmount = min($totalCanClaimAmount, $remainingBonus); // 实际领取金额(不超过剩余总额)
  458. $actualClaimTimes = floor($rewardAmount / $perReward); // 实际领取次数
  459. // 更新记录
  460. DB::table('agent.dbo.first_pay_gift_records')
  461. ->where('user_id', $userId)
  462. ->update([
  463. 'betting_bonus_claimed' => $giftRecord->betting_bonus_claimed + $rewardAmount,
  464. 'updated_at' => date('Y-m-d H:i:s')
  465. ]);
  466. \Log::info('下注奖励领取', [
  467. 'user_id' => $userId,
  468. 'can_claim_times' => $canClaimTimes,
  469. 'actual_claim_times' => $actualClaimTimes,
  470. 'reward_amount' => $rewardAmount
  471. ]);
  472. }
  473. // 下注任务领取
  474. elseif ($rewardType === 'betting_task') {
  475. if ($giftRecord->betting_task_claimed == 1) {
  476. return apiReturnFail('下注任务奖励已领取');
  477. }
  478. $bettingTaskData = json_decode($giftRecord->betting_task_data, true);
  479. if (!$bettingTaskData) {
  480. return apiReturnFail('没有下注任务配置');
  481. }
  482. $betPayTimes = $bettingTaskData['bet_pay_times'] ?? 60;
  483. $requiredBet = $giftRecord->pay_amount * $betPayTimes;
  484. $currentBet = $giftRecord->betting_current_bet;
  485. if ($currentBet < $requiredBet) {
  486. return apiReturnFail('下注金额不足,需要下注 ' . $requiredBet);
  487. }
  488. $rewardAmount = $giftRecord->betting_task_total;
  489. // 更新记录
  490. DB::table('agent.dbo.first_pay_gift_records')
  491. ->where('user_id', $userId)
  492. ->update([
  493. 'betting_task_claimed' => 1,
  494. 'updated_at' => date('Y-m-d H:i:s')
  495. ]);
  496. }
  497. else {
  498. return apiReturnFail('无效的奖励类型');
  499. }
  500. // 添加奖励到用户账户
  501. if ($rewardAmount > 0) {
  502. OuroGameService::AddScore($userId, $rewardAmount * NumConfig::NUM_VALUE, 52, true); // 52=首充礼包奖励
  503. \Log::info('首充礼包奖励领取', [
  504. 'user_id' => $userId,
  505. 'reward_type' => $rewardType,
  506. 'amount' => $rewardAmount
  507. ]);
  508. }
  509. return apiReturnSuc([
  510. 'amount' => $rewardAmount,
  511. 'message' => '成功领取 ' . $rewardAmount . ' 奖励'
  512. ]);
  513. } catch (\Exception $e) {
  514. \Log::error('首充礼包奖励领取失败', [
  515. 'user_id' => $userId,
  516. 'error' => $e->getMessage()
  517. ]);
  518. return apiReturnFail('领取失败:' . $e->getMessage());
  519. }
  520. }
  521. /**
  522. * 获取首充礼包数据(包含充值档位列表)
  523. * @param $firstPayGift 礼包配置
  524. * @param $timeLeft 剩余秒数(已经是用户实际剩余时间)
  525. */
  526. private function getFirstPayGiftData($firstPayGift, $timeLeft)
  527. {
  528. // 获取支付方式
  529. // $names = DB::table('agent.dbo.admin_configs')
  530. // ->where([
  531. // 'type' => 'pay_method',
  532. // 'status' => 1,
  533. // ])
  534. // ->selectRaw('id, name, status, new_pay_type as type')
  535. // ->orderByDesc('sort')
  536. // ->get()
  537. // ->toArray();
  538. // 获取充值档位
  539. $list = DB::table('agent.dbo.recharge_gear')
  540. ->select('money', 'favorable_price', 'give','gear')
  541. ->orderBy('money', 'asc')
  542. ->where('status', 1)
  543. ->where('in_shop', 1)
  544. ->get();
  545. // $gear = \GuzzleHttp\json_encode($names);
  546. foreach ($list as &$val) {
  547. $val->favorable_price = $val->favorable_price + $val->give;
  548. // $val->gear = $gear;
  549. $val->recommend = 0;
  550. if ($val->money == $firstPayGift->recommend) {
  551. $val->recommend = 1;
  552. }
  553. if (!empty($val->gear)) {
  554. $val->gear = Util::filterGearByDevice($val->gear);
  555. }
  556. }
  557. return apiReturnSuc([
  558. 'list' => $list,
  559. 'gift_info' => [
  560. 'gift_id' => $firstPayGift->gift_id,
  561. 'gift_name' => $firstPayGift->gift_name,
  562. 'total_bonus' => $firstPayGift->total_bonus,
  563. 'bonus_instantly' => $firstPayGift->bonus_instantly,
  564. 'day_rewards' => $firstPayGift->day_rewards?json_decode($firstPayGift->day_rewards):'',
  565. 'betting_bonus' => $firstPayGift->betting_bonus?json_decode($firstPayGift->betting_bonus):'',
  566. 'betting_task' => $firstPayGift->betting_task?json_decode($firstPayGift->betting_task):'',
  567. ],
  568. 'time_left' => $timeLeft, // 剩余秒数
  569. 'expire_at' => time() + $timeLeft // 过期时间戳
  570. ]);
  571. }
  572. public function firstPayMulti(Request $request)
  573. {
  574. $user = LoginController::checkLogin($request);
  575. if($user){
  576. if(env('CONFIG_24680_NFTD_99',0)==0)if($user->Channel==99)return apiReturnFail();
  577. $fpkey='Firstpay_'.$user->UserID;
  578. if(Redis::exists($fpkey)){
  579. $data=Redis::get($fpkey);
  580. $data=json_decode($data,true);
  581. $data['timeleft']=86400-(time()-$data['buytime']);
  582. return apiReturnSuc(['leftitem'=>$data]);
  583. }
  584. $user_recharge = DB::table(TableName::QPAccountsDB() . 'YN_VIPAccount')
  585. ->where('UserID', $user->UserID)
  586. ->value('Recharge') ?: 0;
  587. if($user_recharge)return apiReturnFail();
  588. }
  589. $items=DB::table('agent.dbo.recharge_gear')
  590. ->where('status',1)
  591. ->where('second_give','>',0)
  592. ->where('first_pay','>=', 1)
  593. ->select('gift_id','money','give','favorable_price','second_give')->get()->each(function($item){
  594. $item->id=28;
  595. })->toArray();
  596. $default=count($items)-1;
  597. return apiReturnSuc(compact('items','default'));
  598. }
  599. // 破产礼包
  600. public function bankruptcyGift(Request $request)
  601. {
  602. $user = $request->user();
  603. // 判断用户是否充值
  604. $user_recharge = DB::table(TableName::QPAccountsDB() . 'YN_VIPAccount')
  605. ->where('UserID', $user->UserID)
  606. ->value('Recharge') ?: 0;
  607. if ($user_recharge <= 0) {
  608. return apiReturnFail('用户未充值');
  609. }
  610. // 获取所有破产礼包配置 (gift_id=302)
  611. $bankruptcyGifts = DB::table('agent.dbo.recharge_gift')
  612. ->where('gift_id', 302)
  613. ->get();
  614. if ($bankruptcyGifts->isEmpty()) {
  615. return apiReturnFail('破产礼包配置不存在');
  616. }
  617. $result = [];
  618. // 遍历每条礼包配置,根据 recommend 关联 recharge_gear
  619. foreach ($bankruptcyGifts as $gift) {
  620. $gear = DB::table('agent.dbo.recharge_gear')
  621. ->select('money', 'favorable_price', 'gear')
  622. ->where('money', $gift->recommend)
  623. ->where('status', 1)
  624. ->first();
  625. if ($gear) {
  626. $gear->gift_id = $gift->gift_id;
  627. $gear->total_bonus = $gift->total_bonus;
  628. $gear->bonus = $gift->total_bonus - 100;
  629. if (!empty($gear->gear)) {
  630. $gear->gear = Util::filterGearByDevice($gear->gear);
  631. }
  632. $result[] = $gear;
  633. }
  634. }
  635. // 按 money 排序
  636. usort($result, function($a, $b) {
  637. return $a->money <=> $b->money;
  638. });
  639. return apiReturnSuc($result);
  640. }
  641. public function getSecondGive(Request $request)
  642. {
  643. $user = $request->user();
  644. $fpkey='Firstpay_'.$user->UserID;
  645. if(Redis::exists($fpkey)){
  646. $data=Redis::get($fpkey);
  647. $data=json_decode($data,true);
  648. $data['timeleft']=86400-(time()-$data['buytime']);
  649. if($data['timeleft']<=0) {
  650. Redis::del($fpkey);
  651. //加钱
  652. if($data['second_give']){
  653. $czReason=$data['czReason'];
  654. $cjReason=$data['cjReason'];
  655. [$OrgScore,$NowScore]=OuroGameService::AddScore($user->UserID,$data['second_give']*NumConfig::NUM_VALUE,$cjReason);
  656. //更新二次领钱记录
  657. DB::table(TableName::agent() . 'guide_payment')->where('UserID',$user->UserID)->update([
  658. 'GetSecondTime' => now(),
  659. 'SecondScoreNum'=>$data['second_give']*NumConfig::NUM_VALUE
  660. ]);
  661. return apiReturnSuc(compact('OrgScore','NowScore'));
  662. }
  663. return apiReturnSuc();
  664. }
  665. }
  666. return apiReturnFail(['web.withdraw.try_again_later','Tente novamente mais tarde']);
  667. }
  668. }