Browse Source

feat: 添加异常错误上传

TIANYONG 2 weeks ago
parent
commit
66c83dcec9

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

@@ -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
+  };
+}

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

@@ -13,6 +13,7 @@ import "./theme.css";
 import "./custom-plugins/guide-driver/index.less"
 import { getQuery } from "/src/utils/queryString";
 import { getRequestHostname } from "/src/utils"
+import useErrorLog from "/src/hooks/errorLog"
 
 (function () {
 	const query = getQuery();
@@ -34,4 +35,21 @@ import { getRequestHostname } from "/src/utils"
 	});
 })();
 
-createApp(App).use(router).mount("#app");
+
+
+const app = createApp(App)
+
+app.use(router)
+
+//createApp(App).use(router).mount("#app");
+
+// app.config.errorHandler = (err, instance, info) => {
+// 	console.error('Vue全局错误:', err, info);
+// };
+  
+
+// 监听错误信息
+const errorLog = useErrorLog();
+errorLog.startListenErrorLog();
+
+app.mount("#app")  

+ 17 - 16
src/page-instrument/view-detail/index.tsx

@@ -360,22 +360,23 @@ export default defineComponent({
       // console.timeEnd("渲染加载耗时");
     };
     function handleOnRendered(osmd: any) {
-      try{
-        handleRendered(osmd)
-      }catch(err:any){
-        console.log(err, "err")
-        // 需要向外面(iframe)派发计时器数据的时候触发
-        if(query.isbeatTimes){
-          console.log("webApi_beatTimes",err)
-          window.parent.postMessage(
-            {
-              api: "webApi_beatTimes",
-              data: "节拍器时值错误!!"
-            },
-            "*"
-          );
-        }
-      }
+      // try{
+      //   handleRendered(osmd)
+      // }catch(err:any){
+      //   console.log(err, "err")
+      //   // 需要向外面(iframe)派发计时器数据的时候触发
+      //   if(query.isbeatTimes){
+      //     console.log("webApi_beatTimes",err)
+      //     window.parent.postMessage(
+      //       {
+      //         api: "webApi_beatTimes",
+      //         data: "节拍器时值错误!!"
+      //       },
+      //       "*"
+      //     );
+      //   }
+      // }
+      handleRendered(osmd)
     }
     /** 指法配置 */
     const fingerConfig = computed<any>(() => {

+ 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异常收集,需要使用api-auth
+    data
+  });
 };

+ 1 - 1
src/utils/request.ts

@@ -19,7 +19,7 @@ request.interceptors.request.use(
 	(url, options) => {
 		// 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 ? '/api-auth' : storeData.platformApi
 		const _prefix = storeData.proxy + platformApi;
 		/** 
 		 * 只有后台才去设置