generated from liutk/owl-admin-base
6-23新增活动竞猜记录接口
parent
d8fcac442f
commit
14dc6271d7
|
|
@ -7,7 +7,9 @@ use Illuminate\Http\Request;
|
|||
use Illuminate\Support\Arr;
|
||||
use App\Models\Activity;
|
||||
use App\Models\ActivityGame;
|
||||
use App\Models\UserGame;
|
||||
use App\Http\Resources\Api\ActivityGameResource;
|
||||
use App\Http\Resources\Api\UserGameResource;
|
||||
use App\Http\Requests\JoinGameRequest;
|
||||
use App\Services\Api\ActivityGameService;
|
||||
|
||||
|
|
@ -69,4 +71,24 @@ class ActivityGameController extends ApiController
|
|||
return $this->error($res['message']);
|
||||
}
|
||||
}
|
||||
|
||||
public function latestUserGame(Request $request)
|
||||
{
|
||||
$list = UserGame::with('user', 'game', 'userActivity')->orderBy('created_at', 'desc')->limit(30)->get();
|
||||
return $this->success(['list' => UserGameResource::collection($list)->resolve()]);
|
||||
}
|
||||
|
||||
public function activityUserGame(Request $request)
|
||||
{
|
||||
$activityId = $request->input('activity_id', 0);
|
||||
$userId = $request->input('user_id', auth('api')->user()?->id ?? 0);
|
||||
|
||||
$list = UserGame::with('user', 'game')->where([
|
||||
'activity_id' => $activityId,
|
||||
'user_id' => $userId,
|
||||
])->orderBy('created_at', 'desc')
|
||||
->simplePaginate($request->query('per_page', 20));
|
||||
|
||||
return $this->success(['list'=>UserGameResource::collection($list)->resolve()]);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Resources\Api;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class UserGameResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'activity_id' => $this->activity_id,
|
||||
'user_id' => $this->user_id,
|
||||
'nick_name' => $this->whenLoaded('user', function () {
|
||||
return $this->user?->nick_name ?? '';
|
||||
}, ''),
|
||||
'avatar' => $this->whenLoaded('user', function () {
|
||||
return $this->user?->avatar ?? '';
|
||||
}, ''),
|
||||
'game_name' => $this->whenLoaded('game', function () {
|
||||
return $this->game?->name ?? '';
|
||||
}, ''),
|
||||
'game_home_field' => $this->whenLoaded('game', function () {
|
||||
return $this->game?->home_field ?? '';
|
||||
}, ''),
|
||||
'game_away' => $this->whenLoaded('game', function () {
|
||||
return $this->game?->away ?? '';
|
||||
}, ''),
|
||||
'game_at' => $this->whenLoaded('game', function () {
|
||||
return $this->game?->game_at ? $this->game->game_at->format('Y-m-d H:i:s'): '';
|
||||
}),
|
||||
'game_day' => $this->whenLoaded('game', function () {
|
||||
return $this->game?->game_at ? $this->game->game_at->format('Y-m-d'): '';
|
||||
}),
|
||||
'score' => $this->score,
|
||||
'status' => $this->whenLoaded('game', function () {
|
||||
return $this->game?->state ==0 ?'0':($this->is_right ? '1':'2');
|
||||
}, '0'),
|
||||
'history' => $this->whenLoaded('userActivity', function () {
|
||||
return ($this->userActivity->join_times ?? 0).'中'.($this->userActivity->right_times ?? 0);
|
||||
}, ''),
|
||||
];
|
||||
}
|
||||
|
||||
public function with($request)
|
||||
{
|
||||
return ['code' => Response::HTTP_OK, 'message' => ''];
|
||||
}
|
||||
}
|
||||
|
|
@ -13,6 +13,7 @@ class UserGame extends Model
|
|||
protected $fillable = [
|
||||
'user_id',
|
||||
'activity_id',
|
||||
'user_activity_id',
|
||||
'game_id',
|
||||
'score',
|
||||
'is_right'
|
||||
|
|
@ -32,4 +33,9 @@ class UserGame extends Model
|
|||
{
|
||||
return $this->belongsTo(ActivityGame::class, 'game_id');
|
||||
}
|
||||
|
||||
public function userActivity()
|
||||
{
|
||||
return $this->belongsTo(UserActivity::class, 'user_activity_id');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,12 @@ class ActivityGameService
|
|||
'user_id' => $user->id,
|
||||
'game_id' => $game->id,
|
||||
])->exists()){
|
||||
$res['message'] = '您已参与竞猜,请勿重复提交';
|
||||
$res['message'] = '您已参与该场竞猜,请勿重复提交';
|
||||
return $res;
|
||||
}
|
||||
//
|
||||
if(UserGame::whereDate('created_at', now())->exists()){
|
||||
$res['message'] = '您今日已参与竞猜,请明天再来';
|
||||
return $res;
|
||||
}
|
||||
//判断该场竞猜是否还能参加
|
||||
|
|
@ -64,6 +69,7 @@ class ActivityGameService
|
|||
UserGame::create([
|
||||
'user_id' => $user->id,
|
||||
'activity_id' =>$game->activity_id,
|
||||
'user_activity_id' =>$userActivity->id,
|
||||
'game_id'=>$game->id,
|
||||
'score'=>$score,
|
||||
]);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('user_games', function (Blueprint $table) {
|
||||
//
|
||||
$table->unsignedBigInteger('user_activity_id')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('user_games', function (Blueprint $table) {
|
||||
//
|
||||
$table->dropColumn(['user_activity_id']);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
@ -41,6 +41,10 @@ Route::group(['prefix' => 'miniprogram', 'namespace' => 'Api\Miniprogram'], func
|
|||
// 获取配置内容;
|
||||
Route::get('config-info', [App\Http\Controllers\Api\SettingController::class, 'info']);
|
||||
|
||||
//6-23新增首页竞猜记录,活动页竞猜记录
|
||||
Route::get('latest-game-logs', [App\Http\Controllers\Api\ActivityGameController::class, 'latestUserGame']);
|
||||
Route::get('activity-game-logs', [App\Http\Controllers\Api\ActivityGameController::class, 'activityUserGame']);
|
||||
|
||||
// 已授权绑定手机号
|
||||
Route::middleware([HasBindPhone::class])->group(function(){
|
||||
//参与竞猜
|
||||
|
|
|
|||
Loading…
Reference in New Issue