Skip to content

Commit

Permalink
播放列表排序显示,修复顺序播放问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Jun 20, 2024
1 parent d2473ec commit 4ad6bcc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
22 changes: 21 additions & 1 deletion xiaomusic/static/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ $(function(){
$('#music_list').change(function() {
const selectedValue = $(this).val();
$('#music_name').empty();
$.each(data[selectedValue], function(index, item) {
const sorted_musics = data[selectedValue].sort(custom_sort_key);
$.each(sorted_musics, function(index, item) {
$('#music_name').append($('<option></option>').val(item).text(item));
});
});
Expand Down Expand Up @@ -163,4 +164,23 @@ $(function(){
setInterval(() => {
get_playing_music();
}, 3000);

function custom_sort_key(a, b) {
// 使用正则表达式提取数字前缀
const numericPrefixA = a.match(/^(\d+)/) ? parseInt(a.match(/^(\d+)/)[1], 10) : null;
const numericPrefixB = b.match(/^(\d+)/) ? parseInt(b.match(/^(\d+)/)[1], 10) : null;

// 如果两个键都有数字前缀,则按数字大小排序
if (numericPrefixA !== null && numericPrefixB !== null) {
return numericPrefixA - numericPrefixB;
}

// 如果一个键有数字前缀而另一个没有,则有数字前缀的键排在前面
if (numericPrefixA !== null) return -1;
if (numericPrefixB !== null) return 1;

// 如果两个键都没有数字前缀,则按照常规字符串排序
return a.localeCompare(b);
}

});
5 changes: 3 additions & 2 deletions xiaomusic/xiaomusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,10 @@ def _gen_all_music_list(self):
# 歌曲排序或者打乱顺序
def _gen_play_list(self):
if self.play_type == PLAY_TYPE_RND:
self._play_list.sort(key=custom_sort_key)
else:
random.shuffle(self._play_list)
else:
self._play_list.sort(key=custom_sort_key)
self.log.debug("play_list:%s", self._play_list)

# 把下载的音乐加入播放列表
def add_download_music(self, name):
Expand Down

0 comments on commit 4ad6bcc

Please sign in to comment.