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

View File

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