fix(api): cfg 改用类静态缓存并同步 setCfg 写入,修复后台保存后回显旧值;移除临时诊断探针
这个提交包含在:
@@ -32,6 +32,9 @@ class V1 extends Api
|
||||
/** @var bool 是否已尝试解析登录态 */
|
||||
protected $resolved = false;
|
||||
|
||||
/** @var array|null 业务配置运行时缓存(setCfg 会同步更新) */
|
||||
protected static $cfgCache = null;
|
||||
|
||||
/* ==================== 响应输出 ==================== */
|
||||
|
||||
/**
|
||||
@@ -245,20 +248,22 @@ class V1 extends Api
|
||||
/**
|
||||
* 读取配置,key 形如 fee.singlePost / ui.logo / base.appName
|
||||
* 数组/对象值以 JSON 存取
|
||||
*
|
||||
* 缓存说明:使用类静态缓存(而非函数 static),以便 setCfg() 写入后同步失效,
|
||||
* 保证「同一请求内写后即读」能拿到新值(如后台保存 UI 配置后立刻回显)。
|
||||
*/
|
||||
protected function cfg($key, $default = null)
|
||||
{
|
||||
static $cache = null;
|
||||
if ($cache === null) {
|
||||
$cache = [];
|
||||
if (self::$cfgCache === null) {
|
||||
self::$cfgCache = [];
|
||||
foreach (Db::name('fc_config')->select() as $row) {
|
||||
$cache[$row['key']] = $row['value'];
|
||||
self::$cfgCache[$row['key']] = $row['value'];
|
||||
}
|
||||
}
|
||||
if (!array_key_exists($key, $cache)) {
|
||||
if (!array_key_exists($key, self::$cfgCache)) {
|
||||
return $default;
|
||||
}
|
||||
$val = $cache[$key];
|
||||
$val = self::$cfgCache[$key];
|
||||
$decoded = json_decode($val, true);
|
||||
if (json_last_error() === JSON_ERROR_NONE && (is_array($decoded) || $decoded === null)) {
|
||||
return $decoded;
|
||||
@@ -266,7 +271,7 @@ class V1 extends Api
|
||||
return $val;
|
||||
}
|
||||
|
||||
/** 写入配置 */
|
||||
/** 写入配置(并同步运行时缓存) */
|
||||
protected function setCfg($key, $value)
|
||||
{
|
||||
$val = (is_scalar($value) || $value === null) ? (string)$value : json_encode($value, JSON_UNESCAPED_UNICODE);
|
||||
@@ -277,6 +282,10 @@ class V1 extends Api
|
||||
'key' => $key, 'value' => $val, 'createtime' => time(), 'updatetime' => time(),
|
||||
]);
|
||||
}
|
||||
if (self::$cfgCache === null) {
|
||||
self::$cfgCache = [];
|
||||
}
|
||||
self::$cfgCache[$key] = $val;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
在新工单中引用
屏蔽一个用户