package com.lgx8.common.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.lgx8.common.util.ArrayUtil;
import com.lgx8.gateway.dao.IOrderProductDao;
import com.lgx8.right.entities.RunTimeUser;
import com.lgx8.right.entities.User;
import com.lgx8.right.service.IUserCardService;
/**
* Servlet implementation class SmallLoginServlet
*/
public class SmallLoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SmallLoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String username = (String) request.getParameter("username");
String password = (String) request.getParameter("password");
String code = (String) request.getParameter("validateCode");
String productid = (String)request.getParameter("productid");
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IUserCardService userCardService = (IUserCardService) ac.getBean("userCardService");
PrintWriter out = response.getWriter();
String result = "";
if (!code.equalsIgnoreCase((String) request.getSession()
.getAttribute("validateCode"))) {
result = "验证码错误!";
out.write(result);
out.flush();
return ;
}
User authuser = userCardService.auth(new User(username, password));
if (authuser != null) {
RunTimeUser rtUser = new RunTimeUser(authuser);
if(rtUser.getUsertype()!=User.USER_TYPE_NORMAL) {
result = "非会员账号,不能进行兑换";
out.write(result);
out.flush();
return ;
}
rtUser.setUserIp(request.getRemoteAddr());
rtUser.setSessionID(request.getSession().getId());
String sid = request.getSession().getId();
request.getSession().removeAttribute(sid);
request.getSession().setAttribute(sid, rtUser);
if (!ArrayUtil.isEmptyCollection(rtUser.getRoles())) {
rtUser.setIsmanager(true);
}
if(rtUser.getUsertype()==2){
rtUser.setMerchant(true);
}
if(rtUser.getUsertype()==3){
rtUser.setOrganization(true);
}
// 设置当前登录时间ip 下次登录时拿上次的登录时间ip
authuser.setLastlogin(new Date());
authuser.setIp(request.getRemoteAddr());
userCardService.updateUser(authuser);
/*String url = "/page/management/member/member_welcome.jsp";
if (rtUser.isIsmanager()) {
url = "/page/management/common/main.jsp";
rtUser.setUrl(url);
request.getSession().setAttribute(sid, rtUser);
}
if(rtUser.isMerchant()){
url = "/page/management/merchant/merchant_main.jsp";
rtUser.setUrl(url);
request.getSession().setAttribute(sid, rtUser);
}
if(rtUser.isOrganization()){
url = "/page/management/organization/origanization_main.jsp";
rtUser.setUrl(url);
request.getSession().setAttribute(sid, rtUser);
}
rtUser.setUrl(url); */
request.getSession().setAttribute(sid, rtUser);
if(productid == null || "".equals(productid))
{
productid = "0";
}
IOrderProductDao orderProductDao = (IOrderProductDao) ac.getBean("orderProductDao");
if(orderProductDao.checkOrderProduct(Long.parseLong(productid), rtUser.getId(), "0"))
{
result = "2";
}else
{
result = "1";
}
out.write(result);
out.flush();
return ;
} else {
result = "用户名或密码错误";
out.write(result);
out.flush();
return ;
}
}
}