feat: 修改vuex为pinia

修改vuex为pinia
This commit is contained in:
zc
2021-12-21 07:33:30 +08:00
parent 9f8d5d757b
commit d53c13ecba
10 changed files with 210 additions and 217 deletions

View File

@@ -1,18 +1,24 @@
import {Module} from "vuex";
import {AppState,RootStateTypes} from "@store/interface";
// import {Module} from "vuex";
import {AppState} from "@store/interface";
import {Local} from "@utils/storage";
const appModule: Module<AppState, RootStateTypes> = {
namespaced: true,
state: {
// import { storageLocal } from "/@/utils/storage";
import { store } from "@/store";
// import { appType } from "./types";
import { defineStore } from "pinia";
// import { getConfig } from "/@/config";
export const useAppStore = defineStore({
id: "youlai-app",
state: ():AppState=>({
device: 'desktop',
sidebar: {
opened: Local.get('sidebarStatus') ? !!+Local.get('sidebarStatus') : true,
withoutAnimation: false
}
},
mutations: {
TOGGLE_SIDEBAR: state => {
}),
actions: {
async TOGGLE_SIDEBAR(state:any) {
state.sidebar.opened = !state.sidebar.opened
console.log('state.sidebar.opened',state.sidebar.opened)
state.sidebar.withoutAnimation = false
@@ -22,28 +28,26 @@ const appModule: Module<AppState, RootStateTypes> = {
Local.set('sidebarStatus', 0)
}
},
CLOSE_SIDEBAR: (state, withoutAnimation) => {
async CLOSE_SIDEBAR (state:any, withoutAnimation:any) {
Local.set('sidebarStatus', 0)
state.sidebar.opened = false
state.sidebar.withoutAnimation = withoutAnimation
},
TOGGLE_DEVICE: (state, device) => {
async TOGGLE_DEVICE(state:any, device:any) {
console.log('TOGGLE_DEVICE',device)
state.device = device
}
},
actions: {
toggleSideBar({commit}) {
commit('TOGGLE_SIDEBAR')
},
closeSideBar({commit}, {withoutAnimation}) {
commit('CLOSE_SIDEBAR', withoutAnimation)
},
async toggleDevice({commit}, device) {
commit('TOGGLE_DEVICE', device)
}
// toggleSideBar({commit}) {
// commit('TOGGLE_SIDEBAR')
// },
// closeSideBar({commit}, {withoutAnimation}) {
// commit('CLOSE_SIDEBAR', withoutAnimation)
// },
// async toggleDevice({commit}, device) {
// commit('TOGGLE_DEVICE', device)
// }
}
})
export function useAppStoreHook() {
return useAppStore(store);
}
export default appModule;

View File

@@ -1,9 +1,10 @@
import {Module} from "vuex";
// import {Module} from "vuex";
import {PermissionState, RootStateTypes} from "@store/interface";
import {RouteRecordRaw} from 'vue-router'
import {constantRoutes} from '@/router'
import {listRoutes} from "@/api/system/menu";
import { defineStore } from "pinia";
import { store } from "@/store";
const modules = import.meta.glob("../../views/**/**.vue");
export const Layout = () => import( '@/layout/index.vue')
@@ -45,20 +46,18 @@ export const filterAsyncRoutes = (routes: RouteRecordRaw[], roles: string[]) =>
}
const permissionModule: Module<PermissionState, RootStateTypes> = {
namespaced: true,
state: {
export const usePermissionStore = defineStore({
id:"youlai-permission",
state:():PermissionState=>( {
routes: [],
addRoutes: []
},
mutations: {
SET_ROUTES: (state: PermissionState, routes: RouteRecordRaw[]) => {
state.addRoutes = routes
state.routes = constantRoutes.concat(routes)
}
},
}),
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) => {
listRoutes().then(response => {
const asyncRoutes = response.data
@@ -68,7 +67,7 @@ const permissionModule: Module<PermissionState, RootStateTypes> = {
} else {
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
}
commit('SET_ROUTES', accessedRoutes)
this.SET_ROUTES(accessedRoutes)
resolve(accessedRoutes)
}).catch(error => {
reject(error)
@@ -76,5 +75,7 @@ const permissionModule: Module<PermissionState, RootStateTypes> = {
})
}
}
})
export function usePermissionStoreHook() {
return usePermissionStore(store);
}
export default permissionModule;

View File

@@ -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 defaultSettings from '../../settings'
const {showSettings, tagsView, fixedHeader, sidebarLogo} = defaultSettings
const settingModule: Module<SettingState, RootStateTypes> = {
namespaced: true,
state: {
export const useSettingStore = defineStore({
id: "pure-setting",
state:():SettingState =>({
theme: '',
showSettings: showSettings,
tagsView: tagsView,
fixedHeader: fixedHeader,
sidebarLogo: sidebarLogo,
},
mutations: {
CHANGE_SETTING: (state: SettingState, payload: { key: string, value: any }) => {
}),
actions: {
async CHANGE_SETTING( payload: { key: string, value: any }){
const {key, value} = payload
switch (key) {
case 'theme':
state.theme = value
this.theme = value
break
case 'showSettings':
state.showSettings = value
this.showSettings = value
break
case 'fixedHeader':
state.fixedHeader = value
this.fixedHeader = value
break
case 'tagsView':
state.tagsView = value
this.tagsView = value
break
case 'sidebarLogo':
state.sidebarLogo = value
this.sidebarLogo = value
break
default:
break
}
}
},
actions: {
changeSetting({commit}, data) {
commit('CHANGE_SETTING', data)
},
changeSetting(data:any) {
this.CHANGE_SETTING(data)
}
}
})
export function useSettingStoreHook() {
return useSettingStore(store);
}
export default settingModule;

View File

@@ -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";
const tagsViewModule: Module<TagsViewState, RootStateTypes> = {
namespaced: true,
state: {
const tagsViewStore=defineStore({
id:"youlai-tagsView",
state:():TagsViewState=>( {
visitedViews: [],
cachedViews: []
},
mutations: {
ADD_VISITED_VIEW: (state, view) => {
if (state.visitedViews.some(v => v.path === view.path)) return
state.visitedViews.push(
}),
actions: {
async ADD_VISITED_VIEW ( view:any) {
if (this.visitedViews.some(v => v.path === view.path)) return
this.visitedViews.push(
Object.assign({}, view, {
title: view.meta?.title || 'no-name'
})
)
},
ADD_CACHED_VIEW: (state, view) => {
if (state.cachedViews.includes(view.name)) return
async ADD_CACHED_VIEW(view:any) {
if (this.cachedViews.includes(view.name)) return
if (!view.meta.noCache) {
state.cachedViews.push(view.name)
this.cachedViews.push(view.name)
}
},
DEL_VISITED_VIEW: (state, view) => {
for (const [i, v] of state.visitedViews.entries()) {
async DEL_VISITED_VIEW( view:any) {
for (const [i, v] of this.visitedViews.entries()) {
if (v.path === view.path) {
state.visitedViews.splice(i, 1)
this.visitedViews.splice(i, 1)
break
}
}
},
DEL_CACHED_VIEW: (state, view) => {
const index = state.cachedViews.indexOf(view.name)
index > -1 && state.cachedViews.splice(index, 1)
async DEL_CACHED_VIEW ( view:any) {
const index = this.cachedViews.indexOf(view.name)
index > -1 && this.cachedViews.splice(index, 1)
},
DEL_OTHERS_VISITED_VIEWS: (state, view) => {
state.visitedViews = state.visitedViews.filter(v => {
async DEL_OTHERS_VISITED_VIEWS (view:any) {
this.visitedViews = this.visitedViews.filter(v => {
return v.meta?.affix || v.path === view.path
})
},
DEL_OTHERS_CACHED_VIEWS: (state, view) => {
const index = state.cachedViews.indexOf(view.name)
async DEL_OTHERS_CACHED_VIEWS( view:any) {
const index = this.cachedViews.indexOf(view.name)
if (index > -1) {
state.cachedViews = state.cachedViews.slice(index, index + 1)
this.cachedViews = this.cachedViews.slice(index, index + 1)
} else {
// if index = -1, there is no cached tags
state.cachedViews = []
this.cachedViews = []
}
},
DEL_ALL_VISITED_VIEWS: state => {
DEL_ALL_VISITED_VIEWS() {
// keep affix tags
const affixTags = state.visitedViews.filter(tag => tag.meta?.affix)
state.visitedViews = affixTags
const affixTags = this.visitedViews.filter(tag => tag.meta?.affix)
this.visitedViews = affixTags
},
DEL_ALL_CACHED_VIEWS: state => {
state.cachedViews = []
DEL_ALL_CACHED_VIEWS() {
this.cachedViews = []
},
UPDATE_VISITED_VIEW: (state, view) => {
for (let v of state.visitedViews) {
UPDATE_VISITED_VIEW (view:any) {
for (let v of this.visitedViews) {
if (v.path === view.path) {
v = Object.assign(v, view)
break
}
}
}
},
actions: {
addView({ dispatch }, view) {
dispatch('addVisitedView', view)
dispatch('addCachedView', view)
},
addVisitedView({ commit }, view) {
commit('ADD_VISITED_VIEW', view)
addView( view:any) {
this.addVisitedView( view)
this.addCachedView(view)
},
addCachedView({ commit }, view) {
commit('ADD_CACHED_VIEW', view)
addVisitedView( view:any) {
this.ADD_VISITED_VIEW( view)
},
delView({ dispatch, state }, view) {
addCachedView( view:any) {
this.ADD_CACHED_VIEW(view)
},
delView( view:any) {
return new Promise(resolve => {
dispatch('delVisitedView', view)
dispatch('delCachedView', view)
this.delVisitedView(view)
this.delCachedView( view)
resolve({
visitedViews: [...state.visitedViews],
cachedViews: [...state.cachedViews]
visitedViews: [...this.visitedViews],
cachedViews: [...this.cachedViews]
})
})
},
delVisitedView({ commit, state }, view) {
delVisitedView( view:any) {
return new Promise(resolve => {
commit('DEL_VISITED_VIEW', view)
resolve([...state.visitedViews])
this.DEL_VISITED_VIEW( view)
resolve([...this.visitedViews])
})
},
delCachedView({ commit, state }, view) {
delCachedView( view:any) {
return new Promise(resolve => {
commit('DEL_CACHED_VIEW', view)
resolve([...state.cachedViews])
this.DEL_CACHED_VIEW(view)
resolve([...this.cachedViews])
})
},
delOthersViews({ dispatch, state }, view) {
delOthersViews( view:any) {
return new Promise(resolve => {
dispatch('delOthersVisitedViews', view)
dispatch('delOthersCachedViews', view)
this.delOthersVisitedViews(view)
this.delOthersCachedViews(view)
resolve({
visitedViews: [...state.visitedViews],
cachedViews: [...state.cachedViews]
visitedViews: [...this.visitedViews],
cachedViews: [...this.cachedViews]
})
})
},
delOthersVisitedViews({ commit, state }, view) {
delOthersVisitedViews( view:any) {
return new Promise(resolve => {
commit('DEL_OTHERS_VISITED_VIEWS', view)
resolve([...state.visitedViews])
this.DEL_OTHERS_VISITED_VIEWS(view)
resolve([...this.visitedViews])
})
},
delOthersCachedViews({ commit, state }, view) {
delOthersCachedViews( view:any) {
return new Promise(resolve => {
commit('DEL_OTHERS_CACHED_VIEWS', view)
resolve([...state.cachedViews])
this.DEL_OTHERS_CACHED_VIEWS(view)
resolve([...this.cachedViews])
})
},
delAllViews({ dispatch, state }, view) {
delAllViews( view:any) {
return new Promise(resolve => {
dispatch('delAllVisitedViews', view)
dispatch('delAllCachedViews', view)
this.delAllVisitedViews()
this.delAllCachedViews()
resolve({
visitedViews: [...state.visitedViews],
cachedViews: [...state.cachedViews]
visitedViews: [...this.visitedViews],
cachedViews: [...this.cachedViews]
})
})
},
delAllVisitedViews({ commit, state }) {
delAllVisitedViews() {
return new Promise(resolve => {
commit('DEL_ALL_VISITED_VIEWS')
resolve([...state.visitedViews])
this.DEL_ALL_VISITED_VIEWS
resolve([...this.visitedViews])
})
},
delAllCachedViews({ commit, state }) {
delAllCachedViews() {
return new Promise(resolve => {
commit('DEL_ALL_CACHED_VIEWS')
resolve([...state.cachedViews])
this.DEL_ALL_CACHED_VIEWS
resolve([...this.cachedViews])
})
},
updateVisitedView({ commit }, view) {
commit('UPDATE_VISITED_VIEW', view)
updateVisitedView( view:any) {
this.UPDATE_VISITED_VIEW(view)
}
}
})
export function tagsViewStoreHook() {
return tagsViewStore(store);
}
export default tagsViewModule;

View File

@@ -1,4 +1,5 @@
import {Module} from "vuex";
import { defineStore } from "pinia";
import { store } from "@/store";
import {UserState, RootStateTypes} from "@store/interface";
import {Local} from "@utils/storage";
import {getUserInfo, login, logout} from "@api/login";
@@ -15,36 +16,35 @@ const getDefaultState = () => {
}
}
const userModule: Module<UserState, RootStateTypes> = {
namespaced: true,
state: {
export const useUserStore = defineStore({
id:"youlai-user",
state: ():UserState=>({
token: Local.get('token') || '',
nickname: '',
avatar: '',
roles: [],
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: {
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 登录用户信息
@@ -53,7 +53,7 @@ const userModule: Module<UserState, RootStateTypes> = {
* code: 验证码
* 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
return new Promise((resolve, reject) => {
login(
@@ -68,7 +68,7 @@ const userModule: Module<UserState, RootStateTypes> = {
const {access_token, token_type} = response.data
const accessToken = token_type + " " + access_token
Local.set("token", accessToken)
commit('SET_TOKEN', accessToken)
this.SET_TOKEN(accessToken)
resolve(access_token)
}).catch(error => {
reject(error)
@@ -78,7 +78,7 @@ const userModule: Module<UserState, RootStateTypes> = {
/**
* 获取用户信息(昵称、头像、角色集合、权限集合)
*/
getUserInfo({commit, state}) {
getUserInfo() {
return new Promise(((resolve, reject) => {
getUserInfo().then(response => {
const {data} = response
@@ -90,10 +90,10 @@ const userModule: Module<UserState, RootStateTypes> = {
if (!roles || roles.length <= 0) {
reject('getUserInfo: roles must be a non-null array!')
}
commit('SET_NICKNAME', nickname)
commit('SET_AVATAR', avatar)
commit('SET_ROLES', roles)
commit('SET_PERMS', perms)
this.SET_NICKNAME(nickname)
this.SET_AVATAR( avatar)
this.SET_ROLES( roles)
this.SET_PERMS( perms)
resolve(data)
}).catch(error => {
@@ -106,11 +106,11 @@ const userModule: Module<UserState, RootStateTypes> = {
/**
* 注销
*/
logout({commit, state}) {
logout() {
return new Promise(((resolve, reject) => {
logout().then(() => {
Local.remove('token')
commit('RESET_STATE')
this.RESET_STATE()
resetRouter()
resolve(null)
}).catch(error => {
@@ -122,15 +122,17 @@ const userModule: Module<UserState, RootStateTypes> = {
/**
* 清除 Token
*/
resetToken({commit}){
resetToken(){
return new Promise(resolve=>{
Local.remove('token')
commit('RESET_STATE')
this.RESET_STATE()
resolve(null)
})
}
}
})
export function useUserStoreHook() {
return useUserStore(store);
}
export default userModule;