From b114996e1c942532c40f9401d991fc0bbf15339a Mon Sep 17 00:00:00 2001 From: landaiqing <3517283258@qq.com> Date: Mon, 21 Oct 2024 17:42:05 +0800 Subject: [PATCH] :sparkles: add vite obfuscator --- components.d.ts | 14 -- package.json | 1 + src/api/oauth/index.ts | 2 +- src/store/modules/clientStore.ts | 88 ++++----- src/store/modules/websocketStore.ts | 6 + src/utils/websocket/websocket.ts | 5 + src/views/Login/LoginFooter.vue | 26 +-- src/views/Main/MainPage.vue | 3 - src/views/QRLogin/QRLogin.vue | 23 +-- src/views/QRLogin/QRLoginFooter.vue | 26 +-- vite.config.ts | 41 ++++ yarn.lock | 295 ++++++++++++++++++++++++++-- 12 files changed, 393 insertions(+), 137 deletions(-) diff --git a/components.d.ts b/components.d.ts index 85bc554..73874af 100644 --- a/components.d.ts +++ b/components.d.ts @@ -15,40 +15,27 @@ declare module 'vue' { AConfigProvider: typeof import('ant-design-vue/es')['ConfigProvider'] ADivider: typeof import('ant-design-vue/es')['Divider'] ADropdown: typeof import('ant-design-vue/es')['Dropdown'] - AEmpty: typeof import('ant-design-vue/es')['Empty'] AFlex: typeof import('ant-design-vue/es')['Flex'] AForm: typeof import('ant-design-vue/es')['Form'] AFormItem: typeof import('ant-design-vue/es')['FormItem'] - AImage: typeof import('ant-design-vue/es')['Image'] - AImagePreviewGroup: typeof import('ant-design-vue/es')['ImagePreviewGroup'] AInput: typeof import('ant-design-vue/es')['Input'] AInputPassword: typeof import('ant-design-vue/es')['InputPassword'] - AList: typeof import('ant-design-vue/es')['List'] - AListItem: typeof import('ant-design-vue/es')['ListItem'] AMenu: typeof import('ant-design-vue/es')['Menu'] AMenuItem: typeof import('ant-design-vue/es')['MenuItem'] AModal: typeof import('ant-design-vue/es')['Modal'] - APagination: typeof import('ant-design-vue/es')['Pagination'] - APopover: typeof import('ant-design-vue/es')['Popover'] AQrcode: typeof import('ant-design-vue/es')['QRCode'] - ASkeleton: typeof import('ant-design-vue/es')['Skeleton'] ASpin: typeof import('ant-design-vue/es')['Spin'] ATabPane: typeof import('ant-design-vue/es')['TabPane'] ATabs: typeof import('ant-design-vue/es')['Tabs'] - ATag: typeof import('ant-design-vue/es')['Tag'] - ATextarea: typeof import('ant-design-vue/es')['Textarea'] ATooltip: typeof import('ant-design-vue/es')['Tooltip'] - AUpload: typeof import('ant-design-vue/es')['Upload'] BoxDog: typeof import('./src/components/BoxDog/BoxDog.vue')['default'] Card3D: typeof import('./src/components/Card3D/Card3D.vue')['default'] - CloseCircleOutlined: typeof import('@ant-design/icons-vue')['CloseCircleOutlined'] Clouds: typeof import('./src/components/Clouds/Clouds.vue')['default'] CommentInput: typeof import('./src/components/CommentReply/src/CommentInput/CommentInput.vue')['default'] CommentList: typeof import('./src/components/CommentReply/src/CommentList/CommentList.vue')['default'] CommentReply: typeof import('./src/components/CommentReply/index.vue')['default'] DynamicTitle: typeof import('./src/components/DynamicTitle/DynamicTitle.vue')['default'] EffectsCard: typeof import('./src/components/EffectsCard/EffectsCard.vue')['default'] - EyeOutlined: typeof import('@ant-design/icons-vue')['EyeOutlined'] ForgetPage: typeof import('./src/views/Forget/ForgetPage.vue')['default'] GithubOutlined: typeof import('@ant-design/icons-vue')['GithubOutlined'] LandingPage: typeof import('./src/views/Landing/LandingPage.vue')['default'] @@ -68,7 +55,6 @@ declare module 'vue' { SafetyOutlined: typeof import('@ant-design/icons-vue')['SafetyOutlined'] TabletOutlined: typeof import('@ant-design/icons-vue')['TabletOutlined'] UserOutlined: typeof import('@ant-design/icons-vue')['UserOutlined'] - WarningOutlined: typeof import('@ant-design/icons-vue')['WarningOutlined'] WechatOutlined: typeof import('@ant-design/icons-vue')['WechatOutlined'] } } diff --git a/package.json b/package.json index 7c65519..04759ab 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "typescript-eslint": "^8.9.0", "unplugin-vue-components": "^0.27.4", "vite": "^5.4.9", + "vite-plugin-bundle-obfuscator": "^1.2.0", "vite-plugin-chunk-split": "^0.5.0", "vue-tsc": "^2.1.6" } diff --git a/src/api/oauth/index.ts b/src/api/oauth/index.ts index eac3867..a3c04fb 100644 --- a/src/api/oauth/index.ts +++ b/src/api/oauth/index.ts @@ -11,7 +11,7 @@ export const generateClientId = () => { }, cacheFor: { mode: "restore", - expire: 1000 * 60 * 60 * 24 * 7 + expire: 1000 * 60 * 60 * 24 } } ); diff --git a/src/store/modules/clientStore.ts b/src/store/modules/clientStore.ts index 3212d44..554060e 100644 --- a/src/store/modules/clientStore.ts +++ b/src/store/modules/clientStore.ts @@ -1,49 +1,49 @@ import {defineStore} from 'pinia'; import {ref} from "vue"; -const expiresTime: number = 60 * 30; // 自定义过期时间 30 分钟 -const expiredStorage: Storage = getExpiredStorage(localStorage, expiresTime); - -function getExpiredStorage(storage: Storage, expiresTime: string | number | Date) { - return { - getItem(key: string) { - const itemStr = storage.getItem(key); - if (!itemStr) { - return null; - } - // 读取的时候是字符串,需要转换为对象 - const item = JSON.parse(itemStr); - const now: Date = new Date(); - if (now.getTime() > item.expiry) { - storage.removeItem(key); - return null; - } - // pinia 的持久化插件要求返回JSON字符串 - return JSON.stringify(item.value); - }, - setItem(key: string, value: string) { - const now = new Date(); - const item = { - value: JSON.parse(value), // value是JSON字符串,需要转换为对象 - expiry: now.getTime() + Number(expiresTime) * 1000, - }; - storage.setItem(key, JSON.stringify(item)); // 存储又要转为JSON字符串 - }, - removeItem(key: string) { - storage.removeItem(key); - }, - clear() { - Object.keys(storage).forEach((key: string) => { - storage.removeItem(key); - }); - }, - key(index: number): string | null { - const keys = Object.keys(storage); - return keys[index] || null; - }, - length: Object.keys(storage).length, - } as Storage; -} +// const expiresTime: number = 60 * 30; // 自定义过期时间 30 分钟 +// const expiredStorage: Storage = getExpiredStorage(localStorage, expiresTime); +// +// function getExpiredStorage(storage: Storage, expiresTime: string | number | Date) { +// return { +// getItem(key: string) { +// const itemStr = storage.getItem(key); +// if (!itemStr) { +// return null; +// } +// // 读取的时候是字符串,需要转换为对象 +// const item = JSON.parse(itemStr); +// const now: Date = new Date(); +// if (now.getTime() > item.expiry) { +// storage.removeItem(key); +// return null; +// } +// // pinia 的持久化插件要求返回JSON字符串 +// return JSON.stringify(item.value); +// }, +// setItem(key: string, value: string) { +// const now = new Date(); +// const item = { +// value: JSON.parse(value), // value是JSON字符串,需要转换为对象 +// expiry: now.getTime() + Number(expiresTime) * 1000, +// }; +// storage.setItem(key, JSON.stringify(item)); // 存储又要转为JSON字符串 +// }, +// removeItem(key: string) { +// storage.removeItem(key); +// }, +// clear() { +// Object.keys(storage).forEach((key: string) => { +// storage.removeItem(key); +// }); +// }, +// key(index: number): string | null { +// const keys = Object.keys(storage); +// return keys[index] || null; +// }, +// length: Object.keys(storage).length, +// } as Storage; +// } export const useClientStore = defineStore( 'clientId', @@ -68,7 +68,7 @@ export const useClientStore = defineStore( // 开启数据持久化 persist: { key: 'clientId', - storage: expiredStorage, + storage: localStorage, pick: ["clientId"], } } diff --git a/src/store/modules/websocketStore.ts b/src/store/modules/websocketStore.ts index a61187e..c7b235a 100644 --- a/src/store/modules/websocketStore.ts +++ b/src/store/modules/websocketStore.ts @@ -35,12 +35,18 @@ export const useWebSocketStore = defineStore('websocket', () => { state.wsService?.close(isActiveClose); } + // 新增的获取 WebSocket 状态的方法 + function getReadyState() { + return state.wsService ? state.wsService.getReadyState() : WebSocket.CLOSED; + } + return { initialize, sendMessage, on, onEvent, close, + getReadyState }; }, { persist: false, diff --git a/src/utils/websocket/websocket.ts b/src/utils/websocket/websocket.ts index 5ae61d1..b52c476 100644 --- a/src/utils/websocket/websocket.ts +++ b/src/utils/websocket/websocket.ts @@ -21,6 +21,7 @@ export class WebSocketService { this.ws.addEventListener('message', this.handleMessage); this.ws.addEventListener('error', this.handleError); this.ws.addEventListener('close', this.handleClose); + } public close(isActiveClose = false): void { @@ -82,4 +83,8 @@ export class WebSocketService { console.warn('尝试发送消息时WebSocket未连接'); } } + + public getReadyState(): number { + return this.ws ? this.ws.readyState : WebSocket.CLOSED; + } } diff --git a/src/views/Login/LoginFooter.vue b/src/views/Login/LoginFooter.vue index f896f57..54ab284 100644 --- a/src/views/Login/LoginFooter.vue +++ b/src/views/Login/LoginFooter.vue @@ -43,8 +43,7 @@ const client = useStore().client; * Get the redirect url of Github OAuth */ async function getGithubRedirectUrl() { - const clientId: string = await getLocalClientId() as string; - const res: any = await getGithubUrl(clientId); + const res: any = await getGithubUrl(client.getClientId() as string); if (res.code === 200 && res.data) { githubRedirectUrl.value = res.data; } @@ -64,8 +63,7 @@ async function getGiteeRedirectUrl() { * Get the redirect url of QQ OAuth */ async function getQQRedirectUrl() { - const clientId: string = await getLocalClientId() as string; - const res: any = await getQQUrl(clientId); + const res: any = await getQQUrl(client.getClientId() as string); if (res.code === 200 && res.data) { qqRedirectUrl.value = res.data; } @@ -81,18 +79,6 @@ async function getClientId() { } } -/** - * 获取本地client_id - */ -async function getLocalClientId() { - if (client.getClientId()) { - return client.getClientId(); - } else { - await getClientId(); - return client.getClientId(); - } -} - /** * Open the Github OAuth page in a new window with debounce */ @@ -223,9 +209,11 @@ function openQQUrl() { } onBeforeMount(() => { - getGithubRedirectUrl(); - getGiteeRedirectUrl(); - getQQRedirectUrl(); + getClientId().then(() => { + getGithubRedirectUrl(); + getGiteeRedirectUrl(); + getQQRedirectUrl(); + }); });