| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- /*
- * 登录用户
- *
- * @Author: DCCloud
- * @Date: 2022-09-06 20:55:09
- */
- import _ from 'lodash';
- import { defineStore } from 'pinia';
- import localKey from '/@/constants/local-storage-key-const';
- import { HOME_PAGE_NAME } from '/@/constants/system/home-const';
- import { MENU_TYPE_ENUM } from '/@/constants/system/menu-const';
- import { messageApi } from '/@/api/support/message-api.js';
- import { smartSentry } from '/@/lib/smart-sentry.js';
- import { loginApi } from '/@/api/system/login-api';
- import { localRead, localSave, localRemove } from '/@/utils/local-util';
- import { autoCompleteProps } from 'ant-design-vue/lib/auto-complete';
- export const useUserStore = defineStore({
- id: 'userStore',
- state: () => ({
- token: '',
- //员工id
- employeeId: '',
- // 头像
- avatar: '',
- //登录名
- loginName: '',
- //姓名
- actualName: '',
- //手机号
- phone: '',
- //部门id
- departmentId: '',
- //部门名词
- departmentName: '',
- //是否需要修改密码
- needUpdatePwdFlag: false,
- //是否为超级管理员
- administratorFlag: true,
- //上次登录ip
- lastLoginIp: '',
- //上次登录ip地区
- lastLoginIpRegion: '',
- //上次登录 设备
- lastLoginUserAgent: '',
- //上次登录时间
- lastLoginTime: '',
- //左侧菜单树形结构
- menuTree: [],
- //存在页面路由的菜单集合
- menuRouterList: [],
- //是否完成menuRouter初始化
- menuRouterInitFlag: false,
- //父类菜单集合
- menuParentIdListMap: new Map(),
- // 功能点集合
- pointsList: [],
- // 标签页
- tagNav: null,
- // 缓存
- keepAliveIncludes: [],
- // 未读消息数量
- unreadMessageCount: 0,
- // 待办工作数
- toBeDoneCount: 0,
- }),
- getters: {
- getToken(state) {
- if (state.token) {
- return state.token;
- }
- return localRead(localKey.USER_TOKEN);
- },
- getNeedUpdatePwdFlag(state) {
- return state.needUpdatePwdFlag;
- },
- //是否初始化了 路由
- getMenuRouterInitFlag(state) {
- return state.menuRouterInitFlag;
- },
- //菜单树
- getMenuTree(state) {
- return state.menuTree;
- },
- //菜单的路由
- getMenuRouterList(state) {
- return state.menuRouterList;
- },
- //菜单的父级id
- getMenuParentIdListMap(state) {
- return state.menuParentIdListMap;
- },
- //功能点
- getPointList(state) {
- if (_.isEmpty(state.pointsList)) {
- let localUserPoints = localRead(localKey.USER_POINTS) || '';
- state.pointsList = localUserPoints ? JSON.parse(localUserPoints) : [];
- }
- return state.pointsList;
- },
- //标签页
- getTagNav(state) {
- if (_.isNull(state.tagNav)) {
- let localTagNav = localRead(localKey.USER_TAG_NAV) || '';
- state.tagNav = localTagNav ? JSON.parse(localTagNav) : [];
- }
- let tagNavList = _.cloneDeep(state.tagNav) || [];
- tagNavList.unshift({
- menuName: HOME_PAGE_NAME,
- menuTitle: '首页',
- });
- return tagNavList;
- },
- },
- actions: {
- autoLogin(name) {
- return new Promise((resolve, reject) => {
- loginApi.autoLogin(name).then(res => {
- resolve(res)
- }).catch(error => {
- reject(error)
- })
- })
- },
- setSSO(info) {
- return new Promise((resolve, reject) => {
- loginApi.login(info).then(res => {
- resolve(res)
- }).catch(error => {
- reject(error)
- })
- })
- },
- logout() {
- this.token = '';
- this.menuList = [];
- this.tagNav = [];
- this.unreadMessageCount = 0;
- localRemove(localKey.USER_TOKEN);
- localRemove(localKey.USER_POINTS);
- localRemove(localKey.USER_TAG_NAV);
- localRemove('loginFrom');
- },
- // 查询未读消息数量
- async queryUnreadMessageCount() {
- try {
- let result = await messageApi.queryUnreadCount();
- this.unreadMessageCount = result.data;
- } catch (e) {
- smartSentry.captureError(e);
- }
- },
- async queryToBeDoneList() {
- try {
- let localToBeDoneList = localRead(localKey.TO_BE_DONE);
- if (localToBeDoneList) {
- this.toBeDoneCount = JSON.parse(localToBeDoneList).filter((e) => !e.doneFlag).length;
- }
- } catch (err) {
- smartSentry.captureError(err);
- }
- },
- //设置登录信息
- setUserLoginInfo(data) {
- // 用户基本信息
- this.token = data.token;
- this.employeeId = data.employeeId;
- this.avatar = data.avatar;
- this.loginName = data.loginName;
- this.actualName = data.actualName;
- this.phone = data.phone;
- this.departmentId = data.departmentId;
- this.departmentName = data.departmentName;
- this.needUpdatePwdFlag = data.needUpdatePwdFlag;
- this.administratorFlag = data.administratorFlag;
- this.lastLoginIp = data.lastLoginIp;
- this.lastLoginIpRegion = data.lastLoginIpRegion;
- this.lastLoginUserAgent = data.lastLoginUserAgent;
- this.lastLoginTime = data.lastLoginTime;
- //菜单权限
- this.menuTree = buildMenuTree(data.menuList);
- //拥有路由的菜单
- this.menuRouterList = data.menuList.filter((e) => e.path || e.frameUrl);
- //父级菜单集合
- this.menuParentIdListMap = buildMenuParentIdListMap(this.menuTree);
- //功能点
- this.pointsList = data.menuList.filter((menu) => menu.menuType === MENU_TYPE_ENUM.POINTS.value && menu.visibleFlag && !menu.disabledFlag);
- // 获取用户未读消息
- this.queryUnreadMessageCount();
- // 获取待办工作数
- this.queryToBeDoneList();
- },
- setToken(token) {
- this.token = token;
- },
- //设置标签页
- setTagNav(route, from) {
- if (_.isNull(this.tagNav)) {
- let localTagNav = localRead(localKey.USER_TAG_NAV) || '';
- this.tagNav = localTagNav ? JSON.parse(localTagNav) : [];
- }
- // name唯一标识
- let name = route.name;
- if (!name || name === HOME_PAGE_NAME || name === 'error-403' || name === 'error-404') {
- return;
- }
- let findTag = (this.tagNav || []).find((e) => e.menuName === name);
- if (findTag) {
- // @ts-ignore
- findTag.fromMenuName = from.name;
- findTag.fromMenuQuery = from.query;
- for (const i of this.tagNav) {
- if (i.menuName === name) {
- i.menuQuery = route.query
- }
- }
- } else {
- // @ts-ignore
- this.tagNav.push({
- // @ts-ignore
- menuName: name,
- // @ts-ignore
- menuTitle: route.meta.title,
- menuQuery: route.query,
- // @ts-ignore
- fromMenuName: from.name,
- fromMenuQuery: from.query,
- });
- }
- localSave(localKey.USER_TAG_NAV, JSON.stringify(this.tagNav));
- },
- //关闭标签页
- closeTagNav(menuName, closeAll) {
- if (_.isEmpty(this.getTagNav)) return;
- if (closeAll && !menuName) {
- this.tagNav = [];
- this.clearKeepAliveIncludes();
- } else {
- let findIndex = (this.tagNav || []).findIndex((e) => e.menuName === menuName);
- if (closeAll) {
- if (findIndex === -1) {
- this.tagNav = [];
- this.clearKeepAliveIncludes();
- } else {
- let tagNavElement = (this.tagNav || [])[findIndex];
- this.tagNav = [tagNavElement];
- this.clearKeepAliveIncludes(tagNavElement.menuName);
- }
- } else {
- (this.tagNav || []).splice(findIndex, 1);
- this.deleteKeepAliveIncludes(menuName);
- }
- }
- localSave(localKey.USER_TAG_NAV, JSON.stringify(this.tagNav));
- },
- //关闭页面
- closePage(route, router, path) {
- if (!this.getTagNav || _.isEmpty(this.getTagNav)) return;
- if (path) {
- router.push({ path });
- } else {
- // 寻找tagNav
- let index = this.getTagNav.findIndex((e) => e.menuName === route.name);
- if (index === -1) {
- router.push({ name: HOME_PAGE_NAME });
- } else {
- let tagNav = this.getTagNav[index];
- if (tagNav.fromMenuName && this.getTagNav.some((e) => e.menuName === tagNav.fromMenuName)) {
- router.push({ name: tagNav.fromMenuName, query: tagNav.fromMenuQuery });
- } else {
- // 查询左侧tag
- let leftTagNav = this.getTagNav[index - 1];
- router.push({ name: leftTagNav.menuName, query: leftTagNav.menuQuery });
- }
- }
- }
- this.closeTagNav(route.name, false);
- },
- // 加入缓存
- pushKeepAliveIncludes(val) {
- if (!val) {
- return;
- }
- if (!this.keepAliveIncludes) {
- this.keepAliveIncludes = [];
- }
- if (this.keepAliveIncludes.length < 30) {
- let number = this.keepAliveIncludes.findIndex((e) => e === val);
- if (number === -1) {
- this.keepAliveIncludes.push(val);
- }
- }
- },
- // 删除缓存
- deleteKeepAliveIncludes(val) {
- if (!this.keepAliveIncludes || !val) {
- return;
- }
- let number = this.keepAliveIncludes.findIndex((e) => e === val);
- if (number !== -1) {
- this.keepAliveIncludes.splice(number, 1);
- }
- },
- // 清空缓存
- clearKeepAliveIncludes(val) {
- if (!val || !this.keepAliveIncludes.includes(val)) {
- this.keepAliveIncludes = [];
- return;
- }
- this.keepAliveIncludes = [val];
- },
- },
- });
- /**
- * 构建菜单父级集合
- */
- function buildMenuParentIdListMap(menuTree) {
- let menuParentIdListMap = new Map();
- recursiveBuildMenuParentIdListMap(menuTree, [], menuParentIdListMap);
- return menuParentIdListMap;
- }
- function recursiveBuildMenuParentIdListMap(menuList, parentMenuList, menuParentIdListMap) {
- for (const e of menuList) {
- // 顶级parentMenuList清空
- if (e.parentId === 0) {
- parentMenuList = [];
- }
- let menuIdStr = e.menuId.toString();
- let cloneParentMenuList = _.cloneDeep(parentMenuList);
- if (!_.isEmpty(e.children) && e.menuName) {
- // 递归
- cloneParentMenuList.push({ name: menuIdStr, title: e.menuName });
- recursiveBuildMenuParentIdListMap(e.children, cloneParentMenuList, menuParentIdListMap);
- } else {
- menuParentIdListMap.set(menuIdStr, cloneParentMenuList);
- }
- }
- }
- /**
- * 构建菜单树
- *
- * @param menuList
- * @returns
- */
- function buildMenuTree(menuList) {
- //1 获取所有 有效的 目录和菜单
- let catalogAndMenuList = menuList.filter((menu) => menu.menuType !== MENU_TYPE_ENUM.POINTS.value && menu.visibleFlag && !menu.disabledFlag);
- //2 获取顶级目录
- let topCatalogList = catalogAndMenuList.filter((menu) => menu.parentId === 0);
- for (const topCatalog of topCatalogList) {
- buildMenuChildren(topCatalog, catalogAndMenuList);
- }
- return topCatalogList;
- }
- function buildMenuChildren(menu, allMenuList) {
- let children = allMenuList.filter((e) => e.parentId === menu.menuId);
- if (children.length === 0) {
- return;
- }
- menu.children = children;
- for (const item of children) {
- buildMenuChildren(item, allMenuList);
- }
- }
|