|
@@ -0,0 +1,92 @@
|
|
|
+import dayjs from 'dayjs';
|
|
|
+import { sysExceptionLogSave } from '/src/utils/baseApi'
|
|
|
+import { storeData } from "/src/store";
|
|
|
+import { browser } from "/src/utils";
|
|
|
+import state from "/src/state";
|
|
|
+
|
|
|
+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() {
|
|
|
+ const _uploadErrorLog = async (event: any) => {
|
|
|
+ let defaultParams = {
|
|
|
+ appKey: 'GYM', // 应用标识(GYT,GYM,KT,KLX,CBS),可用值:GYM,GYT,KLX,KLX_JG,KT,CBS
|
|
|
+ appType: browser().android ? 'ANDROID' : browser().ios && storeData.isApp ? 'IOS' : 'WEB', // 应用类型(IOS,ANDROID,HARMONY),可用值:IOS,ANDROID,HARMONY,WEB
|
|
|
+ clientType: '', // 客户端类型(TEACHER,STUDENT,SCHOOL,BACKEND),可用值:BACKEND,SCHOOL,TEACHER,STUDENT,TENANT
|
|
|
+ content: '', // 内容
|
|
|
+ deviceType: null, // 设备类型
|
|
|
+ deviceVersion: null, // 设备版本
|
|
|
+ exceptionTime: null, // 异常时间
|
|
|
+ exceptionType: 'ERROR', // 异常类型(ERROR,RECORD),可用值:ERROR,RECORD
|
|
|
+ phone: null, // 手机号
|
|
|
+ userAgent: window.navigator.userAgent, // 客户端信息
|
|
|
+
|
|
|
+ }
|
|
|
+ console.log('errorLog','错误',event)
|
|
|
+ try {
|
|
|
+ const href = window.location.href;
|
|
|
+
|
|
|
+ console.log(window.location.hash, 'errorLog')
|
|
|
+
|
|
|
+ // 错误信息
|
|
|
+ // 资源加载失败,可以在这里处理错误
|
|
|
+ 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
|
|
|
+ };reason: ${event.reason?.toString() || ''};`;
|
|
|
+ // }
|
|
|
+
|
|
|
+ const params = [
|
|
|
+ {
|
|
|
+ ...defaultParams,
|
|
|
+ clientType: state.systemType === 'teacher' ? 'TEACHER' : state.systemType === 'student' ? 'STUDENT' : 'BACKEND',
|
|
|
+ content: contentError,
|
|
|
+ exceptionTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
|
|
|
+ phone: storeData.user?.phone,
|
|
|
+ userAgent: window.navigator.userAgent,
|
|
|
+ }
|
|
|
+ ];
|
|
|
+ console.log(params, '错误信息', 'errorLog')
|
|
|
+ await sysExceptionLogSave(params);
|
|
|
+ } catch {
|
|
|
+ //
|
|
|
+ }
|
|
|
+ };
|
|
|
+ /**
|
|
|
+ * 开始监听错误日志并上传
|
|
|
+ */
|
|
|
+ const startListenErrorLog = () => {
|
|
|
+ console.log('mount useErrorLog');
|
|
|
+ window.addEventListener('error', _uploadErrorLog);
|
|
|
+ window.addEventListener('unhandledrejection', _uploadErrorLog);
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 停止监听
|
|
|
+ */
|
|
|
+ const stopListenErrorLog = () => {
|
|
|
+ window.removeEventListener('error', _uploadErrorLog);
|
|
|
+ window.removeEventListener('unhandledrejection', _uploadErrorLog);
|
|
|
+ };
|
|
|
+
|
|
|
+ return {
|
|
|
+ startListenErrorLog,
|
|
|
+ stopListenErrorLog
|
|
|
+ };
|
|
|
+}
|