From 26e761a7100b4ef6b6f788d8abaf839976f7c824 Mon Sep 17 00:00:00 2001
From: fengshuonan <sn93@qq.com>
Date: Fri, 1 Jan 2021 20:24:29 +0800
Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E9=A6=96=E9=A1=B5=E4=BF=A1?=
 =?UTF-8?q?=E6=81=AF=E8=8E=B7=E5=8F=96=E5=B0=81=E8=A3=85=E5=88=B0service?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../index/controller/IndexViewController.java | 59 +------------
 .../modular/index/service/IndexService.java   | 87 +++++++++++++++++++
 2 files changed, 91 insertions(+), 55 deletions(-)

diff --git a/src/main/java/cn/stylefeng/guns/modular/index/controller/IndexViewController.java b/src/main/java/cn/stylefeng/guns/modular/index/controller/IndexViewController.java
index 2882ba11..c9fe7e60 100644
--- a/src/main/java/cn/stylefeng/guns/modular/index/controller/IndexViewController.java
+++ b/src/main/java/cn/stylefeng/guns/modular/index/controller/IndexViewController.java
@@ -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";
     }
 
diff --git a/src/main/java/cn/stylefeng/guns/modular/index/service/IndexService.java b/src/main/java/cn/stylefeng/guns/modular/index/service/IndexService.java
index 8e2c37b7..5688bc92 100644
--- a/src/main/java/cn/stylefeng/guns/modular/index/service/IndexService.java
+++ b/src/main/java/cn/stylefeng/guns/modular/index/service/IndexService.java
@@ -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;
+    }
 
 }