// my-component.js // 引入全局 Vue 对象(因在 HTML 中通过 script 引入,Vue 已挂载到 window) const { defineComponent, ref, onMounted, nextTick } = Vue; // 定义组件(直接使用模板) export const headTop = defineComponent({ name: "headTop", props: { page: { type: String, default: "", }, }, setup(props) { onMounted(() => { getHistorySearch(); }); let state = ref(0); // 是否已经签到 let userInfoWinTimerCount = 0; const userInfoWinTimer = setInterval(() => { if (location.host == "127.0.0.1:5501") return; if (todaysignedState) { state.value = todaysigned; clearInterval(userInfoWinTimer); } userInfoWinTimerCount++; if (userInfoWinTimerCount >= 3000) clearInterval(userInfoWinTimer); }, 50); const signIn = () => { ajax("/v2/api/forum/sign").then((res) => { if (res.code != 200) { creationAlertBox("error", res.message); return; } let data = res.data; state.value = 1; creationAlertBox("success", res.message || "签到成功"); }); }; let pitchState = ref(false); let page = ref(...props.page); // console.log("page", page.value); let input = ref(""); let defaultSearchText = ref("屯特"); let historySearchList = ref([]); // 历史搜索数据 // 获取历史搜索 const getHistorySearch = () => { const data = JSON.parse(localStorage.getItem("history-search")) || []; console.log("data", data); historySearchList.value = data; }; // 跳转搜索 const searchEvent = (value) => { const kw = value || input.value || defaultSearchText.value; historySearchList.value.unshift(kw); historySearchList.value = [...new Set(historySearchList.value)]; if (historySearchList.value.length > 10) historySearchList.value = historySearchList.value.splice(0, 10); localStorage.setItem("history-search", JSON.stringify(historySearchList.value)); redirectToExternalWebsite("/search/" + kw); searchInputBlur(); }; let searchHistoryShowState = ref(false); // 历史记录的展开状态 let searchInputState = ref(false); // 搜索框的状态 // 切换历史记录展示状态 const searchInputFocus = () => { searchInputState.value = true; if (historySearchList.value.length == 0) return; searchHistoryShowState.value = true; }; const searchInputBlur = () => { setTimeout(() => { searchInputState.value = false; searchHistoryShowState.value = false; }, 200); }; return { historySearchList, searchEvent, searchInputState, searchHistoryShowState, searchInputFocus, searchInputBlur, page, pitchState, state, signIn, input, defaultSearchText }; }, template: `
历史搜索
{{ item }}
`, });