Skip to content

Commit 051b8b2

Browse files
committed
feat:#1 搜索关键字
1 parent c4f3194 commit 051b8b2

File tree

7 files changed

+132
-31
lines changed

7 files changed

+132
-31
lines changed

Diff for: components/default/HeaderMenu.vue

+45-2
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,32 @@
3636
</div>
3737
</el-menu-item>
3838
</template>
39+
<el-form :inline="true" class="s-form">
40+
<el-form-item>
41+
<el-input
42+
style="max-width: 100%;min-width: 400px;"
43+
:placeholder="$t('header.search.k.placeholder')"
44+
maxlength="50"
45+
show-word-limit
46+
v-model="k"
47+
@keyup.enter.native="doSearch"
48+
/>
49+
</el-form-item>
50+
<el-form-item>
51+
<el-button type="primary" @click="doSearch">{{ $t('header.search') }}</el-button>
52+
</el-form-item>
53+
</el-form>
3954
</el-menu>
55+
4056
</client-only>
4157
</template>
4258

4359
<script lang="ts" setup>
44-
import {ElMenu, ElMenuItem, ElSubMenu} from "element-plus";
45-
import {ref} from 'vue'
60+
import {ElMenu, ElMenuItem, ElSubMenu, ElInput, ElButton, ElForm, ElFormItem} from "element-plus";
61+
import {onMounted, ref} from 'vue'
4662
import logUtil from "~/lib/logUtil";
63+
import {useSearchStore} from "~/stores/searchStore";
64+
import {inBrowser} from "~/lib/util";
4765
4866
const router = useRouter();
4967
@@ -96,6 +114,25 @@ const handleSelect = (key: string, keyPath: string[]) => {
96114
router.push({path: key});
97115
98116
}
117+
118+
const k = ref("")
119+
const {sk} = useSearchStore()
120+
const doSearch = () => {
121+
const key = "/s/" + k.value
122+
router.push({path: key});
123+
124+
if (inBrowser()) {
125+
if (window.location.href.indexOf("/s/") > -1) {
126+
window.location.href=key
127+
}
128+
}
129+
130+
console.log("doSearch=>" + key)
131+
}
132+
133+
onMounted(() => {
134+
k.value = sk
135+
})
99136
</script>
100137

101138
<script lang="ts">
@@ -115,4 +152,10 @@ export default {
115152
vertical-align: middle;
116153
padding-right: 5px;
117154
}
155+
156+
/* 搜索 */
157+
.s-form {
158+
padding-top: 12px;
159+
margin-left: 20px;
160+
}
118161
</style>

Diff for: components/default/HomePostList.vue

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<template>
22
<div id="postList">
33
<div v-if="postData.postList.length > 0">
4+
<el-row class="k-show">
5+
<el-col v-if="props.keyword !== ''" class="s-keyword-dark" :spans="24">
6+
关键字: {{ props.keyword }}
7+
</el-col>
8+
</el-row>
49
<el-card v-for="post in postData.postList" :key="post.postid" class="post-item">
510
<el-row>
6-
<el-col v-if="postData.keyword !== ''" class="s-keyword-dark" :spans="24">
7-
关键字: {{ postData.keyword }}
8-
</el-col>
911
<el-col
1012
v-if="!isMobile && post.thumbnails.length > 0"
1113
:xs="24"
@@ -41,7 +43,7 @@
4143
发布于 {{ post.dateCreated || (new Date()).toISOString() }}
4244
</div>
4345
<nuxt-link :to="'/post/' + post.postid + '.html'">
44-
<el-button type="text" class="read-more">查看全文</el-button>
46+
<el-link :underline="false" class="read-more">查看全文</el-link>
4547
</nuxt-link>
4648
</div>
4749
</el-col>
@@ -55,18 +57,30 @@
5557
import {SERVER_API_CONSTANTS} from "~/lib/constants/serverApiConstants";
5658
import {Post} from "~/lib/common/post";
5759
import {isMobile} from "~/lib/util";
58-
import {ElButton, ElCard, ElCol, ElRow} from "element-plus";
60+
import {ElCard, ElCol, ElLink, ElRow} from "element-plus";
61+
import logUtil from "~/lib/logUtil";
62+
63+
const props = defineProps({
64+
keyword: {
65+
type: String,
66+
default: ""
67+
},
68+
page: {
69+
type: Number,
70+
default: 1
71+
}
72+
})
5973
6074
const postData = ref({
6175
isMobile: isMobile(),
62-
keyword: "",
6376
postList: <Post[]>[]
6477
})
6578
6679
const route = useRoute()
6780
6881
const homePostsUrl = route.query.t ? SERVER_API_CONSTANTS.SERVER_API_GET_RECENT_POSTS + "?t=" + route.query.t :
6982
SERVER_API_CONSTANTS.SERVER_API_GET_RECENT_POSTS
83+
logUtil.logError(props.keyword)
7084
const {data} = await useFetch(homePostsUrl)
7185
// @ts-ignore
7286
// postData.value.postList = data.value.data
@@ -100,8 +114,8 @@ export default {
100114
}
101115
102116
#postList .post-item {
103-
margin: 0;
104-
padding: 10px;
117+
margin: 0 0 10px 0;
118+
padding: 0 10px;
105119
}
106120
107121
#postList .bottom {
@@ -153,4 +167,8 @@ export default {
153167
#postList .s-keyword-dark {
154168
color: red;
155169
}
170+
171+
#postList .k-show {
172+
margin-bottom: 20px;
173+
}
156174
</style>

Diff for: locales/en_US.ts

+2
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ export default {
99
"theme.choose.terwer": "Terwer theme",
1010
"ele.select.placeholder": "Select",
1111
"header.nowis":"Now is",
12+
"header.search":"Search",
13+
"header.search.k.placeholder":"Please input...",
1214
}

Diff for: locales/zh_CN.ts

+2
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ export default {
99
"theme.choose.terwer": "特维主题",
1010
"ele.select.placeholder": "请选择",
1111
"header.nowis":"现在是",
12+
"header.search":"搜索",
13+
"header.search.k.placeholder":"请输入关键字",
1214
}

Diff for: pages/post/[postid].vue

+20-21
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
<template>
22
<div id="post" class="post-detail-content-box">
3-
<!-- 导航 -->
4-
<!--
5-
<el-breadcrumb separator="/">
6-
<el-breadcrumb-item
7-
v-for="item in items"
8-
:key="item.text"
9-
:to="item.to"
10-
>
11-
{{ item.text }}
12-
</el-breadcrumb-item>
13-
</el-breadcrumb>
14-
-->
15-
163
<!-- 文章标题 -->
174
<div id="postTitle">
185
<div class="title-text">
@@ -21,19 +8,17 @@
218
<input type="hidden" :value="postObj.postid"/>
229
</div>
2310

24-
<!--
25-
<div v-if="postObj.tagArray">
11+
<div id="tagArea" v-if="postTags.length>0">
2612
<NuxtLink
27-
v-for="tagItem in postObj.tagArray"
28-
:key="tagItem.tag"
29-
:to="'/tag/' + tagItem.tag"
13+
v-for="tagItem in postTags"
14+
:key="tagItem"
15+
:to="'/tag/' + tagItem"
3016
>
31-
<el-tag :type="tagItem.color" class="post-tag">
32-
{{ tagItem.tag }}
17+
<el-tag class="post-tag">
18+
{{ tagItem }}
3319
</el-tag>
3420
</NuxtLink>
3521
</div>
36-
-->
3722

3823
<!-- 文章详情 -->
3924
<div
@@ -56,9 +41,12 @@
5641
import logUtil from "~/lib/logUtil";
5742
import {SERVER_API_CONSTANTS} from "~/lib/constants/serverApiConstants";
5843
import {Post} from "~/lib/common/post";
44+
import {isEmptyString} from "~/lib/util";
45+
import {ElTag} from "element-plus";
5946
6047
const route = useRoute()
6148
let postObj = new Post()
49+
let postTags = <string[]>[]
6250
6351
let postUrl = SERVER_API_CONSTANTS.SERVER_API_GET_POST;
6452
if (route.query.t) {
@@ -79,6 +67,9 @@ if (route.params.postid) {
7967
const {data} = await useFetch(postUrl, {initialCache: false})
8068
// @ts-ignore
8169
postObj = data.value.data
70+
if (postObj.mt_keywords && !isEmptyString(postObj.mt_keywords)) {
71+
postTags = postObj.mt_keywords.split(",")
72+
}
8273
logUtil.logInfo(postUrl + " data=>", data.value)
8374
</script>
8475

@@ -107,4 +98,12 @@ export default {
10798
font-size: 32px;
10899
color: var(--el-color-primary);
109100
}
101+
102+
#tagArea {
103+
margin-top: 10px;
104+
}
105+
106+
#tagArea .post-tag {
107+
margin-right: 10px;
108+
}
110109
</style>

Diff for: pages/s/[keyword].vue

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<template>
2+
<HomePostList :keyword="sk"></HomePostList>
3+
</template>
4+
5+
<script lang="ts" setup>
6+
import HomePostList from "~/components/default/HomePostList.vue";
7+
import {useSearchStore} from "~/stores/searchStore";
8+
9+
const route = useRoute()
10+
const {sk, setSk} = useSearchStore()
11+
setSk(route.params.keyword.toString())
12+
13+
</script>
14+
15+
<script lang="ts">
16+
export default {
17+
name: "keyword"
18+
}
19+
</script>
20+
21+
<style scoped>
22+
23+
</style>

Diff for: stores/searchStore.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {defineStore} from "pinia";
2+
3+
/**
4+
* 搜索关键字存储
5+
*/
6+
export const useSearchStore = defineStore("sk", () => {
7+
const sk = ref("")
8+
9+
function setSk(s: string) {
10+
sk.value = s
11+
}
12+
13+
return {sk, setSk}
14+
})

0 commit comments

Comments
 (0)