@kevairy/template 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +14 -0
- package/package.json +37 -0
- package/template/.env +22 -0
- package/template/.env.development +13 -0
- package/template/.env.production +10 -0
- package/template/.prettierignore +3 -0
- package/template/.prettierrc +20 -0
- package/template/.stylelintignore +9 -0
- package/template/.stylelintrc.cjs +84 -0
- package/template/LICENSE +21 -0
- package/template/README.md +22 -0
- package/template/_gitignore +19 -0
- package/template/eslint-auto-import.json +324 -0
- package/template/eslint.config.mjs +83 -0
- package/template/index.html +25 -0
- package/template/package.json +79 -0
- package/template/playwright.config.ts +25 -0
- package/template/public/favicon.ico +0 -0
- package/template/src/App.vue +39 -0
- package/template/src/adapters/auth/index.ts +9 -0
- package/template/src/adapters/auth/mock.ts +10 -0
- package/template/src/adapters/auth/types.ts +11 -0
- package/template/src/adapters/navigation/index.ts +13 -0
- package/template/src/adapters/navigation/types.ts +6 -0
- package/template/src/api/auth.ts +23 -0
- package/template/src/assets/images/avatar/avatar.webp +0 -0
- package/template/src/assets/images/avatar/avatar1.webp +0 -0
- package/template/src/assets/images/avatar/avatar10.webp +0 -0
- package/template/src/assets/images/avatar/avatar2.webp +0 -0
- package/template/src/assets/images/avatar/avatar3.webp +0 -0
- package/template/src/assets/images/avatar/avatar4.webp +0 -0
- package/template/src/assets/images/avatar/avatar5.webp +0 -0
- package/template/src/assets/images/avatar/avatar6.webp +0 -0
- package/template/src/assets/images/avatar/avatar7.webp +0 -0
- package/template/src/assets/images/avatar/avatar8.webp +0 -0
- package/template/src/assets/images/avatar/avatar9.webp +0 -0
- package/template/src/assets/images/ceremony/hb.png +0 -0
- package/template/src/assets/images/ceremony/sd.png +0 -0
- package/template/src/assets/images/ceremony/xc.png +0 -0
- package/template/src/assets/images/ceremony/yd.png +0 -0
- package/template/src/assets/images/common/logo.webp +0 -0
- package/template/src/assets/images/draw/draw1.png +0 -0
- package/template/src/assets/images/favicon.ico +0 -0
- package/template/src/assets/images/lock/bg_dark.webp +0 -0
- package/template/src/assets/images/lock/bg_light.webp +0 -0
- package/template/src/assets/images/login/kylin-hero-source.png +0 -0
- package/template/src/assets/images/login/lf_icon2.webp +0 -0
- package/template/src/assets/images/settings/menu_layouts/dual_column.png +0 -0
- package/template/src/assets/images/settings/menu_layouts/horizontal.png +0 -0
- package/template/src/assets/images/settings/menu_layouts/mixed.png +0 -0
- package/template/src/assets/images/settings/menu_layouts/vertical.png +0 -0
- package/template/src/assets/images/settings/menu_styles/dark.png +0 -0
- package/template/src/assets/images/settings/menu_styles/design.png +0 -0
- package/template/src/assets/images/settings/menu_styles/light.png +0 -0
- package/template/src/assets/images/settings/theme_styles/dark.png +0 -0
- package/template/src/assets/images/settings/theme_styles/light.png +0 -0
- package/template/src/assets/images/settings/theme_styles/system.png +0 -0
- package/template/src/assets/images/svg/403.svg +1 -0
- package/template/src/assets/images/svg/404.svg +1 -0
- package/template/src/assets/images/svg/500.svg +5 -0
- package/template/src/assets/images/svg/login_icon.svg +1 -0
- package/template/src/assets/images/user/avatar.webp +0 -0
- package/template/src/assets/images/user/bg.webp +0 -0
- package/template/src/assets/styles/core/app.scss +227 -0
- package/template/src/assets/styles/core/dark.scss +93 -0
- package/template/src/assets/styles/core/el-dark.scss +2 -0
- package/template/src/assets/styles/core/el-light.scss +34 -0
- package/template/src/assets/styles/core/el-ui.scss +519 -0
- package/template/src/assets/styles/core/md.scss +1036 -0
- package/template/src/assets/styles/core/mixin.scss +157 -0
- package/template/src/assets/styles/core/reset.scss +41 -0
- package/template/src/assets/styles/core/router-transition.scss +104 -0
- package/template/src/assets/styles/core/tailwind.css +219 -0
- package/template/src/assets/styles/core/theme-animation.scss +63 -0
- package/template/src/assets/styles/core/theme-change.scss +11 -0
- package/template/src/assets/styles/custom/one-dark-pro.scss +98 -0
- package/template/src/assets/styles/index.scss +23 -0
- package/template/src/assets/svg/loading.ts +32 -0
- package/template/src/bootstrap/initializeApp.ts +30 -0
- package/template/src/components/core/banners/art-basic-banner/index.vue +343 -0
- package/template/src/components/core/banners/art-card-banner/index.vue +114 -0
- package/template/src/components/core/base/art-back-to-top/index.vue +40 -0
- package/template/src/components/core/base/art-logo/index.vue +34 -0
- package/template/src/components/core/base/art-svg-icon/index.vue +24 -0
- package/template/src/components/core/cards/art-bar-chart-card/index.vue +103 -0
- package/template/src/components/core/cards/art-data-list-card/index.vue +74 -0
- package/template/src/components/core/cards/art-donut-chart-card/index.vue +124 -0
- package/template/src/components/core/cards/art-image-card/index.vue +89 -0
- package/template/src/components/core/cards/art-line-chart-card/index.vue +126 -0
- package/template/src/components/core/cards/art-progress-card/index.vue +86 -0
- package/template/src/components/core/cards/art-stats-card/index.vue +67 -0
- package/template/src/components/core/cards/art-timeline-list-card/index.vue +69 -0
- package/template/src/components/core/charts/art-bar-chart/index.vue +203 -0
- package/template/src/components/core/charts/art-dual-bar-compare-chart/index.vue +195 -0
- package/template/src/components/core/charts/art-h-bar-chart/index.vue +208 -0
- package/template/src/components/core/charts/art-k-line-chart/index.vue +152 -0
- package/template/src/components/core/charts/art-line-chart/index.vue +371 -0
- package/template/src/components/core/charts/art-radar-chart/index.vue +105 -0
- package/template/src/components/core/charts/art-ring-chart/index.vue +133 -0
- package/template/src/components/core/charts/art-scatter-chart/index.vue +115 -0
- package/template/src/components/core/feedback/art-page-loading/index.vue +281 -0
- package/template/src/components/core/forms/art-button-more/index.vue +71 -0
- package/template/src/components/core/forms/art-button-table/index.vue +59 -0
- package/template/src/components/core/forms/art-drag-verify/index.vue +430 -0
- package/template/src/components/core/forms/art-excel-export/index.vue +390 -0
- package/template/src/components/core/forms/art-excel-import/index.vue +62 -0
- package/template/src/components/core/forms/art-form/index.vue +507 -0
- package/template/src/components/core/forms/art-wang-editor/index.vue +261 -0
- package/template/src/components/core/forms/art-wang-editor/style.scss +273 -0
- package/template/src/components/core/layouts/art-breadcrumb/index.vue +142 -0
- package/template/src/components/core/layouts/art-chat-window/index.vue +262 -0
- package/template/src/components/core/layouts/art-fast-enter/index.vue +113 -0
- package/template/src/components/core/layouts/art-fireworks-effect/index.vue +633 -0
- package/template/src/components/core/layouts/art-global-component/index.vue +14 -0
- package/template/src/components/core/layouts/art-global-search/index.vue +431 -0
- package/template/src/components/core/layouts/art-header-bar/index.vue +485 -0
- package/template/src/components/core/layouts/art-header-bar/widget/ArtUserMenu.vue +118 -0
- package/template/src/components/core/layouts/art-menus/art-horizontal-menu/index.vue +110 -0
- package/template/src/components/core/layouts/art-menus/art-horizontal-menu/widget/HorizontalSubmenu.vue +106 -0
- package/template/src/components/core/layouts/art-menus/art-mixed-menu/index.vue +279 -0
- package/template/src/components/core/layouts/art-menus/art-sidebar-menu/index.vue +354 -0
- package/template/src/components/core/layouts/art-menus/art-sidebar-menu/style.scss +251 -0
- package/template/src/components/core/layouts/art-menus/art-sidebar-menu/theme.scss +258 -0
- package/template/src/components/core/layouts/art-menus/art-sidebar-menu/widget/SidebarSubmenu.vue +190 -0
- package/template/src/components/core/layouts/art-notification/index.vue +456 -0
- package/template/src/components/core/layouts/art-page-content/index.vue +137 -0
- package/template/src/components/core/layouts/art-settings-panel/composables/useSettingsConfig.ts +248 -0
- package/template/src/components/core/layouts/art-settings-panel/composables/useSettingsHandlers.ts +167 -0
- package/template/src/components/core/layouts/art-settings-panel/composables/useSettingsPanel.ts +233 -0
- package/template/src/components/core/layouts/art-settings-panel/composables/useSettingsState.ts +37 -0
- package/template/src/components/core/layouts/art-settings-panel/index.vue +72 -0
- package/template/src/components/core/layouts/art-settings-panel/style.scss +92 -0
- package/template/src/components/core/layouts/art-settings-panel/widget/BasicSettings.vue +77 -0
- package/template/src/components/core/layouts/art-settings-panel/widget/BoxStyleSettings.vue +38 -0
- package/template/src/components/core/layouts/art-settings-panel/widget/ColorSettings.vue +35 -0
- package/template/src/components/core/layouts/art-settings-panel/widget/ContainerSettings.vue +33 -0
- package/template/src/components/core/layouts/art-settings-panel/widget/MenuLayoutSettings.vue +31 -0
- package/template/src/components/core/layouts/art-settings-panel/widget/MenuStyleSettings.vue +44 -0
- package/template/src/components/core/layouts/art-settings-panel/widget/SectionTitle.vue +17 -0
- package/template/src/components/core/layouts/art-settings-panel/widget/SettingActions.vue +236 -0
- package/template/src/components/core/layouts/art-settings-panel/widget/SettingDrawer.vue +51 -0
- package/template/src/components/core/layouts/art-settings-panel/widget/SettingHeader.vue +18 -0
- package/template/src/components/core/layouts/art-settings-panel/widget/SettingItem.vue +101 -0
- package/template/src/components/core/layouts/art-settings-panel/widget/ThemeSettings.vue +28 -0
- package/template/src/components/core/layouts/art-work-tab/index.vue +584 -0
- package/template/src/components/core/media/art-cutter-img/index.vue +350 -0
- package/template/src/components/core/media/art-video-player/index.vue +111 -0
- package/template/src/components/core/others/art-menu-right/index.vue +415 -0
- package/template/src/components/core/others/art-watermark/index.vue +64 -0
- package/template/src/components/core/text-effect/art-count-to/index.vue +310 -0
- package/template/src/components/core/text-effect/art-festival-text-scroll/index.vue +32 -0
- package/template/src/components/core/text-effect/art-text-scroll/index.vue +285 -0
- package/template/src/components/core/theme/theme-svg/index.vue +100 -0
- package/template/src/components/core/views/exception/ArtException.vue +55 -0
- package/template/src/components/core/views/login/AuthTopBar.vue +149 -0
- package/template/src/components/core/views/login/LoginLeftView.vue +602 -0
- package/template/src/components/core/views/result/ArtResultPage.vue +43 -0
- package/template/src/components/core/widget/art-icon-button/index.vue +23 -0
- package/template/src/config/assets/images.ts +61 -0
- package/template/src/config/index.ts +170 -0
- package/template/src/config/modules/component.ts +98 -0
- package/template/src/config/modules/fastEnter.ts +16 -0
- package/template/src/config/modules/festival.ts +51 -0
- package/template/src/config/modules/headerBar.ts +64 -0
- package/template/src/config/setting.ts +109 -0
- package/template/src/directives/core/auth.ts +66 -0
- package/template/src/directives/core/highlight-loader.ts +37 -0
- package/template/src/directives/core/highlight.ts +253 -0
- package/template/src/directives/core/ripple.ts +116 -0
- package/template/src/directives/core/roles.ts +87 -0
- package/template/src/directives/index.ts +14 -0
- package/template/src/enums/appEnum.ts +81 -0
- package/template/src/enums/formEnum.ts +17 -0
- package/template/src/env.d.ts +32 -0
- package/template/src/hooks/core/useAppMode.ts +45 -0
- package/template/src/hooks/core/useAuth.ts +74 -0
- package/template/src/hooks/core/useCeremony.ts +184 -0
- package/template/src/hooks/core/useChart.ts +745 -0
- package/template/src/hooks/core/useCommon.ts +87 -0
- package/template/src/hooks/core/useFastEnter.ts +55 -0
- package/template/src/hooks/core/useHeaderBar.ts +201 -0
- package/template/src/hooks/core/useLayoutHeight.ts +148 -0
- package/template/src/hooks/core/useTableColumns.ts +313 -0
- package/template/src/hooks/core/useTheme.ts +174 -0
- package/template/src/hooks/index.ts +29 -0
- package/template/src/locales/index.ts +123 -0
- package/template/src/locales/langs/en.json +283 -0
- package/template/src/locales/langs/zh.json +283 -0
- package/template/src/main.ts +29 -0
- package/template/src/mock/auth.ts +46 -0
- package/template/src/plugins/echarts.ts +76 -0
- package/template/src/plugins/index.ts +6 -0
- package/template/src/router/core/ComponentLoader.ts +82 -0
- package/template/src/router/core/IframeRouteManager.ts +78 -0
- package/template/src/router/core/MenuProcessor.ts +280 -0
- package/template/src/router/core/RoutePermissionValidator.ts +161 -0
- package/template/src/router/core/RouteRegistry.ts +90 -0
- package/template/src/router/core/RouteTransformer.ts +132 -0
- package/template/src/router/core/RouteValidator.ts +187 -0
- package/template/src/router/core/index.ts +14 -0
- package/template/src/router/guards/afterEach.ts +34 -0
- package/template/src/router/guards/beforeEach.ts +442 -0
- package/template/src/router/index.ts +24 -0
- package/template/src/router/modules/dashboard.ts +23 -0
- package/template/src/router/modules/examples.ts +109 -0
- package/template/src/router/modules/exception.ts +46 -0
- package/template/src/router/modules/index.ts +12 -0
- package/template/src/router/modules/result.ts +32 -0
- package/template/src/router/routes/asyncRoutes.ts +9 -0
- package/template/src/router/routes/staticRoutes.ts +60 -0
- package/template/src/router/routesAlias.ts +8 -0
- package/template/src/services/auth.ts +15 -0
- package/template/src/store/index.ts +52 -0
- package/template/src/store/modules/menu.ts +109 -0
- package/template/src/store/modules/setting.ts +450 -0
- package/template/src/store/modules/user.ts +218 -0
- package/template/src/store/modules/worktab.ts +568 -0
- package/template/src/types/api/api.d.ts +86 -0
- package/template/src/types/common/index.ts +95 -0
- package/template/src/types/common/response.ts +32 -0
- package/template/src/types/component/chart.ts +324 -0
- package/template/src/types/component/index.ts +86 -0
- package/template/src/types/config/index.ts +253 -0
- package/template/src/types/directive/directive.d.ts +15 -0
- package/template/src/types/import/auto-imports.d.ts +319 -0
- package/template/src/types/import/components.d.ts +126 -0
- package/template/src/types/index.ts +22 -0
- package/template/src/types/router/index.ts +80 -0
- package/template/src/types/store/index.ts +157 -0
- package/template/src/utils/form/index.ts +9 -0
- package/template/src/utils/form/validator.ts +316 -0
- package/template/src/utils/http/contract.ts +39 -0
- package/template/src/utils/http/error.ts +182 -0
- package/template/src/utils/http/index.ts +226 -0
- package/template/src/utils/http/status.ts +18 -0
- package/template/src/utils/index.ts +31 -0
- package/template/src/utils/navigation/index.ts +10 -0
- package/template/src/utils/navigation/jump.ts +68 -0
- package/template/src/utils/navigation/route.ts +85 -0
- package/template/src/utils/navigation/worktab.ts +67 -0
- package/template/src/utils/router.ts +63 -0
- package/template/src/utils/socket/index.ts +423 -0
- package/template/src/utils/storage/index.ts +7 -0
- package/template/src/utils/storage/storage-config.ts +127 -0
- package/template/src/utils/storage/storage-key-manager.ts +97 -0
- package/template/src/utils/storage/storage.ts +250 -0
- package/template/src/utils/sys/console.ts +9 -0
- package/template/src/utils/sys/error-handle.ts +143 -0
- package/template/src/utils/sys/index.ts +2 -0
- package/template/src/utils/sys/mittBus.ts +61 -0
- package/template/src/utils/ui/animation.ts +80 -0
- package/template/src/utils/ui/colors.ts +273 -0
- package/template/src/utils/ui/emojo.ts +24 -0
- package/template/src/utils/ui/iconify-loader.ts +31 -0
- package/template/src/utils/ui/index.ts +11 -0
- package/template/src/utils/ui/loading.ts +158 -0
- package/template/src/utils/ui/tabs.ts +60 -0
- package/template/src/views/auth/forget-password/index.vue +62 -0
- package/template/src/views/auth/login/index.vue +166 -0
- package/template/src/views/auth/login/style.css +38 -0
- package/template/src/views/auth/register/index.vue +240 -0
- package/template/src/views/dashboard/console/index.vue +173 -0
- package/template/src/views/examples/analytics/index.vue +389 -0
- package/template/src/views/examples/complex-form/index.vue +395 -0
- package/template/src/views/examples/crud-workspace/index.vue +412 -0
- package/template/src/views/examples/navigation/detail/index.vue +151 -0
- package/template/src/views/examples/navigation/state/index.vue +226 -0
- package/template/src/views/examples/overview/index.vue +309 -0
- package/template/src/views/examples/permissions/index.vue +303 -0
- package/template/src/views/examples/search-table/index.vue +182 -0
- package/template/src/views/examples/search-table/search.config.ts +81 -0
- package/template/src/views/examples/search-table/service.ts +116 -0
- package/template/src/views/examples/search-table/table.config.ts +37 -0
- package/template/src/views/exception/403/index.vue +16 -0
- package/template/src/views/exception/404/index.vue +16 -0
- package/template/src/views/exception/500/index.vue +16 -0
- package/template/src/views/index/index.vue +29 -0
- package/template/src/views/index/style.scss +93 -0
- package/template/src/views/outside/Iframe.vue +42 -0
- package/template/src/views/result/fail/index.vue +28 -0
- package/template/src/views/result/success/index.vue +21 -0
- package/template/tests/e2e/auth.spec.ts +14 -0
- package/template/tests/e2e/search-table.spec.ts +154 -0
- package/template/tests/unit/adapters/auth.spec.ts +38 -0
- package/template/tests/unit/config/app-config.spec.ts +17 -0
- package/template/tests/unit/directives/highlight.spec.ts +20 -0
- package/template/tests/unit/http/contract.spec.ts +47 -0
- package/template/tests/unit/mock/search-table.spec.ts +62 -0
- package/template/tests/unit/router/examples.spec.ts +52 -0
- package/template/tsconfig.json +28 -0
- package/template/vite.config.ts +181 -0
- package/template/vitest.config.ts +27 -0
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
interface WebSocketOptions {
|
|
2
|
+
url?: string
|
|
3
|
+
messageHandler: (event: MessageEvent) => void
|
|
4
|
+
reconnectInterval?: number // 重连间隔(ms)
|
|
5
|
+
heartbeatInterval?: number // 心跳检测间隔(ms)
|
|
6
|
+
pingInterval?: number // 发送ping间隔(ms)
|
|
7
|
+
reconnectTimeout?: number // 重连超时时间(ms)
|
|
8
|
+
maxReconnectAttempts?: number // 最大重连次数
|
|
9
|
+
connectionTimeout?: number // 连接建立超时时间(ms)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default class WebSocketClient {
|
|
13
|
+
private static instance: WebSocketClient | null = null
|
|
14
|
+
private ws: WebSocket | null = null
|
|
15
|
+
private url: string
|
|
16
|
+
private messageHandler: (event: MessageEvent) => void
|
|
17
|
+
private reconnectInterval: number
|
|
18
|
+
private heartbeatInterval: number
|
|
19
|
+
private pingInterval: number
|
|
20
|
+
private reconnectTimeout: number
|
|
21
|
+
private maxReconnectAttempts: number
|
|
22
|
+
private connectionTimeout: number
|
|
23
|
+
private reconnectAttempts: number = 0 // 当前重连次数
|
|
24
|
+
|
|
25
|
+
// 消息队列 - 缓存连接建立前的消息
|
|
26
|
+
private messageQueue: Array<string | ArrayBufferLike | Blob | ArrayBufferView> = []
|
|
27
|
+
|
|
28
|
+
// 定时器
|
|
29
|
+
private detectionTimer: NodeJS.Timeout | null = null
|
|
30
|
+
private timeoutTimer: NodeJS.Timeout | null = null
|
|
31
|
+
private reconnectTimer: NodeJS.Timeout | null = null
|
|
32
|
+
private pingTimer: NodeJS.Timeout | null = null
|
|
33
|
+
private connectionTimer: NodeJS.Timeout | null = null // 连接超时定时器
|
|
34
|
+
|
|
35
|
+
// 状态标识
|
|
36
|
+
private isConnected: boolean = false
|
|
37
|
+
private isConnecting: boolean = false // 是否正在连接中
|
|
38
|
+
private stopReconnect: boolean = false
|
|
39
|
+
private isReconnecting: boolean = false
|
|
40
|
+
|
|
41
|
+
private constructor(options: WebSocketOptions) {
|
|
42
|
+
this.url = options.url || (process.env.VUE_APP_LOGIN_WEBSOCKET as string)
|
|
43
|
+
this.messageHandler = options.messageHandler
|
|
44
|
+
this.reconnectInterval = options.reconnectInterval ?? 20 * 1000 // 默认20秒
|
|
45
|
+
this.heartbeatInterval = options.heartbeatInterval ?? 5 * 1000 // 默认5秒
|
|
46
|
+
this.pingInterval = options.pingInterval ?? 10 * 1000 // 默认10秒
|
|
47
|
+
this.reconnectTimeout = options.reconnectTimeout ?? 30 * 1000 // 默认30秒
|
|
48
|
+
this.maxReconnectAttempts = options.maxReconnectAttempts ?? 10 // 默认最多重连10次
|
|
49
|
+
this.connectionTimeout = options.connectionTimeout ?? 10 * 1000 // 连接超时10秒
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 单例模式获取实例
|
|
53
|
+
static getInstance(options: WebSocketOptions): WebSocketClient {
|
|
54
|
+
if (!WebSocketClient.instance) {
|
|
55
|
+
WebSocketClient.instance = new WebSocketClient(options)
|
|
56
|
+
} else {
|
|
57
|
+
// 更新消息处理器
|
|
58
|
+
WebSocketClient.instance.messageHandler = options.messageHandler
|
|
59
|
+
// 如果提供了新的URL,则更新并重新连接
|
|
60
|
+
if (options.url && WebSocketClient.instance.url !== options.url) {
|
|
61
|
+
WebSocketClient.instance.url = options.url
|
|
62
|
+
WebSocketClient.instance.reconnectAttempts = 0
|
|
63
|
+
WebSocketClient.instance.init()
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return WebSocketClient.instance
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// 初始化连接
|
|
70
|
+
init(): void {
|
|
71
|
+
this.connect(true)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
private connect(resetReconnectAttempts: boolean = false): void {
|
|
75
|
+
// 如果正在连接中,不重复连接
|
|
76
|
+
if (this.isConnecting) {
|
|
77
|
+
console.log('正在建立WebSocket连接中...')
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// 如果已连接,不重复连接
|
|
82
|
+
if (this.ws?.readyState === WebSocket.OPEN) {
|
|
83
|
+
console.warn('WebSocket连接已存在')
|
|
84
|
+
this.flushMessageQueue() // 确保队列中的消息被发送
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
this.isConnecting = true
|
|
90
|
+
this.stopReconnect = false
|
|
91
|
+
if (resetReconnectAttempts) {
|
|
92
|
+
this.reconnectAttempts = 0
|
|
93
|
+
this.isReconnecting = false
|
|
94
|
+
this.clearTimer('reconnectTimer')
|
|
95
|
+
}
|
|
96
|
+
this.ws = new WebSocket(this.url)
|
|
97
|
+
|
|
98
|
+
// 设置连接超时检测
|
|
99
|
+
this.clearTimer('connectionTimer')
|
|
100
|
+
this.connectionTimer = setTimeout(() => {
|
|
101
|
+
console.error(`WebSocket连接超时 (${this.connectionTimeout}ms):${this.url}`)
|
|
102
|
+
this.handleConnectionTimeout()
|
|
103
|
+
}, this.connectionTimeout)
|
|
104
|
+
|
|
105
|
+
this.ws.onopen = (event) => this.handleOpen(event)
|
|
106
|
+
this.ws.onmessage = (event) => this.handleMessage(event)
|
|
107
|
+
this.ws.onclose = (event) => this.handleClose(event)
|
|
108
|
+
this.ws.onerror = (event) => this.handleError(event)
|
|
109
|
+
} catch (error) {
|
|
110
|
+
console.error('WebSocket初始化失败:', error)
|
|
111
|
+
this.isConnecting = false
|
|
112
|
+
this.reconnect()
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// 处理连接超时
|
|
117
|
+
private handleConnectionTimeout(): void {
|
|
118
|
+
if (this.ws?.readyState !== WebSocket.OPEN) {
|
|
119
|
+
console.error('WebSocket连接超时,强制关闭连接')
|
|
120
|
+
this.ws?.close(1000, 'Connection timeout')
|
|
121
|
+
this.isConnecting = false
|
|
122
|
+
this.reconnect()
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// 关闭连接
|
|
127
|
+
close(force?: boolean): void {
|
|
128
|
+
this.clearAllTimers()
|
|
129
|
+
this.stopReconnect = true
|
|
130
|
+
this.isReconnecting = false
|
|
131
|
+
this.isConnecting = false
|
|
132
|
+
|
|
133
|
+
if (this.ws) {
|
|
134
|
+
// 1000 表示正常关闭
|
|
135
|
+
this.ws.close(force ? 1001 : 1000, force ? 'Force closed' : 'Normal close')
|
|
136
|
+
this.ws = null
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
this.isConnected = false
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// 发送消息 - 增加消息队列
|
|
143
|
+
send(data: string | ArrayBufferLike | Blob | ArrayBufferView, immediate: boolean = false): void {
|
|
144
|
+
// 如果要求立即发送且未连接,则直接报错
|
|
145
|
+
if (immediate && (!this.ws || this.ws.readyState !== WebSocket.OPEN)) {
|
|
146
|
+
console.error('WebSocket未连接,无法立即发送消息')
|
|
147
|
+
return
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// 如果未连接且不要求立即发送,则加入消息队列
|
|
151
|
+
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
152
|
+
console.log('WebSocket未连接,消息已加入队列等待发送')
|
|
153
|
+
this.messageQueue.push(data)
|
|
154
|
+
// 如果未在重连中,则尝试重连
|
|
155
|
+
if (!this.isConnecting && !this.stopReconnect) {
|
|
156
|
+
this.init()
|
|
157
|
+
}
|
|
158
|
+
return
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
try {
|
|
162
|
+
this.ws.send(data)
|
|
163
|
+
} catch (error) {
|
|
164
|
+
console.error('WebSocket发送消息失败:', error)
|
|
165
|
+
// 发送失败时将消息加入队列,等待重连后重试
|
|
166
|
+
this.messageQueue.push(data)
|
|
167
|
+
this.reconnect()
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// 发送队列中的消息
|
|
172
|
+
private flushMessageQueue(): void {
|
|
173
|
+
if (this.messageQueue.length > 0 && this.ws?.readyState === WebSocket.OPEN) {
|
|
174
|
+
console.log(`发送队列中的${this.messageQueue.length}条消息`)
|
|
175
|
+
while (this.messageQueue.length > 0) {
|
|
176
|
+
const data = this.messageQueue.shift()
|
|
177
|
+
if (data) {
|
|
178
|
+
try {
|
|
179
|
+
this.ws?.send(data)
|
|
180
|
+
} catch (error) {
|
|
181
|
+
console.error('发送队列消息失败:', error)
|
|
182
|
+
// 如果发送失败,将消息放回队列头部
|
|
183
|
+
if (data) this.messageQueue.unshift(data)
|
|
184
|
+
break
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// 处理连接打开
|
|
192
|
+
private handleOpen(event: Event): void {
|
|
193
|
+
console.log('WebSocket连接成功', event)
|
|
194
|
+
this.clearTimer('connectionTimer') // 清除连接超时定时器
|
|
195
|
+
this.isConnected = true
|
|
196
|
+
this.isConnecting = false
|
|
197
|
+
this.isReconnecting = false
|
|
198
|
+
this.stopReconnect = false
|
|
199
|
+
this.reconnectAttempts = 0 // 重置重连次数
|
|
200
|
+
this.startHeartbeat()
|
|
201
|
+
this.startPing()
|
|
202
|
+
this.flushMessageQueue() // 发送队列中的消息
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// 处理收到的消息
|
|
206
|
+
private handleMessage(event: MessageEvent): void {
|
|
207
|
+
console.log('收到WebSocket消息:', event)
|
|
208
|
+
this.resetHeartbeat()
|
|
209
|
+
this.messageHandler(event)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// 处理连接关闭
|
|
213
|
+
private handleClose(event: CloseEvent): void {
|
|
214
|
+
console.log(
|
|
215
|
+
`WebSocket断开: 代码=${event.code}, 原因=${event.reason}, 干净关闭=${event.wasClean}`
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
// 1000 是正常关闭代码
|
|
219
|
+
const isNormalClose = event.code === 1000
|
|
220
|
+
|
|
221
|
+
this.isConnected = false
|
|
222
|
+
this.isConnecting = false
|
|
223
|
+
this.clearConnectionTimers()
|
|
224
|
+
this.ws = null
|
|
225
|
+
|
|
226
|
+
if (!this.stopReconnect && !isNormalClose) {
|
|
227
|
+
this.reconnect()
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// 处理错误 - 增加详细错误信息
|
|
232
|
+
private handleError(event: Event): void {
|
|
233
|
+
console.error('WebSocket连接错误:')
|
|
234
|
+
console.error('错误事件:', event)
|
|
235
|
+
console.error(
|
|
236
|
+
'当前连接状态:',
|
|
237
|
+
this.ws?.readyState ? this.getReadyStateText(this.ws.readyState) : '未初始化'
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
this.isConnected = false
|
|
241
|
+
this.isConnecting = false
|
|
242
|
+
|
|
243
|
+
// 只有在未停止重连的情况下才尝试重连
|
|
244
|
+
if (!this.stopReconnect) {
|
|
245
|
+
this.reconnect()
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
private closeCurrentSocketForReconnect(): void {
|
|
250
|
+
this.clearConnectionTimers()
|
|
251
|
+
this.isConnected = false
|
|
252
|
+
this.isConnecting = false
|
|
253
|
+
|
|
254
|
+
if (this.ws) {
|
|
255
|
+
this.ws.onopen = null
|
|
256
|
+
this.ws.onmessage = null
|
|
257
|
+
this.ws.onclose = null
|
|
258
|
+
this.ws.onerror = null
|
|
259
|
+
|
|
260
|
+
if (this.ws.readyState === WebSocket.OPEN || this.ws.readyState === WebSocket.CONNECTING) {
|
|
261
|
+
this.ws.close(1001, 'Reconnect')
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
this.ws = null
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// 转换连接状态为文本描述
|
|
269
|
+
private getReadyStateText(state: number): string {
|
|
270
|
+
switch (state) {
|
|
271
|
+
case WebSocket.CONNECTING:
|
|
272
|
+
return 'CONNECTING (0) - 正在连接'
|
|
273
|
+
case WebSocket.OPEN:
|
|
274
|
+
return 'OPEN (1) - 已连接'
|
|
275
|
+
case WebSocket.CLOSING:
|
|
276
|
+
return 'CLOSING (2) - 正在关闭'
|
|
277
|
+
case WebSocket.CLOSED:
|
|
278
|
+
return 'CLOSED (3) - 已关闭'
|
|
279
|
+
default:
|
|
280
|
+
return `未知状态 (${state})`
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// 开始心跳检测
|
|
285
|
+
private startHeartbeat(): void {
|
|
286
|
+
this.clearTimer('detectionTimer')
|
|
287
|
+
this.clearTimer('timeoutTimer')
|
|
288
|
+
|
|
289
|
+
this.detectionTimer = setTimeout(() => {
|
|
290
|
+
this.isConnected = this.ws?.readyState === WebSocket.OPEN
|
|
291
|
+
|
|
292
|
+
if (!this.isConnected) {
|
|
293
|
+
console.warn('WebSocket心跳检测失败,尝试重连')
|
|
294
|
+
this.reconnect()
|
|
295
|
+
|
|
296
|
+
this.timeoutTimer = setTimeout(() => {
|
|
297
|
+
console.warn('WebSocket重连超时')
|
|
298
|
+
this.close()
|
|
299
|
+
}, this.reconnectTimeout)
|
|
300
|
+
}
|
|
301
|
+
}, this.heartbeatInterval)
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// 重置心跳检测
|
|
305
|
+
private resetHeartbeat(): void {
|
|
306
|
+
this.clearTimer('detectionTimer')
|
|
307
|
+
this.clearTimer('timeoutTimer')
|
|
308
|
+
this.startHeartbeat()
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// 开始发送ping消息
|
|
312
|
+
private startPing(): void {
|
|
313
|
+
this.clearTimer('pingTimer')
|
|
314
|
+
|
|
315
|
+
this.pingTimer = setInterval(() => {
|
|
316
|
+
if (this.ws?.readyState !== WebSocket.OPEN) {
|
|
317
|
+
console.warn('WebSocket未连接,停止发送ping')
|
|
318
|
+
this.clearTimer('pingTimer')
|
|
319
|
+
this.reconnect()
|
|
320
|
+
return
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
try {
|
|
324
|
+
this.ws.send('ping')
|
|
325
|
+
console.log('发送ping消息')
|
|
326
|
+
} catch (error) {
|
|
327
|
+
console.error('发送ping消息失败:', error)
|
|
328
|
+
this.clearTimer('pingTimer')
|
|
329
|
+
this.reconnect()
|
|
330
|
+
}
|
|
331
|
+
}, this.pingInterval)
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// 重连 - 增加重连次数限制
|
|
335
|
+
private reconnect(): void {
|
|
336
|
+
if (this.stopReconnect || this.isConnecting || this.reconnectInterval <= 0) {
|
|
337
|
+
return
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// 检查是否超过最大重连次数
|
|
341
|
+
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
342
|
+
console.error(`已达到最大重连次数(${this.maxReconnectAttempts}),停止重连`)
|
|
343
|
+
this.close(true)
|
|
344
|
+
return
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
this.reconnectAttempts++
|
|
348
|
+
this.isReconnecting = true
|
|
349
|
+
this.closeCurrentSocketForReconnect()
|
|
350
|
+
|
|
351
|
+
const delay = this.calculateReconnectDelay()
|
|
352
|
+
console.log(
|
|
353
|
+
`将在${delay / 1000}秒后尝试重新连接(第${this.reconnectAttempts}/${this.maxReconnectAttempts}次)`
|
|
354
|
+
)
|
|
355
|
+
|
|
356
|
+
this.clearTimer('reconnectTimer')
|
|
357
|
+
this.reconnectTimer = setTimeout(() => {
|
|
358
|
+
console.log(`尝试重新连接WebSocket(第${this.reconnectAttempts}次)`)
|
|
359
|
+
this.connect(false)
|
|
360
|
+
}, delay)
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// 计算重连延迟 - 指数退避策略
|
|
364
|
+
private calculateReconnectDelay(): number {
|
|
365
|
+
// 基础延迟 + 随机值,避免多个客户端同时重连
|
|
366
|
+
const jitter = Math.random() * 1000 // 0-1秒的随机延迟
|
|
367
|
+
const baseDelay = Math.min(
|
|
368
|
+
this.reconnectInterval * Math.pow(1.5, this.reconnectAttempts - 1),
|
|
369
|
+
this.reconnectInterval * 5
|
|
370
|
+
)
|
|
371
|
+
return baseDelay + jitter
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// 清除指定定时器
|
|
375
|
+
private clearTimer(
|
|
376
|
+
timerName:
|
|
377
|
+
| 'detectionTimer'
|
|
378
|
+
| 'timeoutTimer'
|
|
379
|
+
| 'reconnectTimer'
|
|
380
|
+
| 'pingTimer'
|
|
381
|
+
| 'connectionTimer'
|
|
382
|
+
): void {
|
|
383
|
+
if (this[timerName]) {
|
|
384
|
+
clearTimeout(this[timerName] as NodeJS.Timeout)
|
|
385
|
+
this[timerName] = null
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// 清除所有定时器
|
|
390
|
+
private clearAllTimers(): void {
|
|
391
|
+
this.clearConnectionTimers()
|
|
392
|
+
this.clearTimer('reconnectTimer')
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
private clearConnectionTimers(): void {
|
|
396
|
+
this.clearTimer('detectionTimer')
|
|
397
|
+
this.clearTimer('timeoutTimer')
|
|
398
|
+
this.clearTimer('pingTimer')
|
|
399
|
+
this.clearTimer('connectionTimer')
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// 获取当前连接状态
|
|
403
|
+
get isWebSocketConnected(): boolean {
|
|
404
|
+
return this.isConnected
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// 获取当前连接状态文本
|
|
408
|
+
get connectionStatusText(): string {
|
|
409
|
+
if (this.isConnecting) return '正在连接'
|
|
410
|
+
if (this.isConnected) return '已连接'
|
|
411
|
+
if (this.isReconnecting && this.reconnectAttempts > 0)
|
|
412
|
+
return `重连中(${this.reconnectAttempts}/${this.maxReconnectAttempts})`
|
|
413
|
+
return '已断开'
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// 销毁实例
|
|
417
|
+
static destroyInstance(): void {
|
|
418
|
+
if (WebSocketClient.instance) {
|
|
419
|
+
WebSocketClient.instance.close()
|
|
420
|
+
WebSocketClient.instance = null
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 存储配置管理模块
|
|
3
|
+
*
|
|
4
|
+
* 提供统一的本地存储配置和工具方法
|
|
5
|
+
*
|
|
6
|
+
* ## 主要功能
|
|
7
|
+
*
|
|
8
|
+
* - 版本化存储键管理,支持多版本数据隔离
|
|
9
|
+
* - 存储键名生成和解析(带版本前缀)
|
|
10
|
+
* - 版本号提取和验证
|
|
11
|
+
* - 存储键匹配的正则表达式生成
|
|
12
|
+
* - 旧版本存储键兼容处理
|
|
13
|
+
* - 升级和登出延迟配置
|
|
14
|
+
* - 主题存储键配置
|
|
15
|
+
*
|
|
16
|
+
* ## 使用场景
|
|
17
|
+
*
|
|
18
|
+
* - Pinia Store 持久化存储
|
|
19
|
+
* - 应用版本升级时的数据迁移
|
|
20
|
+
* - 多版本数据清理
|
|
21
|
+
* - 存储键的统一管理和规范
|
|
22
|
+
*
|
|
23
|
+
* 存储键格式:sys-v{version}-{storeId}
|
|
24
|
+
* 例如:sys-v1.0.0-user, sys-v1.0.0-setting
|
|
25
|
+
*
|
|
26
|
+
* @module utils/storage/storage-config
|
|
27
|
+
* @author Art Design Pro Team
|
|
28
|
+
*/
|
|
29
|
+
import AppConfig from '@/config'
|
|
30
|
+
|
|
31
|
+
export class StorageConfig {
|
|
32
|
+
/** 当前应用版本 */
|
|
33
|
+
static readonly CURRENT_VERSION = __APP_VERSION__
|
|
34
|
+
|
|
35
|
+
/** 存储键前缀 */
|
|
36
|
+
static readonly STORAGE_PREFIX = `${AppConfig.storage.namespace}-v`
|
|
37
|
+
|
|
38
|
+
/** 版本键名 */
|
|
39
|
+
static readonly VERSION_KEY = `${AppConfig.storage.namespace}-version`
|
|
40
|
+
|
|
41
|
+
/** 主题键名(index.html中使用了,如果修改,需要同步修改) */
|
|
42
|
+
static readonly THEME_KEY = `${AppConfig.storage.namespace}-theme`
|
|
43
|
+
|
|
44
|
+
/** 上次登录用户ID键名(用于判断是否为同一用户登录) */
|
|
45
|
+
static readonly LAST_USER_ID_KEY = `${AppConfig.storage.namespace}-last-user-id`
|
|
46
|
+
|
|
47
|
+
/** 响应式布局切换时暂存桌面端菜单类型 */
|
|
48
|
+
static readonly RESPONSIVE_MENU_TYPE_KEY = `${AppConfig.storage.namespace}-responsive-menu-type`
|
|
49
|
+
|
|
50
|
+
/** 跳过升级检查的版本 */
|
|
51
|
+
static readonly SKIP_UPGRADE_VERSION = '1.0.0'
|
|
52
|
+
|
|
53
|
+
/** 升级处理延迟时间(毫秒) */
|
|
54
|
+
static readonly UPGRADE_DELAY = 1000
|
|
55
|
+
|
|
56
|
+
/** 登出延迟时间(毫秒) */
|
|
57
|
+
static readonly LOGOUT_DELAY = 1000
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 生成版本化的存储键名
|
|
61
|
+
* @param storeId 存储ID
|
|
62
|
+
* @param version 版本号,默认使用当前版本
|
|
63
|
+
*/
|
|
64
|
+
static generateStorageKey(storeId: string, version: string = this.CURRENT_VERSION): string {
|
|
65
|
+
return `${this.STORAGE_PREFIX}${version}-${storeId}`
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 生成旧版本的存储键名(不带分隔符)
|
|
70
|
+
* @param version 版本号,默认使用当前版本
|
|
71
|
+
*/
|
|
72
|
+
static generateLegacyKey(version: string = this.CURRENT_VERSION): string {
|
|
73
|
+
return `${this.STORAGE_PREFIX}${version}`
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 创建存储键匹配的正则表达式
|
|
78
|
+
* @param storeId 存储ID
|
|
79
|
+
*/
|
|
80
|
+
static createKeyPattern(storeId: string): RegExp {
|
|
81
|
+
return new RegExp(`^${this.STORAGE_PREFIX}[^-]+-${storeId}$`)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 创建当前版本存储键匹配的正则表达式
|
|
86
|
+
*/
|
|
87
|
+
static createCurrentVersionPattern(): RegExp {
|
|
88
|
+
return new RegExp(`^${this.STORAGE_PREFIX}${this.CURRENT_VERSION}-`)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 创建任意版本存储键匹配的正则表达式
|
|
93
|
+
*/
|
|
94
|
+
static createVersionPattern(): RegExp {
|
|
95
|
+
return new RegExp(`^${this.STORAGE_PREFIX}`)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 检查是否为当前版本的键
|
|
100
|
+
*/
|
|
101
|
+
static isCurrentVersionKey(key: string): boolean {
|
|
102
|
+
return key.startsWith(`${this.STORAGE_PREFIX}${this.CURRENT_VERSION}`)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* 检查是否为版本化的键
|
|
107
|
+
*/
|
|
108
|
+
static isVersionedKey(key: string): boolean {
|
|
109
|
+
return key.startsWith(this.STORAGE_PREFIX)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 从存储键中提取版本号
|
|
114
|
+
*/
|
|
115
|
+
static extractVersionFromKey(key: string): string | null {
|
|
116
|
+
const match = key.match(new RegExp(`^${this.STORAGE_PREFIX}([^-]+)`))
|
|
117
|
+
return match ? match[1] : null
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* 从存储键中提取存储ID
|
|
122
|
+
*/
|
|
123
|
+
static extractStoreIdFromKey(key: string): string | null {
|
|
124
|
+
const match = key.match(new RegExp(`^${this.STORAGE_PREFIX}[^-]+-(.+)$`))
|
|
125
|
+
return match ? match[1] : null
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 存储键名管理器模块
|
|
3
|
+
*
|
|
4
|
+
* 提供智能的版本化存储键管理和数据迁移功能
|
|
5
|
+
*
|
|
6
|
+
* ## 主要功能
|
|
7
|
+
*
|
|
8
|
+
* - 自动生成当前版本的存储键名
|
|
9
|
+
* - 检测当前版本数据是否存在
|
|
10
|
+
* - 查找其他版本的同名存储数据
|
|
11
|
+
* - 自动将旧版本数据迁移到当前版本
|
|
12
|
+
* - 数据迁移日志记录
|
|
13
|
+
* - 迁移失败的错误处理
|
|
14
|
+
*
|
|
15
|
+
* ## 使用场景
|
|
16
|
+
*
|
|
17
|
+
* - Pinia Store 持久化插件中获取存储键
|
|
18
|
+
* - 应用版本升级时自动迁移用户数据
|
|
19
|
+
* - 避免版本升级导致的数据丢失
|
|
20
|
+
* - 实现平滑的版本过渡
|
|
21
|
+
*
|
|
22
|
+
* ## 工作流程
|
|
23
|
+
*
|
|
24
|
+
* 1. 优先使用当前版本的存储键
|
|
25
|
+
* 2. 如果当前版本无数据,查找其他版本的同名数据
|
|
26
|
+
* 3. 找到旧版本数据后自动迁移到当前版本
|
|
27
|
+
* 4. 返回当前版本的存储键供使用
|
|
28
|
+
*
|
|
29
|
+
* @module utils/storage/storage-key-manager
|
|
30
|
+
* @author Art Design Pro Team
|
|
31
|
+
*/
|
|
32
|
+
import { StorageConfig } from '@/utils/storage'
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 存储键名管理器
|
|
36
|
+
* 负责处理版本化的存储键名生成和数据迁移
|
|
37
|
+
*/
|
|
38
|
+
export class StorageKeyManager {
|
|
39
|
+
/**
|
|
40
|
+
* 获取当前版本的存储键名
|
|
41
|
+
*/
|
|
42
|
+
private getCurrentVersionKey(storeId: string): string {
|
|
43
|
+
return StorageConfig.generateStorageKey(storeId)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 检查当前版本的数据是否存在
|
|
48
|
+
*/
|
|
49
|
+
private hasCurrentVersionData(key: string): boolean {
|
|
50
|
+
return localStorage.getItem(key) !== null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 查找其他版本的同名存储键
|
|
55
|
+
*/
|
|
56
|
+
private findExistingKey(storeId: string): string | null {
|
|
57
|
+
const storageKeys = Object.keys(localStorage)
|
|
58
|
+
const pattern = StorageConfig.createKeyPattern(storeId)
|
|
59
|
+
|
|
60
|
+
return storageKeys.find((key) => pattern.test(key) && localStorage.getItem(key)) || null
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 将数据从旧版本迁移到当前版本
|
|
65
|
+
*/
|
|
66
|
+
private migrateData(fromKey: string, toKey: string): void {
|
|
67
|
+
try {
|
|
68
|
+
const existingData = localStorage.getItem(fromKey)
|
|
69
|
+
if (existingData) {
|
|
70
|
+
localStorage.setItem(toKey, existingData)
|
|
71
|
+
console.info(`[Storage] 已迁移数据: ${fromKey} → ${toKey}`)
|
|
72
|
+
}
|
|
73
|
+
} catch (error) {
|
|
74
|
+
console.warn(`[Storage] 数据迁移失败: ${fromKey}`, error)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 获取持久化存储的键名(支持自动数据迁移)
|
|
80
|
+
*/
|
|
81
|
+
getStorageKey(storeId: string): string {
|
|
82
|
+
const currentKey = this.getCurrentVersionKey(storeId)
|
|
83
|
+
|
|
84
|
+
// 优先使用当前版本的数据
|
|
85
|
+
if (this.hasCurrentVersionData(currentKey)) {
|
|
86
|
+
return currentKey
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 查找并迁移其他版本的数据
|
|
90
|
+
const existingKey = this.findExistingKey(storeId)
|
|
91
|
+
if (existingKey) {
|
|
92
|
+
this.migrateData(existingKey, currentKey)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return currentKey
|
|
96
|
+
}
|
|
97
|
+
}
|