|
@@ -0,0 +1,205 @@
|
|
|
+import { state } from '@/state';
|
|
|
+import dayjs from 'dayjs';
|
|
|
+import request from '@/helpers/request';
|
|
|
+import { router as routeCommon } from '../../router/routes-common'
|
|
|
+import schoolRouter from '../../router/routes-school'
|
|
|
+import teacherRouter from '../../router/routes-teacher'
|
|
|
+import studentRouter from '../../router/routes-student'
|
|
|
+
|
|
|
+/** 错误信息列表 */
|
|
|
+export const api_sysExceptionLogSave = (params: any): Promise<any> => {
|
|
|
+ return request.post(state.platformApi + '/sysExceptionLog/save', {
|
|
|
+ data: params,
|
|
|
+ hideLoading: true
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+type uploadType = {
|
|
|
+ clientType?: string;
|
|
|
+ phone?: string | undefined | null;
|
|
|
+ userAgent?: string;
|
|
|
+ appType?: string;
|
|
|
+ content?: string;
|
|
|
+ exceptionType?: string;
|
|
|
+ exceptionTime?: string;
|
|
|
+ deviceType?: string | null;
|
|
|
+ deviceVersion?: string | null
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 页面有报错时上传错误日志
|
|
|
+ * @params
|
|
|
+ */
|
|
|
+export default function useErrorLog() {
|
|
|
+ let _whiteBlanks: string[] = [];
|
|
|
+
|
|
|
+ let _defaultParams = {
|
|
|
+ clientType: state.platformType,
|
|
|
+ phone: '',
|
|
|
+ userAgent: '',
|
|
|
+ appType: 'WEB',
|
|
|
+ content: '',
|
|
|
+ exceptionType: 'ERROR',
|
|
|
+ exceptionTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
|
|
|
+ deviceType: null,
|
|
|
+ deviceVersion: null
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 操作记录的缓存 */
|
|
|
+ const operationErrorLog = (type: 'set' | 'get' | 'remove', params?: any) => {
|
|
|
+ if (type === "set") {
|
|
|
+ const tempList = operationErrorLog('get') || []
|
|
|
+ tempList.push(params)
|
|
|
+ } else if (type === 'get') {
|
|
|
+ const tempList = localStorage.getItem('orchestra-error-log')
|
|
|
+ return tempList ? JSON.parse(tempList) : []
|
|
|
+ } else if (type === "remove") {
|
|
|
+ localStorage.removeItem('orchestra-error-log')
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const _uploadErrorLog = async (event: any) => {
|
|
|
+ try {
|
|
|
+ /*
|
|
|
+ 日志上报:1.手机号、应用端 iOS|安卓 App类型(老师端|学生端|web)、App版本、系统信息(系统版本|web userAgent)、错误信息(什么位置出现,错误内容) 错误时间 上报类型(错误、埋点)
|
|
|
+ Phone、 type 、content
|
|
|
+ 手机号|上报类型 (error、record)|content 内容jsonString (应用端 iOS|安卓 App类型(老师端|学生端|web)、App版本、系统信息(系统版本|web userAgent)、错误信息(什么位置出现,错误内容) 错误时间)
|
|
|
+ 手机号 |上报类型 |业务平台|应用端(iOS|安卓)|
|
|
|
+ */
|
|
|
+ const href = window.location.href;
|
|
|
+ const index = _whiteBlanks.findIndex(
|
|
|
+ (item: string) => href.indexOf(item) !== -1
|
|
|
+ );
|
|
|
+
|
|
|
+ const whiteIp = ['online.lexiaoya.cn', 'test.lexiaoya.cn', 'dev.lexiaoya.cn']
|
|
|
+ console.log(window.location.hash, index, 'errorLog')
|
|
|
+ if (!whiteIp.includes(window.location.host)) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (!(window.location.hash === '#/' || index !== -1)) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 错误信息
|
|
|
+ // 资源加载失败,可以在这里处理错误
|
|
|
+ const contentError = `Error message: ${event.target.tagName || ''};${event.target.src || event.target.href || ''
|
|
|
+ };lineno: ${event.lineno || ''};message: ${event.message || ''
|
|
|
+ };filename: ${event.filename || ''};fileUrl: ${window.location.href
|
|
|
+ }`;
|
|
|
+ // }
|
|
|
+
|
|
|
+ const params = [
|
|
|
+ {
|
|
|
+ ..._defaultParams,
|
|
|
+ phone: state.user.data?.phone,
|
|
|
+ userAgent: window.navigator.userAgent,
|
|
|
+ content: contentError,
|
|
|
+ exceptionTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
|
|
|
+ }
|
|
|
+ ];
|
|
|
+ // console.log(params)
|
|
|
+ // 如果不是登录则存在缓存里面
|
|
|
+ if (state.user.status !== "login") {
|
|
|
+ operationErrorLog('set', params)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ await api_sysExceptionLogSave(params);
|
|
|
+ } catch {
|
|
|
+ //
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ /** 初始化在缓存里面的数据 */
|
|
|
+ const _initUpdaloadErrorLog = async () => {
|
|
|
+ if (state.user.status !== 'login') return
|
|
|
+ try {
|
|
|
+ const tempList = operationErrorLog('get') || []
|
|
|
+ if (tempList.length > 0) {
|
|
|
+ await api_sysExceptionLogSave(tempList);
|
|
|
+ operationErrorLog('remove')
|
|
|
+ }
|
|
|
+ } catch {
|
|
|
+ //
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 开始监听错误日志并上传
|
|
|
+ */
|
|
|
+ const startListenErrorLog = (params?: uploadType) => {
|
|
|
+ _defaultParams = Object.assign(_defaultParams, params);
|
|
|
+
|
|
|
+ console.log('mount useErrorLog');
|
|
|
+ window.addEventListener('error', _uploadErrorLog);
|
|
|
+
|
|
|
+ setTimeout(() => {
|
|
|
+ _initUpdaloadErrorLog()
|
|
|
+ }, 3000);
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 停止监听
|
|
|
+ */
|
|
|
+ const stopListenErrorLog = () => {
|
|
|
+ window.removeEventListener('error', _uploadErrorLog);
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化需要监听的路由
|
|
|
+ */
|
|
|
+ const _listenRouter = () => {
|
|
|
+ _whiteBlanks = []
|
|
|
+
|
|
|
+ // 学校路由
|
|
|
+ const tempRouteSchool: string[] = []
|
|
|
+ if (state.platformType === "SCHOOL") {
|
|
|
+ schoolRouter.forEach((route: any) => {
|
|
|
+ if (route.children && route.children.length > 0) {
|
|
|
+ route.children.forEach(child => {
|
|
|
+ if (child.meta?.errorLog) {
|
|
|
+ tempRouteSchool.push(child.path)
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ // 老师路由
|
|
|
+ const tempRouteTeacher: string[] = []
|
|
|
+ if (state.platformType === "TEACHER") {
|
|
|
+ teacherRouter.forEach((route: any) => {
|
|
|
+ if (route.children && route.children.length > 0) {
|
|
|
+ route.children.forEach(child => {
|
|
|
+ if (child.meta?.errorLog) {
|
|
|
+ tempRouteSchool.push(child.path)
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ // 学生路由
|
|
|
+ const tempRouteStudent: string[] = []
|
|
|
+ if (state.platformType === "STUDENT") {
|
|
|
+ studentRouter.forEach((route: any) => {
|
|
|
+ if (route.children && route.children.length > 0) {
|
|
|
+ route.children.forEach(child => {
|
|
|
+ if (child.meta?.errorLog) {
|
|
|
+ tempRouteSchool.push(child.path)
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ _whiteBlanks.push(...tempRouteSchool, ...tempRouteTeacher, ...tempRouteStudent)
|
|
|
+ }
|
|
|
+
|
|
|
+ _listenRouter()
|
|
|
+
|
|
|
+ return {
|
|
|
+ startListenErrorLog,
|
|
|
+ stopListenErrorLog
|
|
|
+ };
|
|
|
+}
|