增强供应商配置:添加网站地址字段和智能推测功能
- 添加websiteUrl可选字段到Provider类型 - 实现API地址到网站地址的自动推测逻辑(去除api.前缀) - 在添加/编辑供应商表单中增加网站地址字段 - 供应商列表智能显示:有网址显示可点击链接,无网址显示API地址 - 提升用户体验:避免点击API端点地址导致的错误页面
This commit is contained in:
@@ -4,6 +4,7 @@ export interface Provider {
|
||||
apiUrl: string
|
||||
apiKey: string
|
||||
model?: string
|
||||
websiteUrl?: string
|
||||
}
|
||||
|
||||
export interface AppConfig {
|
||||
|
||||
35
src/shared/utils.ts
Normal file
35
src/shared/utils.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* 从API地址推测对应的网站地址
|
||||
* @param apiUrl API地址
|
||||
* @returns 推测的网站地址,如果无法推测则返回空字符串
|
||||
*/
|
||||
export function inferWebsiteUrl(apiUrl: string): string {
|
||||
if (!apiUrl || !apiUrl.trim()) {
|
||||
return ''
|
||||
}
|
||||
|
||||
try {
|
||||
const url = new URL(apiUrl.trim())
|
||||
|
||||
// 如果是localhost或IP地址,去掉路径部分
|
||||
if (url.hostname === 'localhost' || /^\d+\.\d+\.\d+\.\d+$/.test(url.hostname)) {
|
||||
return `${url.protocol}//${url.host}`
|
||||
}
|
||||
|
||||
// 处理域名,去掉api前缀
|
||||
let hostname = url.hostname
|
||||
|
||||
// 去掉 api. 前缀
|
||||
if (hostname.startsWith('api.')) {
|
||||
hostname = hostname.substring(4)
|
||||
}
|
||||
|
||||
// 构建推测的网站地址
|
||||
const port = url.port ? `:${url.port}` : ''
|
||||
return `${url.protocol}//${hostname}${port}`
|
||||
|
||||
} catch (error) {
|
||||
// URL解析失败,返回空字符串
|
||||
return ''
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user