Skip to content

Commit

Permalink
feat:#1 搜索基本完成,暂停开发
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Oct 10, 2022
1 parent 051b8b2 commit 7dd20b4
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 28 deletions.
8 changes: 5 additions & 3 deletions components/default/HeaderMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
maxlength="50"
show-word-limit
v-model="k"
@input="handleKInput"
@keyup.enter.native="doSearch"
/>
</el-form-item>
Expand Down Expand Up @@ -117,14 +118,15 @@ const handleSelect = (key: string, keyPath: string[]) => {
const k = ref("")
const {sk} = useSearchStore()
const handleKInput = () => {
k.value = k.value.replace("_", " ")
}
const doSearch = () => {
const key = "/s/" + k.value
router.push({path: key});
if (inBrowser()) {
if (window.location.href.indexOf("/s/") > -1) {
window.location.href=key
}
window.location.href = key
}
console.log("doSearch=>" + key)
Expand Down
94 changes: 82 additions & 12 deletions components/default/HomePostList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,33 @@
</el-row>
</el-card>
</div>

<!-- 分页 -->
<el-pagination
small
background
layout="prev, pager, next"
:total="total"
class="mt-4"
:page-size="MAX_PAGE_SIZE"
@prev-click="handlePrevPage"
@next-click="handleNextPage"
@current-change="handleCurrentPage"
/>

<el-alert class="top-data-tip"
title="温馨提示:如果平台是思源笔记,请保证思源笔记启动并且打开伺服,默认伺服地址:http://127.0.0.1:6806。"
type="info" :closable="false"/>
</div>
</template>

<script lang="ts" setup>
import {SERVER_API_CONSTANTS} from "~/lib/constants/serverApiConstants";
import {Post} from "~/lib/common/post";
import {isMobile} from "~/lib/util";
import {ElCard, ElCol, ElLink, ElRow} from "element-plus";
import {ElCard, ElCol, ElLink, ElRow, ElAlert, ElPagination} from "element-plus";
import logUtil from "~/lib/logUtil";
import {onMounted} from "vue";
const props = defineProps({
keyword: {
Expand All @@ -76,19 +94,71 @@ const postData = ref({
postList: <Post[]>[]
})
/* 分页 */
const MAX_PAGE_SIZE = 5;
const total = ref(0)
const currentPage = ref(props.page)
const route = useRoute()
const homePostsUrl = route.query.t ? SERVER_API_CONSTANTS.SERVER_API_GET_RECENT_POSTS + "?t=" + route.query.t :
SERVER_API_CONSTANTS.SERVER_API_GET_RECENT_POSTS
logUtil.logError(props.keyword)
const {data} = await useFetch(homePostsUrl)
// @ts-ignore
// postData.value.postList = data.value.data
data.value.data.forEach((item: Post) => {
postData.value.postList.push(item)
const loadHomeData = async () => {
let homePostsUrl = SERVER_API_CONSTANTS.SERVER_API_GET_RECENT_POSTS
if (route.query.t) {
homePostsUrl = homePostsUrl + "?t=" + route.query.t
}
if (props.keyword) {
if (homePostsUrl.indexOf("?") > -1) {
homePostsUrl = homePostsUrl + "&"
} else {
homePostsUrl = homePostsUrl + "?"
}
homePostsUrl = homePostsUrl + "k=" + props.keyword
}
if (currentPage.value >= 0) {
if (homePostsUrl.indexOf("?") > -1) {
homePostsUrl = homePostsUrl + "&"
} else {
homePostsUrl = homePostsUrl + "?"
}
homePostsUrl = homePostsUrl + "pg=" + currentPage.value
}
logUtil.logError(homePostsUrl)
const {data} = await useFetch(homePostsUrl, {initialCache: false})
postData.value.postList = []
// @ts-ignore
// postData.value.postList = data.value.data
data.value.data.forEach((item: Post) => {
postData.value.postList.push(item)
})
// logUtil.logError(data.value.data.length)
// logUtil.logInfo(SERVER_API_CONSTANTS.SERVER_API_GET_RECENT_POSTS + " data=>", data.value)
// 总数
total.value = 100
// total.value = await getRootBlocksCount(props.keyword)
}
const handlePrevPage = async (curPage: number) => {
currentPage.value = curPage;
logUtil.logInfo("handlePrevPage, currentPage=>", currentPage.value)
await loadHomeData();
}
const handleNextPage = async (curPage: number) => {
currentPage.value = curPage;
logUtil.logInfo("handleNextPage, currentPage=>", currentPage.value)
await loadHomeData();
}
const handleCurrentPage = async (curPage: number) => {
currentPage.value = curPage;
logUtil.logInfo("handleCurrentPage, currentPage=>", currentPage.value)
await loadHomeData();
}
await loadHomeData();
onMounted(async () => {
await loadHomeData();
})
// logUtil.logError(data.value.data.length)
// logUtil.logInfo(SERVER_API_CONSTANTS.SERVER_API_GET_RECENT_POSTS + " data=>", data.value)
// definePageMeta({
// layout: "custom",
Expand Down Expand Up @@ -125,7 +195,7 @@ export default {
#postList .read-more {
padding: 0;
margin-top: 15px;
margin: 15px 0;
float: left;
font-weight: bold;
}
Expand Down
1 change: 1 addition & 0 deletions lib/platform/siyuan/siYuanApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class SiYuanApi {
ORDER BY b1.updated DESC,b1.created DESC LIMIT ${page},${pagesize}
)
ORDER BY b2.updated DESC,b2.created DESC`

let data = await this.sql(stmt)
return data
}
Expand Down
1 change: 1 addition & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,6 @@ export default defineNuxtConfig({
WORDPRESS_API_URL: process.env.WORDPRESS_API_URL,
WORDPRESS_USERNAME: process.env.WORDPRESS_USERNAME,
WORDPRESS_PASSWORD: process.env.WORDPRESS_PASSWORD,
DEFAULT_TYPE: process.env.DEFAULT_TYPE
}
})
5 changes: 4 additions & 1 deletion pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<template>
<HomePostList/>
<HomePostList :page="pg"/>
</template>

<script lang="ts" setup>
const route = useRoute()
const pg = route.query.pg || 1
useHead({
title: 'My App Index',
// or, instead:
Expand Down
2 changes: 1 addition & 1 deletion pages/post/[postid].vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<NuxtLink
v-for="tagItem in postTags"
:key="tagItem"
:to="'/tag/' + tagItem"
:to="'/s/' + tagItem"
>
<el-tag class="post-tag">
{{ tagItem }}
Expand Down
4 changes: 3 additions & 1 deletion plugins/hljs/vue-hljs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "./vs.css";
import {renderHTML} from "~/lib/markdownUtil";
import {unescapeHTML} from "~/lib/strUtil";
import logUtil from "~/lib/logUtil";
import {prettyHtml} from "~/lib/htmlUtil";

const vueHljs = {};

Expand All @@ -15,7 +16,8 @@ vueHljs.install = Vue => {
);

Vue.directive("highlight", el => {
const html = renderHTML(el.innerHTML)
let html = renderHTML(el.innerHTML)
html = prettyHtml(html)
// console.log(html)
el.innerHTML = html;

Expand Down
10 changes: 6 additions & 4 deletions server/api/endpoint/getPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ import {API_TYPE_CONSTANTS} from "~/lib/constants";
import {API} from "~/lib/api";

export default defineEventHandler(async (event) => {
const env = useRuntimeConfig()
console.log('Runtime env:', env)

const query = useQuery(event)

if (query.t instanceof Array) {
throw new Error("参数类型错误")
}
const type = query.t || API_TYPE_CONSTANTS.API_TYPE_WORDPRESS

const defaultType = env.DEFAULT_TYPE || API_TYPE_CONSTANTS.API_TYPE_WORDPRESS
const type = query.t || defaultType

if (query.postid instanceof Array) {
throw new Error("参数类型错误")
}
const postid = query.postid || "1"

const env = useRuntimeConfig()
console.log('Runtime env:', env)

const api = new API(type, env)

const result = await api.getPost(postid)
Expand Down
23 changes: 17 additions & 6 deletions server/api/endpoint/getRecentPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,34 @@ import {API} from "~/lib/api";
import logUtil from "~/lib/logUtil";

export default defineEventHandler(async (event) => {
const env = useRuntimeConfig()
logUtil.logInfo('Runtime env:', env)

const query = useQuery(event)
if (query.t instanceof Array) {
throw new Error("参数类型错误")
}
logUtil.logInfo("query.t", query.t)

const type = query.t || API_TYPE_CONSTANTS.API_TYPE_WORDPRESS
if (query.pg instanceof Array) {
throw new Error("参数类型错误")
}

const defaultType = env.DEFAULT_TYPE || API_TYPE_CONSTANTS.API_TYPE_WORDPRESS
const type = query.t || defaultType
const num = 10
const page = 1
const keyword = ""
const page = query.pg || "1"

const env = useRuntimeConfig()
logUtil.logInfo('Runtime env:', env)
if (query.k instanceof Array) {
throw new Error("参数类型错误")
}

const keyword = query.k || ""

const api = new API(type, env)

const result = await api.getRecentPosts(num, page, keyword)
// logUtil.logInfo("keyword=>", keyword)
const result = await api.getRecentPosts(num, parseInt(page) - 1, keyword)

return {
code: 0,
Expand Down

0 comments on commit 7dd20b4

Please sign in to comment.