1
0
mirror of https://github.com/hanxi/xiaomusic.git synced 2026-04-04 11:45:14 +08:00

feat: 高级设置增加:口令平台偏好设置,方便语音指令播放时指定平台。 (#804)

This commit is contained in:
boluofan
2026-03-25 17:16:56 +08:00
committed by GitHub
parent 5bdd4a203d
commit 4118c472a1
5 changed files with 186 additions and 23 deletions

View File

@@ -305,6 +305,38 @@ async def update_advanced_config(request: Request):
return {"success": False, "error": str(e)}
@router.get("/api/box-play-platform/load")
def get_box_play_platform():
"""获取音响口令搜索平台偏好"""
try:
platform = xiaomusic.js_plugin_manager.get_box_play_platform_preference()
platforms = xiaomusic.js_plugin_manager.get_platforms()
return {
"success": True,
"data": {
"platform": platform,
"platforms": platforms,
},
}
except Exception as e:
return {"success": False, "error": str(e)}
@router.post("/api/box-play-platform/update")
async def update_box_play_platform(request: Request):
"""更新口令搜索平台偏好"""
try:
request_json = await request.json()
platform = request_json.get("platform")
if platform is None:
return {"success": False, "error": "Missing parameters"}
return xiaomusic.js_plugin_manager.update_box_play_platform( platform)
except Exception as e:
return {"success": False, "error": str(e)}
# ----------------------------密码验证接口---------------------------------------
@router.get("/api/password/check")
def check_password_required():

View File

@@ -387,10 +387,17 @@ class JSPluginManager:
result_data = await self.simple_async_get(
url=lx_server_info.get("base_url") + "/music/config"
)
if result_data and "player.enableAuth" in result_data and "user.enablePublicRestriction" in result_data:
if (
result_data
and "player.enableAuth" in result_data
and "user.enablePublicRestriction" in result_data
):
return {"success": True, "data": "LX Server接口正常"}
else:
return {"success": False, "error": "不是合法的LX Server接口请确认后重新配置"}
return {
"success": False,
"error": "不是合法的LX Server接口请确认后重新配置",
}
else:
return {"success": False, "error": "LX Server接口未配置"}
except Exception as e:
@@ -486,6 +493,51 @@ class JSPluginManager:
self.log.error(f"Failed to update LXServer platforms: {e}")
return {"success": False, "error": str(e)}
def get_box_play_platform_preference(self) -> str:
"""获取用户配置的口令搜索偏好"""
try:
config_data = self._get_config_data()
if self.is_lx_server():
lx_server_info = config_data.get("lx_server_info", {})
return lx_server_info.get("box_play_platform", "all")
else:
music_free_info = config_data.get("music_free_info", {})
return music_free_info.get("box_play_platform", "all")
except Exception:
return "all"
def update_box_play_platform(self, platform: str) -> dict[str, Any]:
"""更新平台偏好设置
Args:
platform: 偏好的平台标识,"all"表示聚合搜索
Returns:
dict: 更新结果字典
"""
try:
if os.path.exists(self.plugins_config_path):
with open(self.plugins_config_path, encoding="utf-8") as f:
config_data = json.load(f)
if self.is_lx_server():
lx_server_info = config_data.get("lx_server_info", {})
lx_server_info["box_play_platform"] = platform
config_data["lx_server_info"] = lx_server_info
else:
music_free_info = config_data.get("music_free_info", {})
music_free_info["box_play_platform"] = platform
config_data["music_free_info"] = music_free_info
with open(self.plugins_config_path, "w", encoding="utf-8") as f:
json.dump(config_data, f, ensure_ascii=False, indent=2)
self._invalidate_config_cache()
return {"success": True}
else:
return {"success": False, "error": "Config file not found"}
except Exception as e:
self.log.error(f"Failed to update box play platform: {e}")
return {"success": False, "error": str(e)}
def get_plugin_source(self) -> dict[str, Any]:
"""获取插件源配置信息
Returns:
@@ -872,30 +924,28 @@ class JSPluginManager:
self.log.error(f"Failed to read enabled plugins from config: {e}")
return result
def is_lx_server(self) -> bool:
"""判定是否使用lx_server接口"""
def _get_api_type(self) -> int:
"""获取接口类型1=mf_plugins2=lx_server"""
try:
config_data = self._get_config_data()
if config_data:
back_conf_info = config_data.get("back_conf_info", {})
# 1mf_plugins2lx_server
api_type = back_conf_info.get("api_type", 1)
return api_type == 2
else:
return False
except Exception as e:
self.log.error(f"Failed to read platforms from config: {e}")
return False
return back_conf_info.get("api_type", 1)
return 1
except Exception:
return 1
def is_lx_server(self) -> bool:
"""判定是否使用lx_server接口"""
return self._get_api_type() == 2
def get_platforms(self) -> dict[Any, Any]:
"""获取音乐平台列表"""
try:
self._invalidate_config_cache()
api_type = self._get_api_type()
config_data = self._get_config_data()
if config_data:
back_conf_info = config_data.get("back_conf_info", {})
# 1mf_plugins2lx_server
api_type = back_conf_info.get("api_type", 1)
if api_type == 2:
lx_server_info = config_data.get("lx_server_info", {})
platforms = lx_server_info.get("platforms", {})

View File

@@ -358,8 +358,10 @@ class OnlineMusicService:
song_name = artist_song_list[0]
await self.xiaomusic.do_play_music_list(did, list_name, song_name)
else:
# 获取用户配置的平台偏好
user_preference = self.js_plugin_manager.get_box_play_platform_preference()
# 获取歌曲列表
result = await self.get_music_list_online(keyword=name, limit=10)
result = await self.get_music_list_online(plugin=user_preference, keyword=name, limit=10)
self.log.info(f"在线搜索歌手的歌曲列表: {result}")
if result.get("success") and result.get("total") > 0:
@@ -380,9 +382,11 @@ class OnlineMusicService:
async def add_singer_song(self, list_name, name):
try:
# 获取用户配置的平台偏好
user_preference = self.js_plugin_manager.get_box_play_platform_preference()
self.log.info(f"追加歌手歌曲,当前页码: {self._singer_add_page}")
result = await self.get_music_list_online(
keyword=name, page=self._singer_add_page, limit=10
plugin=user_preference, keyword=name, page=self._singer_add_page, limit=10
)
if result.get("success") and result.get("total") > 0:
self._handle_music_list(result.get("data"), list_name, True)
@@ -545,8 +549,9 @@ class OnlineMusicService:
# 在线搜索搜索最符合的一首歌并播放
async def search_top_one_play(self, did, search_key, name):
try:
user_preference = self.js_plugin_manager.get_box_play_platform_preference()
# 获取歌曲列表
result = await self.get_music_list_online(keyword=name, limit=10)
result = await self.get_music_list_online(plugin=user_preference, keyword=name, limit=10)
if result.get("success") and result.get("total") > 0:
# 打印输出 result.data

View File

@@ -18,11 +18,13 @@
"kw": "小蜗音乐",
"wy": "小芸音乐",
"mg": "小蜜音乐"
}
},
"box_play_platform": "all"
},
"music_free_info": {
"enabled_plugins": [],
"plugin_source": {"source_url": ""},
"plugins_info": []
"plugins_info": [],
"box_play_platform": "all"
}
}

View File

@@ -222,7 +222,7 @@
.modal-body .config-card-title {
font-weight: 600;
font-size: 15px;
font-size: 18px;
color: #333;
margin-bottom: 12px;
padding-bottom: 10px;
@@ -1031,12 +1031,27 @@
</div>
</div>
<div class="config-row">
<div class="config-label">模型</div>
<div class="config-label">模型名称</div>
<div class="config-value">
<input type="text" class="config-input" id="aiModelInput" placeholder="非必填默认为qwen-flash">
</div>
</div>
</div>
<!-- 偏好设置卡片 -->
<div class="config-card" id="boxPlayPlatformCard">
<div class="config-card-title">偏好设置</div>
<div class="config-row">
<div class="config-label">口令搜索偏好</div>
<div class="config-value">
<span style="color: #999; font-size: 12px; margin-top: 4px; display: inline-block;">
口令搜索时的偏好平台,默认聚合搜索是搜索所有
</span>
<select class="config-input" id="boxPlayPlatformSelect">
<option value="all">聚合搜索</option>
</select>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button class="modal-btn modal-btn-secondary" onclick="closeAdvancedConfig()">取消</button>
@@ -1526,6 +1541,8 @@
modal.classList.add('show');
// 加载当前配置
await loadAdvancedConfig();
// 加载平台偏好设置
await loadBoxPlayPlatform();
}
// 关闭高级设置模态框
@@ -1568,7 +1585,7 @@
aiModelInput.value = aiapiInfo.model || '';
}
// 保存高级配置
// 保存高级配置(包含平台偏好)
async function saveAdvancedConfig() {
try {
const autoAddSong = document.getElementById('autoAddSongCheckbox').checked;
@@ -1584,6 +1601,7 @@
model: aiModel
};
// 保存高级配置
const response = await fetch('/api/advanced-config/update', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
@@ -1595,6 +1613,8 @@
const data = await response.json();
if (data.success) {
// 同时保存平台偏好
await saveBoxPlayPlatform();
alert('保存成功');
closeAdvancedConfig();
} else {
@@ -1605,6 +1625,60 @@
}
}
// 加载平台偏好设置
async function loadBoxPlayPlatform() {
try {
const response = await fetch('/api/box-play-platform/load');
const data = await response.json();
if (data.success) {
displayBoxPlayPlatform(data.data);
}
} catch (error) {
console.error('加载平台偏好设置失败:', error);
}
}
// 显示平台偏好设置
function displayBoxPlayPlatform(config) {
const platformSelect = document.getElementById('boxPlayPlatformSelect');
// 清空并填充平台选项
platformSelect.innerHTML = '<option value="all">聚合搜索</option>';
// 处理 platforms 为对象的情况
const platformsObj = config.platforms || {};
Object.entries(platformsObj).forEach(([key, name]) => {
const option = document.createElement('option');
option.value = key;
option.textContent = name;
platformSelect.appendChild(option);
});
// 设置当前选中的平台
platformSelect.value = config.platform;
}
// 保存平台偏好设置
async function saveBoxPlayPlatform() {
try {
const platform = document.getElementById('boxPlayPlatformSelect').value;
const response = await fetch('/api/box-play-platform/update', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
platform: platform
})
});
const data = await response.json();
if (!data.success) {
throw new Error(data.error || '保存失败');
}
} catch (error) {
throw error;
}
}
// 插件导入功能
function uploadPlugins() {
// 创建文件输入元素