RoleController.java 3.58 KB
   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
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
package com.lyms.talkonlineweb.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyms.talkonlineweb.domain.LymsRole;
import com.lyms.talkonlineweb.domain.LymsUser;
import com.lyms.talkonlineweb.result.BaseResponse;
import com.lyms.talkonlineweb.service.LymsRoleService;
import com.lyms.talkonlineweb.service.LymsUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
* 角色管理
*/
@RestController
@Log4j2
public class RoleController {
@Autowired
private LymsUserService lymsUserService;

@Autowired
private LymsRoleService lymsRoleService;
/**
* 保存角色
* @param role
* @return
*/
@PostMapping("saveRole")
public BaseResponse saveRole(LymsRole role){
BaseResponse baseResponse=new BaseResponse();

// LymsUser user=lymsUserService.getUserByToken(Authorization);
// role.setCreatedby(user.getUid());

if(role.getRid()==null){
role.setCreatedtime(new Date());
}else {
// role.setUpdatedby(user.getUid());
role.setUpdatedTime(new Date());
}
boolean f=false;

if(!StringUtils.isEmpty(role.getRname())){
f=lymsRoleService.saveOrUpdate(role);
}
System.out.println(role);

baseResponse.setErrorcode(f==true?0:1);
return baseResponse;
}

/**
* 查询角色列表
* @param role
* @param current
* @param size
* @return
*/
@GetMapping("sltRole")
public BaseResponse sltRole(LymsRole role,int current,int size){
BaseResponse baseResponse=new BaseResponse();
Page<LymsRole> page=new Page<>(current,size);
Page<LymsRole> rolePage=lymsRoleService.page(page, Wrappers.query(role).orderByDesc("updated_time","createdtime"));
baseResponse.setObject(rolePage);
return baseResponse;
}

/**
* 删除角色
* @param rid
* @return
*/
@GetMapping("delRole")
public BaseResponse delRole(int rid){
BaseResponse baseResponse=new BaseResponse();
boolean f=lymsRoleService.removeById(rid);
baseResponse.setErrorcode(f==true?0:1);
return baseResponse;
}

/**
* 根据用户获取角色
* @param uid
* @return
*/
@GetMapping("getRolesByUid")
public BaseResponse getRolesByUid(int uid){
BaseResponse baseResponse=new BaseResponse();
List<LymsRole> rLst=lymsRoleService.getRolesByUid(uid);
baseResponse.setObject(rLst);
return baseResponse;
}

/**
* 添加角色包含的权限
* @param rid
* @param perms 如果是多个权限需要用","分割
* @return
*/
@PostMapping("addRoleByPerms")
public BaseResponse addRoleByPerms(int rid,String perms){
BaseResponse baseResponse=new BaseResponse();
String[] pArr=perms.split(",");
int cnt=0;

cnt=lymsRoleService.delPersByRole(rid);//删除旧的关系
for (int i = 0; i < pArr.length; i++) {
cnt=lymsRoleService.addRoleByPerms(rid,Integer.parseInt(pArr[i]));
}
// baseResponse.setObject(rLst);
return baseResponse;
}

}