cosey 0.10.8 → 0.10.10
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/components/editor/hooks/useHistoryColor.js +1 -2
- package/components/highlight/highlight.api.js +15 -0
- package/components/upload/upload.api.js +1 -1
- package/config/api.d.ts +5 -5
- package/config/index.d.ts +6 -24
- package/config/index.js +10 -14
- package/config/root-config-provider.js +2 -2
- package/config/upload.d.ts +1 -1
- package/hooks/index.d.ts +0 -1
- package/hooks/index.js +0 -1
- package/hooks/useColorScheme.js +1 -2
- package/index.d.ts +8 -12
- package/index.js +21 -34
- package/layout/index.d.ts +1 -3
- package/layout/index.js +2 -61
- package/locale/index.d.ts +3 -3
- package/locale/index.js +6 -7
- package/package.json +2 -2
- package/persist/index.d.ts +4 -0
- package/persist/index.js +11 -0
- package/request/Http.d.ts +3 -1
- package/request/Http.js +1 -0
- package/request/index.d.ts +5 -2
- package/request/index.js +97 -2
- package/router/guard/auth.js +3 -4
- package/router/guard/pageTitle.js +2 -2
- package/router/index.d.ts +4 -2
- package/router/index.js +9 -4
- package/store/index.d.ts +4 -5
- package/store/index.js +3 -13
- package/store/layout.d.ts +57 -25
- package/store/layout.js +19 -14
- package/store/pinia.d.ts +4 -0
- package/store/pinia.js +11 -0
- package/store/plugin.js +5 -6
- package/store/user.d.ts +33 -2
- package/store/user.js +22 -30
- package/hooks/usePersist.d.ts +0 -9
- package/hooks/usePersist.js +0 -17
- package/request/useRequest.d.ts +0 -13
- package/request/useRequest.js +0 -109
- package/store/appearance.d.ts +0 -7
- package/store/appearance.js +0 -9
package/router/guard/auth.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { globalConfig } from '../../config/index.js';
|
|
2
2
|
import { useUserStore } from '../../store/user.js';
|
|
3
3
|
import { TOKEN_NAME, ROUTER_TO } from '../../constant.js';
|
|
4
|
-
import {
|
|
4
|
+
import { persist } from '../../persist/index.js';
|
|
5
5
|
|
|
6
6
|
function registerAuthGuard(router) {
|
|
7
7
|
let firstTimeAddRoutes = false;
|
|
8
8
|
router.beforeEach(async (to) => {
|
|
9
|
-
const
|
|
10
|
-
const routerConfig = useGlobalConfig().router;
|
|
9
|
+
const { router: routerConfig } = globalConfig;
|
|
11
10
|
const token = persist.get(TOKEN_NAME);
|
|
12
11
|
const userStore = useUserStore();
|
|
13
12
|
if (to.path === "/" && routerConfig.homePath !== "/") {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { globalConfig } from '../../config/index.js';
|
|
2
2
|
|
|
3
3
|
function registerPageTitleGuard(router) {
|
|
4
4
|
router.afterEach((to) => {
|
|
5
|
-
const siteConfig =
|
|
5
|
+
const { site: siteConfig } = globalConfig;
|
|
6
6
|
const title = to.meta.title ? to.meta.title + " - " + siteConfig.name : siteConfig.name;
|
|
7
7
|
document.title = title;
|
|
8
8
|
});
|
package/router/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { type RouteRecordRaw, type RouterOptions, type RouterHistory } from 'vue-router';
|
|
1
|
+
import { type RouteRecordRaw, type RouterOptions, type RouterHistory, Router } from 'vue-router';
|
|
2
2
|
import type { AnyAbility } from '@casl/ability';
|
|
3
|
+
import { App } from 'vue';
|
|
3
4
|
export { type MenuItem, type MenuNode, getMenus, getMenusMap, getMenuPathKeys, getBreadcrumbRoutes, } from './menus';
|
|
4
5
|
export { defineRoute, defineRoutes, mergeRouteModules } from './utils';
|
|
6
|
+
export declare let router: Router;
|
|
5
7
|
export interface CoseyRouterOptions extends Omit<RouterOptions, 'routes' | 'history'> {
|
|
6
8
|
history?: RouterHistory;
|
|
7
9
|
dynamic?: RouteRecordRaw[];
|
|
@@ -10,7 +12,7 @@ export interface CoseyRouterOptions extends Omit<RouterOptions, 'routes' | 'hist
|
|
|
10
12
|
}
|
|
11
13
|
export declare const getAllDynamicRoutes: () => RouteRecordRaw[];
|
|
12
14
|
export declare const getAllStaticRoutes: () => RouteRecordRaw[];
|
|
13
|
-
export declare function
|
|
15
|
+
export declare function launchRouter(app: App, options?: CoseyRouterOptions): void;
|
|
14
16
|
declare module 'vue-router' {
|
|
15
17
|
interface RouteMeta {
|
|
16
18
|
title?: string;
|
package/router/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { createWebHashHistory, createRouter } from 'vue-router';
|
|
2
2
|
import { staticRoutes } from './routes/index.js';
|
|
3
3
|
import { cloneDeep } from 'lodash-es';
|
|
4
|
+
import { registerRouterGuard } from './guard/index.js';
|
|
4
5
|
export { getBreadcrumbRoutes, getMenuPathKeys, getMenus, getMenusMap } from './menus/index.js';
|
|
5
6
|
export { defineRoute, defineRoutes, mergeRouteModules } from './utils.js';
|
|
6
7
|
|
|
8
|
+
let router;
|
|
7
9
|
let customDynamicRoutes = [];
|
|
8
10
|
let customStaticRoutes = [];
|
|
9
11
|
const getAllDynamicRoutes = () => {
|
|
@@ -12,7 +14,7 @@ const getAllDynamicRoutes = () => {
|
|
|
12
14
|
const getAllStaticRoutes = () => {
|
|
13
15
|
return [...staticRoutes, ...customStaticRoutes];
|
|
14
16
|
};
|
|
15
|
-
function
|
|
17
|
+
function launchRouter(app, options = {}) {
|
|
16
18
|
const { static: static$ = [], dynamic = [], listening = true, ...restOptions } = options;
|
|
17
19
|
customStaticRoutes = static$;
|
|
18
20
|
customDynamicRoutes = dynamic;
|
|
@@ -27,9 +29,12 @@ function createCoseyRouter(options = {}) {
|
|
|
27
29
|
history: restOptions.history || createWebHashHistory()
|
|
28
30
|
}
|
|
29
31
|
);
|
|
30
|
-
|
|
32
|
+
router = createRouter(mergedOptions);
|
|
31
33
|
router.listening = listening;
|
|
32
|
-
|
|
34
|
+
if (listening) {
|
|
35
|
+
registerRouterGuard(router);
|
|
36
|
+
}
|
|
37
|
+
app.use(router);
|
|
33
38
|
}
|
|
34
39
|
|
|
35
|
-
export {
|
|
40
|
+
export { getAllDynamicRoutes, getAllStaticRoutes, launchRouter, router };
|
package/store/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
export
|
|
5
|
-
export { useUserStore, useAppearanceStore, useLayoutStore };
|
|
1
|
+
import { pinia, launchStore } from './pinia';
|
|
2
|
+
import { useLayoutStore, useOuterLayoutStore } from './layout';
|
|
3
|
+
import { useUserStore, useOuterUserStore } from './user';
|
|
4
|
+
export { pinia, launchStore, useUserStore, useOuterUserStore, useLayoutStore, useOuterLayoutStore };
|
package/store/index.js
CHANGED
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export { useLayoutStore } from './layout.js';
|
|
3
|
-
export { useUserStore } from './user.js';
|
|
4
|
-
export { useAppearanceStore } from './appearance.js';
|
|
5
|
-
import { piniaPluginPersist } from './plugin.js';
|
|
6
|
-
|
|
7
|
-
function createPiniaStore() {
|
|
8
|
-
const pinia = createPinia();
|
|
9
|
-
pinia.use(piniaPluginPersist);
|
|
10
|
-
return pinia;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export { createPiniaStore };
|
|
1
|
+
export { launchStore, pinia } from './pinia.js';
|
|
2
|
+
export { useLayoutStore, useOuterLayoutStore } from './layout.js';
|
|
3
|
+
export { useOuterUserStore, useUserStore } from './user.js';
|
package/store/layout.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { RouteMeta } from 'vue-router';
|
|
1
|
+
import { type RouteMeta } from 'vue-router';
|
|
2
|
+
import { type StoreGeneric } from 'pinia';
|
|
2
3
|
export type LayoutMenuType = 'vertical' | 'biserial' | 'horizontal' | 'horizontal-vertical' | 'horizontal-biserial';
|
|
3
4
|
export interface LayoutTab {
|
|
4
5
|
name: string;
|
|
@@ -15,18 +16,18 @@ export declare const useLayoutStore: import("pinia").StoreDefinition<"cosey-layo
|
|
|
15
16
|
isHorizontal: import("vue").ComputedRef<boolean>;
|
|
16
17
|
isHorizontalVertical: import("vue").ComputedRef<boolean>;
|
|
17
18
|
isHorizontalBiserial: import("vue").ComputedRef<boolean>;
|
|
18
|
-
menus: import("vue").ComputedRef<import("
|
|
19
|
-
menusMap: import("vue").ComputedRef<Record<string | number, import("
|
|
20
|
-
firstLevelMenus: import("vue").ComputedRef<import("
|
|
19
|
+
menus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
20
|
+
menusMap: import("vue").ComputedRef<Record<string | number, import("../router").MenuNode>>;
|
|
21
|
+
firstLevelMenus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
21
22
|
firstLevelActive: import("vue").Ref<string, string>;
|
|
22
|
-
secondLevelMenus: import("vue").ComputedRef<import("
|
|
23
|
+
secondLevelMenus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
23
24
|
secondLevelActive: import("vue").Ref<string, string>;
|
|
24
|
-
thirdLevelMenus: import("vue").ComputedRef<import("
|
|
25
|
-
topMenus: import("vue").ComputedRef<import("
|
|
25
|
+
thirdLevelMenus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
26
|
+
topMenus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
26
27
|
topActive: import("vue").Ref<string, string>;
|
|
27
|
-
snugMenus: import("vue").ComputedRef<import("
|
|
28
|
+
snugMenus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
28
29
|
snugActive: import("vue").WritableComputedRef<string, string>;
|
|
29
|
-
defaultMenus: import("vue").ComputedRef<import("
|
|
30
|
+
defaultMenus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
30
31
|
tabbarVisible: import("vue").Ref<boolean, boolean>;
|
|
31
32
|
activeTab: import("vue").Ref<string, string>;
|
|
32
33
|
refreshing: import("vue").Ref<boolean, boolean>;
|
|
@@ -95,18 +96,18 @@ export declare const useLayoutStore: import("pinia").StoreDefinition<"cosey-layo
|
|
|
95
96
|
isHorizontal: import("vue").ComputedRef<boolean>;
|
|
96
97
|
isHorizontalVertical: import("vue").ComputedRef<boolean>;
|
|
97
98
|
isHorizontalBiserial: import("vue").ComputedRef<boolean>;
|
|
98
|
-
menus: import("vue").ComputedRef<import("
|
|
99
|
-
menusMap: import("vue").ComputedRef<Record<string | number, import("
|
|
100
|
-
firstLevelMenus: import("vue").ComputedRef<import("
|
|
99
|
+
menus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
100
|
+
menusMap: import("vue").ComputedRef<Record<string | number, import("../router").MenuNode>>;
|
|
101
|
+
firstLevelMenus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
101
102
|
firstLevelActive: import("vue").Ref<string, string>;
|
|
102
|
-
secondLevelMenus: import("vue").ComputedRef<import("
|
|
103
|
+
secondLevelMenus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
103
104
|
secondLevelActive: import("vue").Ref<string, string>;
|
|
104
|
-
thirdLevelMenus: import("vue").ComputedRef<import("
|
|
105
|
-
topMenus: import("vue").ComputedRef<import("
|
|
105
|
+
thirdLevelMenus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
106
|
+
topMenus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
106
107
|
topActive: import("vue").Ref<string, string>;
|
|
107
|
-
snugMenus: import("vue").ComputedRef<import("
|
|
108
|
+
snugMenus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
108
109
|
snugActive: import("vue").WritableComputedRef<string, string>;
|
|
109
|
-
defaultMenus: import("vue").ComputedRef<import("
|
|
110
|
+
defaultMenus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
110
111
|
tabbarVisible: import("vue").Ref<boolean, boolean>;
|
|
111
112
|
activeTab: import("vue").Ref<string, string>;
|
|
112
113
|
refreshing: import("vue").Ref<boolean, boolean>;
|
|
@@ -175,18 +176,18 @@ export declare const useLayoutStore: import("pinia").StoreDefinition<"cosey-layo
|
|
|
175
176
|
isHorizontal: import("vue").ComputedRef<boolean>;
|
|
176
177
|
isHorizontalVertical: import("vue").ComputedRef<boolean>;
|
|
177
178
|
isHorizontalBiserial: import("vue").ComputedRef<boolean>;
|
|
178
|
-
menus: import("vue").ComputedRef<import("
|
|
179
|
-
menusMap: import("vue").ComputedRef<Record<string | number, import("
|
|
180
|
-
firstLevelMenus: import("vue").ComputedRef<import("
|
|
179
|
+
menus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
180
|
+
menusMap: import("vue").ComputedRef<Record<string | number, import("../router").MenuNode>>;
|
|
181
|
+
firstLevelMenus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
181
182
|
firstLevelActive: import("vue").Ref<string, string>;
|
|
182
|
-
secondLevelMenus: import("vue").ComputedRef<import("
|
|
183
|
+
secondLevelMenus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
183
184
|
secondLevelActive: import("vue").Ref<string, string>;
|
|
184
|
-
thirdLevelMenus: import("vue").ComputedRef<import("
|
|
185
|
-
topMenus: import("vue").ComputedRef<import("
|
|
185
|
+
thirdLevelMenus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
186
|
+
topMenus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
186
187
|
topActive: import("vue").Ref<string, string>;
|
|
187
|
-
snugMenus: import("vue").ComputedRef<import("
|
|
188
|
+
snugMenus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
188
189
|
snugActive: import("vue").WritableComputedRef<string, string>;
|
|
189
|
-
defaultMenus: import("vue").ComputedRef<import("
|
|
190
|
+
defaultMenus: import("vue").ComputedRef<import("../router").MenuItem[]>;
|
|
190
191
|
tabbarVisible: import("vue").Ref<boolean, boolean>;
|
|
191
192
|
activeTab: import("vue").Ref<string, string>;
|
|
192
193
|
refreshing: import("vue").Ref<boolean, boolean>;
|
|
@@ -245,3 +246,34 @@ export declare const useLayoutStore: import("pinia").StoreDefinition<"cosey-layo
|
|
|
245
246
|
tabbarHeight: import("vue").Ref<number, number>;
|
|
246
247
|
headerHeight: import("vue").ComputedRef<number>;
|
|
247
248
|
}, "reload" | "updateKeepAliveInclude">>;
|
|
249
|
+
export declare const useOuterLayoutStore: (hot?: StoreGeneric) => import("pinia").Store<"cosey-user", Pick<{
|
|
250
|
+
dynamicRoutes: import("vue").ShallowRef<any[], any[]>;
|
|
251
|
+
userInfo: import("vue").Ref<import("./user").UserInfo | undefined, import("./user").UserInfo | undefined>;
|
|
252
|
+
requestedUserInfo: import("vue").Ref<boolean, boolean>;
|
|
253
|
+
login: (data: any) => Promise<void>;
|
|
254
|
+
getUserInfo: () => Promise<import("./user").UserInfo | undefined>;
|
|
255
|
+
changePassword: (data: any) => Promise<void>;
|
|
256
|
+
setAuthorization: () => Promise<void>;
|
|
257
|
+
addDynamicRoutes: () => Promise<void>;
|
|
258
|
+
logout: (lastPath?: string) => Promise<void>;
|
|
259
|
+
}, "dynamicRoutes" | "userInfo" | "requestedUserInfo">, Pick<{
|
|
260
|
+
dynamicRoutes: import("vue").ShallowRef<any[], any[]>;
|
|
261
|
+
userInfo: import("vue").Ref<import("./user").UserInfo | undefined, import("./user").UserInfo | undefined>;
|
|
262
|
+
requestedUserInfo: import("vue").Ref<boolean, boolean>;
|
|
263
|
+
login: (data: any) => Promise<void>;
|
|
264
|
+
getUserInfo: () => Promise<import("./user").UserInfo | undefined>;
|
|
265
|
+
changePassword: (data: any) => Promise<void>;
|
|
266
|
+
setAuthorization: () => Promise<void>;
|
|
267
|
+
addDynamicRoutes: () => Promise<void>;
|
|
268
|
+
logout: (lastPath?: string) => Promise<void>;
|
|
269
|
+
}, never>, Pick<{
|
|
270
|
+
dynamicRoutes: import("vue").ShallowRef<any[], any[]>;
|
|
271
|
+
userInfo: import("vue").Ref<import("./user").UserInfo | undefined, import("./user").UserInfo | undefined>;
|
|
272
|
+
requestedUserInfo: import("vue").Ref<boolean, boolean>;
|
|
273
|
+
login: (data: any) => Promise<void>;
|
|
274
|
+
getUserInfo: () => Promise<import("./user").UserInfo | undefined>;
|
|
275
|
+
changePassword: (data: any) => Promise<void>;
|
|
276
|
+
setAuthorization: () => Promise<void>;
|
|
277
|
+
addDynamicRoutes: () => Promise<void>;
|
|
278
|
+
logout: (lastPath?: string) => Promise<void>;
|
|
279
|
+
}, "changePassword" | "login" | "logout" | "getUserInfo" | "setAuthorization" | "addDynamicRoutes">>;
|
package/store/layout.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { defineStore } from 'pinia';
|
|
2
|
-
import { useRoute } from 'vue-router';
|
|
3
|
-
import { getAllStaticRoutes } from '../router/index.js';
|
|
4
|
-
import { useGlobalConfig } from '../config/index.js';
|
|
5
1
|
import { ref, watch, computed, nextTick } from 'vue';
|
|
6
|
-
import {
|
|
2
|
+
import { defineStore } from 'pinia';
|
|
3
|
+
import { router, getAllStaticRoutes } from '../router/index.js';
|
|
4
|
+
import { useOuterUserStore, useUserStore } from './user.js';
|
|
5
|
+
import { pinia } from './pinia.js';
|
|
6
|
+
import { globalConfig } from '../config/index.js';
|
|
7
7
|
import { useWindowResize } from '../hooks/useWindowResize.js';
|
|
8
8
|
import { getMenus, getMenusMap, getBreadcrumbRoutes } from '../router/menus/index.js';
|
|
9
9
|
|
|
10
10
|
const useLayoutStore = defineStore(
|
|
11
11
|
"cosey-layout",
|
|
12
12
|
() => {
|
|
13
|
-
const { layout: layoutConfig } =
|
|
14
|
-
const
|
|
15
|
-
const userStore =
|
|
13
|
+
const { layout: layoutConfig } = globalConfig;
|
|
14
|
+
const currentRoute = router.currentRoute;
|
|
15
|
+
const userStore = useOuterUserStore();
|
|
16
16
|
const sidebarVisible = ref(layoutConfig.sidebarVisible);
|
|
17
17
|
const collapse = ref(layoutConfig.collapse);
|
|
18
18
|
const isMobile = ref(false);
|
|
@@ -82,8 +82,8 @@ const useLayoutStore = defineStore(
|
|
|
82
82
|
}
|
|
83
83
|
return menuType.value === "biserial" || menuType.value === "horizontal-vertical" ? secondLevelMenus.value : menuType.value === "horizontal-biserial" ? thirdLevelMenus.value : firstLevelMenus.value;
|
|
84
84
|
});
|
|
85
|
-
watch([() =>
|
|
86
|
-
const node = menusMap.value[
|
|
85
|
+
watch([() => currentRoute.value.name, menus], () => {
|
|
86
|
+
const node = menusMap.value[currentRoute.value.name];
|
|
87
87
|
if (node) {
|
|
88
88
|
const nodes = getBreadcrumbRoutes(node);
|
|
89
89
|
topActive.value = nodes[0].name;
|
|
@@ -125,12 +125,14 @@ const useLayoutStore = defineStore(
|
|
|
125
125
|
);
|
|
126
126
|
const reload = () => {
|
|
127
127
|
refreshing.value = true;
|
|
128
|
-
if (keepAliveInclude.value.includes(
|
|
129
|
-
keepAliveInclude.value = keepAliveInclude.value.filter(
|
|
128
|
+
if (keepAliveInclude.value.includes(currentRoute.value.name)) {
|
|
129
|
+
keepAliveInclude.value = keepAliveInclude.value.filter(
|
|
130
|
+
(item) => item !== currentRoute.value.name
|
|
131
|
+
);
|
|
130
132
|
}
|
|
131
133
|
nextTick(() => {
|
|
132
134
|
refreshing.value = false;
|
|
133
|
-
keepAliveInclude.value = [...keepAliveInclude.value,
|
|
135
|
+
keepAliveInclude.value = [...keepAliveInclude.value, currentRoute.value.name];
|
|
134
136
|
});
|
|
135
137
|
};
|
|
136
138
|
const asideWidth = ref(layoutConfig.asideWidth);
|
|
@@ -229,5 +231,8 @@ const useLayoutStore = defineStore(
|
|
|
229
231
|
}
|
|
230
232
|
}
|
|
231
233
|
);
|
|
234
|
+
const useOuterLayoutStore = (hot) => {
|
|
235
|
+
return useUserStore(pinia, hot);
|
|
236
|
+
};
|
|
232
237
|
|
|
233
|
-
export { useLayoutStore };
|
|
238
|
+
export { useLayoutStore, useOuterLayoutStore };
|
package/store/pinia.d.ts
ADDED
package/store/pinia.js
ADDED
package/store/plugin.js
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
import { watch } from 'vue';
|
|
2
|
-
import {
|
|
2
|
+
import { persist } from '../persist/index.js';
|
|
3
3
|
|
|
4
4
|
function piniaPluginPersist(context) {
|
|
5
5
|
const {
|
|
6
6
|
store,
|
|
7
|
-
options: { persist }
|
|
7
|
+
options: { persist: persistConfig }
|
|
8
8
|
} = context;
|
|
9
|
-
const pickKeys =
|
|
10
|
-
const persistIns = usePersist();
|
|
9
|
+
const pickKeys = persistConfig?.pick || [];
|
|
11
10
|
const persistKey = `Cosey:pinia:${store.$id}`;
|
|
12
|
-
const localData =
|
|
11
|
+
const localData = persist.get(persistKey) || {};
|
|
13
12
|
store.$patch(localData);
|
|
14
13
|
watch(
|
|
15
14
|
pickKeys.map((key) => () => store[key]),
|
|
16
15
|
() => {
|
|
17
|
-
|
|
16
|
+
persist.set(persistKey, Object.fromEntries(pickKeys.map((key) => [key, store[key]])));
|
|
18
17
|
}
|
|
19
18
|
);
|
|
20
19
|
}
|
package/store/user.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import { type StoreGeneric } from 'pinia';
|
|
2
|
+
export interface UserInfo {
|
|
2
3
|
id?: number;
|
|
3
4
|
username?: string;
|
|
4
5
|
nickname: string;
|
|
@@ -36,4 +37,34 @@ export declare const useUserStore: import("pinia").StoreDefinition<"cosey-user",
|
|
|
36
37
|
addDynamicRoutes: () => Promise<void>;
|
|
37
38
|
logout: (lastPath?: string) => Promise<void>;
|
|
38
39
|
}, "changePassword" | "login" | "logout" | "getUserInfo" | "setAuthorization" | "addDynamicRoutes">>;
|
|
39
|
-
export {
|
|
40
|
+
export declare const useOuterUserStore: (hot?: StoreGeneric) => import("pinia").Store<"cosey-user", Pick<{
|
|
41
|
+
dynamicRoutes: import("vue").ShallowRef<any[], any[]>;
|
|
42
|
+
userInfo: import("vue").Ref<UserInfo | undefined, UserInfo | undefined>;
|
|
43
|
+
requestedUserInfo: import("vue").Ref<boolean, boolean>;
|
|
44
|
+
login: (data: any) => Promise<void>;
|
|
45
|
+
getUserInfo: () => Promise<UserInfo | undefined>;
|
|
46
|
+
changePassword: (data: any) => Promise<void>;
|
|
47
|
+
setAuthorization: () => Promise<void>;
|
|
48
|
+
addDynamicRoutes: () => Promise<void>;
|
|
49
|
+
logout: (lastPath?: string) => Promise<void>;
|
|
50
|
+
}, "dynamicRoutes" | "userInfo" | "requestedUserInfo">, Pick<{
|
|
51
|
+
dynamicRoutes: import("vue").ShallowRef<any[], any[]>;
|
|
52
|
+
userInfo: import("vue").Ref<UserInfo | undefined, UserInfo | undefined>;
|
|
53
|
+
requestedUserInfo: import("vue").Ref<boolean, boolean>;
|
|
54
|
+
login: (data: any) => Promise<void>;
|
|
55
|
+
getUserInfo: () => Promise<UserInfo | undefined>;
|
|
56
|
+
changePassword: (data: any) => Promise<void>;
|
|
57
|
+
setAuthorization: () => Promise<void>;
|
|
58
|
+
addDynamicRoutes: () => Promise<void>;
|
|
59
|
+
logout: (lastPath?: string) => Promise<void>;
|
|
60
|
+
}, never>, Pick<{
|
|
61
|
+
dynamicRoutes: import("vue").ShallowRef<any[], any[]>;
|
|
62
|
+
userInfo: import("vue").Ref<UserInfo | undefined, UserInfo | undefined>;
|
|
63
|
+
requestedUserInfo: import("vue").Ref<boolean, boolean>;
|
|
64
|
+
login: (data: any) => Promise<void>;
|
|
65
|
+
getUserInfo: () => Promise<UserInfo | undefined>;
|
|
66
|
+
changePassword: (data: any) => Promise<void>;
|
|
67
|
+
setAuthorization: () => Promise<void>;
|
|
68
|
+
addDynamicRoutes: () => Promise<void>;
|
|
69
|
+
logout: (lastPath?: string) => Promise<void>;
|
|
70
|
+
}, "changePassword" | "login" | "logout" | "getUserInfo" | "setAuthorization" | "addDynamicRoutes">>;
|
package/store/user.js
CHANGED
|
@@ -1,43 +1,32 @@
|
|
|
1
1
|
import { shallowRef, ref } from 'vue';
|
|
2
|
-
import { useRoute, useRouter } from 'vue-router';
|
|
3
2
|
import { defineStore } from 'pinia';
|
|
4
|
-
import { getAllDynamicRoutes } from '../router/index.js';
|
|
3
|
+
import { getAllDynamicRoutes, router } from '../router/index.js';
|
|
5
4
|
import { TOKEN_NAME } from '../constant.js';
|
|
6
|
-
import { useGlobalConfig } from '../config/index.js';
|
|
7
5
|
import { NOT_FOUND_ROUTE_NAME, NotFoundRoute } from '../router/not-found.js';
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
6
|
+
import { persist } from '../persist/index.js';
|
|
7
|
+
import { pinia } from './pinia.js';
|
|
8
|
+
import { globalConfig } from '../config/index.js';
|
|
10
9
|
import { warningOnce } from '../utils/warning.js';
|
|
11
10
|
|
|
12
11
|
const useUserStore = defineStore("cosey-user", () => {
|
|
13
|
-
const
|
|
14
|
-
const router = useRouter();
|
|
15
|
-
const persist = usePersist();
|
|
16
|
-
const {
|
|
17
|
-
router: routerConfig,
|
|
18
|
-
api: apiConfig,
|
|
19
|
-
filterRoute,
|
|
20
|
-
defineAuthority
|
|
21
|
-
} = useGlobalConfig() || {};
|
|
22
|
-
const filterRouteHandler = isFunction(filterRoute) ? filterRoute : filterRoute.hook();
|
|
23
|
-
const defineAuthorityHandler = isFunction(defineAuthority) ? defineAuthority : defineAuthority.hook();
|
|
12
|
+
const { router: routerConfig, api: apiConfig, filterRoute, defineAuthority } = globalConfig;
|
|
24
13
|
if (!apiConfig?.login) {
|
|
25
14
|
warningOnce(!!apiConfig?.login, 'The "login" api is required.');
|
|
26
15
|
}
|
|
27
16
|
if (!apiConfig?.getUserInfo) {
|
|
28
17
|
warningOnce(!!apiConfig?.getUserInfo, 'The "getUserInfo" api is required.');
|
|
29
18
|
}
|
|
30
|
-
const loginApi = apiConfig?.login
|
|
31
|
-
const getUserInfoApi = apiConfig?.getUserInfo
|
|
32
|
-
const changePasswordApi = apiConfig?.changePassword
|
|
33
|
-
const logoutApi = apiConfig?.logout
|
|
19
|
+
const loginApi = apiConfig?.login;
|
|
20
|
+
const getUserInfoApi = apiConfig?.getUserInfo;
|
|
21
|
+
const changePasswordApi = apiConfig?.changePassword;
|
|
22
|
+
const logoutApi = apiConfig?.logout;
|
|
34
23
|
const dynamicRoutes = shallowRef([]);
|
|
35
24
|
const userInfo = ref();
|
|
36
25
|
const requestedUserInfo = ref(false);
|
|
37
26
|
const login = async (data) => {
|
|
38
27
|
await loginApi?.(data).then((token) => {
|
|
39
28
|
persist.set(TOKEN_NAME, token);
|
|
40
|
-
router.push(
|
|
29
|
+
router.push(router.currentRoute.value.query.redirect || routerConfig.homePath);
|
|
41
30
|
});
|
|
42
31
|
};
|
|
43
32
|
const getUserInfo = async () => {
|
|
@@ -58,12 +47,12 @@ const useUserStore = defineStore("cosey-user", () => {
|
|
|
58
47
|
});
|
|
59
48
|
};
|
|
60
49
|
const setAuthorization = async () => {
|
|
61
|
-
return
|
|
50
|
+
return defineAuthority(userInfo.value);
|
|
62
51
|
};
|
|
63
52
|
const mapRoute = (routes) => {
|
|
64
|
-
return routes.map((
|
|
65
|
-
const result =
|
|
66
|
-
const node = result === true ?
|
|
53
|
+
return routes.map((route) => {
|
|
54
|
+
const result = filterRoute(route);
|
|
55
|
+
const node = result === true ? route : result;
|
|
67
56
|
if (node && node.children && node.children.length) {
|
|
68
57
|
node.children = mapRoute(node.children);
|
|
69
58
|
}
|
|
@@ -76,8 +65,8 @@ const useUserStore = defineStore("cosey-user", () => {
|
|
|
76
65
|
const addDynamicRoutes = async () => {
|
|
77
66
|
const filteredRoutes = filterRoutes(getAllDynamicRoutes());
|
|
78
67
|
router.removeRoute(NOT_FOUND_ROUTE_NAME);
|
|
79
|
-
filteredRoutes.forEach((
|
|
80
|
-
router.addRoute(
|
|
68
|
+
filteredRoutes.forEach((route) => {
|
|
69
|
+
router.addRoute(route);
|
|
81
70
|
});
|
|
82
71
|
router.addRoute("Exception", NotFoundRoute);
|
|
83
72
|
dynamicRoutes.value = filteredRoutes;
|
|
@@ -92,8 +81,8 @@ const useUserStore = defineStore("cosey-user", () => {
|
|
|
92
81
|
redirect: lastPath
|
|
93
82
|
}
|
|
94
83
|
});
|
|
95
|
-
dynamicRoutes.value.forEach((
|
|
96
|
-
router.removeRoute(
|
|
84
|
+
dynamicRoutes.value.forEach((route) => {
|
|
85
|
+
router.removeRoute(route.name);
|
|
97
86
|
});
|
|
98
87
|
dynamicRoutes.value = [];
|
|
99
88
|
};
|
|
@@ -113,5 +102,8 @@ const useUserStore = defineStore("cosey-user", () => {
|
|
|
113
102
|
logout
|
|
114
103
|
};
|
|
115
104
|
});
|
|
105
|
+
const useOuterUserStore = (hot) => {
|
|
106
|
+
return useUserStore(pinia, hot);
|
|
107
|
+
};
|
|
116
108
|
|
|
117
|
-
export { useUserStore };
|
|
109
|
+
export { useOuterUserStore, useUserStore };
|
package/hooks/usePersist.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import Persist from '@gunny/persist';
|
|
2
|
-
import { type InjectionKey } from 'vue';
|
|
3
|
-
export declare const persistContextKey: InjectionKey<Persist>;
|
|
4
|
-
export declare const createPersist: (name: string, type: "local" | "session") => Persist;
|
|
5
|
-
/**
|
|
6
|
-
* 持久化数据,保存在硬盘里而不是在内存中,刷新页面后还能读取到数据。
|
|
7
|
-
* https://github.com/sutras/persist#readme
|
|
8
|
-
*/
|
|
9
|
-
export declare function usePersist(): Persist;
|
package/hooks/usePersist.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import Persist from '@gunny/persist';
|
|
2
|
-
import { inject } from 'vue';
|
|
3
|
-
|
|
4
|
-
const persistContextKey = Symbol("persistContext");
|
|
5
|
-
const createPersist = (name, type) => {
|
|
6
|
-
return new Persist(name, { type });
|
|
7
|
-
};
|
|
8
|
-
let defaultPersist;
|
|
9
|
-
function usePersist() {
|
|
10
|
-
let persist = inject(persistContextKey, defaultPersist);
|
|
11
|
-
if (!persist) {
|
|
12
|
-
persist = defaultPersist = createPersist("CoseyAdmin", "local");
|
|
13
|
-
}
|
|
14
|
-
return persist;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export { createPersist, persistContextKey, usePersist };
|
package/request/useRequest.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { type CreateAxiosDefaults } from 'axios';
|
|
2
|
-
import { Http } from './Http';
|
|
3
|
-
import { type HttpConfig } from '../config/http';
|
|
4
|
-
export declare function useRequest(config?: CreateAxiosDefaults, useHttpConfig?: HttpConfig): {
|
|
5
|
-
fn: <T extends (http: Http) => (...args: any[]) => any, R = {
|
|
6
|
-
(...args: Parameters<ReturnType<T>>): ReturnType<ReturnType<T>>;
|
|
7
|
-
abort: () => void;
|
|
8
|
-
}>(handler: T) => R;
|
|
9
|
-
map: <T extends Record<string, (http: Http) => (...args: any[]) => any>, R = { [K in keyof T]: {
|
|
10
|
-
(...args: Parameters<ReturnType<T[K]>>): ReturnType<ReturnType<T[K]>>;
|
|
11
|
-
abort: () => void;
|
|
12
|
-
}; }>(object: T) => R;
|
|
13
|
-
};
|
package/request/useRequest.js
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { useRouter, useRoute } from 'vue-router';
|
|
2
|
-
import axios, { AxiosError } from 'axios';
|
|
3
|
-
import { ElMessage } from 'element-plus';
|
|
4
|
-
import { merge, defaults, pick, get } from 'lodash-es';
|
|
5
|
-
import { useUserStore } from '../store/user.js';
|
|
6
|
-
import { TOKEN_NAME, ROUTER_TO } from '../constant.js';
|
|
7
|
-
import { Http } from './Http.js';
|
|
8
|
-
import { useGlobalConfig } from '../config/index.js';
|
|
9
|
-
import { usePersist } from '../hooks/usePersist.js';
|
|
10
|
-
import { isObject } from '../utils/is.js';
|
|
11
|
-
|
|
12
|
-
function useRequest(config = {}, useHttpConfig) {
|
|
13
|
-
const persist = usePersist();
|
|
14
|
-
const userStore = useUserStore();
|
|
15
|
-
const router = useRouter();
|
|
16
|
-
const route = useRoute();
|
|
17
|
-
const { http: httpConfig, router: routerConfig } = useGlobalConfig();
|
|
18
|
-
const handleError = (resData, mergedHttpConfig) => {
|
|
19
|
-
const httpPath = mergedHttpConfig.path;
|
|
20
|
-
const code = get(resData, httpPath.code);
|
|
21
|
-
const message = get(resData, httpPath.message) || "Error";
|
|
22
|
-
const data = httpPath.data ? get(resData, httpPath.data) : resData;
|
|
23
|
-
if (code !== mergedHttpConfig.code.success) {
|
|
24
|
-
ElMessage.error({
|
|
25
|
-
message,
|
|
26
|
-
duration: mergedHttpConfig.errorDuration
|
|
27
|
-
});
|
|
28
|
-
if (code === mergedHttpConfig.code.forbidden) {
|
|
29
|
-
router.push(routerConfig.homePath);
|
|
30
|
-
} else if (code === mergedHttpConfig.code.unauthorized) {
|
|
31
|
-
if (persist.get(TOKEN_NAME)) {
|
|
32
|
-
userStore.logout(persist.get(ROUTER_TO) || route.fullPath);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
return Promise.reject(new Error(message));
|
|
36
|
-
}
|
|
37
|
-
if (mergedHttpConfig.originalData) {
|
|
38
|
-
return resData;
|
|
39
|
-
}
|
|
40
|
-
return data;
|
|
41
|
-
};
|
|
42
|
-
const createAxios = (createHttpConfig) => {
|
|
43
|
-
const mergedHttpConfig = merge({}, httpConfig, useHttpConfig, createHttpConfig);
|
|
44
|
-
const axiosIns = axios.create(
|
|
45
|
-
defaults(config, pick(mergedHttpConfig, ["baseURL", "timeout", "headers"]))
|
|
46
|
-
);
|
|
47
|
-
axiosIns.interceptors.request.use(
|
|
48
|
-
(config2) => {
|
|
49
|
-
Object.assign(config2, mergedHttpConfig.headers);
|
|
50
|
-
const token = persist.get(TOKEN_NAME);
|
|
51
|
-
if (token) {
|
|
52
|
-
config2.headers[mergedHttpConfig.authHeaderKey] = mergedHttpConfig.authScheme ? `${mergedHttpConfig.authScheme} ${token}` : token;
|
|
53
|
-
}
|
|
54
|
-
return config2;
|
|
55
|
-
},
|
|
56
|
-
(error) => {
|
|
57
|
-
ElMessage.error({
|
|
58
|
-
message: error,
|
|
59
|
-
duration: mergedHttpConfig.errorDuration
|
|
60
|
-
});
|
|
61
|
-
return Promise.reject(error);
|
|
62
|
-
}
|
|
63
|
-
);
|
|
64
|
-
axiosIns.interceptors.response.use(
|
|
65
|
-
(response) => {
|
|
66
|
-
if (mergedHttpConfig.originalResponse) {
|
|
67
|
-
return response;
|
|
68
|
-
}
|
|
69
|
-
const { data: resData } = response;
|
|
70
|
-
return handleError(resData, mergedHttpConfig);
|
|
71
|
-
},
|
|
72
|
-
(error) => {
|
|
73
|
-
if (error instanceof AxiosError && error.response && isObject(error.response.data) && get(error.response.data, mergedHttpConfig.path.code)) {
|
|
74
|
-
return handleError(error.response.data, mergedHttpConfig);
|
|
75
|
-
} else {
|
|
76
|
-
ElMessage.error({
|
|
77
|
-
message: error.message,
|
|
78
|
-
duration: mergedHttpConfig.errorDuration
|
|
79
|
-
});
|
|
80
|
-
return Promise.reject(error);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
);
|
|
84
|
-
return axiosIns;
|
|
85
|
-
};
|
|
86
|
-
const fn = (handler) => {
|
|
87
|
-
const http = new Http(createAxios);
|
|
88
|
-
const wrappedFn = (...args) => {
|
|
89
|
-
return handler(http)(...args);
|
|
90
|
-
};
|
|
91
|
-
wrappedFn.abort = () => {
|
|
92
|
-
http.abort();
|
|
93
|
-
};
|
|
94
|
-
return wrappedFn;
|
|
95
|
-
};
|
|
96
|
-
const map = (object) => {
|
|
97
|
-
const result = {};
|
|
98
|
-
for (const [key, handler] of Object.entries(object)) {
|
|
99
|
-
result[key] = fn(handler);
|
|
100
|
-
}
|
|
101
|
-
return result;
|
|
102
|
-
};
|
|
103
|
-
return {
|
|
104
|
-
fn,
|
|
105
|
-
map
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export { useRequest };
|