2026-04-30 09:40:50 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 文件功能:聊天室座驾前台接口控制器。
|
|
|
|
|
*
|
|
|
|
|
* 提供座驾列表、用户当前座驾、购买记录与购买座驾接口。
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Http\Requests\BuyRideRequest;
|
2026-04-30 09:55:20 +08:00
|
|
|
use App\Models\Ride;
|
2026-04-30 09:40:50 +08:00
|
|
|
use App\Models\Room;
|
|
|
|
|
use App\Services\ChatStateService;
|
|
|
|
|
use App\Services\RideService;
|
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 聊天室座驾控制器
|
|
|
|
|
* 负责前台座驾页面的数据读取与购买操作。
|
|
|
|
|
*/
|
|
|
|
|
class RideController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 构造座驾控制器依赖。
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
private readonly RideService $rideService,
|
|
|
|
|
private readonly ChatStateService $chatState,
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取座驾页面需要的商品、当前座驾和购买记录。
|
|
|
|
|
*/
|
|
|
|
|
public function items(): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$user = Auth::user();
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'items' => $this->rideService->activeItems()
|
2026-04-30 09:55:20 +08:00
|
|
|
->map(fn (Ride $item) => $this->rideService->formatItem($item))
|
2026-04-30 09:40:50 +08:00
|
|
|
->values(),
|
|
|
|
|
'current_ride' => $this->rideService->formatCurrentRide($user),
|
|
|
|
|
'purchases' => $this->rideService->purchaseRecords($user),
|
|
|
|
|
'user_jjb' => $user->jjb ?? 0,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 购买座驾并返回最新金币和当前座驾状态。
|
|
|
|
|
*/
|
|
|
|
|
public function buy(BuyRideRequest $request): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$user = Auth::user();
|
|
|
|
|
$roomId = (int) $request->integer('room_id');
|
|
|
|
|
$room = Room::query()->findOrFail($roomId);
|
|
|
|
|
|
|
|
|
|
if (! $room->canUserEnter($user) || ! $this->chatState->isUserInRoom($roomId, $user->username)) {
|
|
|
|
|
return response()->json(['status' => 'error', 'message' => '请先进入当前房间后再购买座驾。'], 403);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 09:55:20 +08:00
|
|
|
$item = Ride::query()->findOrFail((int) $request->integer('item_id'));
|
2026-04-30 10:02:59 +08:00
|
|
|
$result = $this->rideService->buy($user, $item, $roomId);
|
2026-04-30 09:40:50 +08:00
|
|
|
|
|
|
|
|
if (! $result['ok']) {
|
|
|
|
|
return response()->json(['status' => 'error', 'message' => $result['message']], 400);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => $result['message'],
|
|
|
|
|
'current_ride' => $result['current_ride'] ?? null,
|
|
|
|
|
'purchases' => $this->rideService->purchaseRecords($user->fresh()),
|
|
|
|
|
'jjb' => $user->fresh()->jjb,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|