auth.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import _ from 'lodash';
  2. import data from './data.json';
  3. /* eslint-disable */
  4. const LOGIN_STORAGE_KEY = 'AI.LOGIN_INFO';
  5. const SWITCH_OPTIONS_KEY = 'AI.SWITCH_OPTIONS';
  6. /**
  7. * 保存开关信息到localstorage
  8. * @param {*} token&user对象&权限
  9. */
  10. const testSaveLoginInfo = () => {
  11. localStorage.setItem(
  12. LOGIN_STORAGE_KEY,
  13. JSON.stringify({
  14. ...data,
  15. })
  16. );
  17. };
  18. /**
  19. * 获取登录用户权限
  20. */
  21. const getPermits = () => _.get(getLoginInfo(), 'permissions', []);
  22. const getLoginInfo = () => JSON.parse(localStorage.getItem(LOGIN_STORAGE_KEY));
  23. /**
  24. * 获取登录用户的token
  25. */
  26. const getToken = () => localStorage.getItem('AI.token');
  27. /**
  28. * 获取登录用户信息
  29. */
  30. const getUser = () => {
  31. try {
  32. const loginInfo = getLoginInfo();
  33. return {
  34. userId: _.get(loginInfo, 'userId', ''),
  35. userName: _.get(loginInfo, 'userName', ''),
  36. tenantId: _.get(loginInfo, 'tenantId', ''),
  37. tenantName: _.get(loginInfo, 'tenantName', ''),
  38. };
  39. } catch (err) {
  40. return { error: '获取[登录用户信息]接口报错详情:' + err.description };
  41. }
  42. };
  43. /**
  44. * 判断登录⽤户是否是管理员
  45. */
  46. const isAdmin = () => {
  47. try {
  48. const loginInfo = getLoginInfo();
  49. const platformFlag = _.get(loginInfo, 'platformFlag', null);
  50. return platformFlag === 1 ? true : false;
  51. } catch (err) {
  52. return { error: '获取[登录⽤户是否是管理员]接口报错详情:' + err.description };
  53. }
  54. };
  55. /**
  56. * 获取登录角色组(待改造)
  57. */
  58. const getGroups = () => {
  59. try {
  60. const loginInfo = getLoginInfo();
  61. const groups = _.get(loginInfo, 'groups', []);
  62. return groups;
  63. } catch (err) {
  64. return { error: '获取[获取登录用户组]接口报错详情:' + err.description };
  65. }
  66. };
  67. /**
  68. * 获取登录角色组
  69. */
  70. const getRoles = () => {
  71. try {
  72. const loginInfo = getLoginInfo();
  73. const roles = _.get(loginInfo, 'roles', null);
  74. let rolesArray = [];
  75. for (let i = 0; i < roles.length; i++) {
  76. rolesArray.push({ roleId: roles[i], roleName: '' });
  77. }
  78. return rolesArray;
  79. } catch (err) {
  80. return { error: '获取[获取登录角色组]接口报错详情:' + err.description };
  81. }
  82. };
  83. /**
  84. * 判断登录⽤户是否分配⻆⾊ID
  85. */
  86. const hasRole = roleId => {
  87. try {
  88. const roles = getRoles();
  89. let num = _.findIndex(roles, function(role) {
  90. return role.roleId === roleId;
  91. });
  92. return num !== -1 ? true : false;
  93. } catch (err) {
  94. return { error: '获取[判断登录⽤户是否分配⻆⾊ID]接口报错详情:' + err.description };
  95. }
  96. };
  97. /**
  98. * 判断登录⽤户是否分配⻆⾊ID-多个
  99. */
  100. const hasAnyRole = roleId => {
  101. try {
  102. const roles = getRoles();
  103. let num = _.findIndex(roles, function(role) {
  104. return _.indexOf(roleId, role) !== -1;
  105. });
  106. return num !== -1 ? true : false;
  107. } catch (err) {
  108. return { error: '获取[判断登录⽤户是否分配⻆⾊ID-多个]接口报错详情:' + err.description };
  109. }
  110. };
  111. /**
  112. * 判断登录⽤户是否包含授权
  113. */
  114. const hasPermission = (permission, operation) => {
  115. let bol = false;
  116. try {
  117. //获取全量权限数据
  118. const permits = getPermits();
  119. console.dir(permits);
  120. let str = permission;
  121. //如果第二个参数有值那么拼接到一起获取
  122. if (operation && operation !== '') {
  123. str = permission + ':' + operation;
  124. }
  125. bol = isPermiScope(permits, str);
  126. } catch (err) {
  127. return { error: '获取[判断登录⽤户是否包含授权]接口报错详情:' + err.description };
  128. }
  129. return bol;
  130. };
  131. /**
  132. * 判断登录⽤户是否包含任⼀授权
  133. */
  134. const hasAnyPermission = (permissions, operation) => {
  135. //区分是单个数组参数,还是资源领域+资源操作
  136. let bol = false;
  137. try {
  138. //获取全量权限数据
  139. const permits = getPermits();
  140. console.dir(permits);
  141. if (permissions instanceof Array) {
  142. //单个数组参数
  143. bol = arrayHasPermission(permits, permissions);
  144. } else {
  145. //资源领域+资源操作
  146. if (operation instanceof Array) {
  147. let array = [];
  148. for (let i = 0; i < operation.length; i++) {
  149. array.push(permissions + ':' + operation[i]);
  150. }
  151. bol = arrayHasPermission(permits, array);
  152. } else {
  153. return false;
  154. console.dir('operation参数不是数组:' + operation);
  155. }
  156. }
  157. return bol;
  158. } catch (err) {
  159. return false;
  160. return {
  161. error: '获取[判断登录⽤户是否包含任⼀授权]接口报错详情:' + err.description,
  162. };
  163. }
  164. };
  165. /**
  166. * 获取用户菜单数据(待改造)
  167. */
  168. const getMenus = () => {
  169. try {
  170. const loginInfo = getLoginInfo();
  171. const menus = _.get(loginInfo, 'modules', null);
  172. return menus;
  173. } catch (err) {
  174. return { error: '获取[获取用户菜单数据]接口报错详情:' + err.description };
  175. }
  176. };
  177. /**
  178. * 获取用户操作权限数据(待改造)
  179. */
  180. const getPermissions = permissions => {
  181. try {
  182. const loginInfo = getLoginInfo();
  183. const permissions = _.get(loginInfo, 'permissions', null);
  184. if (permissions) {
  185. return permissions;
  186. } else {
  187. return permissions;
  188. }
  189. } catch (err) {
  190. return { error: '获取[获取用户操作权限数据]接口报错详情:' + err.description };
  191. }
  192. };
  193. /**
  194. * 获取登录⽤户授权的菜单列表(待改造)
  195. */
  196. const getModules = () => {
  197. try {
  198. const loginInfo = getLoginInfo();
  199. const modules = _.get(loginInfo, 'modules', null);
  200. if (modules) {
  201. return modules;
  202. } else {
  203. return modules;
  204. }
  205. } catch (err) {
  206. return { error: '获取[获取登录⽤户授权的菜单列表]接口报错详情:' + err.description };
  207. }
  208. };
  209. /**
  210. * 获取登录用户的开关数据
  211. */
  212. const getSwitchInfo = () => JSON.parse(localStorage.getItem(SWITCH_OPTIONS_KEY));
  213. const isPermiScope = (permits, value) => {
  214. let bol = true;
  215. let num = _.findIndex(permits, function(permit) {
  216. return permit.indexOf(value) !== -1;
  217. });
  218. //获取对应权限资源操作判断
  219. let val = permits[num];
  220. if (val) {
  221. if (val.split(':').length > 2) {
  222. let permiScope = val.split(':')[2];
  223. if (permiScope === -1 || permiScope === '-1') {
  224. bol = false;
  225. }
  226. } else {
  227. bol = false;
  228. console.dir('permits[num]属性截取后长度不足:' + val);
  229. }
  230. } else {
  231. bol = false;
  232. console.dir('permits[num]未找到属性permits:' + 'num值为:' + num);
  233. }
  234. return bol;
  235. };
  236. const arrayHasPermission = (permits, permissions) => {
  237. let bol = true;
  238. for (let i = 0; i < permissions.length; i++) {
  239. bol = isPermiScope(permits, permissions[i]);
  240. if (bol === false) {
  241. break;
  242. }
  243. }
  244. return bol;
  245. };
  246. export default {
  247. getToken,
  248. getUser,
  249. getRoles,
  250. hasRole,
  251. getGroups,
  252. isAdmin,
  253. hasAnyRole,
  254. hasPermission,
  255. hasAnyPermission,
  256. getPermissions,
  257. getMenus,
  258. getModules,
  259. getSwitchInfo,
  260. testSaveLoginInfo,
  261. };