feat: 新增专用的播放歌曲和播放歌单接口,解决默认口令提示词被修改了导致后台失效的问题

This commit is contained in:
涵曦 2024-12-02 11:40:26 +08:00
parent cb2af0ee9f
commit df3c4b7fa9
3 changed files with 81 additions and 8 deletions

View File

@ -325,6 +325,44 @@ class UrlInfo(BaseModel):
url: str
class DidPlayMusic(BaseModel):
did: str
musicname: str = ""
searchkey: str = ""
@app.post("/playmusic")
async def playmusic(data: DidPlayMusic, Verifcation=Depends(verification)):
did = data.did
musicname = data.musicname
searchkey = data.searchkey
if not xiaomusic.did_exist(did):
return {"ret": "Did not exist"}
log.info(f"playmusic {did} musicname:{musicname} searchkey:{searchkey}")
await xiaomusic.do_play(did, musicname, searchkey)
return {"ret": "OK"}
class DidPlayMusicList(BaseModel):
did: str
listname: str = ""
musicname: str = ""
@app.post("/playmusiclist")
async def playmusiclist(data: DidPlayMusicList, Verifcation=Depends(verification)):
did = data.did
listname = data.listname
musicname = data.musicname
if not xiaomusic.did_exist(did):
return {"ret": "Did not exist"}
log.info(f"playmusiclist {did} listname:{listname} musicname:{musicname}")
await xiaomusic.do_play_music_list(did, listname, musicname)
return {"ret": "OK"}
@app.post("/downloadjson")
async def downloadjson(data: UrlInfo, Verifcation=Depends(verification)):
log.info(data)

View File

@ -192,18 +192,32 @@ $(function(){
});
}
function do_play_music_list(listname, musicname) {
$.ajax({
type: "POST",
url: "/playmusiclist",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({did: did, listname: listname, musicname: musicname}),
success: () => {
console.log("do_play_music_list succ", listname, musicname);
},
error: () => {
console.log("do_play_music_list failed", listname, musicname);
}
});
}
$("#play_music_list").on("click", () => {
var music_list = $("#music_list").val();
var music_name = $("#music_name").val();
let cmd = "播放列表" + music_list + "|" + music_name;
if (no_warning) {
sendcmd(cmd);
do_play_music_list(music_list, music_name);
return;
}
$.get(`/musicinfo?name=${music_name}`, function(data, status) {
console.log(data);
if (data.ret == "OK") {
validHost(data.url) && sendcmd(cmd);
validHost(data.url) && do_play_music_list(music_list, music_name);
}
});
});
@ -268,6 +282,22 @@ $(function(){
$container.append($button);
}
function do_play_music(musicname, searchkey) {
$.ajax({
type: "POST",
url: "/playmusic",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({did: did, musicname: musicname, searchkey: searchkey}),
success: () => {
console.log("do_play_music succ", musicname, searchkey);
},
error: () => {
console.log("do_play_music failed", musicname, searchkey);
}
});
}
$("#play").on("click", () => {
var search_key = $("#music-name").val();
if (search_key == null) {
@ -277,8 +307,7 @@ $(function(){
if (filename == null) {
filename = "";
}
let cmd = "播放歌曲" + search_key + "|" + filename;
sendcmd(cmd);
do_play_music(filename, search_key);
});
$("#volume").on('change', function () {

View File

@ -950,14 +950,17 @@ class XiaoMusic:
parts = arg1.split("|")
list_name = parts[0]
music_name = ""
if len(parts) > 1:
music_name = parts[1]
return await self.do_play_music_list(did, list_name, music_name)
async def do_play_music_list(self, did, list_name, music_name=""):
list_name = self._find_real_music_list_name(list_name)
if list_name not in self.music_list:
await self.do_tts(did, f"播放列表{list_name}不存在")
return
music_name = ""
if len(parts) > 1:
music_name = parts[1]
await self.devices[did].play_music_list(list_name, music_name)
# 播放一个播放列表里第几个
@ -992,6 +995,9 @@ class XiaoMusic:
if name == "":
name = search_key
return await self.do_play(name, search_key)
async def do_play(self, did, name, search_key):
return await self.devices[did].play(name, search_key)
# 本地播放