From d0046bb26f6d1aecc420f35797fa0db5e75cd6bc Mon Sep 17 00:00:00 2001 From: Lei OT Date: Thu, 16 May 2024 14:49:02 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20pagespy=20=E5=BC=95=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 10 ---------- src/stores/AuthStore.js | 15 ++++++++------- src/utils/commons.js | 17 +++++++++++++++++ src/utils/pagespy.js | 15 +++++++++++++++ src/views/PageSpy.jsx | 7 ++++--- 5 files changed, 44 insertions(+), 20 deletions(-) create mode 100644 src/utils/pagespy.js diff --git a/index.html b/index.html index 4fa4442..4054cc1 100644 --- a/index.html +++ b/index.html @@ -7,20 +7,10 @@ 销售平台 - - -

VERSION: %BUILD_VERSION%

- diff --git a/src/stores/AuthStore.js b/src/stores/AuthStore.js index c8bb625..3a16688 100644 --- a/src/stores/AuthStore.js +++ b/src/stores/AuthStore.js @@ -1,6 +1,7 @@ import { create } from 'zustand' import { fetchJSON, appendRequestHeader } from '@/utils/request' -import { isEmpty, isNotEmpty } from '@/utils/commons' +import { isEmpty, isNotEmpty, } from '@/utils/commons' +import { loadPageSpy } from '@/utils/pagespy' const useAuthStore = create((set, get) => ({ @@ -82,15 +83,15 @@ const useAuthStore = create((set, get) => ({ } if (sessionData !== null) { - const sesstionObj = JSON.parse(sessionData) + const sesstionObj = JSON.parse(sessionData); set(() => ({ - loginUser: sesstionObj - })) + loginUser: sesstionObj, + })); - appendRequestHeader('X-User-Id', sesstionObj.userId) + appendRequestHeader('X-User-Id', sesstionObj.userId); - window.__spytitle = sesstionObj.username; - window.initPageSpy(); + const __spytitle = sesstionObj.username; + loadPageSpy(__spytitle); } }, diff --git a/src/utils/commons.js b/src/utils/commons.js index c14e42d..b5d2587 100644 --- a/src/utils/commons.js +++ b/src/utils/commons.js @@ -548,3 +548,20 @@ export const clearAllCaches = async (cb) => { console.error('Error clearing caches or unregistering service worker:', error); } }; + +export const loadScript = (src) => { + return new Promise((resolve, reject) => { + const script = document.createElement('script'); + script.type = 'text/javascript'; + script.onload = resolve; + script.onerror = reject; + script.crossOrigin = 'anonymous'; + script.src = src; + if (document.head.append) { + document.head.append(script); + } else { + document.getElementsByTagName('head')[0].appendChild(script); + } + }); +}; + diff --git a/src/utils/pagespy.js b/src/utils/pagespy.js new file mode 100644 index 0000000..59ec128 --- /dev/null +++ b/src/utils/pagespy.js @@ -0,0 +1,15 @@ +import { loadScript } from '@/utils/commons'; + +export const loadPageSpy = (title) => { + const PageSpySrc = [ + 'https://page-spy.mycht.cn/page-spy/index.min.js', + 'https://page-spy.mycht.cn/plugin/data-harbor/index.min.js', + 'https://page-spy.mycht.cn/plugin/rrweb/index.min.js', + ]; + Promise.all(PageSpySrc.map((src) => loadScript(src))).then(() => { + // 注册插件 + PageSpy.registerPlugin(new DataHarborPlugin({ maximum: 2 * 1024 * 1024 })); + // 实例化 PageSpy + window.$pageSpy = new PageSpy({ api: 'page-spy.mycht.cn', project: 'Sales CRM', title: title, autoRender: false }); + }); +}; diff --git a/src/views/PageSpy.jsx b/src/views/PageSpy.jsx index 2a56907..e9b8679 100644 --- a/src/views/PageSpy.jsx +++ b/src/views/PageSpy.jsx @@ -1,8 +1,9 @@ -const PageSpy = () => { +const PageSpyLog = () => { return ( <> {window.$pageSpy && ( - { window.$pageSpy.triggerPlugins('onOfflineLog', 'download'); window.$pageSpy.triggerPlugins('onOfflineLog', 'upload'); @@ -13,4 +14,4 @@ const PageSpy = () => { ); }; -export default PageSpy; +export default PageSpyLog;