refactor: 更新 pinia store 的调用方式
This commit is contained in:
parent
ade5d806c7
commit
869986bc50
@ -1,11 +1,11 @@
|
|||||||
import { type Directive } from "vue"
|
import { type Directive } from "vue"
|
||||||
import { useUserStoreHook } from "@/store/modules/user"
|
import { useUserStore } from "@/store/modules/user"
|
||||||
|
|
||||||
/** 权限指令,和权限判断函数 checkPermission 功能类似 */
|
/** 权限指令,和权限判断函数 checkPermission 功能类似 */
|
||||||
export const permission: Directive = {
|
export const permission: Directive = {
|
||||||
mounted(el, binding) {
|
mounted(el, binding) {
|
||||||
const { value: permissionRoles } = binding
|
const { value: permissionRoles } = binding
|
||||||
const { roles } = useUserStoreHook()
|
const { roles } = useUserStore()
|
||||||
if (Array.isArray(permissionRoles) && permissionRoles.length > 0) {
|
if (Array.isArray(permissionRoles) && permissionRoles.length > 0) {
|
||||||
const hasPermission = roles.some((role) => permissionRoles.includes(role))
|
const hasPermission = roles.some((role) => permissionRoles.includes(role))
|
||||||
// hasPermission || (el.style.display = "none") // 隐藏
|
// hasPermission || (el.style.display = "none") // 隐藏
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
// core
|
// core
|
||||||
import { createApp } from "vue"
|
|
||||||
import App from "@/App.vue"
|
import App from "@/App.vue"
|
||||||
import store from "@/store"
|
import { createApp } from "vue"
|
||||||
import router from "@/router"
|
import { pinia } from "@/store"
|
||||||
|
import { router } from "@/router"
|
||||||
import "@/router/permission"
|
import "@/router/permission"
|
||||||
// load
|
// load
|
||||||
import { loadSvg } from "@/icons"
|
import { loadSvg } from "@/icons"
|
||||||
@ -26,7 +26,7 @@ loadSvg(app)
|
|||||||
/** 加载自定义指令 */
|
/** 加载自定义指令 */
|
||||||
loadDirectives(app)
|
loadDirectives(app)
|
||||||
|
|
||||||
app.use(store).use(router)
|
app.use(pinia).use(router)
|
||||||
router.isReady().then(() => {
|
router.isReady().then(() => {
|
||||||
app.mount("#app")
|
app.mount("#app")
|
||||||
})
|
})
|
||||||
|
@ -289,7 +289,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
const router = createRouter({
|
export const router = createRouter({
|
||||||
history,
|
history,
|
||||||
routes: routeSettings.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
|
routes: routeSettings.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
|
||||||
})
|
})
|
||||||
@ -309,5 +309,3 @@ export function resetRouter() {
|
|||||||
window.location.reload()
|
window.location.reload()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default router
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import router from "@/router"
|
import { router } from "@/router"
|
||||||
import { useUserStoreHook } from "@/store/modules/user"
|
import { useUserStoreHook } from "@/store/modules/user"
|
||||||
import { usePermissionStoreHook } from "@/store/modules/permission"
|
import { usePermissionStoreHook } from "@/store/modules/permission"
|
||||||
import { ElMessage } from "element-plus"
|
import { ElMessage } from "element-plus"
|
||||||
@ -10,17 +10,15 @@ import isWhiteList from "@/config/white-list"
|
|||||||
import NProgress from "nprogress"
|
import NProgress from "nprogress"
|
||||||
import "nprogress/nprogress.css"
|
import "nprogress/nprogress.css"
|
||||||
|
|
||||||
const { setTitle } = useTitle()
|
|
||||||
NProgress.configure({ showSpinner: false })
|
NProgress.configure({ showSpinner: false })
|
||||||
|
const { setTitle } = useTitle()
|
||||||
|
const userStore = useUserStoreHook()
|
||||||
|
const permissionStore = usePermissionStoreHook()
|
||||||
|
|
||||||
router.beforeEach(async (to, _from, next) => {
|
router.beforeEach(async (to, _from, next) => {
|
||||||
NProgress.start()
|
NProgress.start()
|
||||||
const userStore = useUserStoreHook()
|
|
||||||
const permissionStore = usePermissionStoreHook()
|
|
||||||
const token = getToken()
|
|
||||||
|
|
||||||
// 如果没有登陆
|
// 如果没有登陆
|
||||||
if (!token) {
|
if (!getToken()) {
|
||||||
// 如果在免登录的白名单中,则直接进入
|
// 如果在免登录的白名单中,则直接进入
|
||||||
if (isWhiteList(to)) return next()
|
if (isWhiteList(to)) return next()
|
||||||
// 其他没有访问权限的页面将被重定向到登录页面
|
// 其他没有访问权限的页面将被重定向到登录页面
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
import { createPinia } from "pinia"
|
import { createPinia } from "pinia"
|
||||||
|
|
||||||
const store = createPinia()
|
export const pinia = createPinia()
|
||||||
|
|
||||||
export default store
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { reactive, ref, watch } from "vue"
|
import { reactive, ref, watch } from "vue"
|
||||||
|
import { pinia } from "@/store"
|
||||||
import { defineStore } from "pinia"
|
import { defineStore } from "pinia"
|
||||||
import { getSidebarStatus, setSidebarStatus } from "@/utils/cache/local-storage"
|
import { getSidebarStatus, setSidebarStatus } from "@/utils/cache/local-storage"
|
||||||
import { DeviceEnum, SIDEBAR_OPENED, SIDEBAR_CLOSED } from "@/constants/app-key"
|
import { DeviceEnum, SIDEBAR_OPENED, SIDEBAR_CLOSED } from "@/constants/app-key"
|
||||||
@ -45,3 +46,11 @@ export const useAppStore = defineStore("app", () => {
|
|||||||
|
|
||||||
return { device, sidebar, toggleSidebar, closeSidebar, toggleDevice }
|
return { device, sidebar, toggleSidebar, closeSidebar, toggleDevice }
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 SPA 应用中可用于在 pinia 实例被激活前使用 store
|
||||||
|
* 在 SSR 应用中可用于在 setup 外使用 store
|
||||||
|
*/
|
||||||
|
export function useAppStoreHook() {
|
||||||
|
return useAppStore(pinia)
|
||||||
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { ref } from "vue"
|
import { ref } from "vue"
|
||||||
import store from "@/store"
|
import { pinia } from "@/store"
|
||||||
import { defineStore } from "pinia"
|
import { defineStore } from "pinia"
|
||||||
import { type RouteRecordRaw } from "vue-router"
|
import { type RouteRecordRaw } from "vue-router"
|
||||||
import { constantRoutes, dynamicRoutes } from "@/router"
|
import { constantRoutes, dynamicRoutes } from "@/router"
|
||||||
@ -50,7 +50,10 @@ export const usePermissionStore = defineStore("permission", () => {
|
|||||||
return { routes, addRoutes, setRoutes, setAllRoutes }
|
return { routes, addRoutes, setRoutes, setAllRoutes }
|
||||||
})
|
})
|
||||||
|
|
||||||
/** 在 setup 外使用 */
|
/**
|
||||||
|
* 在 SPA 应用中可用于在 pinia 实例被激活前使用 store
|
||||||
|
* 在 SSR 应用中可用于在 setup 外使用 store
|
||||||
|
*/
|
||||||
export function usePermissionStoreHook() {
|
export function usePermissionStoreHook() {
|
||||||
return usePermissionStore(store)
|
return usePermissionStore(pinia)
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { type Ref, ref, watch } from "vue"
|
import { type Ref, ref, watch } from "vue"
|
||||||
|
import { pinia } from "@/store"
|
||||||
import { defineStore } from "pinia"
|
import { defineStore } from "pinia"
|
||||||
import { type LayoutSettings, layoutSettings } from "@/config/layouts"
|
import { type LayoutSettings, layoutSettings } from "@/config/layouts"
|
||||||
import { setConfigLayout } from "@/utils/cache/local-storage"
|
import { setConfigLayout } from "@/utils/cache/local-storage"
|
||||||
@ -38,3 +39,11 @@ export const useSettingsStore = defineStore("settings", () => {
|
|||||||
|
|
||||||
return state
|
return state
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 SPA 应用中可用于在 pinia 实例被激活前使用 store
|
||||||
|
* 在 SSR 应用中可用于在 setup 外使用 store
|
||||||
|
*/
|
||||||
|
export function useSettingsStoreHook() {
|
||||||
|
return useSettingsStore(pinia)
|
||||||
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { ref, watchEffect } from "vue"
|
import { ref, watchEffect } from "vue"
|
||||||
|
import { pinia } from "@/store"
|
||||||
import { defineStore } from "pinia"
|
import { defineStore } from "pinia"
|
||||||
import { useSettingsStore } from "./settings"
|
import { useSettingsStore } from "./settings"
|
||||||
import { type RouteLocationNormalized } from "vue-router"
|
import { type RouteLocationNormalized } from "vue-router"
|
||||||
@ -93,3 +94,11 @@ export const useTagsViewStore = defineStore("tags-view", () => {
|
|||||||
delAllCachedViews
|
delAllCachedViews
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 SPA 应用中可用于在 pinia 实例被激活前使用 store
|
||||||
|
* 在 SSR 应用中可用于在 setup 外使用 store
|
||||||
|
*/
|
||||||
|
export function useTagsViewStoreHook() {
|
||||||
|
return useTagsViewStore(pinia)
|
||||||
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { ref } from "vue"
|
import { ref } from "vue"
|
||||||
import store from "@/store"
|
import { pinia } from "@/store"
|
||||||
import { defineStore } from "pinia"
|
import { defineStore } from "pinia"
|
||||||
import { useTagsViewStore } from "./tags-view"
|
import { useTagsViewStore } from "./tags-view"
|
||||||
import { useSettingsStore } from "./settings"
|
import { useSettingsStore } from "./settings"
|
||||||
@ -63,7 +63,10 @@ export const useUserStore = defineStore("user", () => {
|
|||||||
return { token, roles, username, login, getInfo, changeRoles, logout, resetToken }
|
return { token, roles, username, login, getInfo, changeRoles, logout, resetToken }
|
||||||
})
|
})
|
||||||
|
|
||||||
/** 在 setup 外使用 */
|
/**
|
||||||
|
* 在 SPA 应用中可用于在 pinia 实例被激活前使用 store
|
||||||
|
* 在 SSR 应用中可用于在 setup 外使用 store
|
||||||
|
*/
|
||||||
export function useUserStoreHook() {
|
export function useUserStoreHook() {
|
||||||
return useUserStore(store)
|
return useUserStore(pinia)
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import { useUserStoreHook } from "@/store/modules/user"
|
import { useUserStore } from "@/store/modules/user"
|
||||||
|
|
||||||
/** 全局权限判断函数,和权限指令 v-permission 功能类似 */
|
/** 全局权限判断函数,和权限指令 v-permission 功能类似 */
|
||||||
export const checkPermission = (permissionRoles: string[]): boolean => {
|
export const checkPermission = (permissionRoles: string[]): boolean => {
|
||||||
if (Array.isArray(permissionRoles) && permissionRoles.length > 0) {
|
if (Array.isArray(permissionRoles) && permissionRoles.length > 0) {
|
||||||
const { roles } = useUserStoreHook()
|
const { roles } = useUserStore()
|
||||||
return roles.some((role) => permissionRoles.includes(role))
|
return roles.some((role) => permissionRoles.includes(role))
|
||||||
} else {
|
} else {
|
||||||
console.error("need roles! Like checkPermission(['admin','editor'])")
|
console.error("need roles! Like checkPermission(['admin','editor'])")
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
import axios, { type AxiosInstance, type AxiosRequestConfig } from "axios"
|
import axios, { type AxiosInstance, type AxiosRequestConfig } from "axios"
|
||||||
import { useUserStoreHook } from "@/store/modules/user"
|
import { useUserStore } from "@/store/modules/user"
|
||||||
import { ElMessage } from "element-plus"
|
import { ElMessage } from "element-plus"
|
||||||
import { get, merge } from "lodash-es"
|
import { get, merge } from "lodash-es"
|
||||||
import { getToken } from "./cache/cookies"
|
import { getToken } from "./cache/cookies"
|
||||||
|
|
||||||
/** 退出登录并强制刷新页面(会重定向到登录页) */
|
/** 退出登录并强制刷新页面(会重定向到登录页) */
|
||||||
function logout() {
|
function logout() {
|
||||||
useUserStoreHook().logout()
|
useUserStore().logout()
|
||||||
location.reload()
|
location.reload()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user