feat: 修改vuex为pinia
修改vuex为pinia
This commit is contained in:
@@ -13,6 +13,7 @@
|
|||||||
"nprogress": "^0.2.0",
|
"nprogress": "^0.2.0",
|
||||||
"path-browserify": "^1.0.1",
|
"path-browserify": "^1.0.1",
|
||||||
"path-to-regexp": "^6.2.0",
|
"path-to-regexp": "^6.2.0",
|
||||||
|
"pinia": "^2.0.6",
|
||||||
"screenfull": "^6.0.0",
|
"screenfull": "^6.0.0",
|
||||||
"vue": "^3.2.16",
|
"vue": "^3.2.16",
|
||||||
"vue-router": "^4.0.12",
|
"vue-router": "^4.0.12",
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ export function updateRoleMenu(roleId: number, menuIds: Array<Number>) {
|
|||||||
*
|
*
|
||||||
* @param roleId
|
* @param roleId
|
||||||
*/
|
*/
|
||||||
export function listRolePerms(roleId: number) {
|
export function listRolePermission(roleId: number) {
|
||||||
return request({
|
return request({
|
||||||
url: '/youlai-admin/api/v1/roles/' + roleId + '/permissions',
|
url: '/youlai-admin/api/v1/roles/' + roleId + '/permissions',
|
||||||
method: 'get',
|
method: 'get',
|
||||||
@@ -124,10 +124,10 @@ export function listRolePerms(roleId: number) {
|
|||||||
* @param roleId
|
* @param roleId
|
||||||
* @param permIds
|
* @param permIds
|
||||||
*/
|
*/
|
||||||
export function saveRolePerms(menuId: number, roleId: number, permIds: Array<number>) {
|
export function updateRolePermission(menuId: number, roleId: number, permIds: Array<number>) {
|
||||||
return request({
|
return request({
|
||||||
url: '/youlai-admin/api/v1/roles/' + roleId + '/permissions',
|
url: '/youlai-admin/api/v1/roles/' + roleId + '/permissions',
|
||||||
method: 'put',
|
method: 'put',
|
||||||
data: {menuId: menuId, permIds: permIds}
|
data: {menuId: menuId, permIds: permIds}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
11
src/main.ts
11
src/main.ts
@@ -1,7 +1,8 @@
|
|||||||
import {createApp} from 'vue'
|
import {createApp} from 'vue'
|
||||||
import App from './App.vue'
|
import App from './App.vue'
|
||||||
import router from "./router";
|
import router from "./router";
|
||||||
import {store, key} from './store'
|
// import {store, key} from './store'
|
||||||
|
import { setupStore } from "@/store";
|
||||||
import '@/styles/index.scss'
|
import '@/styles/index.scss'
|
||||||
|
|
||||||
import ElementPlus from 'element-plus'
|
import ElementPlus from 'element-plus'
|
||||||
@@ -29,10 +30,8 @@ for (let iconName in ElIconModules) {
|
|||||||
|
|
||||||
// 全局方法
|
// 全局方法
|
||||||
app.config.globalProperties.$getDictItemsByCode = getDictItemsByCode
|
app.config.globalProperties.$getDictItemsByCode = getDictItemsByCode
|
||||||
|
setupStore(app);
|
||||||
app
|
app.component('Pagination', Pagination) // 全局组件
|
||||||
.component('Pagination', Pagination) // 全局组件
|
|
||||||
.use(router)
|
.use(router)
|
||||||
.use(store, key)
|
|
||||||
.use(ElementPlus, {locale})
|
.use(ElementPlus, {locale})
|
||||||
.mount('#app')
|
.mount('#app')
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import router from "@router";
|
import router from "@router";
|
||||||
import NProgress from 'nprogress';
|
import NProgress from 'nprogress';
|
||||||
import {ElMessage} from "element-plus";
|
import {ElMessage} from "element-plus";
|
||||||
import {store} from "@store";
|
import { usePermissionStoreHook } from "@/store/modules/permission";
|
||||||
|
import { useUserStoreHook } from "@/store/modules/user";
|
||||||
NProgress.configure({showSpinner: false})
|
NProgress.configure({showSpinner: false})
|
||||||
|
|
||||||
// 白名单
|
// 白名单
|
||||||
@@ -11,28 +11,28 @@ const whiteList = ['/login', '/auth-redirect']
|
|||||||
router.beforeEach(async (to, form, next) => {
|
router.beforeEach(async (to, form, next) => {
|
||||||
NProgress.start()
|
NProgress.start()
|
||||||
|
|
||||||
const hasToken = store.state.user.token
|
const hasToken =useUserStoreHook().token
|
||||||
if (hasToken) {
|
if (hasToken) {
|
||||||
// 如果登录成功,跳转到首页
|
// 如果登录成功,跳转到首页
|
||||||
if (to.path === '/login') {
|
if (to.path === '/login') {
|
||||||
next({path: '/'})
|
next({path: '/'})
|
||||||
NProgress.done()
|
NProgress.done()
|
||||||
} else {
|
} else {
|
||||||
const hasGetUserInfo = store.state.user.roles.length > 0
|
const hasGetUserInfo =useUserStoreHook().roles.length > 0
|
||||||
if (hasGetUserInfo) {
|
if (hasGetUserInfo) {
|
||||||
next()
|
next()
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
await store.dispatch('user/getUserInfo')
|
await useUserStoreHook().getUserInfo()
|
||||||
const roles = store.state.user.roles
|
const roles =useUserStoreHook().roles
|
||||||
const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
|
const accessRoutes:any = await usePermissionStoreHook().generateRoutes(roles)
|
||||||
accessRoutes.forEach((route: any) => {
|
accessRoutes.forEach((route: any) => {
|
||||||
router.addRoute(route)
|
router.addRoute(route)
|
||||||
})
|
})
|
||||||
next({...to, replace: true})
|
next({...to, replace: true})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// remove token and go to login page to re-login
|
// remove token and go to login page to re-login
|
||||||
await store.dispatch('user/resetToken')
|
await useUserStoreHook().resetToken()
|
||||||
ElMessage.error(error as any || 'Has Error')
|
ElMessage.error(error as any || 'Has Error')
|
||||||
next(`/login?redirect=${to.path}`)
|
next(`/login?redirect=${to.path}`)
|
||||||
NProgress.done()
|
NProgress.done()
|
||||||
@@ -52,4 +52,4 @@ router.beforeEach(async (to, form, next) => {
|
|||||||
|
|
||||||
router.afterEach(() => {
|
router.afterEach(() => {
|
||||||
NProgress.done()
|
NProgress.done()
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,27 +1,9 @@
|
|||||||
import {InjectionKey} from 'vue'
|
import type { App } from "vue";
|
||||||
import {createStore,useStore as baseUseStore ,Store} from 'vuex'
|
import { createPinia } from "pinia";
|
||||||
import {RootStateTypes} from "@store/interface";
|
const store = createPinia();
|
||||||
|
|
||||||
// Vite 使用特殊的 import.meta.glob 函数从文件系统导入多个模块
|
export function setupStore(app: App<Element>) {
|
||||||
// @see https://cn.vitejs.dev/guide/features.html#glob-import
|
app.use(store);
|
||||||
const moduleFiles = import.meta.globEager('./modules/*.ts')
|
|
||||||
const paths:string[]=[]
|
|
||||||
|
|
||||||
for (const path in moduleFiles) {
|
|
||||||
paths.push(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
const modules = paths.reduce((modules: { [x: string]: any }, modulePath: string) => {
|
|
||||||
const moduleKey = modulePath.replace(/^\.\/modules\/(.*)\.\w+$/, '$1');
|
|
||||||
modules[moduleKey] = moduleFiles[modulePath].default;
|
|
||||||
return modules;
|
|
||||||
}, {});
|
|
||||||
|
|
||||||
export const key: InjectionKey<Store<RootStateTypes>> = Symbol()
|
|
||||||
|
|
||||||
export const store = createStore<RootStateTypes>({modules})
|
|
||||||
|
|
||||||
export function useStore(){
|
|
||||||
return baseUseStore(key)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export { store };
|
||||||
|
|||||||
@@ -1,18 +1,24 @@
|
|||||||
import {Module} from "vuex";
|
// import {Module} from "vuex";
|
||||||
import {AppState,RootStateTypes} from "@store/interface";
|
import {AppState} from "@store/interface";
|
||||||
import {Local} from "@utils/storage";
|
import {Local} from "@utils/storage";
|
||||||
|
|
||||||
const appModule: Module<AppState, RootStateTypes> = {
|
// import { storageLocal } from "/@/utils/storage";
|
||||||
namespaced: true,
|
import { store } from "@/store";
|
||||||
state: {
|
// import { appType } from "./types";
|
||||||
|
import { defineStore } from "pinia";
|
||||||
|
// import { getConfig } from "/@/config";
|
||||||
|
|
||||||
|
export const useAppStore = defineStore({
|
||||||
|
id: "youlai-app",
|
||||||
|
state: ():AppState=>({
|
||||||
device: 'desktop',
|
device: 'desktop',
|
||||||
sidebar: {
|
sidebar: {
|
||||||
opened: Local.get('sidebarStatus') ? !!+Local.get('sidebarStatus') : true,
|
opened: Local.get('sidebarStatus') ? !!+Local.get('sidebarStatus') : true,
|
||||||
withoutAnimation: false
|
withoutAnimation: false
|
||||||
}
|
}
|
||||||
},
|
}),
|
||||||
mutations: {
|
actions: {
|
||||||
TOGGLE_SIDEBAR: state => {
|
async TOGGLE_SIDEBAR(state:any) {
|
||||||
state.sidebar.opened = !state.sidebar.opened
|
state.sidebar.opened = !state.sidebar.opened
|
||||||
console.log('state.sidebar.opened',state.sidebar.opened)
|
console.log('state.sidebar.opened',state.sidebar.opened)
|
||||||
state.sidebar.withoutAnimation = false
|
state.sidebar.withoutAnimation = false
|
||||||
@@ -22,28 +28,26 @@ const appModule: Module<AppState, RootStateTypes> = {
|
|||||||
Local.set('sidebarStatus', 0)
|
Local.set('sidebarStatus', 0)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
CLOSE_SIDEBAR: (state, withoutAnimation) => {
|
async CLOSE_SIDEBAR (state:any, withoutAnimation:any) {
|
||||||
Local.set('sidebarStatus', 0)
|
Local.set('sidebarStatus', 0)
|
||||||
state.sidebar.opened = false
|
state.sidebar.opened = false
|
||||||
state.sidebar.withoutAnimation = withoutAnimation
|
state.sidebar.withoutAnimation = withoutAnimation
|
||||||
},
|
},
|
||||||
TOGGLE_DEVICE: (state, device) => {
|
async TOGGLE_DEVICE(state:any, device:any) {
|
||||||
console.log('TOGGLE_DEVICE',device)
|
console.log('TOGGLE_DEVICE',device)
|
||||||
state.device = device
|
state.device = device
|
||||||
}
|
|
||||||
},
|
|
||||||
actions: {
|
|
||||||
toggleSideBar({commit}) {
|
|
||||||
commit('TOGGLE_SIDEBAR')
|
|
||||||
},
|
},
|
||||||
closeSideBar({commit}, {withoutAnimation}) {
|
// toggleSideBar({commit}) {
|
||||||
commit('CLOSE_SIDEBAR', withoutAnimation)
|
// commit('TOGGLE_SIDEBAR')
|
||||||
},
|
// },
|
||||||
async toggleDevice({commit}, device) {
|
// closeSideBar({commit}, {withoutAnimation}) {
|
||||||
commit('TOGGLE_DEVICE', device)
|
// commit('CLOSE_SIDEBAR', withoutAnimation)
|
||||||
}
|
// },
|
||||||
|
// async toggleDevice({commit}, device) {
|
||||||
|
// commit('TOGGLE_DEVICE', device)
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
export function useAppStoreHook() {
|
||||||
|
return useAppStore(store);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default appModule;
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import {Module} from "vuex";
|
// import {Module} from "vuex";
|
||||||
import {PermissionState, RootStateTypes} from "@store/interface";
|
import {PermissionState, RootStateTypes} from "@store/interface";
|
||||||
import {RouteRecordRaw} from 'vue-router'
|
import {RouteRecordRaw} from 'vue-router'
|
||||||
import {constantRoutes} from '@/router'
|
import {constantRoutes} from '@/router'
|
||||||
import {listRoutes} from "@/api/system/menu";
|
import {listRoutes} from "@/api/system/menu";
|
||||||
|
import { defineStore } from "pinia";
|
||||||
|
import { store } from "@/store";
|
||||||
const modules = import.meta.glob("../../views/**/**.vue");
|
const modules = import.meta.glob("../../views/**/**.vue");
|
||||||
export const Layout = () => import( '@/layout/index.vue')
|
export const Layout = () => import( '@/layout/index.vue')
|
||||||
|
|
||||||
@@ -45,20 +46,18 @@ export const filterAsyncRoutes = (routes: RouteRecordRaw[], roles: string[]) =>
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const permissionModule: Module<PermissionState, RootStateTypes> = {
|
export const usePermissionStore = defineStore({
|
||||||
namespaced: true,
|
id:"youlai-permission",
|
||||||
state: {
|
state:():PermissionState=>( {
|
||||||
routes: [],
|
routes: [],
|
||||||
addRoutes: []
|
addRoutes: []
|
||||||
},
|
}),
|
||||||
mutations: {
|
|
||||||
SET_ROUTES: (state: PermissionState, routes: RouteRecordRaw[]) => {
|
|
||||||
state.addRoutes = routes
|
|
||||||
state.routes = constantRoutes.concat(routes)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
actions: {
|
actions: {
|
||||||
generateRoutes({commit}, roles: string[]) {
|
async SET_ROUTES( routes: RouteRecordRaw[]){
|
||||||
|
this.addRoutes = routes
|
||||||
|
this.routes = constantRoutes.concat(routes)
|
||||||
|
},
|
||||||
|
generateRoutes( roles: string[]) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
listRoutes().then(response => {
|
listRoutes().then(response => {
|
||||||
const asyncRoutes = response.data
|
const asyncRoutes = response.data
|
||||||
@@ -68,7 +67,7 @@ const permissionModule: Module<PermissionState, RootStateTypes> = {
|
|||||||
} else {
|
} else {
|
||||||
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
|
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
|
||||||
}
|
}
|
||||||
commit('SET_ROUTES', accessedRoutes)
|
this.SET_ROUTES(accessedRoutes)
|
||||||
resolve(accessedRoutes)
|
resolve(accessedRoutes)
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
reject(error)
|
reject(error)
|
||||||
@@ -76,5 +75,7 @@ const permissionModule: Module<PermissionState, RootStateTypes> = {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
export function usePermissionStoreHook() {
|
||||||
|
return usePermissionStore(store);
|
||||||
}
|
}
|
||||||
export default permissionModule;
|
|
||||||
|
|||||||
@@ -1,49 +1,50 @@
|
|||||||
import {Module} from "vuex";
|
import { defineStore } from "pinia";
|
||||||
|
import { store } from "@/store";
|
||||||
|
// import {Module} from "vuex";
|
||||||
import {SettingState, RootStateTypes} from "@store/interface";
|
import {SettingState, RootStateTypes} from "@store/interface";
|
||||||
import defaultSettings from '../../settings'
|
import defaultSettings from '../../settings'
|
||||||
|
|
||||||
const {showSettings, tagsView, fixedHeader, sidebarLogo} = defaultSettings
|
const {showSettings, tagsView, fixedHeader, sidebarLogo} = defaultSettings
|
||||||
|
|
||||||
const settingModule: Module<SettingState, RootStateTypes> = {
|
export const useSettingStore = defineStore({
|
||||||
namespaced: true,
|
id: "pure-setting",
|
||||||
state: {
|
state:():SettingState =>({
|
||||||
theme: '',
|
theme: '',
|
||||||
showSettings: showSettings,
|
showSettings: showSettings,
|
||||||
tagsView: tagsView,
|
tagsView: tagsView,
|
||||||
fixedHeader: fixedHeader,
|
fixedHeader: fixedHeader,
|
||||||
sidebarLogo: sidebarLogo,
|
sidebarLogo: sidebarLogo,
|
||||||
},
|
}),
|
||||||
mutations: {
|
actions: {
|
||||||
CHANGE_SETTING: (state: SettingState, payload: { key: string, value: any }) => {
|
async CHANGE_SETTING( payload: { key: string, value: any }){
|
||||||
const {key, value} = payload
|
const {key, value} = payload
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case 'theme':
|
case 'theme':
|
||||||
state.theme = value
|
this.theme = value
|
||||||
break
|
break
|
||||||
case 'showSettings':
|
case 'showSettings':
|
||||||
state.showSettings = value
|
this.showSettings = value
|
||||||
break
|
break
|
||||||
case 'fixedHeader':
|
case 'fixedHeader':
|
||||||
state.fixedHeader = value
|
this.fixedHeader = value
|
||||||
break
|
break
|
||||||
case 'tagsView':
|
case 'tagsView':
|
||||||
state.tagsView = value
|
this.tagsView = value
|
||||||
break
|
break
|
||||||
case 'sidebarLogo':
|
case 'sidebarLogo':
|
||||||
state.sidebarLogo = value
|
this.sidebarLogo = value
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
},
|
changeSetting(data:any) {
|
||||||
actions: {
|
this.CHANGE_SETTING(data)
|
||||||
changeSetting({commit}, data) {
|
|
||||||
commit('CHANGE_SETTING', data)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
export function useSettingStoreHook() {
|
||||||
|
return useSettingStore(store);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default settingModule;
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,158 +1,161 @@
|
|||||||
|
|
||||||
import {Module} from "vuex";
|
import { defineStore } from "pinia";
|
||||||
|
import { store } from "@/store";
|
||||||
|
// import {Module} from "vuex";
|
||||||
import {TagsViewState,RootStateTypes} from "@store/interface";
|
import {TagsViewState,RootStateTypes} from "@store/interface";
|
||||||
|
|
||||||
const tagsViewModule: Module<TagsViewState, RootStateTypes> = {
|
const tagsViewStore=defineStore({
|
||||||
namespaced: true,
|
id:"youlai-tagsView",
|
||||||
state: {
|
state:():TagsViewState=>( {
|
||||||
visitedViews: [],
|
visitedViews: [],
|
||||||
cachedViews: []
|
cachedViews: []
|
||||||
},
|
}),
|
||||||
mutations: {
|
actions: {
|
||||||
ADD_VISITED_VIEW: (state, view) => {
|
async ADD_VISITED_VIEW ( view:any) {
|
||||||
if (state.visitedViews.some(v => v.path === view.path)) return
|
if (this.visitedViews.some(v => v.path === view.path)) return
|
||||||
state.visitedViews.push(
|
this.visitedViews.push(
|
||||||
Object.assign({}, view, {
|
Object.assign({}, view, {
|
||||||
title: view.meta?.title || 'no-name'
|
title: view.meta?.title || 'no-name'
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
ADD_CACHED_VIEW: (state, view) => {
|
async ADD_CACHED_VIEW(view:any) {
|
||||||
if (state.cachedViews.includes(view.name)) return
|
if (this.cachedViews.includes(view.name)) return
|
||||||
if (!view.meta.noCache) {
|
if (!view.meta.noCache) {
|
||||||
state.cachedViews.push(view.name)
|
this.cachedViews.push(view.name)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
DEL_VISITED_VIEW: (state, view) => {
|
async DEL_VISITED_VIEW( view:any) {
|
||||||
for (const [i, v] of state.visitedViews.entries()) {
|
for (const [i, v] of this.visitedViews.entries()) {
|
||||||
if (v.path === view.path) {
|
if (v.path === view.path) {
|
||||||
state.visitedViews.splice(i, 1)
|
this.visitedViews.splice(i, 1)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
DEL_CACHED_VIEW: (state, view) => {
|
async DEL_CACHED_VIEW ( view:any) {
|
||||||
const index = state.cachedViews.indexOf(view.name)
|
const index = this.cachedViews.indexOf(view.name)
|
||||||
index > -1 && state.cachedViews.splice(index, 1)
|
index > -1 && this.cachedViews.splice(index, 1)
|
||||||
},
|
},
|
||||||
|
|
||||||
DEL_OTHERS_VISITED_VIEWS: (state, view) => {
|
async DEL_OTHERS_VISITED_VIEWS (view:any) {
|
||||||
state.visitedViews = state.visitedViews.filter(v => {
|
this.visitedViews = this.visitedViews.filter(v => {
|
||||||
return v.meta?.affix || v.path === view.path
|
return v.meta?.affix || v.path === view.path
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
DEL_OTHERS_CACHED_VIEWS: (state, view) => {
|
async DEL_OTHERS_CACHED_VIEWS( view:any) {
|
||||||
const index = state.cachedViews.indexOf(view.name)
|
const index = this.cachedViews.indexOf(view.name)
|
||||||
if (index > -1) {
|
if (index > -1) {
|
||||||
state.cachedViews = state.cachedViews.slice(index, index + 1)
|
this.cachedViews = this.cachedViews.slice(index, index + 1)
|
||||||
} else {
|
} else {
|
||||||
// if index = -1, there is no cached tags
|
// if index = -1, there is no cached tags
|
||||||
state.cachedViews = []
|
this.cachedViews = []
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
DEL_ALL_VISITED_VIEWS: state => {
|
DEL_ALL_VISITED_VIEWS() {
|
||||||
// keep affix tags
|
// keep affix tags
|
||||||
const affixTags = state.visitedViews.filter(tag => tag.meta?.affix)
|
const affixTags = this.visitedViews.filter(tag => tag.meta?.affix)
|
||||||
state.visitedViews = affixTags
|
this.visitedViews = affixTags
|
||||||
},
|
},
|
||||||
DEL_ALL_CACHED_VIEWS: state => {
|
DEL_ALL_CACHED_VIEWS() {
|
||||||
state.cachedViews = []
|
this.cachedViews = []
|
||||||
},
|
},
|
||||||
|
|
||||||
UPDATE_VISITED_VIEW: (state, view) => {
|
UPDATE_VISITED_VIEW (view:any) {
|
||||||
for (let v of state.visitedViews) {
|
for (let v of this.visitedViews) {
|
||||||
if (v.path === view.path) {
|
if (v.path === view.path) {
|
||||||
v = Object.assign(v, view)
|
v = Object.assign(v, view)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
},
|
|
||||||
actions: {
|
|
||||||
addView({ dispatch }, view) {
|
|
||||||
dispatch('addVisitedView', view)
|
|
||||||
dispatch('addCachedView', view)
|
|
||||||
},
|
},
|
||||||
addVisitedView({ commit }, view) {
|
addView( view:any) {
|
||||||
commit('ADD_VISITED_VIEW', view)
|
this.addVisitedView( view)
|
||||||
|
this.addCachedView(view)
|
||||||
},
|
},
|
||||||
addCachedView({ commit }, view) {
|
addVisitedView( view:any) {
|
||||||
commit('ADD_CACHED_VIEW', view)
|
this.ADD_VISITED_VIEW( view)
|
||||||
},
|
},
|
||||||
delView({ dispatch, state }, view) {
|
addCachedView( view:any) {
|
||||||
|
this.ADD_CACHED_VIEW(view)
|
||||||
|
},
|
||||||
|
delView( view:any) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
dispatch('delVisitedView', view)
|
this.delVisitedView(view)
|
||||||
dispatch('delCachedView', view)
|
this.delCachedView( view)
|
||||||
resolve({
|
resolve({
|
||||||
visitedViews: [...state.visitedViews],
|
visitedViews: [...this.visitedViews],
|
||||||
cachedViews: [...state.cachedViews]
|
cachedViews: [...this.cachedViews]
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
delVisitedView({ commit, state }, view) {
|
delVisitedView( view:any) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
commit('DEL_VISITED_VIEW', view)
|
this.DEL_VISITED_VIEW( view)
|
||||||
resolve([...state.visitedViews])
|
resolve([...this.visitedViews])
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
delCachedView({ commit, state }, view) {
|
delCachedView( view:any) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
commit('DEL_CACHED_VIEW', view)
|
this.DEL_CACHED_VIEW(view)
|
||||||
resolve([...state.cachedViews])
|
resolve([...this.cachedViews])
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
delOthersViews({ dispatch, state }, view) {
|
delOthersViews( view:any) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
dispatch('delOthersVisitedViews', view)
|
this.delOthersVisitedViews(view)
|
||||||
dispatch('delOthersCachedViews', view)
|
this.delOthersCachedViews(view)
|
||||||
resolve({
|
resolve({
|
||||||
visitedViews: [...state.visitedViews],
|
visitedViews: [...this.visitedViews],
|
||||||
cachedViews: [...state.cachedViews]
|
cachedViews: [...this.cachedViews]
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
delOthersVisitedViews({ commit, state }, view) {
|
delOthersVisitedViews( view:any) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
commit('DEL_OTHERS_VISITED_VIEWS', view)
|
this.DEL_OTHERS_VISITED_VIEWS(view)
|
||||||
resolve([...state.visitedViews])
|
resolve([...this.visitedViews])
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
delOthersCachedViews({ commit, state }, view) {
|
delOthersCachedViews( view:any) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
commit('DEL_OTHERS_CACHED_VIEWS', view)
|
this.DEL_OTHERS_CACHED_VIEWS(view)
|
||||||
resolve([...state.cachedViews])
|
resolve([...this.cachedViews])
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
delAllViews({ dispatch, state }, view) {
|
delAllViews( view:any) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
dispatch('delAllVisitedViews', view)
|
this.delAllVisitedViews()
|
||||||
dispatch('delAllCachedViews', view)
|
this.delAllCachedViews()
|
||||||
resolve({
|
resolve({
|
||||||
visitedViews: [...state.visitedViews],
|
visitedViews: [...this.visitedViews],
|
||||||
cachedViews: [...state.cachedViews]
|
cachedViews: [...this.cachedViews]
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
delAllVisitedViews({ commit, state }) {
|
delAllVisitedViews() {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
commit('DEL_ALL_VISITED_VIEWS')
|
this.DEL_ALL_VISITED_VIEWS
|
||||||
resolve([...state.visitedViews])
|
resolve([...this.visitedViews])
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
delAllCachedViews({ commit, state }) {
|
delAllCachedViews() {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
commit('DEL_ALL_CACHED_VIEWS')
|
this.DEL_ALL_CACHED_VIEWS
|
||||||
resolve([...state.cachedViews])
|
resolve([...this.cachedViews])
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
updateVisitedView({ commit }, view) {
|
updateVisitedView( view:any) {
|
||||||
commit('UPDATE_VISITED_VIEW', view)
|
this.UPDATE_VISITED_VIEW(view)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
export function tagsViewStoreHook() {
|
||||||
|
return tagsViewStore(store);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default tagsViewModule;
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import {Module} from "vuex";
|
import { defineStore } from "pinia";
|
||||||
|
import { store } from "@/store";
|
||||||
import {UserState, RootStateTypes} from "@store/interface";
|
import {UserState, RootStateTypes} from "@store/interface";
|
||||||
import {Local} from "@utils/storage";
|
import {Local} from "@utils/storage";
|
||||||
import {getUserInfo, login, logout} from "@api/login";
|
import {getUserInfo, login, logout} from "@api/login";
|
||||||
@@ -15,36 +16,35 @@ const getDefaultState = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const userModule: Module<UserState, RootStateTypes> = {
|
export const useUserStore = defineStore({
|
||||||
namespaced: true,
|
id:"youlai-user",
|
||||||
state: {
|
state: ():UserState=>({
|
||||||
token: Local.get('token') || '',
|
token: Local.get('token') || '',
|
||||||
nickname: '',
|
nickname: '',
|
||||||
avatar: '',
|
avatar: '',
|
||||||
roles: [],
|
roles: [],
|
||||||
perms: []
|
perms: []
|
||||||
},
|
}),
|
||||||
mutations: {
|
|
||||||
RESET_STATE: (state) => {
|
|
||||||
Object.assign(state, getDefaultState())
|
|
||||||
},
|
|
||||||
SET_TOKEN(state: UserState, token: string) {
|
|
||||||
state.token = token
|
|
||||||
},
|
|
||||||
SET_NICKNAME(state: UserState, nickname: string) {
|
|
||||||
state.nickname = nickname
|
|
||||||
},
|
|
||||||
SET_AVATAR(state: UserState, avatar: string) {
|
|
||||||
state.avatar = avatar
|
|
||||||
},
|
|
||||||
SET_ROLES(state: UserState, roles: string[]) {
|
|
||||||
state.roles = roles
|
|
||||||
},
|
|
||||||
SET_PERMS(state: UserState, perms: string[]) {
|
|
||||||
state.perms = perms
|
|
||||||
}
|
|
||||||
},
|
|
||||||
actions: {
|
actions: {
|
||||||
|
async RESET_STATE () {
|
||||||
|
// Object.assign(this.state, getDefaultState())
|
||||||
|
this.$reset()
|
||||||
|
},
|
||||||
|
async SET_TOKEN(token: string) {
|
||||||
|
this.token = token
|
||||||
|
},
|
||||||
|
async SET_NICKNAME( nickname: string) {
|
||||||
|
this.nickname = nickname
|
||||||
|
},
|
||||||
|
async SET_AVATAR(avatar: string) {
|
||||||
|
this.avatar = avatar
|
||||||
|
},
|
||||||
|
async SET_ROLES(roles: string[]) {
|
||||||
|
this.roles = roles
|
||||||
|
},
|
||||||
|
async SET_PERMS( perms: string[]) {
|
||||||
|
this.perms = perms
|
||||||
|
},
|
||||||
/**
|
/**
|
||||||
* 用户登录请求
|
* 用户登录请求
|
||||||
* @param userInfo 登录用户信息
|
* @param userInfo 登录用户信息
|
||||||
@@ -53,7 +53,7 @@ const userModule: Module<UserState, RootStateTypes> = {
|
|||||||
* code: 验证码
|
* code: 验证码
|
||||||
* uuid: 匹配正确验证码的 key
|
* uuid: 匹配正确验证码的 key
|
||||||
*/
|
*/
|
||||||
login({commit}, userInfo: { username: string, password: string, code: string, uuid: string }) {
|
login(userInfo: { username: string, password: string, code: string, uuid: string }) {
|
||||||
const {username, password, code, uuid} = userInfo
|
const {username, password, code, uuid} = userInfo
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
login(
|
login(
|
||||||
@@ -68,7 +68,7 @@ const userModule: Module<UserState, RootStateTypes> = {
|
|||||||
const {access_token, token_type} = response.data
|
const {access_token, token_type} = response.data
|
||||||
const accessToken = token_type + " " + access_token
|
const accessToken = token_type + " " + access_token
|
||||||
Local.set("token", accessToken)
|
Local.set("token", accessToken)
|
||||||
commit('SET_TOKEN', accessToken)
|
this.SET_TOKEN(accessToken)
|
||||||
resolve(access_token)
|
resolve(access_token)
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
reject(error)
|
reject(error)
|
||||||
@@ -78,7 +78,7 @@ const userModule: Module<UserState, RootStateTypes> = {
|
|||||||
/**
|
/**
|
||||||
* 获取用户信息(昵称、头像、角色集合、权限集合)
|
* 获取用户信息(昵称、头像、角色集合、权限集合)
|
||||||
*/
|
*/
|
||||||
getUserInfo({commit, state}) {
|
getUserInfo() {
|
||||||
return new Promise(((resolve, reject) => {
|
return new Promise(((resolve, reject) => {
|
||||||
getUserInfo().then(response => {
|
getUserInfo().then(response => {
|
||||||
const {data} = response
|
const {data} = response
|
||||||
@@ -90,10 +90,10 @@ const userModule: Module<UserState, RootStateTypes> = {
|
|||||||
if (!roles || roles.length <= 0) {
|
if (!roles || roles.length <= 0) {
|
||||||
reject('getUserInfo: roles must be a non-null array!')
|
reject('getUserInfo: roles must be a non-null array!')
|
||||||
}
|
}
|
||||||
commit('SET_NICKNAME', nickname)
|
this.SET_NICKNAME(nickname)
|
||||||
commit('SET_AVATAR', avatar)
|
this.SET_AVATAR( avatar)
|
||||||
commit('SET_ROLES', roles)
|
this.SET_ROLES( roles)
|
||||||
commit('SET_PERMS', perms)
|
this.SET_PERMS( perms)
|
||||||
|
|
||||||
resolve(data)
|
resolve(data)
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
@@ -106,11 +106,11 @@ const userModule: Module<UserState, RootStateTypes> = {
|
|||||||
/**
|
/**
|
||||||
* 注销
|
* 注销
|
||||||
*/
|
*/
|
||||||
logout({commit, state}) {
|
logout() {
|
||||||
return new Promise(((resolve, reject) => {
|
return new Promise(((resolve, reject) => {
|
||||||
logout().then(() => {
|
logout().then(() => {
|
||||||
Local.remove('token')
|
Local.remove('token')
|
||||||
commit('RESET_STATE')
|
this.RESET_STATE()
|
||||||
resetRouter()
|
resetRouter()
|
||||||
resolve(null)
|
resolve(null)
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
@@ -122,15 +122,17 @@ const userModule: Module<UserState, RootStateTypes> = {
|
|||||||
/**
|
/**
|
||||||
* 清除 Token
|
* 清除 Token
|
||||||
*/
|
*/
|
||||||
resetToken({commit}){
|
resetToken(){
|
||||||
return new Promise(resolve=>{
|
return new Promise(resolve=>{
|
||||||
Local.remove('token')
|
Local.remove('token')
|
||||||
commit('RESET_STATE')
|
this.RESET_STATE()
|
||||||
resolve(null)
|
resolve(null)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
export function useUserStoreHook() {
|
||||||
|
return useUserStore(store);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default userModule;
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user