From 1f2873cae16626d74c217171a3c538c499be5e62 Mon Sep 17 00:00:00 2001 From: fanxb Date: Sat, 1 Feb 2020 23:34:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=B0=86=E6=9C=8D=E5=8A=A1=E5=99=A8es?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=94=B9=E4=B8=BA=E6=9C=AC=E5=9C=B0=E6=90=9C?= =?UTF-8?q?=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/src/components/Search/index.jsx | 19 ++++++++------- front/src/util/cacheUtil.js | 34 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) 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; +}