star-cloud/app/Models/Transaction/PassCode.php
sky121113 b42695c4b8 [FEAT] 優化通行碼與取貨碼模組、補齊多國語系系統及介面風格調整
1. 通行碼與取貨碼:新增代碼預覽、重新產生功能與到期時間預覽邏輯。
2. 通行碼:允許天數設為 0 (永久),並改用搜尋式下拉選單選取機台。
3. 取貨碼:加強表單檢核並整合自訂 Toast 提示。
4. 語系優化:補齊繁中、英文鍵值,並完整建立日文語系目錄 (lang/ja) 與 JSON 檔案。
5. 組件優化:更新 PageHeader 與 TabNav 組件,提升 UI 互動性與一致性。
6. 訪客功能:新增通行碼訪客端查詢介面與控制器。
7. 其他模組:同步調整機台、遠端與倉庫模組視圖以符合極簡奢華風。
2026-04-29 16:54:21 +08:00

102 lines
2.1 KiB
PHP

<?php
namespace App\Models\Transaction;
use App\Models\Machine\Machine;
use App\Models\System\User;
use App\Traits\TenantScoped;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class PassCode extends Model
{
use TenantScoped;
protected $fillable = [
'company_id',
'machine_id',
'name',
'code',
'slug',
'expires_at',
'status',
'created_by',
];
protected $casts = [
'expires_at' => 'datetime',
];
/**
* 獲取公開通行連結
*/
public function getTicketUrlAttribute()
{
return $this->slug ? route('pass-code.ticket', $this->slug) : null;
}
/**
* Boot the model.
*/
protected static function booted()
{
static::creating(function ($passCode) {
if (!$passCode->slug) {
$passCode->slug = \Illuminate\Support\Str::random(16);
}
});
}
/**
* 關聯機台
*/
public function machine(): BelongsTo
{
return $this->belongsTo(Machine::class);
}
/**
* 關聯建立者
*/
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}
/**
* 判斷是否可用
*/
public function isValid(): bool
{
if ($this->status !== 'active') {
return false;
}
if ($this->expires_at && $this->expires_at->isPast()) {
return false;
}
return true;
}
/**
* 產生唯一的通行碼 (8位)
*/
public static function generateUniqueCode(int $machineId): string
{
do {
$code = str_pad(rand(0, 99999999), 8, '0', STR_PAD_LEFT);
$exists = self::where('machine_id', $machineId)
->where('code', $code)
->where('status', 'active')
->where(function($query) {
$query->whereNull('expires_at')
->orWhere('expires_at', '>', now());
})
->exists();
} while ($exists);
return $code;
}
}