整理首页信息获取封装到service

pull/65/head
fengshuonan 4 years ago
parent c3532f7918
commit 26e761a710

@ -1,24 +1,14 @@
package cn.stylefeng.guns.modular.index.controller;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.index.service.IndexService;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleRoleInfo;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleUserInfo;
import cn.stylefeng.roses.kernel.menu.modular.service.SysMenuService;
import cn.stylefeng.roses.kernel.resource.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.resource.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.system.modular.organization.entity.HrOrganization;
import cn.stylefeng.roses.kernel.system.modular.organization.service.HrOrganizationService;
import cn.stylefeng.roses.kernel.system.modular.user.service.SysUserService;
import cn.stylefeng.roses.kernel.system.pojo.menu.layui.LayuiAppIndexMenus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import javax.annotation.Resource;
import java.util.List;
/**
*
@ -32,13 +22,7 @@ import java.util.List;
public class IndexViewController {
@Resource
private SysMenuService sysMenuService;
@Resource
private SysUserService sysUserService;
@Resource
private HrOrganizationService hrOrganizationService;
private IndexService indexService;
/**
*
@ -51,20 +35,7 @@ public class IndexViewController {
// 当前用户已经登录,跳转到首页
if (LoginContext.me().hasLogin()) {
LoginUser loginUser = LoginContext.me().getLoginUser();
SimpleUserInfo simpleUserInfo = loginUser.getSimpleUserInfo();
// 渲染首页的菜单
List<LayuiAppIndexMenus> layuiAppIndexMenus = sysMenuService.getLayuiIndexMenus();
model.addAttribute("layuiAppIndexMenus", layuiAppIndexMenus);
// 获取首页的头像
model.addAttribute("avatar", sysUserService.getUserAvatarUrl(simpleUserInfo.getAvatar()));
// 获取人员姓名
model.addAttribute("name", simpleUserInfo.getRealName());
model.addAllAttributes(indexService.createIndexRenderAttributes());
return "/index.html";
}
@ -80,29 +51,7 @@ public class IndexViewController {
*/
@GetResource(name = "个人中心界面", path = "/personal", requiredLogin = false)
public String personal(Model model) {
LoginUser loginUser = LoginContext.me().getLoginUser();
// 用户基本信息
SimpleUserInfo simpleUserInfo = loginUser.getSimpleUserInfo();
model.addAllAttributes(BeanUtil.beanToMap(simpleUserInfo));
// 角色名称
List<SimpleRoleInfo> simpleRoleInfoList = loginUser.getSimpleRoleInfoList();
if (ObjectUtil.isNotEmpty(simpleRoleInfoList)) {
String roleName = simpleRoleInfoList.get(0).getRoleName();
model.addAttribute("roleName", roleName);
}
// 组织机构名称
Long organizationId = loginUser.getOrganizationId();
HrOrganization hrOrganization = hrOrganizationService.getById(organizationId);
if (hrOrganization != null) {
model.addAttribute("orgName", hrOrganization.getOrgName());
}
// 渲染头像的url
model.addAttribute("avatar", sysUserService.getUserAvatarUrl(simpleUserInfo.getAvatar()));
model.addAllAttributes(indexService.createPersonInfoRenderAttributes());
return "/modular/index/personal_info.html";
}

@ -1,7 +1,23 @@
package cn.stylefeng.guns.modular.index.service;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleRoleInfo;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleUserInfo;
import cn.stylefeng.roses.kernel.menu.modular.service.SysMenuService;
import cn.stylefeng.roses.kernel.system.modular.organization.entity.HrOrganization;
import cn.stylefeng.roses.kernel.system.modular.organization.service.HrOrganizationService;
import cn.stylefeng.roses.kernel.system.modular.user.service.SysUserService;
import cn.stylefeng.roses.kernel.system.pojo.menu.layui.LayuiAppIndexMenus;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
*
@ -11,5 +27,76 @@ import org.springframework.stereotype.Service;
@Service
public class IndexService {
@Resource
private SysMenuService sysMenuService;
@Resource
private SysUserService sysUserService;
@Resource
private HrOrganizationService hrOrganizationService;
/**
*
*
* @author fengshuonan
* @date 2021/1/1 18:27
*/
public Map<String, Object> createIndexRenderAttributes() {
HashMap<String, Object> renderMap = new HashMap<>();
LoginUser loginUser = LoginContext.me().getLoginUser();
SimpleUserInfo simpleUserInfo = loginUser.getSimpleUserInfo();
// 渲染首页的菜单
List<LayuiAppIndexMenus> layuiAppIndexMenus = sysMenuService.getLayuiIndexMenus();
renderMap.put("layuiAppIndexMenus", layuiAppIndexMenus);
// 获取首页的头像
renderMap.put("avatar", sysUserService.getUserAvatarUrl(simpleUserInfo.getAvatar()));
// 获取人员姓名
renderMap.put("name", simpleUserInfo.getRealName());
return renderMap;
}
/**
*
*
* @author fengshuonan
* @date 2021/1/1 18:38
*/
public Map<String, Object> createPersonInfoRenderAttributes() {
HashMap<String, Object> renderMap = new HashMap<>();
renderMap.put("roleName", "角色空");
renderMap.put("orgName", "组织架构空");
// 添加用户基本信息字段
LoginUser loginUser = LoginContext.me().getLoginUser();
SimpleUserInfo simpleUserInfo = loginUser.getSimpleUserInfo();
renderMap.putAll(BeanUtil.beanToMap(simpleUserInfo));
// 角色名称
List<SimpleRoleInfo> simpleRoleInfoList = loginUser.getSimpleRoleInfoList();
if (ObjectUtil.isNotEmpty(simpleRoleInfoList)) {
String roleName = simpleRoleInfoList.get(0).getRoleName();
renderMap.put("roleName", roleName);
}
// 组织机构名称
Long organizationId = loginUser.getOrganizationId();
HrOrganization hrOrganization = hrOrganizationService.getById(organizationId);
if (hrOrganization != null) {
renderMap.put("orgName", hrOrganization.getOrgName());
}
// 渲染头像的url
renderMap.put("avatar", sysUserService.getUserAvatarUrl(simpleUserInfo.getAvatar()));
return renderMap;
}
}

Loading…
Cancel
Save