mirror of
https://github.com/lkddi/Xboard.git
synced 2026-04-29 07:27:26 +08:00
fix: gift card redemption limits validation
This commit is contained in:
@@ -110,7 +110,7 @@ class GiftCardCode extends Model
|
|||||||
public function isAvailable(): bool
|
public function isAvailable(): bool
|
||||||
{
|
{
|
||||||
// 检查状态
|
// 检查状态
|
||||||
if ($this->status !== self::STATUS_UNUSED) {
|
if (in_array($this->status, [self::STATUS_EXPIRED, self::STATUS_DISABLED])) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -222,27 +222,27 @@ class GiftCardTemplate extends Model
|
|||||||
*/
|
*/
|
||||||
public function checkUsageLimit(User $user): bool
|
public function checkUsageLimit(User $user): bool
|
||||||
{
|
{
|
||||||
$conditions = $this->conditions ?? [];
|
$limits = $this->limits ?? [];
|
||||||
|
|
||||||
// 检查每用户最大使用次数
|
// 检查每用户最大使用次数
|
||||||
if (isset($conditions['max_use_per_user'])) {
|
if (isset($limits['max_use_per_user'])) {
|
||||||
$usedCount = $this->usages()
|
$usedCount = $this->usages()
|
||||||
->where('user_id', $user->id)
|
->where('user_id', $user->id)
|
||||||
->count();
|
->count();
|
||||||
if ($usedCount >= $conditions['max_use_per_user']) {
|
if ($usedCount >= $limits['max_use_per_user']) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查冷却时间
|
// 检查冷却时间
|
||||||
if (isset($conditions['cooldown_hours'])) {
|
if (isset($limits['cooldown_hours'])) {
|
||||||
$lastUsage = $this->usages()
|
$lastUsage = $this->usages()
|
||||||
->where('user_id', $user->id)
|
->where('user_id', $user->id)
|
||||||
->orderBy('created_at', 'desc')
|
->orderBy('created_at', 'desc')
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
if ($lastUsage && isset($lastUsage->created_at)) {
|
if ($lastUsage && isset($lastUsage->created_at)) {
|
||||||
$cooldownTime = $lastUsage->created_at + ($conditions['cooldown_hours'] * 3600);
|
$cooldownTime = $lastUsage->created_at + ($limits['cooldown_hours'] * 3600);
|
||||||
if (time() < $cooldownTime) {
|
if (time() < $cooldownTime) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user