diff --git a/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java b/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java index db942a7..6e6b919 100644 --- a/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java +++ b/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java @@ -4,6 +4,9 @@ import lombok.Data; import java.util.Date; +/** + * @author ronger + */ @Data public class ArticleDTO { private Integer idArticle; diff --git a/src/main/java/com/rymcu/vertical/dto/TUser.java b/src/main/java/com/rymcu/vertical/dto/TokenUser.java similarity index 72% rename from src/main/java/com/rymcu/vertical/dto/TUser.java rename to src/main/java/com/rymcu/vertical/dto/TokenUser.java index 3f37530..9ab4e78 100644 --- a/src/main/java/com/rymcu/vertical/dto/TUser.java +++ b/src/main/java/com/rymcu/vertical/dto/TokenUser.java @@ -2,8 +2,11 @@ package com.rymcu.vertical.dto; import lombok.Data; +/** + * @author ronger + */ @Data -public class TUser { +public class TokenUser { private String account; @@ -15,4 +18,6 @@ public class TUser { private String avatarUrl; + private Integer weights; + } diff --git a/src/main/java/com/rymcu/vertical/dto/admin/UserRoleDTO.java b/src/main/java/com/rymcu/vertical/dto/admin/UserRoleDTO.java new file mode 100644 index 0000000..ed325c8 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/dto/admin/UserRoleDTO.java @@ -0,0 +1,13 @@ +package com.rymcu.vertical.dto.admin; + +import lombok.Data; + +/** + * @author ronger + */ +@Data +public class UserRoleDTO { + private Integer idUser; + + private Integer idRole; +} diff --git a/src/main/java/com/rymcu/vertical/entity/Role.java b/src/main/java/com/rymcu/vertical/entity/Role.java index ea795c6..c83c25b 100644 --- a/src/main/java/com/rymcu/vertical/entity/Role.java +++ b/src/main/java/com/rymcu/vertical/entity/Role.java @@ -1,5 +1,6 @@ package com.rymcu.vertical.entity; +import com.alibaba.fastjson.annotation.JSONField; import lombok.Data; import javax.persistence.Column; @@ -9,6 +10,9 @@ import javax.persistence.Table; import java.io.Serializable; import java.util.Date; +/** + * @author ronger + */ @Data @Table(name = "vertical_role") public class Role implements Serializable,Cloneable { @@ -29,6 +33,12 @@ public class Role implements Serializable,Cloneable { @Column(name = "input_code") private String inputCode; + /** + * 权重 + * */ + @Column(name = "weights") + private Integer weights; + /** * 状态 * */ @@ -39,11 +49,13 @@ public class Role implements Serializable,Cloneable { * 创建时间 * */ @Column(name = "created_time") + @JSONField(format = "yyyy-MM-dd HH:mm:ss") private Date createdTime; /** * 更新时间 * */ @Column(name = "updated_time") + @JSONField(format = "yyyy-MM-dd HH:mm:ss") private Date updatedTime; } \ No newline at end of file diff --git a/src/main/java/com/rymcu/vertical/mapper/RoleMapper.java b/src/main/java/com/rymcu/vertical/mapper/RoleMapper.java index 4c2f4f3..d0ac733 100644 --- a/src/main/java/com/rymcu/vertical/mapper/RoleMapper.java +++ b/src/main/java/com/rymcu/vertical/mapper/RoleMapper.java @@ -12,4 +12,6 @@ public interface RoleMapper extends Mapper { List selectRoleByIdUser(@Param("id") Integer id); Role selectRoleByInputCode(@Param("inputCode") String inputCode); + + Integer updateStatus(@Param("idRole") Integer idRole, @Param("status") String status); } \ No newline at end of file diff --git a/src/main/java/com/rymcu/vertical/mapper/UserMapper.java b/src/main/java/com/rymcu/vertical/mapper/UserMapper.java index 18f7676..aa2ed61 100644 --- a/src/main/java/com/rymcu/vertical/mapper/UserMapper.java +++ b/src/main/java/com/rymcu/vertical/mapper/UserMapper.java @@ -17,4 +17,10 @@ public interface UserMapper extends Mapper { UserDTO selectUserDTOByNickname(@Param("nickname") String nickname); Integer updatePasswordByAccount(@Param("account") String account, @Param("password") String password); + + Integer selectRoleWeightsByUser(@Param("idUser") Integer idUser); + + Integer updateUserRole(@Param("idUser") Integer idUser, @Param("idRole") Integer idRole); + + Integer updateStatus(@Param("idUser") Integer idUser, @Param("status") String status); } \ No newline at end of file diff --git a/src/main/java/com/rymcu/vertical/service/RoleService.java b/src/main/java/com/rymcu/vertical/service/RoleService.java index 3ef8e69..066f8e3 100644 --- a/src/main/java/com/rymcu/vertical/service/RoleService.java +++ b/src/main/java/com/rymcu/vertical/service/RoleService.java @@ -5,6 +5,7 @@ import com.rymcu.vertical.entity.Role; import com.rymcu.vertical.entity.User; import java.util.List; +import java.util.Map; /** @@ -13,4 +14,10 @@ import java.util.List; public interface RoleService extends Service { List selectRoleByUser(User sysUser); + + List findByIdUser(Integer idUser); + + Map updateStatus(Integer idRole, String status); + + Map saveRole(Role role); } diff --git a/src/main/java/com/rymcu/vertical/service/UserService.java b/src/main/java/com/rymcu/vertical/service/UserService.java index 44e6105..21e4685 100644 --- a/src/main/java/com/rymcu/vertical/service/UserService.java +++ b/src/main/java/com/rymcu/vertical/service/UserService.java @@ -13,7 +13,6 @@ import java.util.Map; */ public interface UserService extends Service { - User findByAccount(String account) throws TooManyResultsException; Map register(String email, String password, String code); @@ -23,4 +22,8 @@ public interface UserService extends Service { UserDTO findUserDTOByNickname(String nickname); Map forgetPassword(String code, String password); + + Map updateUserRole(Integer idUser, Integer idRole); + + Map updateStatus(Integer idUser, String status); } diff --git a/src/main/java/com/rymcu/vertical/service/impl/RoleServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/RoleServiceImpl.java index 32ee6c2..57501c8 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/RoleServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/RoleServiceImpl.java @@ -9,14 +9,18 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** - * Created by CodeGenerator on 2018/05/29. + * + * @author CodeGenerator + * @date 2018/05/29 */ @Service -@Transactional public class RoleServiceImpl extends AbstractService implements RoleService { @Resource private RoleMapper roleMapper; @@ -27,4 +31,41 @@ public class RoleServiceImpl extends AbstractService implements RoleServic return roles; } + @Override + public List findByIdUser(Integer idUser) { + return roleMapper.selectRoleByIdUser(idUser); + } + + @Override + @Transactional + public Map updateStatus(Integer idRole, String status) { + Map map = new HashMap(1); + Integer result = roleMapper.updateStatus(idRole,status); + if(result == 0) { + map.put("message","更新失败!"); + } + return map; + } + + @Override + public Map saveRole(Role role) { + Integer result = 0; + if (role.getIdRole() == null) { + role.setStatus("0"); + role.setCreatedTime(new Date()); + role.setUpdatedTime(role.getCreatedTime()); + result = roleMapper.insertSelective(role); + } else { + role.setCreatedTime(new Date()); + result = roleMapper.updateByPrimaryKeySelective(role); + } + Map map = new HashMap(1); + if (result == 0) { + map.put("message","操作失败!"); + } else { + map.put("role", role); + } + return map; + } + } diff --git a/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java index 1fa03ee..efda0cc 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java @@ -2,14 +2,13 @@ package com.rymcu.vertical.service.impl; import com.rymcu.vertical.core.service.AbstractService; import com.rymcu.vertical.core.service.redis.RedisService; -import com.rymcu.vertical.dto.TUser; +import com.rymcu.vertical.dto.TokenUser; import com.rymcu.vertical.dto.UserDTO; import com.rymcu.vertical.entity.Role; import com.rymcu.vertical.entity.User; import com.rymcu.vertical.jwt.service.TokenManager; import com.rymcu.vertical.mapper.RoleMapper; import com.rymcu.vertical.mapper.UserMapper; -import com.rymcu.vertical.service.ArticleService; import com.rymcu.vertical.service.UserService; import com.rymcu.vertical.util.BeanCopierUtil; import com.rymcu.vertical.util.Utils; @@ -25,7 +24,9 @@ import java.util.Map; /** - * Created by CodeGenerator on 2018/05/29. + * + * @author CodeGenerator + * @date 2018/05/29 */ @Service public class UserServiceImpl extends AbstractService implements UserService { @@ -85,10 +86,11 @@ public class UserServiceImpl extends AbstractService implements UserServic if(Utils.comparePwd(password, user.getPassword())){ user.setLastLoginTime(new Date()); userMapper.updateByPrimaryKeySelective(user); - TUser tUser = new TUser(); - BeanCopierUtil.copy(user,tUser); - tUser.setToken(tokenManager.createToken(account)); - map.put("user",tUser); + TokenUser tokenUser = new TokenUser(); + BeanCopierUtil.copy(user, tokenUser); + tokenUser.setToken(tokenManager.createToken(account)); + tokenUser.setWeights(userMapper.selectRoleWeightsByUser(user.getIdUser())); + map.put("user", tokenUser); } else { map.put("message","密码错误!"); } @@ -118,4 +120,26 @@ public class UserServiceImpl extends AbstractService implements UserServic } return map; } + + @Override + @Transactional + public Map updateUserRole(Integer idUser, Integer idRole) { + Map map = new HashMap(1); + Integer result = userMapper.updateUserRole(idUser,idRole); + if(result == 0) { + map.put("message","更新失败!"); + } + return map; + } + + @Override + @Transactional + public Map updateStatus(Integer idUser, String status) { + Map map = new HashMap(1); + Integer result = userMapper.updateStatus(idUser,status); + if(result == 0) { + map.put("message","更新失败!"); + } + return map; + } } diff --git a/src/main/java/com/rymcu/vertical/util/UserUtils.java b/src/main/java/com/rymcu/vertical/util/UserUtils.java index 55e12f5..0edbd09 100644 --- a/src/main/java/com/rymcu/vertical/util/UserUtils.java +++ b/src/main/java/com/rymcu/vertical/util/UserUtils.java @@ -1,6 +1,6 @@ package com.rymcu.vertical.util; -import com.rymcu.vertical.dto.TUser; +import com.rymcu.vertical.dto.TokenUser; import com.rymcu.vertical.entity.User; import com.rymcu.vertical.jwt.def.JwtConstants; import com.rymcu.vertical.jwt.model.TokenModel; @@ -13,6 +13,9 @@ import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureException; import org.apache.commons.lang.StringUtils; +/** + * @author ronger + */ public class UserUtils { private static UserMapper userMapper = SpringContextHolder.getBean(UserMapper.class); @@ -46,7 +49,7 @@ public class UserUtils { return null; } - public static TUser getTUser(String token) { + public static TokenUser getTokenUser(String token) { if(StringUtils.isNotBlank(token)){ // 验证token Claims claims = null; @@ -61,10 +64,11 @@ public class UserUtils { if (tokenManager.checkToken(model)) { User user = userMapper.findByAccount(account.toString()); if(user != null){ - TUser tUser = new TUser(); - BeanCopierUtil.copy(user,tUser); - tUser.setToken(token); - return tUser; + TokenUser tokenUser = new TokenUser(); + BeanCopierUtil.copy(user, tokenUser); + tokenUser.setToken(token); + tokenUser.setWeights(userMapper.selectRoleWeightsByUser(user.getIdUser())); + return tokenUser; } } } diff --git a/src/main/java/com/rymcu/vertical/web/api/admin/AdminController.java b/src/main/java/com/rymcu/vertical/web/api/admin/AdminController.java index e509e71..f26205a 100644 --- a/src/main/java/com/rymcu/vertical/web/api/admin/AdminController.java +++ b/src/main/java/com/rymcu/vertical/web/api/admin/AdminController.java @@ -4,9 +4,11 @@ import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.rymcu.vertical.core.result.GlobalResult; import com.rymcu.vertical.core.result.GlobalResultGenerator; -import com.rymcu.vertical.dto.admin.TopicDTO; +import com.rymcu.vertical.dto.admin.UserRoleDTO; +import com.rymcu.vertical.entity.Role; import com.rymcu.vertical.entity.Topic; import com.rymcu.vertical.entity.User; +import com.rymcu.vertical.service.RoleService; import com.rymcu.vertical.service.TopicService; import com.rymcu.vertical.service.UserService; import org.apache.commons.lang.StringUtils; @@ -27,6 +29,8 @@ public class AdminController { @Resource private UserService userService; @Resource + private RoleService roleService; + @Resource private TopicService topicService; @GetMapping("/users") @@ -44,6 +48,57 @@ public class AdminController { return GlobalResultGenerator.genSuccessResult(map); } + @GetMapping("/user/{idUser}/role") + public GlobalResult userRole(@PathVariable Integer idUser){ + List roles = roleService.findByIdUser(idUser); + return GlobalResultGenerator.genSuccessResult(roles); + } + + @GetMapping("/roles") + public GlobalResult roles(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows){ + PageHelper.startPage(page, rows); + List list = roleService.findAll(); + PageInfo pageInfo = new PageInfo(list); + Map map = new HashMap(2); + map.put("roles", pageInfo.getList()); + Map pagination = new HashMap(3); + pagination.put("pageSize",pageInfo.getPageSize()); + pagination.put("total",pageInfo.getTotal()); + pagination.put("currentPage",pageInfo.getPageNum()); + map.put("pagination", pagination); + return GlobalResultGenerator.genSuccessResult(map); + } + + @PatchMapping("/user/update-role") + public GlobalResult updateUserRole(@RequestBody UserRoleDTO userRole){ + Map map = userService.updateUserRole(userRole.getIdUser(),userRole.getIdRole()); + return GlobalResultGenerator.genSuccessResult(map); + } + + @PatchMapping("/user/update-status") + public GlobalResult updateUserStatus(@RequestBody User user){ + Map map = userService.updateStatus(user.getIdUser(),user.getStatus()); + return GlobalResultGenerator.genSuccessResult(map); + } + + @PatchMapping("/role/update-status") + public GlobalResult updateRoleStatus(@RequestBody Role role){ + Map map = roleService.updateStatus(role.getIdRole(),role.getStatus()); + return GlobalResultGenerator.genSuccessResult(map); + } + + @PostMapping("/role/post") + public GlobalResult addRole(@RequestBody Role role){ + Map map = roleService.saveRole(role); + return GlobalResultGenerator.genSuccessResult(map); + } + + @PutMapping("/role/post") + public GlobalResult updateRole(@RequestBody Role role){ + Map map = roleService.saveRole(role); + return GlobalResultGenerator.genSuccessResult(map); + } + @GetMapping("/topics") public GlobalResult topics(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows){ PageHelper.startPage(page, rows); @@ -64,8 +119,8 @@ public class AdminController { if (StringUtils.isBlank(topicUri)) { return GlobalResultGenerator.genErrorResult("数据异常!"); } - TopicDTO topic = topicService.findTopicByTopicUri(topicUri,page,rows); - return GlobalResultGenerator.genSuccessResult(); + Map map = topicService.findTopicByTopicUri(topicUri,page,rows); + return GlobalResultGenerator.genSuccessResult(map); } } diff --git a/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java b/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java index 2d65ff2..3946017 100644 --- a/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java +++ b/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java @@ -2,13 +2,12 @@ package com.rymcu.vertical.web.api.common; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; -import com.rymcu.vertical.core.exception.ServiceException; import com.rymcu.vertical.core.result.GlobalResult; import com.rymcu.vertical.core.result.GlobalResultGenerator; import com.rymcu.vertical.core.result.GlobalResultMessage; import com.rymcu.vertical.dto.ArticleDTO; import com.rymcu.vertical.dto.ForgetPasswordDTO; -import com.rymcu.vertical.dto.TUser; +import com.rymcu.vertical.dto.TokenUser; import com.rymcu.vertical.entity.User; import com.rymcu.vertical.service.ArticleService; import com.rymcu.vertical.service.JavaMailService; @@ -19,7 +18,6 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.mail.MessagingException; -import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -121,8 +119,8 @@ public class CommonApiController { @GetMapping("/token/{token}") public GlobalResult token(@PathVariable String token){ - TUser tUser = UserUtils.getTUser(token); - return GlobalResultGenerator.genSuccessResult(tUser); + TokenUser tokenUser = UserUtils.getTokenUser(token); + return GlobalResultGenerator.genSuccessResult(tokenUser); } @PatchMapping("/forget-password") diff --git a/src/main/java/com/rymcu/vertical/web/api/common/UploadController.java b/src/main/java/com/rymcu/vertical/web/api/common/UploadController.java index 4ff0f64..0146f1a 100644 --- a/src/main/java/com/rymcu/vertical/web/api/common/UploadController.java +++ b/src/main/java/com/rymcu/vertical/web/api/common/UploadController.java @@ -2,7 +2,7 @@ package com.rymcu.vertical.web.api.common; import com.rymcu.vertical.core.result.GlobalResult; import com.rymcu.vertical.core.result.GlobalResultGenerator; -import com.rymcu.vertical.dto.TUser; +import com.rymcu.vertical.dto.TokenUser; import com.rymcu.vertical.jwt.def.JwtConstants; import com.rymcu.vertical.util.FileUtils; import com.rymcu.vertical.util.UserUtils; @@ -128,9 +128,9 @@ public class UploadController { if(StringUtils.isBlank(authHeader)){ throw new BaseApiException(ErrorCode.UNAUTHORIZED); } - TUser tUser = UserUtils.getTUser(authHeader); + TokenUser tokenUser = UserUtils.getTokenUser(authHeader); Map map = new HashMap(2); - map.put("uploadToken",tUser.getToken()); + map.put("uploadToken", tokenUser.getToken()); map.put("uploadURL", UPLOAD_URL); return GlobalResultGenerator.genSuccessResult(map); } diff --git a/src/main/java/mapper/RoleMapper.xml b/src/main/java/mapper/RoleMapper.xml index 1ac3699..41720e4 100644 --- a/src/main/java/mapper/RoleMapper.xml +++ b/src/main/java/mapper/RoleMapper.xml @@ -8,16 +8,14 @@ + - - - - - - + + update vertical_role set status = #{status},updated_time = sysdate() where id = #{idRole} + select id, nickname, account, password, status from vertical_user where account = #{account} AND status = 0 @@ -54,5 +60,8 @@ + \ No newline at end of file