package com.ourlinc.helloworld.controller;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.ourlinc.helloworld.model.Sa;
import com.ourlinc.helloworld.model.User;
import com.ourlinc.helloworld.service.UserService;
import com.ourlinc.helloworld.util.WebUtils;
import com.ourlinc.swift.util.Misc;
import com.ourlinc.swift.util.ResultPage;
/**
* 超级管理员控制器
*
* @author lipeiying
*
*/
@Controller
public class SaController {
@Resource(name = "userService")
private UserService m_UserService;
private final static String EMAIL = "lipeiying";
private final static String PASSWORD = "312af04ac0d72c5df7796032f508d3dc";
/**
* 登陆
*
* @param request
* @return
* @throws IOException
*/
@RequestMapping
String login(HttpServletRequest request, HttpServletResponse response)
throws IOException {
String email = request.getParameter("email");
String password = request.getParameter("password");
if (null == email || null == password) {
return "sa/login";
}
// MD5加密密码
String md5Pass = Misc.md5Hash(password);
// 登录成功
if (EMAIL.equals(email) && PASSWORD.equals(md5Pass)) {
Sa sa = new Sa(email, md5Pass);
request.getSession().setAttribute("sa", sa);
response.sendRedirect("/sa/index.jspx");
return null;
} else {
request.setAttribute("errorMsg", "你的email和密码不符,请再试一次");
request.setAttribute("email", email);
request.setAttribute("password", password);
return "sa/login";
}
}
/**
* 退出
*
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping
String logout(HttpServletRequest request, HttpServletResponse response)
throws IOException {
request.getSession().setAttribute("sa", null);
response.sendRedirect("/sa/login.jspx");
return null;
}
@RequestMapping
String index(HttpServletRequest request, HttpServletResponse response)
throws IOException {
Sa sa = (Sa) request.getSession().getAttribute("sa");
if (null == sa) {
return "sa/login";
}
ResultPage<User> rp = m_UserService.listUser();
rp.setPageSize(1000);
rp.gotoPage(1);
request.setAttribute("list", rp);
return "sa/index";
}
/**
* 异步对用户进行操作
*
* @param request
* @param response
* @return
*/
@RequestMapping
String edituser_aj(HttpServletRequest request, HttpServletResponse response) {
Sa sa = (Sa) request.getSession().getAttribute("sa");
if (null == sa) {
return null;
}
String op = WebUtils.toString(request.getParameter("op")).trim();
String id = WebUtils.toString(request.getParameter("id")).trim();
if ("toadmin".equals(op) || "touser".equals(op)) {
User user = m_UserService.getUser(id);
if (null == user) {
return null;
}
// 把用户提升为管理员
if ("toadmin".equals(op)) {
user.userToAdmin();
user.flush();
} else {// 把管理员降为用户
user.adminToUser();
user.flush();
}
}
return null;
}
}