1
This commit is contained in:
63
app/store/modules/log.js
Normal file
63
app/store/modules/log.js
Normal file
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* 日志状态管理
|
||||
*/
|
||||
export default {
|
||||
namespaced: true,
|
||||
state: {
|
||||
logs: [],
|
||||
maxLogs: 1000
|
||||
},
|
||||
mutations: {
|
||||
ADD_LOG(state, logEntry) {
|
||||
state.logs.push(logEntry);
|
||||
if (state.logs.length > state.maxLogs) {
|
||||
state.logs.shift();
|
||||
}
|
||||
},
|
||||
CLEAR_LOGS(state) {
|
||||
state.logs = [];
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
addLog({ commit }, { level, message }) {
|
||||
const timestamp = new Date().toLocaleString();
|
||||
const logEntry = {
|
||||
time: timestamp,
|
||||
level: level.toUpperCase(),
|
||||
message: message
|
||||
};
|
||||
commit('ADD_LOG', logEntry);
|
||||
},
|
||||
clearLogs({ commit }) {
|
||||
commit('CLEAR_LOGS');
|
||||
commit('ADD_LOG', {
|
||||
time: new Date().toLocaleString(),
|
||||
level: 'INFO',
|
||||
message: '日志已清空'
|
||||
});
|
||||
},
|
||||
exportLogs({ state, commit }) {
|
||||
const logText = state.logs.map(log =>
|
||||
`[${log.time}] [${log.level}] ${log.message}`
|
||||
).join('\n');
|
||||
|
||||
const blob = new Blob([logText], { type: 'text/plain' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = `logs_${new Date().toISOString().replace(/[:.]/g, '-')}.txt`;
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
|
||||
commit('ADD_LOG', {
|
||||
time: new Date().toLocaleString(),
|
||||
level: 'INFO',
|
||||
message: '日志已导出'
|
||||
});
|
||||
}
|
||||
},
|
||||
getters: {
|
||||
logEntries: state => state.logs
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user