From 3c7d16b0ff52035d456065430e219cb4fb46e0ca Mon Sep 17 00:00:00 2001 From: "wangiegie@gmail.com" Date: Sat, 11 Nov 2017 17:50:49 +0800 Subject: [PATCH] =?UTF-8?q?end:=20=E6=94=AF=E6=8C=81vuex=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E6=8C=89=E9=92=AE=E6=98=BE=E7=A4=BA=E6=9D=83?= =?UTF-8?q?=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pig/admin/controller/MenuController.java | 2 +- .../pig/admin/controller/UserController.java | 20 ++++++- .../com/github/pig/admin/dto/UserInfo.java | 55 +++++++++++++++++++ .../com/github/pig/admin/entity/SysUser.java | 26 +++++++++ .../pig/admin/service/SysMenuService.java | 8 +++ .../service/impl/SysMenuServiceImpl.java | 21 +++++++ .../main/resources/mapper/SysUserMapper.xml | 8 +++ .../java/com/github/pig/common/vo/UserVo.java | 25 +++++++++ .../github/pig/common/web/BaseController.java | 4 +- 9 files changed, 164 insertions(+), 5 deletions(-) create mode 100644 pig-admin-service/src/main/java/com/github/pig/admin/dto/UserInfo.java diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/controller/MenuController.java b/pig-admin-service/src/main/java/com/github/pig/admin/controller/MenuController.java index c264c275..00cc7696 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/controller/MenuController.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/controller/MenuController.java @@ -56,7 +56,7 @@ public class MenuController extends BaseController { */ @GetMapping(value = "/userTree") public List userTree(HttpServletRequest request) { - Set menus = menuService.findMenuByRole(getRole()); + Set menus = menuService.findMenuByRole(getRole().get(0)); List menuList = new ArrayList<>(); for (MenuVo menuVo : menus) { menuList.add(menuVo.getMenuId()); diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/controller/UserController.java b/pig-admin-service/src/main/java/com/github/pig/admin/controller/UserController.java index 48eb2165..a56fd5b6 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/controller/UserController.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/controller/UserController.java @@ -3,8 +3,10 @@ package com.github.pig.admin.controller; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.plugins.Page; import com.github.pig.admin.dto.UserDto; +import com.github.pig.admin.dto.UserInfo; import com.github.pig.admin.entity.SysUser; import com.github.pig.admin.entity.SysUserRole; +import com.github.pig.admin.service.SysMenuService; import com.github.pig.admin.service.SysUserRoleService; import com.github.pig.admin.service.UserService; import com.github.pig.common.constant.CommonConstant; @@ -27,6 +29,8 @@ public class UserController extends BaseController { private UserService userService; @Autowired private SysUserRoleService sysUserRoleService; + @Autowired + private SysMenuService menuService; /** * 获取当前用户的用户名 @@ -34,8 +38,20 @@ public class UserController extends BaseController { * @return 用户名 */ @GetMapping("/info") - public String user() { - return getUser(); + public UserInfo user() { + SysUser condition = new SysUser(); + condition.setUsername(getUser()); + SysUser sysUser = userService.selectOne(new EntityWrapper<>(condition)); + + UserInfo userInfo = new UserInfo(); + userInfo.setSysUser(sysUser); + //设置角色列表 + String[] roles = getRole().toArray(new String[getRole().size()]); + userInfo.setRoles(roles); + //设置权限列表(menu.permission) + String[] permissions = menuService.findPermission(roles); + userInfo.setPermissions(permissions); + return userInfo; } /** diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/dto/UserInfo.java b/pig-admin-service/src/main/java/com/github/pig/admin/dto/UserInfo.java new file mode 100644 index 00000000..a6413c03 --- /dev/null +++ b/pig-admin-service/src/main/java/com/github/pig/admin/dto/UserInfo.java @@ -0,0 +1,55 @@ +package com.github.pig.admin.dto; + +import com.github.pig.admin.entity.SysUser; + +import java.io.Serializable; + +/** + * @author lengleng + * @date 2017/11/11 + *

+ * commit('SET_ROLES', data) + * commit('SET_NAME', data) + * commit('SET_AVATAR', data) + * commit('SET_INTRODUCTION', data) + * commit('SET_PERMISSIONS', data) + */ +public class UserInfo implements Serializable { + /** + * 用户基本信息 + */ + private SysUser sysUser; + /** + * 权限标识集合 + */ + private String[] permissions; + + /** + * 角色集合 + */ + private String[] roles; + + public String[] getPermissions() { + return permissions; + } + + public void setPermissions(String[] permissions) { + this.permissions = permissions; + } + + public String[] getRoles() { + return roles; + } + + public void setRoles(String[] roles) { + this.roles = roles; + } + + public SysUser getSysUser() { + return sysUser; + } + + public void setSysUser(SysUser sysUser) { + this.sysUser = sysUser; + } +} diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/entity/SysUser.java b/pig-admin-service/src/main/java/com/github/pig/admin/entity/SysUser.java index bd3d0933..47f6e29e 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/entity/SysUser.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/entity/SysUser.java @@ -54,6 +54,16 @@ public class SysUser extends Model { @TableField("del_flag") private String delFlag; + /** + * 简介 + */ + private String introduction; + /** + * 头像 + */ + private String avatar; + + public Integer getUserId() { return userId; @@ -116,6 +126,22 @@ public class SysUser extends Model { return this.userId; } + public String getIntroduction() { + return introduction; + } + + public void setIntroduction(String introduction) { + this.introduction = introduction; + } + + public String getAvatar() { + return avatar; + } + + public void setAvatar(String avatar) { + this.avatar = avatar; + } + @Override public String toString() { return "SysUser{" + diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/service/SysMenuService.java b/pig-admin-service/src/main/java/com/github/pig/admin/service/SysMenuService.java index 86bc6b3d..541b2ede 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/service/SysMenuService.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/service/SysMenuService.java @@ -23,4 +23,12 @@ public interface SysMenuService extends IService { * @return 菜单列表 */ Set findMenuByRole(String role); + + /** + * 通过角色获取菜单权限列表 + * + * @param roles 角色 + * @return 权限列表 + */ + String[] findPermission(String[] roles); } diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/SysMenuServiceImpl.java b/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/SysMenuServiceImpl.java index d7273589..87b8c315 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/SysMenuServiceImpl.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/SysMenuServiceImpl.java @@ -5,10 +5,12 @@ import com.github.pig.admin.entity.SysMenu; import com.github.pig.admin.mapper.SysMenuMapper; import com.github.pig.admin.service.SysMenuService; import com.github.pig.common.vo.MenuVo; +import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; +import java.util.HashSet; import java.util.Set; /** @@ -29,4 +31,23 @@ public class SysMenuServiceImpl extends ServiceImpl impl public Set findMenuByRole(String role) { return sysMenuMapper.findMenuByRoleName(role); } + + @Override + public String[] findPermission(String[] roles) { + Set menuVoSet = new HashSet<>(); + for (String role : roles) { + Set menuVos = findMenuByRole(role); + menuVoSet.addAll(menuVos); + } + + Set permissions = new HashSet<>(); + for (MenuVo menuVo : menuVoSet) { + if (StringUtils.isNotEmpty(menuVo.getPermission())) { + String permission = menuVo.getPermission(); + permissions.add(permission); + } + } + + return permissions.toArray(new String[permissions.size()]); + } } diff --git a/pig-admin-service/src/main/resources/mapper/SysUserMapper.xml b/pig-admin-service/src/main/resources/mapper/SysUserMapper.xml index 3e7e145d..0daa64e0 100644 --- a/pig-admin-service/src/main/resources/mapper/SysUserMapper.xml +++ b/pig-admin-service/src/main/resources/mapper/SysUserMapper.xml @@ -6,6 +6,8 @@ + + @@ -18,6 +20,8 @@ + + @@ -36,6 +40,8 @@ `user`.username, `user`.`password`, `user`.salt, + `user`.introduction, + `user`.avatar, `user`.create_time AS ucreate_time, `user`.update_time AS uupdate_time, `user`.del_flag AS udel_flag, @@ -60,6 +66,8 @@ `user`.username, `user`.`password`, `user`.salt, + `user`.introduction, + `user`.avatar, `user`.create_time AS ucreate_time, `user`.update_time AS uupdate_time, `user`.del_flag AS udel_flag, diff --git a/pig-common/src/main/java/com/github/pig/common/vo/UserVo.java b/pig-common/src/main/java/com/github/pig/common/vo/UserVo.java index 1a559332..abfc342a 100644 --- a/pig-common/src/main/java/com/github/pig/common/vo/UserVo.java +++ b/pig-common/src/main/java/com/github/pig/common/vo/UserVo.java @@ -40,6 +40,15 @@ public class UserVo implements Serializable { * 0-正常,1-删除 */ private String delFlag; + /** + * 简介 + */ + private String introduction; + /** + * 头像 + */ + private String avatar; + /** * 角色列表 */ @@ -108,4 +117,20 @@ public class UserVo implements Serializable { public void setRoleList(List roleList) { this.roleList = roleList; } + + public String getIntroduction() { + return introduction; + } + + public void setIntroduction(String introduction) { + this.introduction = introduction; + } + + public String getAvatar() { + return avatar; + } + + public void setAvatar(String avatar) { + this.avatar = avatar; + } } diff --git a/pig-common/src/main/java/com/github/pig/common/web/BaseController.java b/pig-common/src/main/java/com/github/pig/common/web/BaseController.java index 2f10b41e..2abefbd9 100644 --- a/pig-common/src/main/java/com/github/pig/common/web/BaseController.java +++ b/pig-common/src/main/java/com/github/pig/common/web/BaseController.java @@ -36,12 +36,12 @@ public class BaseController { * * @return 角色名 */ - public String getRole() { + public List getRole() { String authorization = request.getHeader(CommonConstant.REQ_HEADER); String token = StringUtils.substringAfter(authorization, CommonConstant.TOKEN_SPLIT); String key = Base64.getEncoder().encodeToString(CommonConstant.SIGN_KEY.getBytes()); Claims claims = Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody(); List roleNames = (List) claims.get("authorities"); - return roleNames.get(0); + return roleNames; } }