package fcb.project.manager.core.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import fcb.project.manager.base.entity.ContextTokenBean;
import fdage.back.sdk.base.entity.Result;
import fdage.back.sdk.base.enums.ResultCodeEnum;
import fdage.back.sdk.base.exception.CommonBaseException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import javax.servlet.http.HttpServletRequest;

/**
 * 2 * @Author: Abner
 * 3 * @Date: 2021/1/25 10:19
 * 4
 */
public class BaseController {

    public static final String TOKEN = "token";

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected RedisTemplate redisTemplate;

    protected String getToken(){
        return request.getHeader(TOKEN);
    }

    protected ContextTokenBean getContextUserInfo(){
        String token = request.getHeader(TOKEN);
        if(StringUtils.isBlank(token)){
            return null;
        }
        JSONObject tokenJsonObj = (JSONObject) redisTemplate.opsForValue().get(token);
        if(null == tokenJsonObj){
            throw new CommonBaseException(3004 , "token失效");
        }
        ContextTokenBean contextTokenBean = JSON.parseObject(tokenJsonObj.toJSONString() , ContextTokenBean.class);
        if(null == contextTokenBean){
            throw new CommonBaseException(ResultCodeEnum.D101 , "解析token失败");
        }
        return contextTokenBean;
//        return new ContextTokenBean();
    }

}
