Skip to content

Commit

Permalink
feat(music): ranking-list,ranking-detail
Browse files Browse the repository at this point in the history
  • Loading branch information
ninenan committed Jul 19, 2021
1 parent a234a71 commit fcdd08c
Show file tree
Hide file tree
Showing 12 changed files with 367 additions and 27 deletions.
32 changes: 32 additions & 0 deletions src/api/topList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* @Author: NineNan
* @Date: 2021-07-19 21:31:39
* @LastEditTime: 2021-07-19 22:52:22
* @LastEditors: Please set LastEditors
* @Description: topList
* @FilePath: /study_vue03/src/api/topList.ts
*/

import { get } from "@/service/http";
import { IRankList } from "@/types/index";
/**
* 获取排行榜
*/
export const getTopList = <T>(): Promise<T> => {
return get("/api/getTopList") as Promise<T>;
};

/**
* 获取排行榜详情
* @param {IRankList} top
* @returns
*/
export const getTopDetail = <T>(top: {
id: string;
period: string;
}): Promise<T> => {
return get("/api/getTopDetail", {
id: top.id,
period: top.period,
}) as Promise<T>;
};
2 changes: 1 addition & 1 deletion src/components/tab/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default {
},
{
name: "排行",
path: "/music/rankingList",
path: "/music/ranking-list",
},
{
name: "搜索",
Expand Down
5 changes: 4 additions & 1 deletion src/helpers/constant.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: NineNan
* @Date: 2021-05-11 22:42:52
* @LastEditTime: 2021-07-17 23:33:59
* @LastEditTime: 2021-07-19 22:58:00
* @LastEditors: Please set LastEditors
* @Description: constant
* @FilePath: /study_vue03/src/helpers/constant.ts
Expand All @@ -28,6 +28,9 @@ export const ADD_SONG_LYRIC = "ADD_SONG_LYRIC";
// recommend
export const CACHE_ALBUM_INFO = "CACHE_ALBUM_INFO";

// ranking
export const CACHE_RANKING_INFO = "CACHE_RANKING_INFO";

// storage
export const STORAGE = {
favorites: "__FAVORITES__",
Expand Down
19 changes: 16 additions & 3 deletions src/router/modules/music.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: NineNan
* @Date: 2021-05-17 22:55:24
* @LastEditTime: 2021-07-17 23:00:44
* @LastEditTime: 2021-07-19 22:44:40
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /study_vue03/src/router/modules/music.ts
Expand Down Expand Up @@ -34,9 +34,22 @@ const music: RouteRecordRaw = {
component: () => import("@/views/search/index.vue"),
},
{
path: "rankingList",
path: "ranking-list",
name: "rankingList",
component: () => import("@/views/search/index.vue"),
component: () => import("@/views/music/ranking-list/index.vue"),
meta: {
title: "排行榜",
},
children: [
{
path: ":id",
name: "rankingDetail",
component: () => import("@/views/music/ranking-detail/index.vue"),
meta: {
title: "排行榜详情",
},
},
],
},
{
path: "singer",
Expand Down
36 changes: 33 additions & 3 deletions src/store/modules/singer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
/*
* @Author: NineNan
* @Date: 2021-06-05 18:59:41
* @LastEditTime: 2021-07-17 23:38:07
* @LastEditTime: 2021-07-19 23:06:57
* @LastEditors: Please set LastEditors
* @Description: singer store
* @FilePath: /study_vue03/src/store/modules/singer.ts
*/
import { CACHE_SINGER_INFO, CACHE_ALBUM_INFO } from "@/helpers/constant";
import { ISingerStore, ISingerInfo, IRecommendAlbums } from "@/types/index";
import {
CACHE_SINGER_INFO,
CACHE_ALBUM_INFO,
CACHE_RANKING_INFO,
} from "@/helpers/constant";
import {
ISingerStore,
ISingerInfo,
IRecommendAlbums,
IRankList,
} from "@/types/index";
const state: ISingerStore = {
singerInfo: sessionStorage.getItem(CACHE_SINGER_INFO)
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Expand All @@ -17,6 +26,10 @@ const state: ISingerStore = {
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
JSON.parse(sessionStorage.getItem(CACHE_ALBUM_INFO)!)
: null,
rankingInfo: sessionStorage.getItem(CACHE_RANKING_INFO)
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
JSON.parse(sessionStorage.getItem(CACHE_RANKING_INFO)!)
: null,
};

const getters = {
Expand All @@ -31,6 +44,14 @@ const getters = {
getAlbumInfo: (state: ISingerStore): IRecommendAlbums | null => {
return state.albumInfo;
},
/**
* 获取本地 ranking 缓存
* @param state
* @returns
*/
getRankingInfo: (state: ISingerStore): IRankList | null => {
return state.rankingInfo;
},
};

const mutations = {
Expand All @@ -47,6 +68,15 @@ const mutations = {
state.singerInfo = payload;
sessionStorage.setItem(CACHE_ALBUM_INFO, JSON.stringify(payload));
},
/**
* 保存 ranking 详情本地缓存
* @param state
* @param payload
*/
[CACHE_RANKING_INFO]: (state: ISingerStore, payload: IRankList): void => {
state.rankingInfo = payload;
sessionStorage.setItem(CACHE_RANKING_INFO, JSON.stringify(payload));
},
};

export default {
Expand Down
15 changes: 14 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: NineNan
* @Date: 2021-05-10 13:50:43
* @LastEditTime: 2021-07-17 23:36:51
* @LastEditTime: 2021-07-19 22:59:27
* @LastEditors: Please set LastEditors
* @Description: types
* @FilePath: \study-vue3\src\types\index.ts
Expand Down Expand Up @@ -52,6 +52,7 @@ export interface IStore {
export interface ISingerStore {
singerInfo: ISingerInfo | null;
albumInfo: IRecommendAlbums | null;
rankingInfo: IRankList | null;
}

export interface IMusicStore {
Expand Down Expand Up @@ -112,3 +113,15 @@ export interface ISingerList {
list: ISingerInfo[];
title: string;
}

export interface IRankList {
id: number;
name: string;
period: string;
pic: string;
songList: {
id: number;
singerName: string;
songName: string;
}[];
}
File renamed without changes.
117 changes: 117 additions & 0 deletions src/views/music/ranking-detail/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<!--
* @Author: NineNan
* @Date: 2021-07-19 22:22:25
* @LastEditTime: 2021-07-19 23:10:12
* @LastEditors: Please set LastEditors
* @Description: 排行榜详情
* @FilePath: /study_vue03/src/views/music/ranking-detail/index.vue
-->
<template>
<div class="ranking-detail">
<MusicList
:songs="songsList"
:title="title"
:pic="pic"
:loading="loading"
/>
</div>
</template>
<script lang="ts">
import { useRoute, useRouter } from "vue-router";
import { PropType, ref, computed, Ref } from "vue";
import { getTopDetail } from "@/api/topList";
import { processSongs } from "@/api/song";
import { IRankList, ISingerDetailsInfo } from "@/types/index";
import MusicList from "@components/musicList/MusicList.vue";
import { useStore } from "@/store/index";
import singer from "@/store/modules/singer";

interface IProps {
singer: IRankList | null;
}

interface ISingerDetails {
songsList: Ref<ISingerDetailsInfo[]>;
pic: Ref<string | undefined>;
title: Ref<string | undefined>;
loading: Ref<boolean>;
computedSingerInfo: Ref<IRankList | null>;
}

export default {
name: "SingerDetails",
components: {
MusicList,
},
props: {
singer: {
type: Object as PropType<IRankList | null>,
},
},
async setup(props: IProps): Promise<ISingerDetails | undefined> {
const route = useRoute();
const router = useRouter();
const store = useStore();
const loading = ref(true);
let songsList = ref<ISingerDetailsInfo[]>([]);
const cacheInfo = computed(() => store.getters.getRankingInfo);
const computedSingerInfo = computed(() => {
let result: IRankList | null = null;
if (props?.singer?.id) {
result = props.singer;
} else {
if (cacheInfo.value?.id === +route.params.id) {
result = cacheInfo.value;
}
}
return result;
});
const pic = computed(() => {
return computedSingerInfo.value?.pic;
});
const title = computed(() => {
return computedSingerInfo.value?.name;
});

if (!computedSingerInfo.value) {
const path = route.matched[0].path;
router.push({ path });
return;
}

const params = {
id: route.params.id as string,
period: route.params.period as string,
};

const { songs } = await getTopDetail<{ songs: ISingerDetailsInfo[] }>(
params
);

processSongs(songs).then((res) => {
const processSongsRes = res;
songsList.value = processSongsRes;
loading.value = false;
});

return {
pic,
title,
loading,
songsList,
computedSingerInfo,
};
},
};
</script>
<style lang="scss" scoped>
.ranking-detail {
position: fixed;
z-index: 10;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: $color-background;
}
</style>
40 changes: 40 additions & 0 deletions src/views/music/ranking-list/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.ranking-list {
position: fixed;
width: 100%;
top: 44px;
bottom: 0;
.top-list-content {
height: 100%;
overflow: hidden;
.item {
display: flex;
margin: 0 20px;
padding-top: 20px;
height: 100px;
&:last-child {
padding-bottom: 20px;
}
.icon {
flex: 0 0 100px;
width: 100px;
height: 100px;
}
.song-list {
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
padding: 0 20px;
height: 100px;
overflow: hidden;
background: $color-highlight-background;
color: $color-text-d;
font-size: $font-size-small;
.song {
@include no-wrap();
line-height: 26px;
}
}
}
}
}
Loading

0 comments on commit fcdd08c

Please sign in to comment.