|
|
|
@ -1758,7 +1758,12 @@ if (!function_exists('getStockInfo')) { |
|
|
|
// 尝试从缓存获取
|
|
|
|
$cacheKey = 'stock_info_' . $stockCode; |
|
|
|
$cached = \think\facade\Cache::get($cacheKey); |
|
|
|
if (!empty($cached)) { |
|
|
|
if (!empty($cached) && is_array($cached)) { |
|
|
|
// 清理历史缓存里可能残留的非 UTF-8 字段
|
|
|
|
if (!empty($cached['name']) && !mb_check_encoding((string)$cached['name'], 'UTF-8')) { |
|
|
|
$cached['name'] = mb_convert_encoding((string)$cached['name'], 'UTF-8', 'GBK'); |
|
|
|
\think\facade\Cache::set($cacheKey, $cached, 60 * 15 - 10); |
|
|
|
} |
|
|
|
return $cached; |
|
|
|
} |
|
|
|
|
|
|
|
@ -1807,6 +1812,11 @@ if (!function_exists('fetchStockFromSina')) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
// 新浪接口默认 GBK,转 UTF-8 后再解析,避免 JSON 报 Malformed UTF-8
|
|
|
|
if (!mb_check_encoding($response, 'UTF-8')) { |
|
|
|
$response = mb_convert_encoding($response, 'UTF-8', 'GBK'); |
|
|
|
} |
|
|
|
|
|
|
|
// 解析新浪数据格式:var hq_str_sh603933="名称,开盘价,昨收,当前价,最高,最低,...";
|
|
|
|
if (preg_match('/="(.+)"/', $response, $matches)) { |
|
|
|
$fields = explode(',', $matches[1]); |
|
|
|
@ -1815,10 +1825,14 @@ if (!function_exists('fetchStockFromSina')) { |
|
|
|
$prevClose = floatval($fields[2]); |
|
|
|
$change = round($price - $prevClose, 2); |
|
|
|
$changePercent = $prevClose > 0 ? round(($change / $prevClose) * 100, 2) : 0; |
|
|
|
$name = $fields[0]; |
|
|
|
if (!mb_check_encoding($name, 'UTF-8')) { |
|
|
|
$name = mb_convert_encoding($name, 'UTF-8', 'GBK'); |
|
|
|
} |
|
|
|
|
|
|
|
return [ |
|
|
|
'code' => $stockCode, |
|
|
|
'name' => $fields[0], |
|
|
|
'name' => $name, |
|
|
|
'price' => $price > 0 ? number_format($price, 2) : '--', |
|
|
|
'change' => $price > 0 ? (($change >= 0 ? '+' : '') . number_format($change, 2)) : '--', |
|
|
|
'changePercent' => $price > 0 ? (($changePercent >= 0 ? '+' : '') . number_format($changePercent, 2) . '%') : '--', |
|
|
|
|