feat: 統一各模組分頁組件佈局並新增系統設定功能相關檔案
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m5s
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m5s
This commit is contained in:
61
app/Modules/Core/Models/SystemSetting.php
Normal file
61
app/Modules/Core/Models/SystemSetting.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Core\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class SystemSetting extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'group',
|
||||
'key',
|
||||
'value',
|
||||
'type',
|
||||
'description',
|
||||
];
|
||||
|
||||
/**
|
||||
* 同請求內的記憶體快取,避免重複查詢 DB
|
||||
* PHP 請求結束後自動釋放,無需額外處理失效
|
||||
*/
|
||||
protected static array $cache = [];
|
||||
|
||||
/**
|
||||
* 取得特定設定值(含記憶體快取)
|
||||
*/
|
||||
public static function getVal(string $key, $default = null)
|
||||
{
|
||||
if (array_key_exists($key, static::$cache)) {
|
||||
return static::$cache[$key];
|
||||
}
|
||||
|
||||
$setting = self::where('key', $key)->first();
|
||||
|
||||
if (!$setting) {
|
||||
static::$cache[$key] = $default;
|
||||
return $default;
|
||||
}
|
||||
|
||||
$value = $setting->value;
|
||||
|
||||
// 根據 type 進行類別轉換
|
||||
$resolved = match ($setting->type) {
|
||||
'integer', 'number' => (int) $value,
|
||||
'boolean', 'bool' => filter_var($value, FILTER_VALIDATE_BOOLEAN),
|
||||
'json', 'array' => json_decode($value, true),
|
||||
default => $value,
|
||||
};
|
||||
|
||||
static::$cache[$key] = $resolved;
|
||||
|
||||
return $resolved;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除記憶體快取(儲存設定後應呼叫)
|
||||
*/
|
||||
public static function clearCache(): void
|
||||
{
|
||||
static::$cache = [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user