更改word.json加载方式

This commit is contained in:
ZtRXR 2023-08-30 15:46:11 +08:00
parent 3f2a50f86d
commit 7d7bae9a77
2 changed files with 29 additions and 14 deletions

View File

@ -1,4 +1,4 @@
import {readFile} from "fs";
import {readFile, readFileSync} from "fs";
interface Word{
bsm:string
@ -11,12 +11,18 @@ interface Ret{
message:string
haveMess:boolean
}
let words:Word[]
readFile("./word.json",'utf-8',(err, data)=>{
if (err)console.error(err)
words=JSON.parse(data)
})
let words:Word[] = []
const loadWords = ()=>{
const wordRaw = readFileSync('./word.json','utf-8')
words = JSON.parse(wordRaw)
for (let i = 0; i< words.length ; i++){
words[i].id =i;
}
}
export default defineEventHandler((event):Ret=>{
if (words.length===0){
loadWords()
}
const query = getQuery(event)
if (query.id=="错误"){
return {
@ -25,6 +31,7 @@ export default defineEventHandler((event):Ret=>{
haveMess:true
}
}
// @ts-ignore
if (query.id>=words.length){
return {
data:words[0],
@ -32,7 +39,9 @@ export default defineEventHandler((event):Ret=>{
haveMess:true
}
}
return {
// @ts-ignore
data:words[query.id],
message:"",
haveMess:false

View File

@ -1,4 +1,4 @@
import {readFile} from "fs";
import {readFile, readFileSync} from "fs";
import path from "path";
interface Word{
bsm:string
@ -11,17 +11,23 @@ interface Ret{
message:string
haveMess:boolean
}
let words:Word[]
readFile("./word.json",'utf-8',(err, data)=>{
if (err)console.error(err)
words = JSON.parse(data)
let words:Word[] = []
const loadWords = ()=>{
const wordRaw = readFileSync('./word.json','utf-8')
words = JSON.parse(wordRaw)
for (let i = 0; i< words.length ; i++){
words[i].id=i
words[i].id =i;
}
})
}
export default defineEventHandler((event):Ret=>{
if (words.length===0){
loadWords()
}
const retWords:Word[] = []
const query = getQuery(event)
// @ts-ignore
if (query.words=="错误"||query.method=="错误"||!(query.method=="bsmf"||query.method=="bsmc"||query.method=="hz")||query.words.length>20){
return {
data:[],