0
0
Files
Babyclass/app/Http/Controllers/LotteryController.php
2020-08-04 10:09:42 +08:00

126 lines
3.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情等我喝醉了再说吧 |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace App\Http\Controllers;
use App\Events\Lottery as LotteryEvent;
use App\Models\Lottery;
use App\Models\LotteryGift;
use App\Models\LotteryLog;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
/**
* 抽奖
*/
class LotteryController extends Controller
{
public function __construct(Request $request)
{
parent::__construct($request);
View::share('nav', 2);
}
public function index()
{
$lottery = Lottery::find(1);
return view('lottery.index', compact('lottery'));
}
//抽奖
public function draw()
{
$lottery = Lottery::find(1);
if (!$lottery) {
return $this->error('没有这个活动');
} elseif ($lottery->status != 1 || $lottery->start_at->timestamp > time()) {
return $this->error('活动没有开启');
} elseif ($lottery->end_at->timestamp <= time()) {
return $this->error('活动已经结束');
}
$list = LotteryGift::with(['item'])
->where('lottery_id', 1)
->orderBy('chance', 'asc')
->get();
// $newlist = collect([]);
// foreach ($list as $key => $gift) {
// $newlist->put($gift->site, $gift);
// }
$user = Auth::user();
if ($user->lottery_num == 0) {
return $this->error('您没有抽奖机会');
}
$gift = self::get_rand($list);
$user->decrement('lottery_num');
if ($user->id == 1) {
$gift = LotteryGift::find(15);
}
if (empty($gift) || $gift->number == 0 || $gift->type == 0) {
return $this->error(['site' => 6, 'win' => '很遗憾', 'title' => '您没有中奖!', 'lottery_num' => $user->lottery_num]);
} else {
$gift->decrement('number');
$log = $lottery->logs()->create([
'user_id' => $user->id,
'gift_id' => $gift->id,
]);
event(new LotteryEvent($log));
return $this->success(['site' => $gift->site, 'win' => '恭喜您', 'title' => '抽中了 ' . $gift->item->getTitle(), 'lottery_num' => $user->lottery_num, 'allnum' => $gift->allnum, 'randNum' => $gift->randNum]);
}
}
public function get_rand($gifts)
{
$result = '';
$allnum = $gifts->sum('chance');
//概率数组循环
foreach ($gifts as $gift) {
$randNum = mt_rand(1, $allnum);
if ($randNum <= $gift->chance) {
$gift->randNum = $randNum;
$gift->allnum = $allnum;
$result = $gift;
break;
} else {
$allnum -= $gift->chance;
}
}
unset($gifts);
return $result;
}
public function logs(Request $request)
{
$type = $request->type ?? 'goods';
$class = LotteryGift::$class;
$item_type = $class[$type];
$lists = LotteryLog::with(['gift.item'])->whereHas('gift', function ($query) use ($item_type) {
$query->where('type', 1)->where('item_type', $item_type);
})->where('user_id', Auth::id())->get();
view()->share('title', '抽奖礼品');
return view('lottery.logs', compact('lists', 'type'));
}
public function message()
{
return view('lottery.message');
}
}