Feat: 商店功能完整实现(单次特效卡888/周卡8888/改名卡5000,含购买、周卡覆盖、改名黑名单)
This commit is contained in:
@@ -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 或 duration,slug 以 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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user