package com.lgx8.right.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.lgx8.common.servlet.BaseController;
import com.lgx8.common.util.ArrayUtil;
import com.lgx8.common.util.SendSms;
import com.lgx8.management.dao.IMerchantDao;
import com.lgx8.right.dao.IUserDao;
import com.lgx8.right.entities.RunTimeUser;
import com.lgx8.right.entities.User;
import com.lgx8.right.service.IUserCardService;
/**
* 处理用户登录\注册类
*
* @author chenbutao
*
*/
@Controller
public class LoginController extends BaseController {
@Resource(name = "userCardService")
public IUserCardService userCardService;
@Autowired
public IMerchantDao merchantDao;
@Resource(name="userDao")
IUserDao userDao;
@RequestMapping(value = "/login.do")
public ModelAndView login(HttpServletRequest request,
HttpServletResponse response) {
String logout = request.getParameter("logout");
if ("true".equals(logout)) {
String sid = request.getSession().getId();
request.getSession().removeAttribute(sid);
request.getSession().invalidate();
return new ModelAndView("gateway/login");
} else {
String username = (String) request.getParameter("username");
String password = (String) request.getParameter("password");
String code = (String) request.getParameter("validateCode");
if (!code.equalsIgnoreCase((String) request.getSession()
.getAttribute("validateCode"))) {
ModelAndView model = new ModelAndView("gateway/login");
model.addObject("username", username);
model.addObject("codeMsg", "验证码错误");
return model;
}
User authuser = userCardService.auth(new User(username, password));
if(authuser != null && authuser.getLocked())
{
ModelAndView model = new ModelAndView("gateway/login");
model.addObject("username", username);
model.addObject("errMsg", "用户被锁定");
return model;
}
if (authuser != null) {
RunTimeUser rtUser = new RunTimeUser(authuser);
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){
long _mid = rtUser.getOrganizationid();
rtUser.setMerchant(true);
}
if(rtUser.getUsertype()==3){
long _mid = rtUser.getOrganizationid();
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);
return new ModelAndView("management/common/main");
}
//子帐号未授权提示无此帐号
if (rtUser.getUsertype() != User.USER_TYPE_NORMAL) {
ModelAndView model = new ModelAndView("gateway/login");
model.addObject("username", username);
model.addObject("errMsg", "用户名、密码错误");
return model;
}
/*if(rtUser.isMerchant()){
url = "/page/management/merchant/merchant_main.jsp";
rtUser.setUrl(url);
request.getSession().setAttribute(sid, rtUser);
return new ModelAndView("management/merchant/merchant_main");
}
if(rtUser.isOrganization()){
url = "/page/management/organization/origanization_main.jsp";
rtUser.setUrl(url);
request.getSession().setAttribute(sid, rtUser);
return new ModelAndView("management/organization/origanization_main");
}*/
rtUser.setUrl(url);
request.getSession().setAttribute(sid, rtUser);
return new ModelAndView("management/member/member_welcome");
}
ModelAndView model = new ModelAndView("gateway/login");
model.addObject("username", username);
model.addObject("errMsg", "用户名、密码错误");
return model;
}
}
@RequestMapping(value = "/register.do")
public ModelAndView register(HttpServletRequest request,
HttpServletResponse response) {
String mobile = (String) request.getParameter("mobile");
String mcode = (String) request.getParameter("mcode");
String email = (String) request.getParameter("email");
String username = (String) request.getParameter("username");
String password = (String) request.getParameter("password");
if (!mcode.equalsIgnoreCase((String) request.getSession().getAttribute(
"register"))) {
ModelAndView model = new ModelAndView("gateway/register");
model.addObject("codeMsg", "短信码错误");
model.addObject("mobile", mobile);
model.addObject("email", email);
return model;
}
if (userDao.findUserByUserName(username) != null) {
ModelAndView model = new ModelAndView("gateway/register");
model.addObject("codeMsg", "短信码错误");
model.addObject("mobile", mobile);
model.addObject("email", email);
model.addObject("usernameMsg","该帐号已存");
return model;
}
ModelAndView model = new ModelAndView("gateway/login");
User user = new User();
user.setMobile(mobile);
user.setEmail(email);
user.setUsername(username);
user.setPassword(password);
user.setRegistertime(new Date());
user.setLastlogin(new Date());
user.setUsertype(User.USER_TYPE_NORMAL);
user.setEnabled(true);
user.setLocked(false);
user.setCardEnd("0");
user.setCardStart("0");
user.setCurMaxCard("0");
userCardService.registerUser(user);
model.addObject("registerSuc", "注册成功请登录");
PrintWriter out = null;
response.setContentType("text/html;charset=UTF-8");
try {
out = response.getWriter();
out.println("<script type=\"text/javascript\">");
out.println("alert(\"注册成功请登录\");");
out.println(" location.href=\"page/gateway/login.jsp\";");
out.println("</script>");
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return model;
}
/**
* 预留短信发送接口
*
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/sendSms.do")
public ModelAndView sendSms(HttpServletRequest request,
HttpServletResponse response) {
String sendType = request.getParameter("smsfrom");
/*if ("register".equals(sendType)) {
request.getSession().setAttribute("register", "123456");
}
*/
String sendMobile = request.getParameter("mobile");
if ("register".equals(sendType)) {
String regCode = SendSms.randomRegCode();
System.out.println("------------------------------------------------"+regCode);
String content = "您注册的验证码为:" + regCode;
SendSms.sendSms(sendMobile, content);
request.getSession().setAttribute("register", regCode);
}else if("modifyinfo".equals(sendType)){
String regCode = SendSms.randomRegCode();
String content = "您的手机验证码为:" + regCode;
SendSms.sendSms(sendMobile, content);
request.getSession().setAttribute("modifyinfo", regCode);
}
return null;
}
}