feat:优化内容分发网络URL相关处理

This commit is contained in:
妙码生花 2022-08-03 11:34:42 +08:00
parent 8e1e9e49bb
commit d6a9b5cae3
6 changed files with 16 additions and 4 deletions

View File

@ -31,6 +31,7 @@ class Index extends Backend
'siteConfig' => [
'site_name' => get_sys_config('site_name'),
'version' => get_sys_config('version'),
'cdn_url' => full_url(),
],
'terminal' => [
'install_service_port' => Config::get('buildadmin.install_service_port'),

View File

@ -21,6 +21,7 @@ class Index extends Frontend
'site_name' => get_sys_config('site_name'),
'record_number' => get_sys_config('record_number'),
'version' => get_sys_config('version'),
'cdn_url' => full_url(),
],
'open_member_center' => Config::get('buildadmin.open_member_center'),
]);

View File

@ -150,15 +150,21 @@ if (!function_exists('full_url')) {
*/
function full_url($relativeUrl = false, $domain = true, $default = '')
{
$relativeUrl = $relativeUrl ? $relativeUrl : $default;
if (!$relativeUrl) {
return $domain === true ? request()->domain() : $domain;
$cdnUrl = Config::get('buildadmin.cdn_url');
if ($domain === true) {
$domain = $cdnUrl ? $cdnUrl : request()->domain();
} elseif ($domain === false) {
$domain = '';
}
$relativeUrl = $relativeUrl ? $relativeUrl : $default;
if (!$relativeUrl) return $domain;
$regex = "/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/i";
if (preg_match('/^http(s)?:\/\//', $relativeUrl) || preg_match($regex, $relativeUrl) || $domain === false) {
return $relativeUrl;
}
return $domain === true ? request()->domain() . $relativeUrl : $domain . $relativeUrl;
return $domain . $relativeUrl;
}
}

View File

@ -99,6 +99,8 @@ return [
'auto_write_admin_log' => true,
// 缺省头像图片路径
'default_avatar' => '/static/images/avatar.png',
// 内容分发网络URL
'cdn_url' => '',
// 版本号
'version' => 'v1.0.3',
];

View File

@ -121,4 +121,5 @@ export interface SiteConfig {
site_name: string
record_number?: string
version: string
cdn_url: string
}

View File

@ -7,6 +7,7 @@ export const useSiteConfig = defineStore('siteConfig', {
site_name: '',
record_number: '',
version: '',
cdn_url: '',
}
},
})