fix: search issues

master
qier222 4 years ago
parent 0ef108df4c
commit 86452f0420

@ -22,7 +22,7 @@ export function search(params) {
method: "get", method: "get",
params, params,
}).then((data) => { }).then((data) => {
if (data.result.song !== undefined) if (data.result?.song !== undefined)
data.result.song.songs = mapTrackPlayableStatus(data.result.song.songs); data.result.song.songs = mapTrackPlayableStatus(data.result.song.songs);
return data; return data;
}); });

@ -38,7 +38,8 @@
ref="searchInput" ref="searchInput"
:placeholder="inputFocus ? '' : $t('nav.search')" :placeholder="inputFocus ? '' : $t('nav.search')"
v-model="keywords" v-model="keywords"
@focus="focusSearchBox" @keydown.enter="doSearch"
@focus="inputFocus = true"
@blur="inputFocus = false" @blur="inputFocus = false"
/> />
</div> </div>
@ -61,32 +62,29 @@ export default {
return { return {
inputFocus: false, inputFocus: false,
langs: ["zh-CN", "en"], langs: ["zh-CN", "en"],
keywords: "",
}; };
}, },
computed: { computed: {
...mapState(["settings", "search"]), ...mapState(["settings"]),
keywords: {
get() {
return this.search.keywords;
},
set(value) {
this.$store.commit("updateSearch", { key: "keywords", value });
},
},
}, },
methods: { methods: {
go(where) { go(where) {
if (where === "back") this.$router.go(-1); if (where === "back") this.$router.go(-1);
else this.$router.go(1); else this.$router.go(1);
}, },
focusSearchBox() { doSearch() {
this.inputFocus = true; if (!this.keywords) return;
if (this.$route.name !== "search") { if (
this.$route.name === "search" &&
this.$route.params.keywords === this.keywords
) {
return;
}
this.$router.push({ this.$router.push({
name: "search", name: "search",
params: { keywords: this.keywords }, params: { keywords: this.keywords },
}); });
}
}, },
}, },
}; };

@ -36,7 +36,4 @@ export default {
toggleLyrics(state) { toggleLyrics(state) {
state.showLyrics = !state.showLyrics; state.showLyrics = !state.showLyrics;
}, },
updateSearch(state, { key, value }) {
state.search[key] = value;
},
}; };

@ -34,9 +34,6 @@ export default {
afterCreateAddTrackID: 0, afterCreateAddTrackID: 0,
}, },
}, },
search: {
keywords: "",
},
player: JSON.parse(localStorage.getItem("player")), player: JSON.parse(localStorage.getItem("player")),
settings: JSON.parse(localStorage.getItem("settings")), settings: JSON.parse(localStorage.getItem("settings")),
data: JSON.parse(localStorage.getItem("data")), data: JSON.parse(localStorage.getItem("data")),

@ -1,5 +1,5 @@
<template> <template>
<div class="search"> <div class="search" v-show="show">
<div class="row" v-show="artists.length > 0 || albums.length > 0"> <div class="row" v-show="artists.length > 0 || albums.length > 0">
<div class="artists"> <div class="artists">
<div class="section-title" v-show="artists.length > 0" <div class="section-title" v-show="artists.length > 0"
@ -85,6 +85,7 @@
<script> <script>
import { getTrackDetail } from "@/api/track"; import { getTrackDetail } from "@/api/track";
import { search } from "@/api/others"; import { search } from "@/api/others";
import NProgress from "nprogress";
import TrackList from "@/components/TrackList.vue"; import TrackList from "@/components/TrackList.vue";
import MvRow from "@/components/MvRow.vue"; import MvRow from "@/components/MvRow.vue";
@ -99,6 +100,7 @@ export default {
}, },
data() { data() {
return { return {
show: false,
tracks: [], tracks: [],
artists: [], artists: [],
albums: [], albums: [],
@ -108,7 +110,7 @@ export default {
}, },
computed: { computed: {
keywords() { keywords() {
return this.$store.state.search.keywords; return this.$route.params.keywords ?? "";
}, },
haveResult() { haveResult() {
return ( return (
@ -135,20 +137,17 @@ export default {
artists: 100, artists: 100,
playlists: 1000, playlists: 1000,
}; };
const keywords = this.keywords; return search({
return search({ keywords, type: typeTable[type], limit: 16 }).then( keywords: this.keywords,
(result) => { type: typeTable[type],
limit: 16,
}).then((result) => {
return { result: result.result, type }; return { result: result.result, type };
} });
);
}, },
getData() { getData() {
const requests = [ NProgress.start();
this.search("artists"), this.show = false;
this.search("albums"),
this.search("tracks"),
];
const requests2 = [this.search("musicVideos"), this.search("playlists")];
const requestAll = (requests) => { const requestAll = (requests) => {
const keywords = this.keywords; const keywords = this.keywords;
@ -156,6 +155,7 @@ export default {
if (keywords != this.keywords) return; if (keywords != this.keywords) return;
results.map((result) => { results.map((result) => {
const searchType = result.type; const searchType = result.type;
if (result.result === undefined) return;
result = result.result; result = result.result;
switch (searchType) { switch (searchType) {
case "all": case "all":
@ -179,9 +179,18 @@ export default {
break; break;
} }
}); });
NProgress.done();
this.show = true;
}); });
}; };
const requests = [
this.search("artists"),
this.search("albums"),
this.search("tracks"),
];
const requests2 = [this.search("musicVideos"), this.search("playlists")];
requestAll(requests); requestAll(requests);
requestAll(requests2); requestAll(requests2);
}, },
@ -194,19 +203,12 @@ export default {
}, },
}, },
created() { created() {
if (this.keywords.length === 0) { this.getData();
this.$store.commit("updateSearch", {
key: "keywords",
value: this.$route.params.keywords,
});
}
}, },
watch: { watch: {
keywords: function () { keywords: function (newKeywords) {
if (newKeywords.length === 0) return;
this.getData(); this.getData();
this.$router.replace({
params: { keywords: this.keywords },
});
}, },
}, },
}; };

@ -7,7 +7,7 @@
</h1> </h1>
<div v-if="type === 'artists'"> <div v-if="type === 'artists'">
<CoverRow type="artist" :items="result" columnNumber="6" /> <CoverRow type="artist" :items="result" :columnNumber="6" />
</div> </div>
<div v-if="type === 'albums'"> <div v-if="type === 'albums'">
<CoverRow <CoverRow
@ -63,7 +63,7 @@ export default {
}, },
computed: { computed: {
keywords() { keywords() {
return this.$store.state.search.keywords; return this.$route.params.keywords;
}, },
type() { type() {
return camelCase(this.$route.params.type); return camelCase(this.$route.params.type);
@ -131,12 +131,6 @@ export default {
}, },
}, },
created() { created() {
if (this.keywords.length === 0) {
this.$store.commit("updateSearch", {
key: "keywords",
value: this.$route.params.keywords,
});
}
this.fetchData(); this.fetchData();
}, },
}; };

Loading…
Cancel
Save