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