mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 06:29:20 +08:00
update: 升级vben5.5.9到5.7.0
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
export * from './access';
|
||||
export * from './tabbar';
|
||||
export * from './timezone';
|
||||
export * from './user';
|
||||
|
||||
@@ -1,24 +1,35 @@
|
||||
import type { ComputedRef } from 'vue';
|
||||
import type { ComputedRef, VNode } from 'vue';
|
||||
import type {
|
||||
RouteLocationNormalized,
|
||||
RouteLocationNormalizedLoaded,
|
||||
RouteLocationNormalizedLoadedGeneric,
|
||||
Router,
|
||||
RouteRecordNormalized,
|
||||
} from 'vue-router';
|
||||
|
||||
import type { TabDefinition } from '@vben-core/typings';
|
||||
|
||||
import { toRaw } from 'vue';
|
||||
import { markRaw, toRaw } from 'vue';
|
||||
|
||||
import { preferences } from '@vben-core/preferences';
|
||||
import {
|
||||
openRouteInNewWindow,
|
||||
createStack,
|
||||
openWindow,
|
||||
Stack,
|
||||
startProgress,
|
||||
stopProgress,
|
||||
} from '@vben-core/shared/utils';
|
||||
|
||||
import { acceptHMRUpdate, defineStore } from 'pinia';
|
||||
|
||||
interface RouteCached {
|
||||
component: VNode;
|
||||
key: string;
|
||||
route: RouteLocationNormalizedLoadedGeneric;
|
||||
}
|
||||
|
||||
interface TabbarState {
|
||||
cachedRoutes: Map<string, RouteCached>;
|
||||
/**
|
||||
* @zh_CN 当前打开的标签页列表缓存
|
||||
*/
|
||||
@@ -47,8 +58,17 @@ interface TabbarState {
|
||||
* @zh_CN 更新时间,用于一些更新场景,使用watch深度监听的话,会损耗性能
|
||||
*/
|
||||
updateTime?: number;
|
||||
/**
|
||||
* @zh_CN 上一个标签页打开的标签
|
||||
*/
|
||||
visitHistory: Stack<string>;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh_CN 访问历史记录最大数量
|
||||
*/
|
||||
const MAX_VISIT_HISTORY = 50;
|
||||
|
||||
/**
|
||||
* @zh_CN 访问权限相关
|
||||
*/
|
||||
@@ -62,6 +82,9 @@ export const useTabbarStore = defineStore('core-tabbar', {
|
||||
this.tabs = this.tabs.filter(
|
||||
(item) => !keySet.has(getTabKeyFromTab(item)),
|
||||
);
|
||||
if (isVisitHistory()) {
|
||||
this.visitHistory.remove(...keys);
|
||||
}
|
||||
|
||||
await this.updateCacheTabs();
|
||||
},
|
||||
@@ -166,6 +189,10 @@ export const useTabbarStore = defineStore('core-tabbar', {
|
||||
this.tabs.splice(tabIndex, 1, mergedTab);
|
||||
}
|
||||
this.updateCacheTabs();
|
||||
// 添加访问历史记录
|
||||
if (isVisitHistory()) {
|
||||
this.visitHistory.push(tab.key as string);
|
||||
}
|
||||
return tab;
|
||||
},
|
||||
/**
|
||||
@@ -174,6 +201,12 @@ export const useTabbarStore = defineStore('core-tabbar', {
|
||||
async closeAllTabs(router: Router) {
|
||||
const newTabs = this.tabs.filter((tab) => isAffixTab(tab));
|
||||
this.tabs = newTabs.length > 0 ? newTabs : [...this.tabs].splice(0, 1);
|
||||
// 设置访问历史记录
|
||||
if (isVisitHistory()) {
|
||||
this.visitHistory.retain(
|
||||
this.tabs.map((item) => getTabKeyFromTab(item)),
|
||||
);
|
||||
}
|
||||
await this._goToDefaultTab(router);
|
||||
this.updateCacheTabs();
|
||||
},
|
||||
@@ -249,12 +282,44 @@ export const useTabbarStore = defineStore('core-tabbar', {
|
||||
*/
|
||||
async closeTab(tab: TabDefinition, router: Router) {
|
||||
const { currentRoute } = router;
|
||||
const currentTabKey = getTabKey(currentRoute.value);
|
||||
// 关闭不是激活选项卡
|
||||
if (getTabKey(currentRoute.value) !== getTabKeyFromTab(tab)) {
|
||||
if (currentTabKey !== getTabKeyFromTab(tab)) {
|
||||
this._close(tab);
|
||||
this.updateCacheTabs();
|
||||
// 移除访问历史记录
|
||||
if (isVisitHistory()) {
|
||||
this.visitHistory.remove(getTabKeyFromTab(tab));
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (this.getTabs.length <= 1) {
|
||||
console.error('Failed to close the tab; only one tab remains open.');
|
||||
return;
|
||||
}
|
||||
// 从访问历史记录中移除当前关闭的tab
|
||||
if (isVisitHistory()) {
|
||||
this.visitHistory.remove(currentTabKey);
|
||||
this._close(tab);
|
||||
|
||||
let previousTab: TabDefinition | undefined;
|
||||
let previousTabKey: string | undefined;
|
||||
while (true) {
|
||||
previousTabKey = this.visitHistory.pop();
|
||||
if (!previousTabKey) {
|
||||
break;
|
||||
}
|
||||
previousTab = this.getTabByKey(previousTabKey);
|
||||
if (previousTab) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
await (previousTab
|
||||
? this._goToTab(previousTab, router)
|
||||
: this._goToDefaultTab(router));
|
||||
return;
|
||||
}
|
||||
// 未开启访问历史记录,直接跳转下一个或上一个tab
|
||||
const index = this.getTabs.findIndex(
|
||||
(item) => getTabKeyFromTab(item) === getTabKey(currentRoute.value),
|
||||
);
|
||||
@@ -270,8 +335,6 @@ export const useTabbarStore = defineStore('core-tabbar', {
|
||||
} else if (before) {
|
||||
this._close(tab);
|
||||
await this._goToTab(before, router);
|
||||
} else {
|
||||
console.error('Failed to close the tab; only one tab remains open.');
|
||||
}
|
||||
},
|
||||
|
||||
@@ -308,8 +371,9 @@ export const useTabbarStore = defineStore('core-tabbar', {
|
||||
* @zh_CN 新窗口打开标签页
|
||||
* @param tab
|
||||
*/
|
||||
async openTabInNewWindow(tab: TabDefinition) {
|
||||
openRouteInNewWindow(tab.fullPath || tab.path);
|
||||
async openTabInNewWindow(tab: TabDefinition, router: Router) {
|
||||
const href = router.resolve(tab.fullPath || tab.path).href;
|
||||
openWindow(new URL(href, location.href).href, { target: '_blank' });
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -499,12 +563,31 @@ export const useTabbarStore = defineStore('core-tabbar', {
|
||||
}
|
||||
this.cachedTabs = cacheMap;
|
||||
},
|
||||
/**
|
||||
* 添加缓存的route
|
||||
* @param component
|
||||
* @param route
|
||||
*/
|
||||
addCachedRoute(component: VNode, route: RouteLocationNormalizedLoaded) {
|
||||
const key = getTabKey(route);
|
||||
if (this.cachedRoutes.has(key)) {
|
||||
return;
|
||||
}
|
||||
this.cachedRoutes.set(key, {
|
||||
key,
|
||||
component: markRaw(component),
|
||||
route: markRaw(route),
|
||||
});
|
||||
},
|
||||
removeCachedRoute(key: string) {
|
||||
this.cachedRoutes.delete(key);
|
||||
},
|
||||
},
|
||||
getters: {
|
||||
affixTabs(): TabDefinition[] {
|
||||
const affixTabs = this.tabs.filter((tab) => isAffixTab(tab));
|
||||
|
||||
return affixTabs.sort((a, b) => {
|
||||
return [...affixTabs].sort((a, b) => {
|
||||
const orderA = (a.meta?.affixTabOrder ?? 0) as number;
|
||||
const orderB = (b.meta?.affixTabOrder ?? 0) as number;
|
||||
return orderA - orderB;
|
||||
@@ -523,15 +606,37 @@ export const useTabbarStore = defineStore('core-tabbar', {
|
||||
const normalTabs = this.tabs.filter((tab) => !isAffixTab(tab));
|
||||
return [...this.affixTabs, ...normalTabs].filter(Boolean);
|
||||
},
|
||||
getCachedRoutes(): Map<string, RouteCached> {
|
||||
return this.cachedRoutes;
|
||||
},
|
||||
},
|
||||
persist: [
|
||||
// tabs不需要保存在localStorage
|
||||
{
|
||||
pick: ['tabs'],
|
||||
pick: ['tabs', 'visitHistory'],
|
||||
storage: sessionStorage,
|
||||
serializer: {
|
||||
serialize: JSON.stringify,
|
||||
deserialize(value: string) {
|
||||
const parsed = JSON.parse(value);
|
||||
// Stack 类实例经 JSON 序列化后会变成普通对象 {dedup, items, maxSize},
|
||||
// 丢失所有方法和 getter,需要重新构建 Stack 实例
|
||||
if (parsed.visitHistory && !(parsed.visitHistory instanceof Stack)) {
|
||||
const raw = parsed.visitHistory;
|
||||
const stack = createStack<string>(true, MAX_VISIT_HISTORY);
|
||||
if (Array.isArray(raw.items)) {
|
||||
stack.push(...raw.items);
|
||||
}
|
||||
parsed.visitHistory = stack;
|
||||
}
|
||||
return parsed;
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
state: (): TabbarState => ({
|
||||
visitHistory: createStack<string>(true, MAX_VISIT_HISTORY),
|
||||
cachedRoutes: new Map<string, RouteCached>(),
|
||||
cachedTabs: new Set(),
|
||||
dragEndIndex: 0,
|
||||
excludeCachedTabs: new Set(),
|
||||
@@ -579,7 +684,6 @@ function cloneTab(route: TabDefinition): TabDefinition {
|
||||
meta: {
|
||||
...meta,
|
||||
newTabTitle: meta.newTabTitle,
|
||||
tabTitle: meta.tabTitle,
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -629,6 +733,13 @@ function getTabKey(tab: RouteLocationNormalized | RouteRecordNormalized) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh_CN 是否开启访问历史记录
|
||||
*/
|
||||
function isVisitHistory() {
|
||||
return preferences.tabbar.visitHistory;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从tab获取tab页的key
|
||||
* 如果tab没有key,那么就从route获取key
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
import { ref, unref } from 'vue';
|
||||
|
||||
import { DEFAULT_TIME_ZONE_OPTIONS } from '@vben-core/preferences';
|
||||
import {
|
||||
getCurrentTimezone,
|
||||
setCurrentTimezone,
|
||||
} from '@vben-core/shared/utils';
|
||||
|
||||
import { acceptHMRUpdate, defineStore } from 'pinia';
|
||||
|
||||
interface TimezoneHandler {
|
||||
getTimezone?: () => Promise<null | string | undefined>;
|
||||
getTimezoneOptions?: () => Promise<
|
||||
{
|
||||
label: string;
|
||||
value: string;
|
||||
}[]
|
||||
>;
|
||||
setTimezone?: (timezone: string) => Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认时区处理模块
|
||||
* 时区存储基于pinia存储插件
|
||||
*/
|
||||
const getDefaultTimezoneHandler = (): TimezoneHandler => {
|
||||
return {
|
||||
getTimezoneOptions: () => {
|
||||
return Promise.resolve(
|
||||
DEFAULT_TIME_ZONE_OPTIONS.map((item) => {
|
||||
return {
|
||||
label: item.label,
|
||||
value: item.timezone,
|
||||
};
|
||||
}),
|
||||
);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* 自定义时区处理模块
|
||||
*/
|
||||
let customTimezoneHandler: null | Partial<TimezoneHandler> = null;
|
||||
const setTimezoneHandler = (handler: Partial<TimezoneHandler>) => {
|
||||
customTimezoneHandler = handler;
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取时区处理模块
|
||||
*/
|
||||
const getTimezoneHandler = () => {
|
||||
return {
|
||||
...getDefaultTimezoneHandler(),
|
||||
...customTimezoneHandler,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* timezone支持模块
|
||||
*/
|
||||
const useTimezoneStore = defineStore(
|
||||
'core-timezone',
|
||||
() => {
|
||||
const timezoneRef = ref(getCurrentTimezone());
|
||||
|
||||
/**
|
||||
* 初始化时区
|
||||
* Initialize the timezone
|
||||
*/
|
||||
async function initTimezone() {
|
||||
const timezoneHandler = getTimezoneHandler();
|
||||
const timezone = await timezoneHandler.getTimezone?.();
|
||||
if (timezone) {
|
||||
timezoneRef.value = timezone;
|
||||
}
|
||||
// 设置dayjs默认时区
|
||||
setCurrentTimezone(unref(timezoneRef));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置时区
|
||||
* Set the timezone
|
||||
* @param timezone 时区字符串
|
||||
*/
|
||||
async function setTimezone(timezone: string) {
|
||||
const timezoneHandler = getTimezoneHandler();
|
||||
await timezoneHandler.setTimezone?.(timezone);
|
||||
timezoneRef.value = timezone;
|
||||
// 设置dayjs默认时区
|
||||
setCurrentTimezone(timezone);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取时区选项
|
||||
* Get the timezone options
|
||||
*/
|
||||
async function getTimezoneOptions() {
|
||||
const timezoneHandler = getTimezoneHandler();
|
||||
return (await timezoneHandler.getTimezoneOptions?.()) || [];
|
||||
}
|
||||
|
||||
initTimezone().catch((error) => {
|
||||
console.error('Failed to initialize timezone during store setup:', error);
|
||||
});
|
||||
|
||||
function $reset() {
|
||||
timezoneRef.value = getCurrentTimezone();
|
||||
}
|
||||
|
||||
return {
|
||||
timezone: timezoneRef,
|
||||
setTimezone,
|
||||
getTimezoneOptions,
|
||||
$reset,
|
||||
};
|
||||
},
|
||||
{
|
||||
persist: {
|
||||
// 持久化
|
||||
pick: ['timezone'],
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
export { setTimezoneHandler, useTimezoneStore };
|
||||
|
||||
// 解决热更新问题
|
||||
const hot = import.meta.hot;
|
||||
if (hot) {
|
||||
hot.accept(acceptHMRUpdate(useTimezoneStore, hot));
|
||||
}
|
||||
@@ -7,6 +7,28 @@ import SecureLS from 'secure-ls';
|
||||
|
||||
let pinia: Pinia;
|
||||
|
||||
type SecureLSStorage = {
|
||||
get(key: string): any;
|
||||
set(key: string, value: unknown): void;
|
||||
};
|
||||
|
||||
type SecureLSCtor = new (config?: {
|
||||
encodingType?: string;
|
||||
encryptionSecret?: string;
|
||||
isCompression?: boolean;
|
||||
metaKey?: string;
|
||||
}) => SecureLSStorage;
|
||||
|
||||
const secureLSModule = SecureLS as unknown as {
|
||||
default?: SecureLSCtor;
|
||||
SecureLS?: SecureLSCtor;
|
||||
};
|
||||
|
||||
const SecureLSConstructor =
|
||||
secureLSModule.default ??
|
||||
secureLSModule.SecureLS ??
|
||||
(SecureLS as unknown as SecureLSCtor);
|
||||
|
||||
export interface InitStoreOptions {
|
||||
/**
|
||||
* @zh_CN 应用名,由于 @vben/stores 是公用的,后续可能有多个app,为了防止多个app缓存冲突,可在这里配置应用名,应用名将被用于持久化的前缀
|
||||
@@ -21,11 +43,10 @@ export async function initStores(app: App, options: InitStoreOptions) {
|
||||
const { createPersistedState } = await import('pinia-plugin-persistedstate');
|
||||
pinia = createPinia();
|
||||
const { namespace } = options;
|
||||
const ls = new SecureLS({
|
||||
const ls = new SecureLSConstructor({
|
||||
encodingType: 'aes',
|
||||
encryptionSecret: import.meta.env.VITE_APP_STORE_SECURE_KEY,
|
||||
isCompression: true,
|
||||
// @ts-ignore secure-ls does not have a type definition for this
|
||||
metaKey: `${namespace}-secure-meta`,
|
||||
});
|
||||
pinia.use(
|
||||
|
||||
Reference in New Issue
Block a user