/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package zthh.manager.czl.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import zthh.manager.commons.Md5Code;
import zthh.manager.commons.WebUtil;
import zthh.manager.czl.biz.IEmployeeBiz;
import zthh.manager.entity.Employee;
/**
* MyEclipse Struts Creation date: 03-08-2010
*
* XDoclet definition:
*
* @struts.action parameter="op" validate="true"
* @struts.action-forward name="toEmployee" path="/index.jsp"
*/
public class LoginAction extends DispatchAction {
private IEmployeeBiz employeeBiz;
public void setEmployeeBiz(IEmployeeBiz employeeBiz) {
this.employeeBiz = employeeBiz;
}
/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws IOException
*/
@SuppressWarnings("unchecked")
public ActionForward doLogin(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
String loginid = request.getParameter("loginID");
String pwd = request.getParameter("pwd");
String saveLogin = request.getParameter("saveLogin");
PrintWriter out = response.getWriter();
if (loginid != null && !"".equals(loginid) && pwd != null
&& !"".equals(pwd)) {
pwd = Md5Code.EncoderByMd5(pwd.trim());
Employee employee = employeeBiz.getEmployeeByLogin(loginid, pwd);
if (employee != null && employee.getId() != null) {
if (employee.getEnabled() == 1) {
HttpSession session = request.getSession();
ServletContext servletContext = session.getServletContext();
Boolean temp = true;
if (servletContext.getAttribute("eList") != null) {
List<Integer> list = (List<Integer>) servletContext
.getAttribute("eList");
if (list.contains(employee.getId())) {
temp = false;
} else {
list.add(employee.getId());
servletContext.setAttribute("eList", list);
}
} else {
List<Integer> list = new ArrayList<Integer>();
list.add(employee.getId());
servletContext.setAttribute("eList", list);
}
if (temp) {
employee.setLoginTime(new Date());
employeeBiz.updateEmployee(employee);
session.setAttribute("Employee", employee);
if (saveLogin != null && !"0".equals(saveLogin)) {
WebUtil.addCookie(response, "zthhloginID", loginid,
Integer.valueOf(saveLogin) * 24 * 60 * 60);
WebUtil.addCookie(response, "zthhpwd", pwd, Integer
.valueOf(saveLogin) * 24 * 60 * 60);
}
out
.print("<script type='text/javascript'>alert('您已成功登录!');location.href='login.do?op=doShowIndex';</script>");
} else {
out
.print("<script type='text/javascript'>alert('该账号已登录!');location.href='index.jsp';</script>");
}
} else {
switch (employee.getEnabled()) {
case 2:
out
.print("<script type='text/javascript'>alert('该账号已不能使用!');location.href='index.jsp';</script>");
break;
default:
out
.print("<script type='text/javascript'>alert('该账号已不能使用!');location.href='index.jsp';</script>");
break;
}
}
} else {
out
.print("<script type='text/javascript'>alert('登陆名或密码错误!');location.href='index.jsp';</script>");
}
} else {
out
.print("<script type='text/javascript'>alert('请正确填写登陆名或密码!');location.href='index.jsp';</script>");
}
return null;
}
/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws IOException
* @throws IOException
*/
public ActionForward doShowIndex(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
HttpSession session = request.getSession();
Employee employee = (Employee) session.getAttribute("Employee");
if (employee != null && employee.getId() != null) {
switch (employee.getPower()) {
case 1:
return mapping.findForward("toGM");
case 2:
case 3:
case 4:
return mapping.findForward("toSell");
default:
session.removeAttribute("Employee");
break;
}
}
PrintWriter out = response.getWriter();
out
.print("<script type='text/javascript'>alert('请先登录!');location.href='index.jsp';</script>");
return null;
}
/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws IOException
* @throws IOException
* @throws IOException
*/
@SuppressWarnings("unchecked")
public ActionForward doLogout(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
HttpSession session = request.getSession();
Employee employee = (Employee) session.getAttribute("Employee");
if (employee != null && employee.getId() != null) {
ServletContext servletContext = session.getServletContext();
if (servletContext.getAttribute("eList") != null) {
List<Integer> list = (List<Integer>) servletContext
.getAttribute("eList");
if (list.remove(employee.getId())) {
servletContext.setAttribute("eList", list);
}
}
}
session.removeAttribute("Employee");
WebUtil.addCookie(response, "zthhloginID", "", 0);
WebUtil.addCookie(response, "zthhpwd", "", 0);
PrintWriter out = response.getWriter();
out
.print("<script type='text/javascript'>location.href='index.jsp';</script>");
return null;
}
/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws IOException
* @throws IOException
* @throws IOException
* @throws IOException
*/
public ActionForward doShowTip(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
HttpSession session = request.getSession();
Employee employee = (Employee) session.getAttribute("Employee");
PrintWriter out = response.getWriter();
if (employee != null && employee.getId() != null) {
out.print(1);
} else {
out.print(0);
}
return null;
}
}