package com.jianma.xtdm.controller;

import java.awt.image.RenderedImage;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.Optional;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jianma.xtdm.XTDMController;
import com.jianma.xtdm.exception.ServerException;
import com.jianma.xtdm.exception.XTDMException;
import com.jianma.xtdm.model.ResultModel;
import com.jianma.xtdm.model.User;
import com.jianma.xtdm.model.UserPageModel;
import com.jianma.xtdm.service.UserService;
import com.jianma.xtdm.util.GraphicsUtil;
import com.jianma.xtdm.util.ResponseCodeUtil;


/**
 * 大赛用户信息处理
 * 
 * @author dev
 *
 */
@Controller
@RequestMapping(value = "/user")
public class UserController extends XTDMController {

	@Autowired
	@Qualifier(value = "userServiceImpl")
	private UserService userServiceImpl;
	
	@RequestMapping(value = "/dologin")
	public void doLogin(HttpServletRequest request, Model model,HttpServletResponse response) throws IOException {
		String msg = "";
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		token.setRememberMe(true);
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(token);
			if (subject.isAuthenticated()) {
				response.sendRedirect("html/index.html");
				
			} else {
				response.sendRedirect("html/login.html");
			}
		} catch (IncorrectCredentialsException e) {
			msg = "登录密码错误.";
			model.addAttribute("error", msg);
			System.out.println(msg);
		} catch (ExcessiveAttemptsException e) {
			msg = "登录失败次数过多";
			model.addAttribute("error", msg);
			System.out.println(msg);
		} catch (LockedAccountException e) {
			msg = "帐号已被锁定.";
			model.addAttribute("error", msg);
			System.out.println(msg);
		} catch (DisabledAccountException e) {
			msg = "帐号已被禁用. ";
			model.addAttribute("error", msg);
			System.out.println(msg);
		} catch (ExpiredCredentialsException e) {
			msg = "帐号已过期.";
			model.addAttribute("error", msg);
			System.out.println(msg);
		} catch (UnknownAccountException e) {
			msg = "帐号不存在.或者未激活";
			model.addAttribute("error", msg);
			System.out.println(msg);
		} catch (UnauthorizedException e) {
			msg = "您没有得到相应的授权！";
			model.addAttribute("error", msg);
			System.out.println(msg);
		}
	}
	
	@RequestMapping(value = "/getCode", method = RequestMethod.GET)
	public void getCode(HttpServletRequest request, HttpServletResponse response) {
		try {

			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			// 表明生成的响应是图片
			response.setContentType("image/jpeg");

			Map<String, Object> map = new GraphicsUtil().getGraphics();
			request.getSession().setAttribute("rand", map.get("rand"));
			ImageIO.write((RenderedImage) map.get("image"), "JPEG", response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

@@ -116,61 +116,11 @@
 til.
-Date;%0Aimport java.util.Map;%0Aimport java.util.Optional
+Map
 ;%0A%0Ai
@@ -722,62 +722,8 @@
 en;%0A
-import org.apache.shiro.authz.AuthorizationException;%0A
 impo
@@ -988,68 +988,8 @@
 el;%0A
-import org.springframework.web.bind.annotation.RequestBody;%0A
 impo
@@ -1113,440 +1113,46 @@
 od;%0A
-import org.springframework.web.bind.annotation.RequestParam;%0Aimport org.springframework.web.bind.annotation.ResponseBody;%0Aimport org.springframework.web.servlet.ModelAndView;%0A%0Aimport com.jianma.xtdm.XTDMController;%0Aimport com.jianma.xtdm.exception.ServerException;%0Aimport com.jianma.xtdm.exception.XTDMException;%0Aimport com.jianma.xtdm.model.ResultModel;%0Aimport com.jianma.xtdm.model.User;%0Aimport com.jianma.xtdm.model.UserPageModel
+%0Aimport com.jianma.xtdm.XTDMController
 ;%0Aim
@@ -1239,54 +1239,8 @@
 il;%0A
-import com.jianma.xtdm.util.ResponseCodeUtil;%0A
 %0A%0A/*

fix bug