Browse Source

feat: 添加异常日志上报功能

TIANYONG 2 weeks ago
parent
commit
79a7dc1d0b

+ 53 - 0
src/hooks/errorLog/index.ts

@@ -0,0 +1,53 @@
+import { uploadErrorLog } from "./uploadLog";
+
+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) => {
+    // 错误信息
+    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?.message || ''};
+    stack: ${event.reason?.stack || ''};`;
+    uploadErrorLog(contentError)
+  };
+  /**
+   * 开始监听错误日志并上传
+   */
+  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
+  };
+}

+ 44 - 0
src/hooks/errorLog/uploadLog.ts

@@ -0,0 +1,44 @@
+import state from "/src/state";
+import dayjs from 'dayjs';
+import { sysExceptionLogSave } from '/src/utils/baseApi'
+import { browser } from "/src/utils";
+import { storeData } from "/src/store";
+
+// 上传错误日志
+export const uploadErrorLog = async (contentError: string) => {
+	//
+    let defaultParams = {
+		appKey: 'KT', // 应用标识(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 {
+	console.log(window.location.hash, 'errorLog')
+
+	const params = [
+		{
+		...defaultParams,
+		appKey: state.isCbsView ? 'CBS' : 'KT',
+		// clientType: state.systemType === 'teacher' ? 'TEACHER' : state.systemType === 'student' ? 'STUDENT' : 'BACKEND',
+		clientType: storeData.isApp ? '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 {
+	//
+	}
+}

+ 10 - 1
src/page-instrument/main.ts

@@ -12,6 +12,7 @@ import router from "./router";
 import "./theme.css";
 import "./custom-plugins/guide-driver/index.less"
 import { getQuery } from "/src/utils/queryString";
+import useErrorLog from "/src/hooks/errorLog"
 
 (function () {
 	const query = getQuery();
@@ -32,4 +33,12 @@ import { getQuery } from "/src/utils/queryString";
 	});
 })();
 
-createApp(App).use(router).mount("#app");
+// createApp(App).use(router).mount("#app");
+const app = createApp(App)
+app.use(router)
+
+// 监听错误信息
+const errorLog = useErrorLog();
+errorLog.startListenErrorLog();
+
+app.mount("#app")  

+ 6 - 1
src/page-instrument/view-detail/index.tsx

@@ -41,6 +41,7 @@ import { position } from "html2canvas/dist/types/css/property-descriptors/positi
 import Loading from "./loading"
 import LoadingCss from "./loadingCss"
 import { getSvgPngToSize } from "/src/helpers/svgToPng"
+import { uploadErrorLog } from '/src/hooks/errorLog/uploadLog'
 // import bgJson from "./images/index.json";
 
 // const DelayCheck = defineAsyncComponent(() =>
@@ -180,8 +181,10 @@ export default defineComponent({
       loadJson('data');
       try { 
         await getMusicDetail(id);
-      } catch (err) {
+      } catch (err: any) {
         console.error(err);
+        const contentError = `reason: ${err?.message || ''};stack: ${err?.stack || ''};`;
+        uploadErrorLog(contentError)
         state.isLoading = false;
         isEmptyMusicShow.value = true
         // 需要向外面(iframe)派发计时器数据的时候触发
@@ -359,6 +362,8 @@ export default defineComponent({
         handleRendered(osmd)
       }catch(err:any){
         console.log(err, "err")
+        const contentError = `reason: ${err?.message || ''};stack: ${err?.stack || ''};`;
+        uploadErrorLog(contentError)
         // 需要向外面(iframe)派发计时器数据的时候触发
         if(query.isbeatTimes){
           console.log("webApi_beatTimes",err)

+ 9 - 0
src/utils/baseApi.ts

@@ -25,4 +25,13 @@ export const creatMusicSheetImg = (data: any) => {
 export const getInstrumentCode = () => {
   const url = `/musicSheet/instrumentCode`;
   return request.get(url);
+};
+
+/** 上传错误信息 */
+export const sysExceptionLogSave = (data: any): Promise<any> => {
+  return request.post('/sysExceptionLog/save', {
+    requestType: 'json',
+    isExceptionLog: true, // js异常收集,需要使用edu-app
+    data
+  });
 };

+ 1 - 0
src/utils/request.ts

@@ -28,6 +28,7 @@ request.interceptors.request.use(
 		// console.log(9999,storeData.proxy,storeData.platformApi,options)
 		// 内容平台的前缀为cbs-app
 		const platformApi = options.isContentCenter ? '/cbs-app' : storeData.platformApi
+		// const platformApi = options.isContentCenter ? '/cbs-app' : options.isExceptionLog ? '/edu-app' : storeData.platformApi
 		const _prefix = storeData.proxy + platformApi;
 		/** 
 		 * 只有后台才去设置

+ 2 - 2
vite.config.ts

@@ -76,8 +76,8 @@ export default defineConfig({
         // target: "https://kt.colexiu.com",
         // target: "https://test.lexiaoya.cn",
         // target: "https://kt.colexiu.com",
-        // target: "https://test.resource.colexiu.com", // 内容平台开发环境,内容平台开发,需在url链接上加上isCbs=true
-        target: "https://test.kt.colexiu.com",
+        target: "https://test.resource.colexiu.com", // 内容平台开发环境,内容平台开发,需在url链接上加上isCbs=true
+        // target: "https://test.kt.colexiu.com",
         // target: "https://mec.colexiu.com",
         changeOrigin: true,
         rewrite: (path) => path.replace(/^\/instrument/, ""),