LuckDrawController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Wen\Utils\Settings;
  4. use App\Wen\Utils\UserUtils;
  5. use App\Models\User\WxUserLuckDrawRecord;
  6. use Illuminate\Http\Request;
  7. class LuckDrawController extends BaseController{
  8. public function getLuckDrawInfo(Request $request){
  9. $uid = $request->uid;
  10. $freeNumDay = Settings::get('app_luck_draw_times', 3);
  11. $data = [
  12. 'prizeList' => [],
  13. 'userCoins' => UserUtils::user_coins($uid),
  14. 'restFreeNum' => $freeNumDay - $this->getTodayLuckDrawTimes($uid, 4),
  15. 'goldNumEvery' => Settings::get('app_luck_draw_coin_pay', 20),
  16. 'freeNumDay' => $freeNumDay,
  17. 'tips' => Settings::get('app_luck_draw_tips',[]),
  18. 'background' => Settings::get('app_luck_draw_background', 'https://img.mini.minisns.cn/hbx/video/luckdrawtopbg.png')
  19. ];
  20. $app_luck_draw = Settings::get('app_luck_draw', []);
  21. if($app_luck_draw){
  22. foreach ($app_luck_draw as $item){
  23. $data['prizeList'][] = [
  24. 'prizeId'=> $item['id'],
  25. 'prizeName' => $item['name'],
  26. 'prizeStock' => 99999,
  27. 'prizeWeight' => $item['weight'],
  28. 'prizeImage' => $item['img']
  29. ];
  30. }
  31. }
  32. return $this->success($data);
  33. }
  34. public function userLuckDraw(Request $request){
  35. $uid = $request->uid;
  36. $freeNumDay = Settings::get('app_luck_draw_times', 3);
  37. $freeTimes = $this->getTodayLuckDrawTimes($uid, 4);
  38. if($freeNumDay - $freeTimes > 0){
  39. $mostLuckItem = $this->getMostLuck();
  40. UserUtils::user_gift($uid, 1, $mostLuckItem['type'], $mostLuckItem['num'], '抽奖奖励:'.$mostLuckItem['name'], 8, _array_key($mostLuckItem, 'product', 0) );
  41. $model = new WxUserLuckDrawRecord();
  42. $model->user_id = $uid;
  43. $model->pay = 4;
  44. $model->type = $mostLuckItem['type'];
  45. $model->num = $mostLuckItem['num'];
  46. $model->tip = $mostLuckItem['name'];
  47. $model->save();
  48. $data = [
  49. 'userCoins' => UserUtils::user_coins($uid),
  50. 'restFreeNum' => $freeNumDay - $this->getTodayLuckDrawTimes($uid, 4),
  51. 'prizeId' => $mostLuckItem['id']
  52. ];
  53. return $this->success($data);
  54. }else{
  55. $coins = UserUtils::user_coins($uid);
  56. $goldNumEvery = Settings::get('app_luck_draw_coin_pay', 20);
  57. if($coins - $goldNumEvery >= 0){
  58. $r = UserUtils::update_user_coins($uid, 14, -$goldNumEvery, '金币抽奖:-'.$goldNumEvery);
  59. if($r){
  60. $mostLuckItem = $this->getMostLuck();
  61. UserUtils::user_gift($uid, 1, $mostLuckItem['type'], $mostLuckItem['num'], '抽奖奖励:'.$mostLuckItem['name'], 8, _array_key($mostLuckItem, 'product', 0) );
  62. $model = new WxUserLuckDrawRecord();
  63. $model->user_id = $uid;
  64. $model->pay = 0;
  65. $model->type = $mostLuckItem['type'];
  66. $model->num = $mostLuckItem['num'];
  67. $model->tip = $mostLuckItem['name'];
  68. $model->save();
  69. $data = [
  70. 'userCoins' => UserUtils::user_coins($uid),
  71. 'restFreeNum' => $freeNumDay - $this->getTodayLuckDrawTimes($uid, 4),
  72. 'prizeId' => $mostLuckItem['id']
  73. ];
  74. return $this->success($data);
  75. }else{
  76. return $this->fail(200011);
  77. }
  78. // 金币付费
  79. }else{
  80. // 金币不足
  81. return $this->fail(200011);
  82. }
  83. }
  84. }
  85. public function getMostLuck(){
  86. $luck_draw = Settings::get('app_luck_draw', []);
  87. $total_weight = 0;
  88. foreach ($luck_draw as $item){
  89. $total_weight += $item['weight'];
  90. }
  91. $rand = mt_rand(1, $total_weight);
  92. $current = 0;
  93. foreach ($luck_draw as $item){
  94. $current += $item['weight'];
  95. if($current >= $rand){
  96. return $item;
  97. }
  98. }
  99. return null;
  100. }
  101. public function getTodayLuckDrawTimes($uid, $pay_type = -1){
  102. if($pay_type == -1){
  103. return WxUserLuckDrawRecord::where('user_id', $uid)
  104. ->where('created_at', 'like', '%' . date("Y-m-d", time()) . '%')
  105. ->count();
  106. }else{
  107. return WxUserLuckDrawRecord::where([['user_id', '=', $uid], ['pay', '=', $pay_type]])
  108. ->where('created_at', 'like', '%' . date("Y-m-d", time()) . '%')
  109. ->count();
  110. }
  111. }
  112. }