diff --git a/front/src/components/Search/index.jsx b/front/src/components/Search/index.jsx index 0d4ebfd..7185db3 100644 --- a/front/src/components/Search/index.jsx +++ b/front/src/components/Search/index.jsx @@ -2,6 +2,7 @@ import React from "react"; import { Input, Select, Empty } from "antd"; import styles from "./index.module.less"; import httpUtil from "../../util/httpUtil"; +import { keySearch } from "../../util/cacheUtil"; class Search extends React.Component { constructor(props) { @@ -43,8 +44,8 @@ class Search extends React.Component { return; } this.clearTimer(); - this.timer = setTimeout(() => { - this.search(content); + this.timer = setTimeout(async () => { + await this.search(content); this.clearTimer(); }, 200); } @@ -58,16 +59,18 @@ class Search extends React.Component { /** * 关键词检索 */ - search(content) { + async search(content) { if (content.length === 0) { this.setState({ resultList: [] }); return; } - httpUtil - .get( - "/bookmark/searchUserBookmark?content=" + encodeURIComponent(content) - ) - .then(res => this.setState({ resultList: res })); + // httpUtil + // .get( + // "/bookmark/searchUserBookmark?content=" + encodeURIComponent(content) + // ) + // .then(res => this.setState({ resultList: res })); + let resultList = await keySearch(content); + this.setState({ resultList }); } /** diff --git a/front/src/util/cacheUtil.js b/front/src/util/cacheUtil.js index ef527b7..10895a5 100644 --- a/front/src/util/cacheUtil.js +++ b/front/src/util/cacheUtil.js @@ -179,3 +179,37 @@ export async function moveNode(info) { await updateCurrentChangeTime(); return body; } + +/** + * 关键词搜索方法 + * @param {*} content + */ +export async function keySearch(content) { + let time1 = Date.now(); + content = content.toLocaleLowerCase().trim(); + let res = []; + let arrs = Object.values(window[TREE_LIST_KEY]); + for (let i1 = 0, length1 = arrs.length; i1 < length1; i1++) { + for (let i2 = 0, length2 = arrs[i1].length; i2 < length2; i2++) { + let item = arrs[i1][i2]; + if (item.type === 1) { + continue; + } + if (!item.lowName) { + item.lowName = item.name.toLocaleLowerCase(); + } + if (!item.lowUrl) { + item.lowUrl = item.url.toLocaleLowerCase(); + } + if (item.name.indexOf(content) > -1 || item.url.indexOf(content) > -1) { + res.push(item); + if (res.length >= 12) { + console.info("搜索耗时:" + (Date.now() - time1)); + return res; + } + } + } + } + console.info("搜索耗时:" + (Date.now() - time1)); + return res; +}