import { ErrorTypeEnum, formatDateTime } from '@eciol/shared'; import { acceptHMRUpdate, defineStore, pinia } from '@eciol/store'; import type { ErrorLogInfo } from '@eciol/types'; import {projectSetting} from '@/settings'; export interface ErrorLogState { errorLogInfoList: Nullable; errorLogListCount: number; } export const useErrorLogStore = defineStore({ id: 'finance-error-log', state: (): ErrorLogState => ({ errorLogInfoList: null, errorLogListCount: 0, }), getters: { getErrorLogInfoList(state): ErrorLogInfo[] { return state.errorLogInfoList || []; }, getErrorLogListCount(state): number { return state.errorLogListCount; }, }, actions: { addErrorLogInfo(info: ErrorLogInfo) { const item = { ...info, time: formatDateTime(new Date()), }; this.errorLogInfoList = [item, ...(this.errorLogInfoList || [])]; this.errorLogListCount += 1; }, setErrorLogListCount(count: number): void { this.errorLogListCount = count; }, /** * Triggered after ajax request error * @param error * @returns */ addAjaxErrorInfo(error) { const { useErrorHandle } = projectSetting; if (!useErrorHandle) { return; } const errInfo: Partial = { message: error.message, type: ErrorTypeEnum.AJAX, }; if (error.response) { const { config: { url = '', data: params = '', method = 'get', headers = {} } = {}, data = {}, } = error.response; errInfo.url = url; errInfo.name = 'Ajax Error!'; errInfo.file = '-'; errInfo.stack = JSON.stringify(data); errInfo.detail = JSON.stringify({ params, method, headers }); } this.addErrorLogInfo(errInfo as ErrorLogInfo); }, }, }); // 解决热更新问题 const hot = import.meta.hot; if (hot) { hot.accept(acceptHMRUpdate(useErrorLogStore, hot)); } // Need to be used outside the setup export function useErrorLogStoreWithOut() { return useErrorLogStore(pinia); }