This commit is contained in:
zhuoda
2020-01-11 09:10:29 +08:00
parent e55e1b2f33
commit 215556f73a
608 changed files with 7 additions and 3 deletions

View File

@@ -0,0 +1,25 @@
import Vue from 'vue';
import Vuex from 'vuex';
import user from './module/user';
import app from './module/app';
import notice from './module/notice';
Vue.use(Vuex);
export default new Vuex.Store({
state: {
//
},
mutations: {
//
},
actions: {
//
},
modules: {
user,
notice,
app
}
});

View File

@@ -0,0 +1,131 @@
import {
getBreadCrumbList,
setTagNavListInLocalStorage,
getMenuByRouter,
getTagNavListFromLocalStorage,
getHomeRoute,
getNextRoute,
routeHasExist,
routeEqual,
getRouteTitleHandled
} from '@/lib/menu-func';
import { localSave, localRead } from '@/lib/local';
import router from '@/router';
import { routers } from '@/router/routers';
import config from '@/config';
const { homeName } = config;
// 关闭页面+tag
const closePage = (state, route) => {
const nextRoute = getNextRoute(state.tagNavList, route);
state.tagNavList = state.tagNavList.filter(item => {
return !routeEqual(item, route);
});
router.push(nextRoute);
};
export default {
state: {
// 缓存路由
keepAliveIncludes: [],
// 面包屑列表
breadCrumbList: [],
// tag列表
tagNavList: [],
// 首页路由
homeRoute: {},
// 本地缓存
local: localRead('local'),
// 错误列表
errorList: [],
hasReadErrorPage: false
},
getters: {
// 左侧菜单
menuList: (state, getters, rootState) =>
getMenuByRouter(routers, rootState.user.access),
errorCount: state => state.errorList.length
},
mutations: {
// 加入缓存
pushKeepAliveIncludes (state, val) {
if (state.keepAliveIncludes.length < 30) {
let number = state.keepAliveIncludes.findIndex(e => e === val);
if (number === -1) {
state.keepAliveIncludes.push(val);
}
}
},
// 删除缓存
deleteKeepAliveIncludes (state, val) {
let number = state.keepAliveIncludes.findIndex(e => e === val);
if (number !== -1) {
state.keepAliveIncludes.splice(number, 1);
}
},
// 清空缓存
clearKeepAliveIncludes (state, val) {
state.keepAliveIncludes = [val];
},
// 关闭其他
deleteOtherKeepAliveIncludes (state, val) {
state.keepAliveIncludes.forEach((item, index, arr) => {
if (item !== config.homeName && item !== val) {
arr.splice(index, 1);
}
});
},
// 设置其是否出现在面包屑中
setBreadCrumb (state, route) {
state.breadCrumbList = getBreadCrumbList(route, state.homeRoute);
},
// 初始化首页使用
setHomeRoute (state, routes) {
state.homeRoute = getHomeRoute(routes, homeName);
},
// 设置tag列表
setTagNavList (state, list) {
let tagList = [];
if (list) {
tagList = [...list];
} else tagList = getTagNavListFromLocalStorage() || [];
if (tagList[0] && tagList[0].name !== homeName) tagList.shift();
let homeTagIndex = tagList.findIndex(item => item.name === homeName);
if (homeTagIndex > 0) {
let homeTag = tagList.splice(homeTagIndex, 1)[0];
tagList.unshift(homeTag);
}
state.tagNavList = tagList;
setTagNavListInLocalStorage([...tagList]);
},
// 关闭tag
closeTag (state, route) {
let tag = state.tagNavList.filter(item => routeEqual(item, route));
route = tag[0] ? tag[0] : null;
if (!route) return;
closePage(state, route);
},
// 关闭当前tag且不进行跳转
closeTagNotPushNextRoute (state, route) {
state.tagNavList = state.tagNavList.filter(item => {
return !routeEqual(item, route);
});
},
// 新增tag
addTag (state, { route, type = 'unshift' }) {
let router = getRouteTitleHandled(route);
if (!routeHasExist(state.tagNavList, router)) {
if (type === 'push') state.tagNavList.push(router);
else {
if (router.name === homeName) state.tagNavList.unshift(router);
else state.tagNavList.splice(1, 0, router);
}
setTagNavListInLocalStorage([...state.tagNavList]);
}
},
// 保存本地信息
setLocal (state, lang) {
localSave('local', lang);
state.local = lang;
}
}
};

View File

@@ -0,0 +1,19 @@
import { noticeApi } from '@/api/notice';
export default {
state: {
noticeList: [],
noticeNumber: 0
},
mutations: {
updateNotice(state, data) {
state.noticeList = [...state.noticeList, ...data];
},
updateNoticeNum(state, data) {
state.noticeNumber = data;
},
restNotice(state) {
state.noticeList = [];
state.noticeNumber = 0;
}
}
};

View File

@@ -0,0 +1,122 @@
import cookie from '@/lib/cookie.js';
import { loginApi } from '@/api/login';
import { localSave, localRead } from '@/lib/local';
import { getType } from '@/lib/util';
import { PRIVILEGE_TYPE_ENUM } from '@/constants/privilege';
const localReadRouterPrivilege = () => {
let map = new Map();
let userRouterPrivilegeString = localRead('userRouterPrivilege');
if (userRouterPrivilegeString) {
let privilegeList = JSON.parse(userRouterPrivilegeString);
if (privilegeList) {
for (const path of privilegeList) {
let key = path.substr(1, 1);
let pathArray = map.get(key);
if (pathArray) {
pathArray.push(path);
} else {
pathArray = [];
pathArray.push(path);
map.set(key, pathArray);
}
}
}
}
return map;
};
export default {
state: {
token: cookie.getToken(),
// session信息
userLoginInfo: {},
isUpdatePrivilege: false,
// key为router name, value为 key的集合,用于v-privilege页面功能点判断
privilegeFunctionPointsMap: new Map(),
// 菜单key权限集合用于左侧是否有菜单权限判断
privilegeMenuKeyList: [],
/**
* key为 router path的首字母value为集合
* 这样做是为了提高查询效率用于vue-router拦截判断path
*/
privilegeRouterPathMap: localReadRouterPrivilege()
},
mutations: {
// 设置token
setToken (state, token) {
state.token = token;
cookie.setToken(token);
},
// 保存用户登录信息
setUserLoginInfo (state, userLoginInfo) {
state.userLoginInfo = userLoginInfo;
localSave('userLoginInfo', JSON.stringify(userLoginInfo));
},
setUserPrivilege (state, privilegeList) {
state.isUpdatePrivilege = true;
let routerPathArray = [];
for (const privilege of privilegeList) {
// 是菜单权限
if (privilege.type === PRIVILEGE_TYPE_ENUM.MENU.value) {
state.privilegeMenuKeyList.push(privilege.key);
if (privilege.url) {
routerPathArray.push(privilege.url);
// 去掉/之后第一个字母
let key = privilege.url.substr(1, 1);
let pathArray = state.privilegeRouterPathMap.get(key);
if (pathArray) {
pathArray.push(privilege.url);
} else {
pathArray = [];
pathArray.push(privilege.url);
state.privilegeRouterPathMap.set(key, pathArray);
}
}
}
// 如果是功能点
if (privilege.type === PRIVILEGE_TYPE_ENUM.POINTS.value) {
if (privilege.parentKey) {
let pointArray = state.privilegeFunctionPointsMap.get(privilege.parentKey);
if (pointArray) {
pointArray.push(privilege.key);
} else {
pointArray = [];
pointArray.push(privilege.key);
state.privilegeFunctionPointsMap.set(privilege.parentKey, pointArray);
}
}
}
}
localSave('userRouterPrivilege', JSON.stringify(routerPathArray));
}
},
getters: {
// 用户功能点权限
userFuncPrivilegeInfo: () => localRead('funcPrivilegeInfo'),
// 用户菜单权限
userMenuPrivilege: state => state.userLoginInfo.privilegeList
},
actions: {
// 登录
handleLogin ({ commit }, params) {
params.loginName = params.loginName.trim();
return new Promise((resolve, reject) => {
loginApi
.login(params)
.then(res => {
localStorage.clear();
const data = res.data;
commit('setToken', data.xaccessToken);
// 保存用户登录
commit('setUserLoginInfo', data);
resolve();
})
.catch(err => {
reject(err);
});
});
}
}
};