156 lines
5.5 KiB
PHP
156 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Coin;
|
|
use App\Models\CoinClue;
|
|
use App\Models\UserCoin;
|
|
use App\Models\UserCoinClue;
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
|
use Illuminate\Http\Request;
|
|
|
|
class CoinController extends Controller
|
|
{
|
|
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
|
|
|
|
public function list(Request $request)
|
|
{
|
|
$type = (int)$request->input('type', 0);
|
|
$list = Coin::query()->where('status', Coin::STATUS_OPEN)
|
|
->where(function ($query) use ($type) {
|
|
if ($type) {
|
|
$query->where('type', $type);
|
|
} else {
|
|
$query->where('type', '!=', Coin::TYPE_JIN);
|
|
}
|
|
})->get();
|
|
$userCoin = UserCoin::query()
|
|
->where('user_id', $request->user()->id)
|
|
->whereIn('coin_id', $list->pluck('id')->toArray())
|
|
->get()->keyBy('coin_id');
|
|
$coinClue = CoinClue::query()->where('is_circle', CoinClue::IS_CIRCLE_YES)->whereIn('coin_id', $list->pluck('id')->toArray())->get()->groupBy('coin_id');
|
|
$userCoinClue = UserCoinClue::query()->where('user_id', $request->user()->id)->where(function ($query) use ($coinClue) {
|
|
if ($k = $coinClue->keys()->toArray()) {
|
|
$query->whereIn('coin_id', $k);
|
|
} else {
|
|
$query->whereRaw('1 != 1');
|
|
}
|
|
})->get()->groupBy('coin_id');
|
|
foreach ($list as $key => &$value) {
|
|
if ($value->type != Coin::TYPE_JIN) {
|
|
$value->clue_count = isset($coinClue[$value->id]) ? $coinClue[$value->id]->count() : 0;
|
|
$value->user_clue_count = isset($userCoinClue[$value->id]) ? $userCoinClue[$value->id]->count() : 0;
|
|
$value->user_clue_count = min($value->user_clue_count, $value->clue_count);
|
|
} else {
|
|
$value->is_unlock = isset($userCoin[$value->id]) ? Coin::IS_UNLOCK_YES : Coin::IS_UNLOCK_NO;
|
|
if ($value->is_unlock) {
|
|
unset($list[$key]);
|
|
}
|
|
}
|
|
}
|
|
unset($value);
|
|
return $this->success($list->values()->toArray());
|
|
}
|
|
|
|
public function info(Request $request)
|
|
{
|
|
$id = $request->input('id');
|
|
if (!$id) {
|
|
$this->error('参数错误');
|
|
}
|
|
$coin = Coin::query()->find($id);
|
|
if (!$coin) {
|
|
$this->error('硬币不存在');
|
|
}
|
|
|
|
return $this->success($coin->toArray());
|
|
}
|
|
|
|
public function clueList(Request $request)
|
|
{
|
|
$id = $request->input('id');
|
|
$userId = $request->user()->id;
|
|
if (!$id) {
|
|
$this->error('参数错误');
|
|
}
|
|
$coin = Coin::query()->find($id);
|
|
if (!$coin) {
|
|
$this->error('硬币不存在');
|
|
}
|
|
if ($coin->type == Coin::TYPE_JIN) {
|
|
return $this->success([[
|
|
'name' => '今日线索',
|
|
'is_unlock' => CoinClue::IS_UNLOCK_YES,
|
|
'type' => CoinClue::TYPE_TEXT,
|
|
'content' => '在上海市里',
|
|
]]);
|
|
}
|
|
|
|
$clue = CoinClue::query()->where('coin_id', $id)->get()->toArray();
|
|
$userCoinClue = UserCoinClue::query()->where(['coin_id' => $id, 'user_id' => $userId])->get()->keyBy('coin_clue_id')->toArray();
|
|
|
|
foreach ($clue as &$value) {
|
|
$value['is_unlock'] = isset($userCoinClue[$value['id']]) ? CoinClue::IS_UNLOCK_YES : CoinClue::IS_UNLOCK_NO;
|
|
}
|
|
unset($value);
|
|
return $this->success($clue);
|
|
}
|
|
|
|
public function userTop(Request $request)
|
|
{
|
|
$list = UserCoin::query()
|
|
->leftJoin('coin', 'coin.id', '=', 'user_coin.coin_id')
|
|
->leftJoin('users', 'user_coin.user_id', '=', 'users.id')
|
|
->selectRaw('user_coin.user_id, count(*) as num, coin.type, users.name')
|
|
->groupBy(['coin.type', 'user_coin.user_id'])
|
|
->orderBy('coin.type', 'asc')
|
|
->orderBy('num', 'desc')
|
|
->get()->groupBy('type')->toArray();
|
|
|
|
|
|
return $this->success($list);
|
|
}
|
|
|
|
/**
|
|
* 前端找到硬币,扫码后获取的信息
|
|
* @param id 硬币id
|
|
* @param token 用于校验用户扫码合法性
|
|
*/
|
|
public function getCoinInfo(Request $request)
|
|
{
|
|
$id = $request->input('id');
|
|
$token = $request->input('token');
|
|
|
|
if (!$id || !$token) return $this->error('参数错误');
|
|
|
|
$coin = Coin::select('id', 'name', 'type', 'longitude', 'latitude', 'address', 'is_unlock')->where('id', $id)->first();
|
|
if ($coin['is_unlock'] === Coin::IS_UNLOCK_YES) {
|
|
return $this->error('该硬币已被领取');
|
|
}
|
|
unset($coin['is_unlock']);
|
|
$returnData = clone $coin;
|
|
unset($coin['name']);
|
|
unset($coin['address']);
|
|
|
|
$token = md5(json_encode($coin));
|
|
if ($token !== $request->input('token')) {
|
|
return $this->error('token错误');
|
|
}
|
|
return $this->success($returnData); // 验证通过,返回此硬币信息
|
|
}
|
|
|
|
/**
|
|
* 查看硬币的找到证据 视频
|
|
*/
|
|
public function getCoinVideo(Request $request)
|
|
{
|
|
$coin_id = $request->input('coin_id');
|
|
// $userId = $request->user();
|
|
$coin = UserCoin::query()->where('coin_id', $coin_id)->first();
|
|
if (!$coin) return $this->error('数据混乱,未找到硬币被领取的信息');
|
|
return $this->success($coin->content);
|
|
}
|
|
}
|