mirror of
https://github.com/hanxi/xiaomusic.git
synced 2026-04-19 21:39:17 +08:00
feat: 优化界面:移除【推送】按钮,统一合并为【播放】 (#803)
This commit is contained in:
4
xiaomusic/static/default/md.js
vendored
4
xiaomusic/static/default/md.js
vendored
@@ -897,7 +897,7 @@ function _refresh_music_list(callback) {
|
||||
$("#music_list").empty();
|
||||
$.get("/musiclist", function (data, status) {
|
||||
console.log(data, status);
|
||||
favoritelist = data["收藏"];
|
||||
favoritelist = data["收藏"] || [];
|
||||
|
||||
// 收集所有播放列表选项
|
||||
var playlistOptions = [];
|
||||
@@ -1574,7 +1574,7 @@ function startWebSocket(did, token) {
|
||||
offset = data.offset || 0;
|
||||
duration = data.duration || 0;
|
||||
|
||||
if (favoritelist.includes(cur_music)) {
|
||||
if (favoritelist && favoritelist.includes(cur_music)) {
|
||||
$(".favorite").addClass("favorite-active");
|
||||
} else {
|
||||
$(".favorite").removeClass("favorite-active");
|
||||
|
||||
129
xiaomusic/static/onlineSearch/index.html
vendored
129
xiaomusic/static/onlineSearch/index.html
vendored
@@ -144,7 +144,7 @@
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.search-btn, .play-all-btn, .push-btn {
|
||||
.search-btn, .play-all-btn {
|
||||
padding: 10px 20px;
|
||||
background-color: #007bff;
|
||||
color: white;
|
||||
@@ -160,12 +160,12 @@
|
||||
gap: 6px;
|
||||
}
|
||||
|
||||
.search-btn:hover, .play-all-btn:hover, .push-btn:hover {
|
||||
.search-btn:hover, .play-all-btn:hover {
|
||||
background-color: #0056b3;
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
|
||||
.search-btn:active, .play-all-btn:active, .push-btn:active {
|
||||
.search-btn:active, .play-all-btn:active {
|
||||
transform: translateY(0);
|
||||
}
|
||||
|
||||
@@ -645,7 +645,7 @@
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.search-btn, .push-btn, .play-all-btn {
|
||||
.search-btn, .play-all-btn {
|
||||
flex: 1;
|
||||
padding: 12px;
|
||||
font-size: 14px;
|
||||
@@ -821,6 +821,9 @@
|
||||
<select class="search-select" id="pluginSelect">
|
||||
<option value="all">聚合搜索</option>
|
||||
</select>
|
||||
<select class="search-select" id="deviceSelect">
|
||||
<option value="web_device">网页端-本地</option>
|
||||
</select>
|
||||
<div class="search-actions">
|
||||
<button class="search-btn" id="searchBtn">
|
||||
<span class="material-icons">search</span>
|
||||
@@ -830,10 +833,6 @@
|
||||
<span class="material-icons">play_arrow</span>
|
||||
<span>播放全部</span>
|
||||
</button>
|
||||
<button class="push-btn" id="pushAllBtn">
|
||||
<span class="material-icons">cast</span>
|
||||
<span>推送全部</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -898,6 +897,7 @@
|
||||
let currentLyrics = []; // 存储解析后的歌词
|
||||
let lyricLines = []; // 存储歌词 DOM 元素
|
||||
let songList = []; // 存储搜索到的歌曲列表
|
||||
let currentDeviceId = localStorage.getItem('cur_did') || 'web_device'; // 当前选中的设备
|
||||
|
||||
// 分页相关变量
|
||||
let currentPage = 1;
|
||||
@@ -932,12 +932,13 @@
|
||||
}
|
||||
//加载插件
|
||||
loadPlugins();
|
||||
// 加载设备列表
|
||||
loadDevices();
|
||||
// 绑定搜索按钮事件
|
||||
document.getElementById('searchBtn').addEventListener('click', searchMusic);
|
||||
|
||||
// 绑定其他按钮事件
|
||||
document.getElementById('pushAllBtn').addEventListener('click', pushAllMusic);
|
||||
document.getElementById('playAllBtn').addEventListener('click', webPlayAllMusic);
|
||||
document.getElementById('playAllBtn').addEventListener('click', handlePlayAllMusic);
|
||||
|
||||
// 绑定回车键事件
|
||||
document.getElementById('searchInput').addEventListener('keypress', function(e) {
|
||||
@@ -952,6 +953,61 @@
|
||||
});
|
||||
});
|
||||
|
||||
// 加载设备列表
|
||||
async function loadDevices() {
|
||||
try {
|
||||
const response = await fetch('/getsetting');
|
||||
const data = await response.json();
|
||||
|
||||
const deviceSelect = document.getElementById('deviceSelect');
|
||||
deviceSelect.innerHTML = '';
|
||||
|
||||
const dids = data.mi_did ? data.mi_did.split(',') : [];
|
||||
const devices = data.devices || {};
|
||||
|
||||
dids.forEach(didValue => {
|
||||
const device = Object.values(devices).find(d => d.did === didValue);
|
||||
if (device) {
|
||||
const option = document.createElement('option');
|
||||
option.value = didValue;
|
||||
option.textContent = device.name;
|
||||
deviceSelect.appendChild(option);
|
||||
}
|
||||
});
|
||||
|
||||
// 添加网页端本地选项
|
||||
const webOption = document.createElement('option');
|
||||
webOption.value = 'web_device';
|
||||
webOption.textContent = '网页端-本地';
|
||||
deviceSelect.appendChild(webOption);
|
||||
|
||||
// 恢复上次选择的设备
|
||||
if (currentDeviceId && dids.includes(currentDeviceId)) {
|
||||
deviceSelect.value = currentDeviceId;
|
||||
} else if (currentDeviceId === 'web_device') {
|
||||
deviceSelect.value = 'web_device';
|
||||
} else if (dids.length > 0) {
|
||||
currentDeviceId = dids[0];
|
||||
deviceSelect.value = currentDeviceId;
|
||||
} else {
|
||||
currentDeviceId = 'web_device';
|
||||
deviceSelect.value = 'web_device';
|
||||
}
|
||||
|
||||
// 设备选择变化时保存
|
||||
deviceSelect.addEventListener('change', function() {
|
||||
currentDeviceId = this.value;
|
||||
localStorage.setItem('cur_did', currentDeviceId);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('加载设备列表失败:', error);
|
||||
// 出错时添加默认选项
|
||||
const deviceSelect = document.getElementById('deviceSelect');
|
||||
deviceSelect.innerHTML = '<option value="web_device">网页端-本地</option>';
|
||||
currentDeviceId = 'web_device';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 平滑滚动到顶部
|
||||
function scrollToTop() {
|
||||
@@ -1099,8 +1155,7 @@
|
||||
</div>
|
||||
<div class="music-actions">
|
||||
<span class="source-tag">${item.platform || 'online'}</span>
|
||||
<button class="web-play-btn" onclick="webPlayMusic(${JSON.stringify(item).replace(/"/g, '"')})">播放</button>
|
||||
<button class="push-btn" onclick="pushMusic(${JSON.stringify(item).replace(/"/g, '"')})">推送</button>
|
||||
<button class="web-play-btn" onclick="handlePlayMusic(${JSON.stringify(item).replace(/"/g, '"')})">播放</button>
|
||||
</div>
|
||||
</div>
|
||||
`).join('');
|
||||
@@ -1169,25 +1224,43 @@
|
||||
|
||||
// 获取当前选中的设备 ID
|
||||
function getCurrentDeviceId() {
|
||||
// 从 localStorage 获取当前设备 ID
|
||||
return localStorage.getItem('cur_did') || '';
|
||||
return currentDeviceId || localStorage.getItem('cur_did') || 'web_device';
|
||||
}
|
||||
|
||||
// 统一的播放处理函数
|
||||
async function handlePlayMusic(mediaItem) {
|
||||
const deviceId = getCurrentDeviceId();
|
||||
if (deviceId === 'web_device') {
|
||||
await webPlayMusic(mediaItem);
|
||||
} else {
|
||||
await pushMusic(mediaItem);
|
||||
}
|
||||
}
|
||||
|
||||
// 统一的播放全部处理函数
|
||||
async function handlePlayAllMusic() {
|
||||
const deviceId = getCurrentDeviceId();
|
||||
if (songList.length === 0) {
|
||||
alert('请先搜索歌曲!');
|
||||
return;
|
||||
}
|
||||
if (deviceId === 'web_device') {
|
||||
await webPlayAllMusic();
|
||||
} else {
|
||||
await pushAllMusic();
|
||||
}
|
||||
}
|
||||
|
||||
// 推送音乐(设备播放)
|
||||
async function pushMusic(mediaItem) {
|
||||
try {
|
||||
const deviceId = getCurrentDeviceId();
|
||||
if (!deviceId || deviceId === '') {
|
||||
alert('请先设置设备!');
|
||||
return;
|
||||
}
|
||||
if (!mediaItem) {
|
||||
alert('歌曲不存在!');
|
||||
return;
|
||||
}
|
||||
const mediaItemList = [mediaItem];
|
||||
let playlistName = '_online_webPush';
|
||||
// 批量推送音乐到设备
|
||||
await pushList(playlistName, deviceId, mediaItemList);
|
||||
} catch (error) {
|
||||
alert('播放出错:' + error.message);
|
||||
@@ -1198,18 +1271,12 @@
|
||||
async function pushAllMusic() {
|
||||
try {
|
||||
const deviceId = getCurrentDeviceId();
|
||||
if (!deviceId || deviceId === '') {
|
||||
alert('请先设置设备!');
|
||||
return;
|
||||
}
|
||||
if (songList.length === 0) {
|
||||
alert('请先搜索歌曲!');
|
||||
return;
|
||||
}
|
||||
console.log("songList",songList)
|
||||
// 格式化 musicList
|
||||
console.log("songList", songList);
|
||||
let playlistName = '_online_webPush';
|
||||
// 批量推送音乐到设备
|
||||
await pushList(playlistName, deviceId, songList);
|
||||
} catch (error) {
|
||||
alert('播放出错:' + error.message);
|
||||
@@ -1217,27 +1284,25 @@
|
||||
}
|
||||
|
||||
// 推送音乐列表到设备
|
||||
async function pushList(playlistName, deviceId, songList){
|
||||
async function pushList(playlistName, deviceId, musicList) {
|
||||
const response = await fetch('/api/device/pushList', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(
|
||||
{ playlistName: playlistName,did: deviceId, songList: songList}
|
||||
{ playlistName: playlistName, did: deviceId, songList: musicList }
|
||||
)
|
||||
});
|
||||
const data = await response.json();
|
||||
// 根据新返回结构调整处理逻辑
|
||||
if (data) {
|
||||
if (data.success) {
|
||||
// 显示服务端返回的具体消息而不是通用提示
|
||||
alert('推送成功!');
|
||||
} else {
|
||||
alert('推送失败:'+data.error);
|
||||
alert('推送失败:' + data.error);
|
||||
}
|
||||
} else {
|
||||
alert('播放失败:' + ('未知错误'));
|
||||
alert('播放失败:未知错误');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user