fix(api): cfg 改用类静态缓存并同步 setCfg 写入,修复后台保存后回显旧值;移除临时诊断探针

这个提交包含在:
linda
2026-10-15 14:57:11 +08:00
父节点 11174f8693
当前提交 60e99e12c3
共有 2 个文件被更改,包括 16 次插入 和 122 次删除

查看文件

@@ -1,115 +0,0 @@
<?php
namespace app\api\controller;
use think\Config;
use think\Route;
/**
* 路由诊断探针(临时,验证完成后删除)
* 访问:GET /api/probe/index
*/
class Probe extends \app\common\controller\Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
public function index()
{
// TP5.0 无 Route::getRules(),用反射读取静态 $rules
$ref = new \ReflectionClass(Route::class);
$prop = $ref->getProperty('rules');
$prop->setAccessible(true);
$rules = $prop->getValue();
$getBucket = isset($rules['get']) ? $rules['get'] : [];
$starBucket = isset($rules['*']) ? $rules['*'] : [];
$out = [
'request_path' => $this->request->path(),
'request_pathinfo' => $this->request->pathinfo(),
'request_module' => $this->request->module(),
'request_controller' => $this->request->controller(),
'request_action' => $this->request->action(),
'server_PATH_INFO' => isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : null,
'get_s' => isset($_GET['s']) ? $_GET['s'] : null,
'url_route_on' => Config::get('url_route_on'),
'route_config_file' => Config::get('route_config_file'),
'pathinfo_depr' => Config::get('pathinfo_depr'),
'CONF_PATH' => CONF_PATH,
'route_file_exists' => is_file(CONF_PATH . 'route.php'),
'runtime_route_file' => is_file(RUNTIME_PATH . 'route.php'),
'rule_buckets' => array_keys($rules),
'get_bucket_count' => count($getBucket),
'star_bucket_count' => count($starBucket),
'get_sample' => array_slice(array_keys($getBucket), 0, 8),
'has_static_rule' => isset($getBucket['api/v1/home/banners']),
'star_has_static' => isset($starBucket['api/v1/home/banners']),
];
// —— 追加:文件真实内容 & OPcache 取证 ——
$rf = CONF_PATH . 'route.php';
$out['route_file_realpath'] = is_file($rf) ? realpath($rf) : null;
$out['route_file_size'] = is_file($rf) ? filesize($rf) : -1;
$out['route_file_mtime'] = is_file($rf) ? date('c', filemtime($rf)) : null;
$loaded = is_file($rf) ? include $rf : null;
$out['included_is_array'] = is_array($loaded);
$out['included_rule_count'] = is_array($loaded) ? count($loaded) : -1;
$out['included_keys'] = is_array($loaded) ? array_slice(array_keys($loaded), 0, 4) : null;
$out['opcache_enabled'] = function_exists('opcache_get_status') ? (bool)@opcache_get_status(false)['opcache_enabled'] : null;
$out['ini_validate_ts'] = ini_get('opcache.validate_timestamps');
$out['ini_revalidate'] = ini_get('opcache.revalidate_freq');
$out['ini_opcache_on'] = ini_get('opcache.enable');
if (function_exists('opcache_get_status')) {
$st = @opcache_get_status(true);
if (!empty($st['scripts'])) {
foreach ($st['scripts'] as $path => $info) {
if (strpos($path, '/application/route.php') !== false) {
$out['opcache_cached_route'] = [
'path' => $path,
'timestamp' => date('c', $info['timestamp']),
'hits' => $info['hits'],
];
}
}
}
}
// —— 决定性测试:手动 import 后,规则是否进入 Route::$rules ——
$out['star_keys_before'] = array_slice(array_keys($starBucket), 0, 5);
$apiKeysBefore = [];
foreach ($rules as $bucket => $items) {
foreach (array_keys((array)$items) as $k) {
if (strpos($k, 'api/v1') === 0) {
$apiKeysBefore[] = $bucket . ' :: ' . $k;
}
}
}
$out['api_v1_rules_before'] = array_slice($apiKeysBefore, 0, 5);
$out['api_v1_count_before'] = count($apiKeysBefore);
if (is_array($loaded)) {
Route::import($loaded);
$prop2 = $ref->getProperty('rules');
$prop2->setAccessible(true);
$rules2 = $prop2->getValue();
$apiKeysAfter = [];
foreach ($rules2 as $bucket => $items) {
foreach (array_keys((array)$items) as $k) {
if (strpos($k, 'api/v1') === 0) {
$apiKeysAfter[] = $bucket . ' :: ' . $k;
}
}
}
$out['api_v1_count_after_manual_import'] = count($apiKeysAfter);
$out['star_count_after'] = isset($rules2['*']) ? count($rules2['*']) : 0;
$out['get_count_after'] = isset($rules2['get']) ? count($rules2['get']) : 0;
}
$out['app_debug'] = \think\Config::get('app_debug');
echo json_encode($out, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
exit;
}
}

查看文件

@@ -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;
}