6
6
v-model:value =" searchInputValue"
7
7
:class =" searchInputFocus ? 'input focus' : 'input'"
8
8
:input-props =" { autoComplete: false }"
9
+ :placeholder =" searchPlaceholder"
9
10
:allow-input =" noSideSpace"
10
- placeholder =" 搜索音乐 / 视频"
11
11
round
12
12
clearable
13
13
@focus =" searchInputToFocus"
43
43
import { storeToRefs } from " pinia" ;
44
44
import { useRouter } from " vue-router" ;
45
45
import { getSongDetail } from " @/api/song" ;
46
+ import { getSearchDefault } from " @/api/search" ;
46
47
import { siteData , siteStatus , musicData } from " @/stores" ;
47
48
import { addSongToNext , initPlayer } from " @/utils/Player" ;
48
49
import formatData from " @/utils/formatData" ;
@@ -55,8 +56,12 @@ const { searchHistory } = storeToRefs(data);
55
56
const { playSongData } = storeToRefs (music);
56
57
const { searchInputFocus } = storeToRefs (status);
57
58
59
+ // 搜索框数据
58
60
const searchInpRef = ref (null );
59
61
const searchInputValue = ref (" " );
62
+ const searchInterval = ref (null );
63
+ const searchRealkeyword = ref (null );
64
+ const searchPlaceholder = ref (" 搜索音乐 / 视频" );
60
65
61
66
// 搜索框输入限制
62
67
const noSideSpace = (value ) => ! value .startsWith (" " );
@@ -80,6 +85,25 @@ const setSearchHistory = (name) => {
80
85
}
81
86
};
82
87
88
+ // 更换搜索框关键词
89
+ const updatePlaceholder = async () => {
90
+ try {
91
+ const result = await getSearchDefault ();
92
+ searchPlaceholder .value = result .data .showKeyword ;
93
+ searchRealkeyword .value = result .data .realkeyword ;
94
+ } catch (error) {
95
+ console .error (" 搜索关键词获取失败:" , error);
96
+ searchPlaceholder .value = " 搜索音乐 / 视频" ;
97
+ }
98
+ };
99
+
100
+ // 更新搜索框关键词
101
+ const changePlaceholder = () => {
102
+ updatePlaceholder ();
103
+ // 5分钟
104
+ searchInterval .value = setInterval (updatePlaceholder, 5 * 60 * 1000 );
105
+ };
106
+
83
107
// 关闭搜索
84
108
const closeSearch = () => {
85
109
// 取消聚焦状态
@@ -107,9 +131,15 @@ const toPlaySong = async (id) => {
107
131
108
132
// 前往搜索
109
133
const toSearch = (val , type = " song" ) => {
110
- if (! val) return false ;
134
+ // 未输入内容且不存在推荐
135
+ if (! val && searchPlaceholder .value === " 搜索音乐 / 视频" ) return false ;
136
+ if (! val && searchPlaceholder .value !== " 搜索音乐 / 视频" && searchRealkeyword .value ) {
137
+ val = searchRealkeyword .value ? .trim ();
138
+ }
111
139
// 取消聚焦状态
112
140
closeSearch ();
141
+ // 更新推荐
142
+ updatePlaceholder ();
113
143
// 触发测试
114
144
if (Number (val) === 114514 ) return router .push (" /test" );
115
145
// 判断类型
@@ -161,6 +191,14 @@ const toSearch = (val, type = "song") => {
161
191
break ;
162
192
}
163
193
};
194
+
195
+ onMounted (() => {
196
+ changePlaceholder ();
197
+ });
198
+
199
+ onBeforeUnmount (() => {
200
+ clearInterval (searchInterval .value );
201
+ });
164
202
< / script>
165
203
166
204
< style lang= " scss" scoped>
0 commit comments