package com.emr.controller;

import com.alibaba.fastjson.JSONObject;
import com.emr.util.ActionScopeUtils;
import com.emr.util.HttpClientUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author HJL
 * @create 2019/4/29
 */
@Controller
public class LoginController {

    @Value("${POWER_URLHEAD}")
    private String POWER_URLHEAD;

    @Value("${POWER_JSP}")
    private String POWER_JSP;

    @Value("${powerUrl}")
    private String powerUrl;

    @RequestMapping(value = "/toLogin")
    public String toLogin(Model model) {
        // 从session获取用户名
        // Subject currentUser = SecurityUtils.getSubject();
        // Session session = currentUser.getSession();
        //  session.removeAttribute("username");
        //  session.removeAttribute("menuList");
        //  session.removeAttribute("userSession");
        //session.invalidate();

        return "redirect:/login.jsp";
    }

    //实现用户登录@PathVariable("username")
    @RequestMapping(value = "/login")
    public String login(Model model, HttpServletRequest request) {
        String token = request.getParameter("token");
        String userName = request.getParameter("userName");
        String name = request.getParameter("name");
        UsernamePasswordToken userToken = new UsernamePasswordToken(userName, "456789");
        Subject subject = SecurityUtils.getSubject();
        subject.login(userToken);
        model.addAttribute("POWER_URLHEAD", POWER_JSP);
        request.getSession().setAttribute("token", token);
       // request.getSession().setAttribute("userName", userName);
       // request.getSession().setAttribute("userName", "123");

        return "index";
    }

    @RequestMapping(value = "/index")
    public String Login(HttpServletResponse response, HttpServletRequest request, Model model) throws IOException {
        return "index";
    }


    @RequestMapping(value = "/toError")
    public String error(Model model) {
        // 从session获取用户名
        // Subject currentUser = SecurityUtils.getSubject();
        // Session session = currentUser.getSession();
        //  session.removeAttribute("username");
        //  session.removeAttribute("menuList");
        //  session.removeAttribute("userSession");
        //session.invalidate();

        return "redirect:/error.jsp";
    }
}
 

