// 在 localStorage.logInfo 中存储日志信息, 以便在前端页面中显示 const { LocalStorage } = require('node-localstorage') const localStorage = new LocalStorage('./localStorage') export interface WebLogSet { type: string content: string } export default { set: (data: WebLogSet): void => { // console.log(`weblog-${data.type}: ${data.content}`) // 获取存储在 localStorage 中的日志信息数组 let logs = JSON.parse(localStorage.getItem('logInfo')) || [] // 创建新的日志对象 const newLog = { time: new Date(), type: data.type, content: data.content } // logs 是不是数组 是的话就push 不是的话就创建一个数组 if (!Array.isArray(logs)) { logs = [] } // 将新的日志对象添加到数组中, 如果前面两条日志信息一样, 则覆盖前一条 if ( logs.length >= 2 && logs[logs.length - 1].content == newLog.content && logs[logs.length - 2].content == newLog.content ) { logs[logs.length - 1] = newLog } else { logs.push(newLog) } localStorage.setItem('logInfo', JSON.stringify(logs)) }, get: (): void => { return localStorage.getItem('logInfo') }, clear: (): void => { localStorage.removeItem('logInfo') } }