master
2022-03-22 296ae8946c001a3d7c5efe29141574603381e99d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { Provide } from '@midwayjs/decorator';
import { CoolController, BaseController } from '@cool-midway/core';
import { Context } from 'vm';
import { BaseSysRoleEntity } from '../../../entity/sys/role';
import { BaseSysRoleService } from '../../../service/sys/role';
 
/**
 * 系统角色
 */
@Provide()
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: BaseSysRoleEntity,
  service: BaseSysRoleService,
  // 新增的时候插入当前用户ID
  insertParam: async (ctx: Context) => {
    return {
      userId: ctx.admin.userId,
    };
  },
  pageQueryOp: {
    keyWordLikeFields: ['name', 'label'],
    where: async (ctx: Context) => {
      const { userId, roleIds, role } = ctx.admin;
      return [
        // 超级管理员的角色不展示
        ['label != :label', { label: 'admin' }],
        // 如果不是超管,只能看到自己新建的或者自己有的角色
        [
          '(userId=:userId or id in (:roleIds))',
          { userId, roleIds },
          role !== 'admin',
        ],
      ];
    },
  },
})
export class BaseSysRoleController extends BaseController {}