|
@@ -1,14 +1,13 @@
|
|
|
import { Provide, Inject } from '@midwayjs/decorator';
|
|
|
import { InjectEntityModel } from '@midwayjs/typegoose';
|
|
|
import { ReturnModelType } from '@typegoose/typegoose';
|
|
|
-import { BaseService, ServiceError, FrameworkErrorEnum } from 'free-midway-component';
|
|
|
+import { BaseService } from 'free-midway-component';
|
|
|
import { Role } from '../../entity/system/role.entity';
|
|
|
import { Menus } from '../../entity/system/menus.entity';
|
|
|
import { MenusService } from './menus.service';
|
|
|
-import { flattenDeep, uniq, get, lowerFirst, upperFirst, last } from 'lodash';
|
|
|
+import { flattenDeep, uniq, get, lowerFirst, upperFirst, last, compact } from 'lodash';
|
|
|
import { Context } from '@midwayjs/koa';
|
|
|
import { I18nService } from '../i18n.service';
|
|
|
-import { FrameErrorEnum } from '../../error/frame.error';
|
|
|
import { Admin } from '../../entity/system/admin.entity';
|
|
|
import { Dept } from '../../entity/system/dept.entity';
|
|
|
type modelType = ReturnModelType<typeof Role>;
|
|
@@ -39,6 +38,43 @@ export class RoleService extends BaseService<modelType> {
|
|
|
if (!admin) return false;
|
|
|
if (admin.is_super === '0') return true;
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 获取用户接口权限编码列表
|
|
|
+ * @returns {Array<string>} apiCodes 用户接口权限编码列表
|
|
|
+ */
|
|
|
+ async getUserApiCodes() {
|
|
|
+ const user = this.ctx.user;
|
|
|
+ const getControllerCode = list => {
|
|
|
+ const result = [];
|
|
|
+ for (const i of list) {
|
|
|
+ const { config = [], children = [] } = i;
|
|
|
+ const l = config.map(i => get(i, 'controller_code'));
|
|
|
+ result.push(...l);
|
|
|
+ if (children && children.length > 0) {
|
|
|
+ const cl = getControllerCode(children);
|
|
|
+ result.push(...cl);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ };
|
|
|
+ if (await this.isSuperAdmin()) {
|
|
|
+ const menus = await this.menusService.queryMenu({ is_use: '0' });
|
|
|
+ // 需要整理出所有 config 中 methodCode = ${controllerCode}.${method} ,query 没有二级code
|
|
|
+ let apiCodes = getControllerCode(menus);
|
|
|
+ apiCodes = compact(apiCodes);
|
|
|
+ return apiCodes;
|
|
|
+ }
|
|
|
+ // 其他用户需要把 部门 和 角色的权限叠加在一起
|
|
|
+ // 部门权限
|
|
|
+ const deptCodes = await this.getDeptCodes(get(user, 'dept'));
|
|
|
+ // 角色权限
|
|
|
+ const roleCodes = await this.getRoleCodes(get(user, 'role'));
|
|
|
+ let allCodes = [...deptCodes, ...roleCodes];
|
|
|
+ allCodes = uniq(allCodes);
|
|
|
+ const menuList = await this.menusModel.find({ route_name: allCodes }).lean();
|
|
|
+ const apiCodes = getControllerCode(menuList);
|
|
|
+ return apiCodes;
|
|
|
+ }
|
|
|
|
|
|
async getUserMenus(needCode = false) {
|
|
|
const user = this.ctx.user;
|
|
@@ -74,9 +110,9 @@ export class RoleService extends BaseService<modelType> {
|
|
|
const result = [];
|
|
|
for (const code of allCodes) {
|
|
|
const arr = code.split('.');
|
|
|
- result.push(last(arr))
|
|
|
+ result.push(last(arr));
|
|
|
}
|
|
|
- return uniq(result)
|
|
|
+ return uniq(result);
|
|
|
}
|
|
|
|
|
|
/**
|