package com.simple.controller; import com.github.pagehelper.PageInfo; import com.simple.common.ResultData; import com.simple.domain.po.*; import com.simple.service.*; import com.simple.shiro.PasswordHelper; import com.simple.shiro.UserSession; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.shiro.SecurityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.Assert; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.Map; /** * @author chenkx * @date 2018-01-05. */ @Api(value = "API - UserInfoController", description = "用户接口") @RestController @RequestMapping("user") public class MallUserInfoController extends BaseController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired MallUserInfoService userInfoService; @Autowired MallUserRoleService userRoleService; @Autowired MallRoleService mallRoleService; @Autowired MallUserRoleService mallUserRoleService; @Autowired MallPermissionService mallPermissionService; @Autowired MallRolePermissionService mallRolePermissionService; @ApiOperation(value = "用户分页接口", response = String.class) @GetMapping("lists") public ResultData listAsPage(MallUserInfo userInfo, Integer pageNum, Integer pageSize) { userInfo.setTenantId(getTenantId()); final PageInfo page = userInfoService.listAsPage(userInfo, pageNum, pageSize); for (MallUserInfo u : page.getList()) { MallUserRole r = new MallUserRole(); r.setUid(u.getId()); PageInfo ur = userRoleService.listAsPage(r, 1, 1); if (ur.getSize() > 0) { MallRole role = mallRoleService.getById(ur.getList().get(0).getRoleId()); if (role != null) { u.setRoleName(role.getName()); u.setRoleId(role.getId()); } } u.setPassword(null);//不返回密码 } return new ResultData(page); } @ApiOperation(value = "用户详情接口", response = String.class) @GetMapping("detail") public ResultData detail(Long id) { final MallUserInfo user = userInfoService.getById(id); return new ResultData(user); } @ApiOperation(value = "创建用户接口", response = String.class) @PostMapping("add") public ResultData createUser(@RequestBody MallUserInfo userInfo) { Assert.notNull(userInfo.getUsername(), "用户名不能为空"); Assert.isTrue(checkUnique(userInfo.getUsername(), 0), "重复的用户名"); Assert.notNull(userInfo.getPassword(), "密码不能为空"); PasswordHelper passwordHelper = new PasswordHelper(); passwordHelper.encryptPassword(userInfo); userInfo.setTenantId(getTenantId()); userInfoService.saveOrUpdate(userInfo); if (userInfo.getRoleId() != null) { MallUserRole r = new MallUserRole(); r.setRoleId(userInfo.getRoleId()); r.setUid(userInfo.getId()); userRoleService.saveOrUpdate(r); } return new ResultData(); } @ApiOperation(value = "修改用户接口", response = String.class) @PostMapping("update") public ResultData updateUser(@RequestBody MallUserInfo userInfo) { Assert.notNull(userInfo.getUsername(), "用户名不能为空"); Assert.isTrue(checkUnique(userInfo.getUsername(), 1), "重复的用户名"); // Assert.notNull(userInfo.getPassword(), "密码不能为空"); // PasswordHelper passwordHelper = new PasswordHelper(); // passwordHelper.encryptPassword(userInfo); userInfo.setPassword(null); userInfo.setTenantId(getTenantId()); userInfoService.saveOrUpdate(userInfo); if (userInfo.getRoleId() != null) { userRoleService.deleteByUserId(userInfo.getId()); MallUserRole r = new MallUserRole(); r.setRoleId(userInfo.getRoleId()); r.setUid(userInfo.getId()); userRoleService.saveOrUpdate(r); } return new ResultData(); } @ApiOperation(value = "删除用户接口", response = String.class) @PostMapping("/del") public ResultData deleteUser(@RequestBody MallUserInfo userInfo) { userInfoService.deleteById(userInfo.getId()); return new ResultData(); } /** * 启用,停用用户 * * @param userId * @param status * @return */ @ApiOperation(value = "起停用户接口", response = String.class) @GetMapping("status") public ResultData modifyStatus(Long userId, Integer status) { MallUserInfo userInfo = userInfoService.getById(userId); userInfo.setStatus(status); userInfo.setTenantId(getTenantId()); userInfoService.saveOrUpdate(userInfo); return new ResultData(userInfo); } @ApiOperation(value = "用户添加角色接口", response = String.class) @PostMapping("setRoles") public ResultData setRoles(Long userId, String roleIds) { /*String[] roIds = roleIds.split(","); List u2rs = new ArrayList<>(roIds.length); for (String roleId : roIds) { MallUserRole userRole = new MallUserRole(); userRole.setRoleId(roleId); userRole.setUid(userId); u2rs.add(userRole); } userRoleService.batchInsert(u2rs);*/ return new ResultData(); } private boolean checkUnique(String username, Integer type) { if (type == 0) { if (userInfoService.cntByUserName(username) > 0) { return false; } } if (type == 1) { if (userInfoService.cntByUserName(username) > 1) { return false; } } return true; } @GetMapping("hasButtonPermission") public ResultData hasButtonPermission(String permissions) { Map map = new HashMap<>(); for (String name : permissions.split(",")) { Long userId = (Long) SecurityUtils.getSubject().getSession().getAttribute(UserSession.userId); boolean has = userInfoService.hasButtonPermission(userId, name); map.put(name, has); } // StringBuffer buffer = new StringBuffer(); // for(Entry entrySet :map.entrySet()){ // buffer.append("var "+entrySet.getKey()); // buffer.append("="+entrySet.getValue()); // buffer.append(";"); // } //return new ResultData(buffer.toString()); return new ResultData(map); } @GetMapping("getUser") public ResultData getUserInfo() { MallUserInfo info = (MallUserInfo) SecurityUtils.getSubject().getSession().getAttribute(UserSession.userInfo); info.setPassword("保密"); if (info.getIsAdmin() == 1) { // isAdmin MallPermission q = new MallPermission(); PageInfo list = mallPermissionService.listAsPage(q, 1, 100); String menus = ""; for (MallPermission rp : list.getList()) { menus += rp.getId() + ","; } if (menus.length() > 0) { menus = menus.substring(0, menus.length() - 1); } info.setMenus(menus); } else { // System.out.println("id:"+ SecurityUtils.getSubject().getSession().getId()); MallUserRole ur = new MallUserRole(); ur.setUid(info.getId()); PageInfo page = mallUserRoleService.listAsPage(ur, 1, 1); if (page.getSize() > 0) { Long roleId = page.getList().get(0).getRoleId(); MallRolePermission p = new MallRolePermission(); p.setRoleId(roleId); p.setTenantId(info.getTenantId()); PageInfo listAsPage = mallRolePermissionService.listAsPage(p, 1, 100); String menus = ""; for (MallRolePermission rp : listAsPage.getList()) { menus += rp.getPermissionId() + ","; } if (menus.length() > 0) { menus = menus.substring(0, menus.length() - 1); } info.setMenus(menus); } } return new ResultData(info); } }