first
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { defineStore, acceptHMRUpdate } from 'pinia'
|
||||
import logo from '@/assets/logo.png'
|
||||
|
||||
export const useAppStore = defineStore({
|
||||
id: 'App',
|
||||
state: () => ({
|
||||
setting: {
|
||||
title: 'Gwen-Admin',
|
||||
sideIsCollapse: false,
|
||||
logo,
|
||||
},
|
||||
}),
|
||||
|
||||
actions: {
|
||||
sideCollapse () {
|
||||
this.setting.sideIsCollapse = !this.setting.sideIsCollapse
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (import.meta.hot) {
|
||||
import.meta.hot.accept(acceptHMRUpdate(useAppStore, import.meta.hot))
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
import { createPinia } from 'pinia'
|
||||
|
||||
export const pinia = createPinia()
|
||||
@@ -0,0 +1,64 @@
|
||||
import { defineStore, acceptHMRUpdate } from 'pinia'
|
||||
import { lastRoutes, asyncRoutes, router } from '@/router'
|
||||
|
||||
function filterRoute (routes, enableNames) {
|
||||
return routes.filter(route => {
|
||||
if (route.children && route.children.length) {
|
||||
return enableNames.includes(route.name) || route.children.some(r => enableNames.includes(r.name))
|
||||
} else {
|
||||
return enableNames.includes(route.name)
|
||||
}
|
||||
}).map(route => {
|
||||
if (route.children && route.children.length) {
|
||||
return {
|
||||
...route,
|
||||
children: filterRoute(route.children, enableNames),
|
||||
}
|
||||
} else {
|
||||
return { ...route }
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const useRouteStore = defineStore({
|
||||
id: 'router',
|
||||
state: () => ({
|
||||
routes: [],
|
||||
activeRoute: '',
|
||||
loaded: 0,
|
||||
keepAlive: [],
|
||||
}),
|
||||
actions: {
|
||||
addRoutes (accessRouteNames) {
|
||||
if (accessRouteNames.includes('*')) {
|
||||
this.routes = asyncRoutes
|
||||
} else {
|
||||
this.routes = filterRoute(asyncRoutes, accessRouteNames)
|
||||
}
|
||||
|
||||
this.routes.forEach(route => {
|
||||
router.addRoute(route)
|
||||
})
|
||||
lastRoutes.forEach(route => {
|
||||
router.addRoute(route)
|
||||
})
|
||||
this.addKeepAlive(this.routes)
|
||||
},
|
||||
addKeepAlive (route) {
|
||||
if (route instanceof Array) {
|
||||
route.forEach(r => {
|
||||
this.addKeepAlive(r)
|
||||
})
|
||||
} else if (route.children && route.children.length) {
|
||||
this.addKeepAlive(route.children)
|
||||
} else if (route.meta?.keepAlive && !this.keepAlive.includes(route.name)) {
|
||||
this.keepAlive.push(route.name)
|
||||
}
|
||||
},
|
||||
|
||||
},
|
||||
})
|
||||
|
||||
if (import.meta.hot) {
|
||||
import.meta.hot.accept(acceptHMRUpdate(useRouteStore, import.meta.hot))
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import { defineStore, acceptHMRUpdate } from 'pinia'
|
||||
|
||||
export const useTagsStore = defineStore({
|
||||
id: 'tags',
|
||||
state: () => ({
|
||||
tags: [],
|
||||
cached: [],
|
||||
}),
|
||||
actions: {
|
||||
initTags () {
|
||||
this.tags.push(
|
||||
{
|
||||
name: 'Home',
|
||||
path: '/Home',
|
||||
title: '首页',
|
||||
active: false,
|
||||
closeable: false,
|
||||
keepAlive: false,
|
||||
})
|
||||
},
|
||||
addTag (route) {
|
||||
const tags = this.tags
|
||||
if (tags.find(t => t.name === route.name)) {
|
||||
tags.forEach(t => t.active = false)
|
||||
tags.find(t => t.name === route.name).active = true
|
||||
} else {
|
||||
tags.forEach(t => t.active = false)
|
||||
if (route.meta?.keepAlive) {
|
||||
this.addCachedTag(route.name)
|
||||
}
|
||||
tags.push({
|
||||
name: route.name,
|
||||
path: route.fullPath,
|
||||
title: route.meta?.title || route.name,
|
||||
active: true,
|
||||
closeable: true,
|
||||
keepAlive: route.meta?.keepAlive,
|
||||
})
|
||||
|
||||
}
|
||||
this.$patch({ tags })
|
||||
},
|
||||
removeTag (tag) {
|
||||
let tags = this.tags
|
||||
if (tags.find(t => t.name === tag.name)) {
|
||||
const index = tags.findIndex(t => t.name === tag.name)
|
||||
if (index > -1) {
|
||||
if (tags[index].keepAlive) {
|
||||
this.removeCachedTag(tags[index].name)
|
||||
}
|
||||
tags.splice(index, 1)
|
||||
}
|
||||
}
|
||||
this.$patch({ tags })
|
||||
},
|
||||
addCachedTag (name) {
|
||||
if (!this.cached.includes(name)) {
|
||||
this.cached.push(name)
|
||||
}
|
||||
},
|
||||
removeCachedTag (name) {
|
||||
if (this.cached.includes(name)) {
|
||||
this.cached.splice(this.cached.indexOf(name), 1)
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
},
|
||||
})
|
||||
|
||||
if (import.meta.hot) {
|
||||
import.meta.hot.accept(acceptHMRUpdate(useTagsStore, import.meta.hot))
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import { defineStore, acceptHMRUpdate } from 'pinia'
|
||||
import { current, login } from '@/api/user'
|
||||
import { setToken, removeToken } from '@/utils/auth'
|
||||
import { useRouteStore } from '@/store/router'
|
||||
|
||||
export const useUserStore = defineStore({
|
||||
id: 'user',
|
||||
state: () => ({
|
||||
nickname: '',
|
||||
username: '',
|
||||
token: '',
|
||||
role: '',
|
||||
avatar: '',
|
||||
route_names: [],
|
||||
}),
|
||||
|
||||
actions: {
|
||||
logout () {
|
||||
removeToken()
|
||||
this.$patch({
|
||||
name: '',
|
||||
role: {},
|
||||
})
|
||||
},
|
||||
|
||||
async login (form) {
|
||||
const res = await login(form).catch(_ => false)
|
||||
if (res) {
|
||||
const userData = res.data
|
||||
setToken(userData.token)
|
||||
//
|
||||
localStorage.setItem('user_info', JSON.stringify({ name: userData.username }))
|
||||
this.$patch({
|
||||
...userData,
|
||||
})
|
||||
if (userData.route_names && userData.route_names.length) {
|
||||
useRouteStore().addRoutes(userData.route_names)
|
||||
}
|
||||
return userData
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
},
|
||||
async info () {
|
||||
const res = await current().catch(_ => false)
|
||||
if (res) {
|
||||
const userData = res.data
|
||||
setToken(userData.token)
|
||||
this.$patch({
|
||||
...userData,
|
||||
})
|
||||
useRouteStore().addRoutes(userData.route_names)
|
||||
return userData
|
||||
}
|
||||
return false
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (import.meta.hot) {
|
||||
import.meta.hot.accept(acceptHMRUpdate(useUserStore, import.meta.hot))
|
||||
}
|
||||
Reference in New Issue
Block a user