Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

231 строка
8.4 KiB

  1. package com.simple.controller;
  2. import com.github.pagehelper.PageInfo;
  3. import com.simple.common.ResultData;
  4. import com.simple.domain.po.*;
  5. import com.simple.service.*;
  6. import com.simple.shiro.PasswordHelper;
  7. import com.simple.shiro.UserSession;
  8. import io.swagger.annotations.Api;
  9. import io.swagger.annotations.ApiOperation;
  10. import org.apache.shiro.SecurityUtils;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.util.Assert;
  15. import org.springframework.web.bind.annotation.*;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. /**
  19. * @author chenkx
  20. * @date 2018-01-05.
  21. */
  22. @Api(value = "API - UserInfoController", description = "用户接口")
  23. @RestController
  24. @RequestMapping("user")
  25. public class MallUserInfoController extends BaseController {
  26. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  27. @Autowired
  28. MallUserInfoService userInfoService;
  29. @Autowired
  30. MallUserRoleService userRoleService;
  31. @Autowired
  32. MallRoleService mallRoleService;
  33. @Autowired
  34. MallUserRoleService mallUserRoleService;
  35. @Autowired
  36. MallPermissionService mallPermissionService;
  37. @Autowired
  38. MallRolePermissionService mallRolePermissionService;
  39. @ApiOperation(value = "用户分页接口", response = String.class)
  40. @GetMapping("lists")
  41. public ResultData listAsPage(MallUserInfo userInfo, Integer pageNum, Integer pageSize) {
  42. userInfo.setTenantId(getTenantId());
  43. final PageInfo<MallUserInfo> page = userInfoService.listAsPage(userInfo, pageNum, pageSize);
  44. for (MallUserInfo u : page.getList()) {
  45. MallUserRole r = new MallUserRole();
  46. r.setUid(u.getId());
  47. PageInfo<MallUserRole> ur = userRoleService.listAsPage(r, 1, 1);
  48. if (ur.getSize() > 0) {
  49. MallRole role = mallRoleService.getById(ur.getList().get(0).getRoleId());
  50. if (role != null) {
  51. u.setRoleName(role.getName());
  52. u.setRoleId(role.getId());
  53. }
  54. }
  55. u.setPassword(null);//不返回密码
  56. }
  57. return new ResultData(page);
  58. }
  59. @ApiOperation(value = "用户详情接口", response = String.class)
  60. @GetMapping("detail")
  61. public ResultData detail(Long id) {
  62. final MallUserInfo user = userInfoService.getById(id);
  63. return new ResultData(user);
  64. }
  65. @ApiOperation(value = "创建用户接口", response = String.class)
  66. @PostMapping("add")
  67. public ResultData createUser(@RequestBody MallUserInfo userInfo) {
  68. Assert.notNull(userInfo.getUsername(), "用户名不能为空");
  69. Assert.isTrue(checkUnique(userInfo.getUsername(), 0), "重复的用户名");
  70. Assert.notNull(userInfo.getPassword(), "密码不能为空");
  71. PasswordHelper passwordHelper = new PasswordHelper();
  72. passwordHelper.encryptPassword(userInfo);
  73. userInfo.setTenantId(getTenantId());
  74. userInfoService.saveOrUpdate(userInfo);
  75. if (userInfo.getRoleId() != null) {
  76. MallUserRole r = new MallUserRole();
  77. r.setRoleId(userInfo.getRoleId());
  78. r.setUid(userInfo.getId());
  79. userRoleService.saveOrUpdate(r);
  80. }
  81. return new ResultData();
  82. }
  83. @ApiOperation(value = "修改用户接口", response = String.class)
  84. @PostMapping("update")
  85. public ResultData updateUser(@RequestBody MallUserInfo userInfo) {
  86. Assert.notNull(userInfo.getUsername(), "用户名不能为空");
  87. Assert.isTrue(checkUnique(userInfo.getUsername(), 1), "重复的用户名");
  88. // Assert.notNull(userInfo.getPassword(), "密码不能为空");
  89. // PasswordHelper passwordHelper = new PasswordHelper();
  90. // passwordHelper.encryptPassword(userInfo);
  91. userInfo.setPassword(null);
  92. userInfo.setTenantId(getTenantId());
  93. userInfoService.saveOrUpdate(userInfo);
  94. if (userInfo.getRoleId() != null) {
  95. userRoleService.deleteByUserId(userInfo.getId());
  96. MallUserRole r = new MallUserRole();
  97. r.setRoleId(userInfo.getRoleId());
  98. r.setUid(userInfo.getId());
  99. userRoleService.saveOrUpdate(r);
  100. }
  101. return new ResultData();
  102. }
  103. @ApiOperation(value = "删除用户接口", response = String.class)
  104. @PostMapping("/del")
  105. public ResultData deleteUser(@RequestBody MallUserInfo userInfo) {
  106. userInfoService.deleteById(userInfo.getId());
  107. return new ResultData();
  108. }
  109. /**
  110. * 启用,停用用户
  111. *
  112. * @param userId
  113. * @param status
  114. * @return
  115. */
  116. @ApiOperation(value = "起停用户接口", response = String.class)
  117. @GetMapping("status")
  118. public ResultData modifyStatus(Long userId, Integer status) {
  119. MallUserInfo userInfo = userInfoService.getById(userId);
  120. userInfo.setStatus(status);
  121. userInfo.setTenantId(getTenantId());
  122. userInfoService.saveOrUpdate(userInfo);
  123. return new ResultData(userInfo);
  124. }
  125. @ApiOperation(value = "用户添加角色接口", response = String.class)
  126. @PostMapping("setRoles")
  127. public ResultData setRoles(Long userId, String roleIds) {
  128. /*String[] roIds = roleIds.split(",");
  129. List<MallUserRole> u2rs = new ArrayList<>(roIds.length);
  130. for (String roleId : roIds) {
  131. MallUserRole userRole = new MallUserRole();
  132. userRole.setRoleId(roleId);
  133. userRole.setUid(userId);
  134. u2rs.add(userRole);
  135. }
  136. userRoleService.batchInsert(u2rs);*/
  137. return new ResultData();
  138. }
  139. private boolean checkUnique(String username, Integer type) {
  140. if (type == 0) {
  141. if (userInfoService.cntByUserName(username) > 0) {
  142. return false;
  143. }
  144. }
  145. if (type == 1) {
  146. if (userInfoService.cntByUserName(username) > 1) {
  147. return false;
  148. }
  149. }
  150. return true;
  151. }
  152. @GetMapping("hasButtonPermission")
  153. public ResultData hasButtonPermission(String permissions) {
  154. Map<String, Boolean> map = new HashMap<>();
  155. for (String name : permissions.split(",")) {
  156. Long userId = (Long)
  157. SecurityUtils.getSubject().getSession().getAttribute(UserSession.userId);
  158. boolean has = userInfoService.hasButtonPermission(userId, name);
  159. map.put(name, has);
  160. }
  161. // StringBuffer buffer = new StringBuffer();
  162. // for(Entry<String, Boolean> entrySet :map.entrySet()){
  163. // buffer.append("var "+entrySet.getKey());
  164. // buffer.append("="+entrySet.getValue());
  165. // buffer.append(";");
  166. // }
  167. //return new ResultData(buffer.toString());
  168. return new ResultData(map);
  169. }
  170. @GetMapping("getUser")
  171. public ResultData getUserInfo() {
  172. MallUserInfo info = (MallUserInfo) SecurityUtils.getSubject().getSession().getAttribute(UserSession.userInfo);
  173. info.setPassword("保密");
  174. if (info.getIsAdmin() == 1) { // isAdmin
  175. MallPermission q = new MallPermission();
  176. PageInfo<MallPermission> list = mallPermissionService.listAsPage(q, 1, 100);
  177. String menus = "";
  178. for (MallPermission rp : list.getList()) {
  179. menus += rp.getId() + ",";
  180. }
  181. if (menus.length() > 0) {
  182. menus = menus.substring(0, menus.length() - 1);
  183. }
  184. info.setMenus(menus);
  185. } else {
  186. // System.out.println("id:"+ SecurityUtils.getSubject().getSession().getId());
  187. MallUserRole ur = new MallUserRole();
  188. ur.setUid(info.getId());
  189. PageInfo<MallUserRole> page = mallUserRoleService.listAsPage(ur, 1, 1);
  190. if (page.getSize() > 0) {
  191. Long roleId = page.getList().get(0).getRoleId();
  192. MallRolePermission p = new MallRolePermission();
  193. p.setRoleId(roleId);
  194. p.setTenantId(info.getTenantId());
  195. PageInfo<MallRolePermission> listAsPage = mallRolePermissionService.listAsPage(p, 1, 100);
  196. String menus = "";
  197. for (MallRolePermission rp : listAsPage.getList()) {
  198. menus += rp.getPermissionId() + ",";
  199. }
  200. if (menus.length() > 0) {
  201. menus = menus.substring(0, menus.length() - 1);
  202. }
  203. info.setMenus(menus);
  204. }
  205. }
  206. return new ResultData(info);
  207. }
  208. }