memberSystem/src/main/java/com/ifish/action/PublicAction.java

171 lines
5.8 KiB
Java

package com.ifish.action;
import com.ifish.entity.Menu;
import com.ifish.entity.Role;
import com.ifish.entity.SecurityUser;
import com.ifish.enums.RoleEnum;
import com.ifish.enums.SysUserEnum;
import com.ifish.menu.IfishMenu;
import com.ifish.service.AdminService;
import com.ifish.service.PublicService;
import com.ifish.ueditor.ActionEnter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* @ClassName: PublicAction
* @Description: 公共页面
* @author ggw
*/
@Controller("publicAction")
@RequestMapping("/public")
public class PublicAction {
@Autowired
private PublicService publicService;
@Autowired
private AdminService adminService;
private static Logger log = LoggerFactory.getLogger(PublicAction.class);
/**
* 登陆页面
*
* @return
*/
@RequestMapping("/login.do")
public ModelAndView login(String status) {
return new ModelAndView("roleAll/login", "status", status);
}
/**
* 文本编辑器配置
*
* @param request
* @param response
*/
@RequestMapping("/config.do")
public void config(HttpServletRequest request, HttpServletResponse response) {
try {
request.setCharacterEncoding("utf-8");
response.setHeader("Content-Type", "text/html");
response.setContentType("application/json");
String rootPath = request.getSession().getServletContext().getRealPath("/");
String exec = new ActionEnter(request, rootPath).exec();
PrintWriter writer = response.getWriter();
writer.write(exec);
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 主页
*
* @return
*/
@RequestMapping("/index.do")
public ModelAndView index() {
String role = null;
String userName = null;
try {
//登录用户
UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
userName = userDetails.getUsername();
//根据登录用户角色类型跳转
Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
for (GrantedAuthority grantedAuthority : authorities) {
role = grantedAuthority.getAuthority();
}
//菜单初始化
if (role != null && userName != null) {
Role curRole = this.adminService.get(role);
List<Menu> menuList = new ArrayList<Menu>();
menuList.addAll(curRole.getMenuList());
//默认选择菜单
IfishMenu.initChioceMenu(userName, null);
//用户菜单
if (userName != null) {
IfishMenu.initMenu(userName, menuList);
}
}
//重定向
String redirect = "";
if (role.equals(RoleEnum.admin.getKey())) {
redirect = "page/admin/home.do";
} else if (role.equals(RoleEnum.user.getKey())) {
redirect = "page/user/home.do";
}
return new ModelAndView("redirect:/" + redirect);
} catch (Exception e) {
e.printStackTrace();
log.error("index page:role:{},error message:{}", role, e.toString());
}
return new ModelAndView("redirect:login.do");
}
/**
* 修改密码页面
*
* @return
*/
@RequestMapping("/changePwd.do")
public ModelAndView changePwd(String status) {
return new ModelAndView("roleAll/changePwd", "status", status);
}
/**
* 修改密码
*
* @return
*/
@RequestMapping("/savePwd.do")
public ModelAndView savePwd(String username, String oldPassword, String newPassword) {
try {
SecurityUser securityUser = publicService.updateSecurityPwd(username, oldPassword, newPassword);
if (securityUser != null) {
return new ModelAndView("redirect:/page/public/changePwd.do", "status", SysUserEnum.success100.getKey());
}
return new ModelAndView("redirect:/page/public/changePwd.do", "status", SysUserEnum.warn105.getKey());
} catch (Exception e) {
log.error("update password:userName:{},error message:{}", username, e.toString());
}
return new ModelAndView("redirect:/page/public/changePwd.do", "status", SysUserEnum.failed101.getKey());
}
/**
* 用户注册
*
* @param securityUser
* @return
*/
@RequestMapping("/signup.do")
public ModelAndView signup(SecurityUser securityUser) {
try {
String code = publicService.createSysUser(securityUser);
return new ModelAndView("redirect:/page/public/login.do", "status", code);
} catch (Exception e) {
log.error("securityUser register:userName:{},error message:{}", securityUser.getUsername(), e.toString());
}
return new ModelAndView("redirect:/page/public/login.do", "status", SysUserEnum.failed101.getKey());
}
}