Feat: 商店功能完整实现(单次特效卡888/周卡8888/改名卡5000,含购买、周卡覆盖、改名黑名单)
This commit is contained in:
@@ -36,6 +36,7 @@ class ChatController extends Controller
|
||||
private readonly ChatStateService $chatState,
|
||||
private readonly MessageFilterService $filter,
|
||||
private readonly VipService $vipService,
|
||||
private readonly \App\Services\ShopService $shopService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -97,8 +98,9 @@ class ChatController extends Controller
|
||||
|
||||
// 渲染主聊天框架视图
|
||||
return view('chat.frame', [
|
||||
'room' => $room,
|
||||
'user' => $user,
|
||||
'room' => $room,
|
||||
'user' => $user,
|
||||
'weekEffect' => $this->shopService->getActiveWeekEffect($user), // 周卡特效(登录自动播放)
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 文件功能:商店控制器
|
||||
* 提供商品列表查询、商品购买、改名卡使用 三个接口
|
||||
*/
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\ShopItem;
|
||||
use App\Services\ShopService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class ShopController extends Controller
|
||||
{
|
||||
/**
|
||||
* 注入商店服务
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly ShopService $shopService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 获取商店商品列表及当前用户状态
|
||||
*
|
||||
* 返回字段:items(商品列表)、user_jjb(当前金币)、
|
||||
* active_week_effect(当前周卡)、has_rename_card(是否持有改名卡)
|
||||
*/
|
||||
public function items(): JsonResponse
|
||||
{
|
||||
$user = Auth::user();
|
||||
$items = ShopItem::active()->map(fn ($item) => [
|
||||
'id' => $item->id,
|
||||
'name' => $item->name,
|
||||
'slug' => $item->slug,
|
||||
'description' => $item->description,
|
||||
'icon' => $item->icon,
|
||||
'price' => $item->price,
|
||||
'type' => $item->type,
|
||||
'duration_days' => $item->duration_days,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'items' => $items,
|
||||
'user_jjb' => $user->jjb ?? 0,
|
||||
'active_week_effect' => $this->shopService->getActiveWeekEffect($user),
|
||||
'has_rename_card' => $this->shopService->hasRenameCard($user),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 购买商品
|
||||
*
|
||||
* @param Request $request 含 item_id
|
||||
*/
|
||||
public function buy(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate(['item_id' => 'required|integer|exists:shop_items,id']);
|
||||
|
||||
$item = ShopItem::find($request->item_id);
|
||||
if (! $item->is_active) {
|
||||
return response()->json(['status' => 'error', 'message' => '该商品已下架。'], 400);
|
||||
}
|
||||
|
||||
$result = $this->shopService->buyItem(Auth::user(), $item);
|
||||
|
||||
if (! $result['ok']) {
|
||||
return response()->json(['status' => 'error', 'message' => $result['message']], 400);
|
||||
}
|
||||
|
||||
$response = ['status' => 'success', 'message' => $result['message']];
|
||||
|
||||
// 单次特效卡:告诉前端立即播放哪个特效
|
||||
if (isset($result['play_effect'])) {
|
||||
$response['play_effect'] = $result['play_effect'];
|
||||
}
|
||||
|
||||
// 返回最新金币余额
|
||||
$response['jjb'] = Auth::user()->fresh()->jjb;
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用改名卡修改昵称
|
||||
*
|
||||
* @param Request $request 含 new_name
|
||||
*/
|
||||
public function rename(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'new_name' => 'required|string|min:1|max:10',
|
||||
]);
|
||||
|
||||
$result = $this->shopService->useRenameCard(Auth::user(), $request->new_name);
|
||||
|
||||
if (! $result['ok']) {
|
||||
return response()->json(['status' => 'error', 'message' => $result['message']], 400);
|
||||
}
|
||||
|
||||
return response()->json(['status' => 'success', 'message' => $result['message']]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user