PayRechargeController.php 32 KB

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