Feat: 商店功能完整实现(单次特效卡888/周卡8888/改名卡5000,含购买、周卡覆盖、改名黑名单)

This commit is contained in:
2026-02-27 15:57:12 +08:00
parent c52998671b
commit 7fb86bfe21
15 changed files with 999 additions and 4 deletions
+4 -2
View File
@@ -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), // 周卡特效(登录自动播放)
]);
}
+105
View File
@@ -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']]);
}
}
+81
View File
@@ -0,0 +1,81 @@
<?php
/**
* 文件功能:商店商品模型
* 对应 shop_items 表,存储商品定义信息
*/
namespace App\Models;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class ShopItem extends Model
{
protected $table = 'shop_items';
protected $fillable = [
'name', 'slug', 'description', 'icon', 'price',
'type', 'duration_days', 'sort_order', 'is_active',
];
protected $casts = [
'is_active' => 'boolean',
];
/**
* 获取该商品的所有购买记录
*/
public function purchases(): HasMany
{
return $this->hasMany(UserPurchase::class);
}
/**
* 是否为特效类商品(instant durationslug once_ week_ 开头)
*/
public function isEffect(): bool
{
return str_starts_with($this->slug, 'once_') || str_starts_with($this->slug, 'week_');
}
/**
* 是否为周卡(duration 类型)
*/
public function isWeekCard(): bool
{
return $this->type === 'duration';
}
/**
* 是否为单次卡(instant 类型)
*/
public function isInstant(): bool
{
return $this->type === 'instant';
}
/**
* 获取特效 key(去掉 once_ / week_ 前缀,返回 fireworks/rain/lightning/snow
*/
public function effectKey(): ?string
{
if (str_starts_with($this->slug, 'once_')) {
return substr($this->slug, 5);
}
if (str_starts_with($this->slug, 'week_')) {
return substr($this->slug, 5);
}
return null;
}
/**
* 获取所有上架商品(按排序)
*/
public static function active(): Collection
{
return static::where('is_active', true)->orderBy('sort_order')->get();
}
}
+57
View File
@@ -0,0 +1,57 @@
<?php
/**
* 文件功能:用户购买记录模型
* 对应 user_purchases 表,追踪每次商品购买的状态与有效期
*/
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class UserPurchase extends Model
{
protected $table = 'user_purchases';
protected $fillable = [
'user_id', 'shop_item_id', 'status', 'price_paid',
'expires_at', 'used_at',
];
protected $casts = [
'expires_at' => 'datetime',
'used_at' => 'datetime',
];
/**
* 购买记录所属用户
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* 购买记录对应的商品
*/
public function shopItem(): BelongsTo
{
return $this->belongsTo(ShopItem::class, 'shop_item_id');
}
/**
* 判断周卡是否仍在有效期内
*/
public function isAlive(): bool
{
if ($this->status !== 'active') {
return false;
}
if ($this->expires_at && $this->expires_at->isPast()) {
return false;
}
return true;
}
}
+34
View File
@@ -0,0 +1,34 @@
<?php
/**
* 文件功能:用户名黑名单模型
* 用户改名后旧名称写入此表,保留期间其他人无法注册该名称
*/
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class UsernameBlacklist extends Model
{
protected $table = 'username_blacklist';
public $timestamps = false; // 只有 created_at 无 updated_at
protected $fillable = ['username', 'reserved_until', 'created_at'];
protected $casts = [
'reserved_until' => 'datetime',
'created_at' => 'datetime',
];
/**
* 判断给定名称是否在黑名单有效期内
*/
public static function isReserved(string $username): bool
{
return static::where('username', $username)
->where('reserved_until', '>', now())
->exists();
}
}
+222
View File
@@ -0,0 +1,222 @@
<?php
/**
* 文件功能:商店业务逻辑服务层
* 处理商品购买、周卡激活/覆盖、改名卡使用等全部核心业务
*/
namespace App\Services;
use App\Models\ShopItem;
use App\Models\User;
use App\Models\UsernameBlacklist;
use App\Models\UserPurchase;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
class ShopService
{
/**
* 购买商品入口:扣金币、按类型分发处理
*
* @return array{ok:bool, message:string, play_effect?:string}
*/
public function buyItem(User $user, ShopItem $item): array
{
// 校验金币是否足够
if ($user->jjb < $item->price) {
return ['ok' => false, 'message' => "金币不足,购买 [{$item->name}] 需要 {$item->price} 金币,当前仅有 {$user->jjb} 金币。"];
}
return match ($item->type) {
'instant' => $this->buyInstant($user, $item),
'duration' => $this->buyWeekCard($user, $item),
'one_time' => $this->buyRenameCard($user, $item),
default => ['ok' => false, 'message' => '未知商品类型'],
};
}
/**
* 购买单次特效卡:立即扣金币,记录已用,返回需要播放的特效 key
*
* @return array{ok:bool, message:string, play_effect?:string}
*/
public function buyInstant(User $user, ShopItem $item): array
{
DB::transaction(function () use ($user, $item) {
// 扣除金币
$user->decrement('jjb', $item->price);
// 写入已使用记录(用于统计)
UserPurchase::create([
'user_id' => $user->id,
'shop_item_id' => $item->id,
'status' => 'used',
'price_paid' => $item->price,
'used_at' => Carbon::now(),
]);
});
return [
'ok' => true,
'message' => "购买成功!{$item->icon} {$item->name} 正在为您播放...",
'play_effect' => $item->effectKey(), // 返回特效 key 让前端立即播放
];
}
/**
* 购买周卡:取消旧周卡(金币不退),激活新周卡,有效期7天
*
* @return array{ok:bool, message:string}
*/
public function buyWeekCard(User $user, ShopItem $item): array
{
DB::transaction(function () use ($user, $item) {
// 将所有已激活的周卡标记为 cancelled(金币不退)
UserPurchase::where('user_id', $user->id)
->where('status', 'active')
->whereHas('shopItem', fn ($q) => $q->where('type', 'duration'))
->update(['status' => 'cancelled']);
// 扣除金币
$user->decrement('jjb', $item->price);
// 写入新的激活记录
UserPurchase::create([
'user_id' => $user->id,
'shop_item_id' => $item->id,
'status' => 'active',
'price_paid' => $item->price,
'expires_at' => Carbon::now()->addDays($item->duration_days ?? 7),
]);
});
return ['ok' => true, 'message' => "购买成功!{$item->icon} {$item->name} 已激活,下次登录自动生效(连续7天)。"];
}
/**
* 购买改名卡:扣金币、写 active 记录备用
*
* @return array{ok:bool, message:string}
*/
public function buyRenameCard(User $user, ShopItem $item): array
{
// 检查是否已有未使用的改名卡
$existing = UserPurchase::where('user_id', $user->id)
->where('status', 'active')
->whereHas('shopItem', fn ($q) => $q->where('slug', 'rename_card'))
->exists();
if ($existing) {
return ['ok' => false, 'message' => '您已有一张未使用的改名卡,使用后再购买。'];
}
DB::transaction(function () use ($user, $item) {
$user->decrement('jjb', $item->price);
UserPurchase::create([
'user_id' => $user->id,
'shop_item_id' => $item->id,
'status' => 'active',
'price_paid' => $item->price,
]);
});
return ['ok' => true, 'message' => '改名卡购买成功!请在商店中使用改名卡修改昵称。'];
}
/**
* 使用改名卡:校验新名、加黑名单、更新用户名
*
* @param string $newName 新昵称
* @return array{ok:bool, message:string}
*/
public function useRenameCard(User $user, string $newName): array
{
$newName = trim($newName);
// 格式校验:1-10字符,中英文数字下划线
if (! preg_match('/^[\x{4e00}-\x{9fa5}a-zA-Z0-9_]{1,10}$/u', $newName)) {
return ['ok' => false, 'message' => '新昵称格式不合法(1-10字,中英文/数字/下划线)。'];
}
// 不能与自己现有名相同
if ($newName === $user->username) {
return ['ok' => false, 'message' => '新昵称与当前昵称相同,请重新输入。'];
}
// 不能与其他用户重名
if (User::where('username', $newName)->exists()) {
return ['ok' => false, 'message' => '该昵称已被他人注册,请换一个。'];
}
// 不能在黑名单保留期内
if (UsernameBlacklist::isReserved($newName)) {
return ['ok' => false, 'message' => '该昵称处于保护期,暂时无法使用。'];
}
// 查找有效的改名卡记录
$purchase = UserPurchase::where('user_id', $user->id)
->where('status', 'active')
->whereHas('shopItem', fn ($q) => $q->where('slug', 'rename_card'))
->first();
if (! $purchase) {
return ['ok' => false, 'message' => '您没有可用的改名卡,请先购买。'];
}
DB::transaction(function () use ($user, $purchase, $newName) {
$oldName = $user->username;
// 修改用户名
$user->username = $newName;
$user->save();
// 旧名入黑名单(保留30天)
UsernameBlacklist::updateOrCreate(
['username' => $oldName],
['reserved_until' => Carbon::now()->addDays(30), 'created_at' => Carbon::now()]
);
// 标记改名卡为已使用
$purchase->update(['status' => 'used', 'used_at' => Carbon::now()]);
});
return ['ok' => true, 'message' => "改名成功!您的新昵称为【{$newName}】,旧昵称将保留30天黑名单。注意:历史消息中的旧名不会同步修改。"];
}
/**
* 获取用户当前激活的周卡特效 key(如 fireworks/rain/lightning/snow
* 过期的周卡会自动标记为 expired
*/
public function getActiveWeekEffect(User $user): ?string
{
$purchase = UserPurchase::with('shopItem')
->where('user_id', $user->id)
->where('status', 'active')
->whereHas('shopItem', fn ($q) => $q->where('type', 'duration'))
->first();
if (! $purchase) {
return null;
}
// 检查是否过期
if ($purchase->expires_at && $purchase->expires_at->isPast()) {
$purchase->update(['status' => 'expired']);
return null;
}
return $purchase->shopItem->effectKey();
}
/**
* 获取用户当前激活的改名卡(是否持有未用改名卡)
*/
public function hasRenameCard(User $user): bool
{
return UserPurchase::where('user_id', $user->id)
->where('status', 'active')
->whereHas('shopItem', fn ($q) => $q->where('slug', 'rename_card'))
->exists();
}
}