parent
f5332c270b
commit
c2270f9b4d
@ -0,0 +1,56 @@
|
||||
import { produce } from 'immer';
|
||||
import { create } from 'zustand';
|
||||
import { devtools, persist } from 'zustand/middleware';
|
||||
|
||||
import { apiGetSongInfo } from '@/services';
|
||||
|
||||
interface AuioState {
|
||||
/** 播放状态 */
|
||||
play: boolean;
|
||||
/** 播放器显示状态 */
|
||||
show: boolean;
|
||||
/** 当前音频信息 */
|
||||
audioInfo: SongInfo | null;
|
||||
/** 播放列表 */
|
||||
playList: Array<SongInfo | null>;
|
||||
/** 显示/隐藏播放器 */
|
||||
setShow: (value: boolean) => void;
|
||||
/** 获取歌曲信息 */
|
||||
getSongInfo: (id: string) => Promise<boolean>;
|
||||
/** 切换歌曲 */
|
||||
switchSong: (id: string) => Promise<boolean>;
|
||||
}
|
||||
|
||||
const useAuioState = create<AuioState>()(
|
||||
devtools(
|
||||
persist(
|
||||
(set) => {
|
||||
const audioInfo = null;
|
||||
const getSongInfo = async (id: string) => {
|
||||
const res = await apiGetSongInfo(id);
|
||||
if (res.code === 200) set(produce((state) => void (state.audioInfo = res.data)));
|
||||
return res.code === 200;
|
||||
};
|
||||
|
||||
return {
|
||||
play: false,
|
||||
show: false,
|
||||
audioInfo,
|
||||
playList: [],
|
||||
switchSong: async (id: string) => {
|
||||
const res = await getSongInfo(id);
|
||||
return res;
|
||||
},
|
||||
setShow: (value) => set({ show: value }),
|
||||
getSongInfo,
|
||||
};
|
||||
},
|
||||
{
|
||||
name: 'audio',
|
||||
getStorage: () => localStorage,
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
export default useAuioState;
|
Loading…
Reference in new issue