From 1608a6049a039c290dbf51f22879981c57b51560 Mon Sep 17 00:00:00 2001 From: Lei OT Date: Tue, 30 Dec 2025 15:23:55 +0800 Subject: [PATCH] refactor: `@/pagespy` --> `@haina/utils-pagespy`; --- package.json | 1 + src/components/LogUploader.jsx | 3 +- src/pageSpy/index.jsx | 85 +--------------------------------- src/stores/Auth.js | 6 +-- 4 files changed, 7 insertions(+), 88 deletions(-) diff --git a/package.json b/package.json index e8bddfd..f2a5b45 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "dependencies": { "@ant-design/icons": "^5.5.1", "@haina/utils-commons": "http://202.103.68.93:9000/npm/utils-commons-0.1.0.tgz", + "@haina/utils-pagespy": "http://202.103.68.93:9000/npm/utils-pagespy-0.1.1.tgz", "@haina/utils-request": "http://202.103.68.93:9000/npm/utils-request-0.1.0.tgz", "@react-pdf/renderer": "^3.4.0", "antd": "^5.28.0", diff --git a/src/components/LogUploader.jsx b/src/components/LogUploader.jsx index b9a40e0..d3eca73 100644 --- a/src/components/LogUploader.jsx +++ b/src/components/LogUploader.jsx @@ -2,7 +2,8 @@ import { useState } from "react"; import { Popover, message, FloatButton, Button, Form, Input } from "antd"; import { BugOutlined } from "@ant-design/icons"; import useAuthStore from "@/stores/Auth"; -import { uploadPageSpyLog, sendNotify } from "@/pageSpy"; +import { sendNotify } from "@/pageSpy"; +import { uploadPageSpyLog } from '@haina/utils-pagespy'; function LogUploader() { const [open, setOpen] = useState(false); diff --git a/src/pageSpy/index.jsx b/src/pageSpy/index.jsx index 23caa6c..a42da80 100644 --- a/src/pageSpy/index.jsx +++ b/src/pageSpy/index.jsx @@ -1,6 +1,5 @@ -import { loadScript } from '@/utils/commons'; import { PROJECT_NAME, BUILD_VERSION } from '@/config'; -import { fetchJSON } from '@/utils/request' +import { fetchJSON } from '@haina/utils-request' import { usingStorage } from "@/hooks/usingStorage"; export const sendNotify = async (message) => { @@ -22,85 +21,3 @@ export const sendNotify = async (message) => { } }); }; - -export const loadPageSpy = (title) => { - - if (import.meta.env.DEV || window.$pageSpy) return - - const PageSpyConfig = { api: 'page-spy.mycht.cn', project: PROJECT_NAME, title: title, autoRender: false }; - const PageSpySrc = [ - 'https://page-spy.mycht.cn/page-spy/index.min.js'+`?v=${BUILD_VERSION}`, - 'https://page-spy.mycht.cn/plugin/data-harbor/index.min.js'+`?v=${BUILD_VERSION}`, - 'https://page-spy.mycht.cn/plugin/rrweb/index.min.js'+`?v=${BUILD_VERSION}`, - ]; - - Promise.all(PageSpySrc.map((src) => loadScript(src))).then(() => { - // 注册插件 - window.$harbor = new DataHarborPlugin(); - window.$rrweb = new RRWebPlugin(); - [window.$harbor, window.$rrweb].forEach(p => { - PageSpy.registerPlugin(p) - }) - window.$pageSpy = new PageSpy(PageSpyConfig); - - window.onerror = async function (msg, url, lineNo, columnNo, error) { - - // 当页面有 iframe 时,rrweb 会报错:Cannot set property attributeName of # which has only a getter - // 而且这个错误无法修复,所以只能先忽略掉 - if (url && url.indexOf('https://page-spy.mycht.cn/plugin/rrweb/index.min.js') > -1) { - console.info('ignore rrweb error') - } else { - // 上传最近 3 分钟的日志 - const now = Date.now() - await window.$harbor.uploadPeriods({ - startTime: now - 3 * 60000, - endTime: now, - remark: `\`onerror\`自动上传. ${msg}`, - }) - } - } - }); -}; - -export const uploadPageSpyLog = async () => { - - if (import.meta.env.DEV) return true; - - if (window.$pageSpy) { - try { - // await window.$harbor.upload() // 上传日志 { clearCache: true, remark: '' } - // 上传最近 1 小时的日志, 直接upload 所有日志: 413 Payload Too Large - const now = Date.now(); - await window.$harbor.uploadPeriods({ - startTime: now - 60 * 60000, - endTime: now, - }); - return true; - } catch (error) { - return false; - } - } else { - return false; - } -} - -/** - * @deprecated - * @outdated - */ -export const PageSpyLog = () => { - return ( - <> - {window.$pageSpy && ( - { - window.$pageSpy.triggerPlugins('onOfflineLog', 'download'); - window.$pageSpy.triggerPlugins('onOfflineLog', 'upload'); - }}> - 上传Debug日志 ({window.$pageSpy.address.substring(0, 4)}) - - )} - - ); -}; diff --git a/src/stores/Auth.js b/src/stores/Auth.js index cf9d9b2..3864930 100644 --- a/src/stores/Auth.js +++ b/src/stores/Auth.js @@ -1,8 +1,8 @@ import { create } from 'zustand' import { devtools } from 'zustand/middleware' import { appendRequestParams, fetchJSON, postForm } from '@haina/utils-request' -import { HT_HOST } from "@/config" -import { loadPageSpy } from '@/pageSpy' +import { HT_HOST, PROJECT_NAME } from "@/config" +import { loadPageSpy } from '@haina/utils-pagespy' import { usingStorage } from '@/hooks/usingStorage' import { isEmpty } from "@haina/utils-commons"; @@ -86,7 +86,7 @@ const useAuthStore = create(devtools((set, get) => ({ } })) - loadPageSpy(`${userJson.LoginName}-${userJson.VName}`) + loadPageSpy(`${userJson.LoginName}-${userJson.VName}`, PROJECT_NAME, false) }, authenticate: async (usr, pwd) => {