DataFront/server/api/tools/qqazk/search.ts

63 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-08-27 10:15:04 +00:00
import {readFile} from "fs";
import path from "path";
interface Word{
bsm:string
hz:string
pyJs:string[]
2023-08-27 16:30:58 +00:00
id:number
2023-08-27 10:15:04 +00:00
}
interface Ret{
2023-08-27 16:30:58 +00:00
data:Word[]
2023-08-27 10:15:04 +00:00
message:string
haveMess:boolean
}
let words:Word[]
readFile("./word.json",'utf-8',(err, data)=>{
if (err)console.error(err)
2023-08-27 16:30:58 +00:00
words = JSON.parse(data)
for (let i =0 ;i<words.length;i++){
words[i].id=i
}
2023-08-27 10:15:04 +00:00
})
export default defineEventHandler((event):Ret=>{
2023-08-27 16:30:58 +00:00
const retWords:Word[] = []
const query = getQuery(event)
if (query.words=="错误"||query.method=="错误"||!(query.method=="bsmf"||query.method=="bsmc"||query.method=="hz")||query.words.length>20){
return {
data:[],
message:"参数错误",
haveMess:true
}
}
const word:string = query.words as unknown as string
if (word==""){
return {
data:words,
message:"",
haveMess:false
}
}
if (query.method=="hz"){
for (let i =0;i<word.length;i++){
for (let j=0;j<words.length;j++){
if (words[j].hz.includes(word[i])){
let newWords:Word = words[j]
retWords.push(newWords)
}
}
}
}else if (query.method=="bsmf"){
words.forEach((wf,index)=>{
if (wf.bsm.startsWith(word)) retWords.push(wf)
})
}else if(query.method=="bsmc"){
words.forEach((wf,index)=>{
if (wf.bsm.includes(word)) retWords.push(wf)
})
}
2023-08-27 10:15:04 +00:00
return {
2023-08-27 16:30:58 +00:00
data:retWords,
2023-08-27 10:15:04 +00:00
message:"",
haveMess:false
}
})