package com.pj.sso;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.pj.project.sys_user.SysUser;

import cn.dev33.satoken.sso.SaSsoManager;
import cn.dev33.satoken.sso.SaSsoProcessor;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;

/**
 * Sa-Token-SSO Client端 Controller 
 * @author kong
 */
@RestController
public class SsoClientController {

	// 进入首页 
	@RequestMapping("/")
	public Object index(HttpServletRequest request) {
		request.setAttribute("isLogin", StpUtil.isLogin());
		String authUrl = "javascript:location.href='" + SaSsoManager.getConfig().getAuthUrl() + "?mode=simple&redirect=' + encodeURIComponent(location.href)";
		authUrl = SaSsoManager.getConfig().getAuthUrl();
		request.setAttribute("authUrl", authUrl);
		if(StpUtil.isLogin()) {
			request.setAttribute("user", StpUtil.getSession().get("user", new SysUser()));
		}
		return new ModelAndView("index.html");
	}

	// SSO-Client端：处理所有SSO相关请求 
	@RequestMapping("/sso/*")
	public Object ssoRequest() {
		return SaSsoProcessor.instance.clientDister();
	}

	// 进入info页（只有登录后才可以访问此页面）
	@RequestMapping("/info.html")
	public Object info() {
		return new ModelAndView("info.html");
	}

	// getInfo接口 （只有登录后才可以调用此接口）
	@RequestMapping("/getCurrInfo")
	public Object getCurrInfo() {
		// 必须登录后才能进入接口  
		StpUtil.checkLogin();
		// 从Session中获取user对象 
		SysUser user = (SysUser)StpUtil.getSession().get("user");
		return SaResult.data(user);
	}
	
}
