123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- import _ from 'lodash';
- import data from './data.json';
- /* eslint-disable */
- const LOGIN_STORAGE_KEY = 'AI.LOGIN_INFO';
- const SWITCH_OPTIONS_KEY = 'AI.SWITCH_OPTIONS';
- /**
- * 保存开关信息到localstorage
- * @param {*} token&user对象&权限
- */
- const testSaveLoginInfo = () => {
- localStorage.setItem(
- LOGIN_STORAGE_KEY,
- JSON.stringify({
- ...data,
- })
- );
- };
- /**
- * 获取登录用户权限
- */
- const getPermits = () => _.get(getLoginInfo(), 'permissions', []);
- const getLoginInfo = () => JSON.parse(localStorage.getItem(LOGIN_STORAGE_KEY));
- /**
- * 获取登录用户的token
- */
- const getToken = () => localStorage.getItem('AI.token');
- /**
- * 获取登录用户信息
- */
- const getUser = () => {
- try {
- const loginInfo = getLoginInfo();
- return {
- userId: _.get(loginInfo, 'userId', ''),
- userName: _.get(loginInfo, 'userName', ''),
- tenantId: _.get(loginInfo, 'tenantId', ''),
- tenantName: _.get(loginInfo, 'tenantName', ''),
- };
- } catch (err) {
- return { error: '获取[登录用户信息]接口报错详情:' + err.description };
- }
- };
- /**
- * 判断登录⽤户是否是管理员
- */
- const isAdmin = () => {
- try {
- const loginInfo = getLoginInfo();
- const platformFlag = _.get(loginInfo, 'platformFlag', null);
- return platformFlag === 1 ? true : false;
- } catch (err) {
- return { error: '获取[登录⽤户是否是管理员]接口报错详情:' + err.description };
- }
- };
- /**
- * 获取登录角色组(待改造)
- */
- const getGroups = () => {
- try {
- const loginInfo = getLoginInfo();
- const groups = _.get(loginInfo, 'groups', []);
- return groups;
- } catch (err) {
- return { error: '获取[获取登录用户组]接口报错详情:' + err.description };
- }
- };
- /**
- * 获取登录角色组
- */
- const getRoles = () => {
- try {
- const loginInfo = getLoginInfo();
- const roles = _.get(loginInfo, 'roles', null);
- let rolesArray = [];
- for (let i = 0; i < roles.length; i++) {
- rolesArray.push({ roleId: roles[i], roleName: '' });
- }
- return rolesArray;
- } catch (err) {
- return { error: '获取[获取登录角色组]接口报错详情:' + err.description };
- }
- };
- /**
- * 判断登录⽤户是否分配⻆⾊ID
- */
- const hasRole = roleId => {
- try {
- const roles = getRoles();
- let num = _.findIndex(roles, function(role) {
- return role.roleId === roleId;
- });
- return num !== -1 ? true : false;
- } catch (err) {
- return { error: '获取[判断登录⽤户是否分配⻆⾊ID]接口报错详情:' + err.description };
- }
- };
- /**
- * 判断登录⽤户是否分配⻆⾊ID-多个
- */
- const hasAnyRole = roleId => {
- try {
- const roles = getRoles();
- let num = _.findIndex(roles, function(role) {
- return _.indexOf(roleId, role) !== -1;
- });
- return num !== -1 ? true : false;
- } catch (err) {
- return { error: '获取[判断登录⽤户是否分配⻆⾊ID-多个]接口报错详情:' + err.description };
- }
- };
- /**
- * 判断登录⽤户是否包含授权
- */
- const hasPermission = (permission, operation) => {
- let bol = false;
- try {
- //获取全量权限数据
- const permits = getPermits();
- console.dir(permits);
- let str = permission;
- //如果第二个参数有值那么拼接到一起获取
- if (operation && operation !== '') {
- str = permission + ':' + operation;
- }
- bol = isPermiScope(permits, str);
- } catch (err) {
- return { error: '获取[判断登录⽤户是否包含授权]接口报错详情:' + err.description };
- }
- return bol;
- };
- /**
- * 判断登录⽤户是否包含任⼀授权
- */
- const hasAnyPermission = (permissions, operation) => {
- //区分是单个数组参数,还是资源领域+资源操作
- let bol = false;
- try {
- //获取全量权限数据
- const permits = getPermits();
- console.dir(permits);
- if (permissions instanceof Array) {
- //单个数组参数
- bol = arrayHasPermission(permits, permissions);
- } else {
- //资源领域+资源操作
- if (operation instanceof Array) {
- let array = [];
- for (let i = 0; i < operation.length; i++) {
- array.push(permissions + ':' + operation[i]);
- }
- bol = arrayHasPermission(permits, array);
- } else {
- return false;
- console.dir('operation参数不是数组:' + operation);
- }
- }
- return bol;
- } catch (err) {
- return false;
- return {
- error: '获取[判断登录⽤户是否包含任⼀授权]接口报错详情:' + err.description,
- };
- }
- };
- /**
- * 获取用户菜单数据(待改造)
- */
- const getMenus = () => {
- try {
- const loginInfo = getLoginInfo();
- const menus = _.get(loginInfo, 'modules', null);
- return menus;
- } catch (err) {
- return { error: '获取[获取用户菜单数据]接口报错详情:' + err.description };
- }
- };
- /**
- * 获取用户操作权限数据(待改造)
- */
- const getPermissions = permissions => {
- try {
- const loginInfo = getLoginInfo();
- const permissions = _.get(loginInfo, 'permissions', null);
- if (permissions) {
- return permissions;
- } else {
- return permissions;
- }
- } catch (err) {
- return { error: '获取[获取用户操作权限数据]接口报错详情:' + err.description };
- }
- };
- /**
- * 获取登录⽤户授权的菜单列表(待改造)
- */
- const getModules = () => {
- try {
- const loginInfo = getLoginInfo();
- const modules = _.get(loginInfo, 'modules', null);
- if (modules) {
- return modules;
- } else {
- return modules;
- }
- } catch (err) {
- return { error: '获取[获取登录⽤户授权的菜单列表]接口报错详情:' + err.description };
- }
- };
- /**
- * 获取登录用户的开关数据
- */
- const getSwitchInfo = () => JSON.parse(localStorage.getItem(SWITCH_OPTIONS_KEY));
- const isPermiScope = (permits, value) => {
- let bol = true;
- let num = _.findIndex(permits, function(permit) {
- return permit.indexOf(value) !== -1;
- });
- //获取对应权限资源操作判断
- let val = permits[num];
- if (val) {
- if (val.split(':').length > 2) {
- let permiScope = val.split(':')[2];
- if (permiScope === -1 || permiScope === '-1') {
- bol = false;
- }
- } else {
- bol = false;
- console.dir('permits[num]属性截取后长度不足:' + val);
- }
- } else {
- bol = false;
- console.dir('permits[num]未找到属性permits:' + 'num值为:' + num);
- }
- return bol;
- };
- const arrayHasPermission = (permits, permissions) => {
- let bol = true;
- for (let i = 0; i < permissions.length; i++) {
- bol = isPermiScope(permits, permissions[i]);
- if (bol === false) {
- break;
- }
- }
- return bol;
- };
- export default {
- getToken,
- getUser,
- getRoles,
- hasRole,
- getGroups,
- isAdmin,
- hasAnyRole,
- hasPermission,
- hasAnyPermission,
- getPermissions,
- getMenus,
- getModules,
- getSwitchInfo,
- testSaveLoginInfo,
- };
|