package com.fzm.mall.server.front.config;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fzm.mall.server.front.constant.MallResponseEnum;
import com.fzm.mall.server.front.user.model.Header;
import com.fzm.mall.server.front.user.model.User;
import com.fzm.mall.server.front.user.service.IRedisService;
import com.fzm.mall.server.front.constant.MALLGlobalConfig;
import com.fzm.mall.server.front.user.service.IUserService;
import com.fzm.mall.server.front.util.HttpUtil;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


/**
 * @author qxl
 * @mail qxl.33.cn
 * @create 2020/7/30 16:09
 * @description
 */

@Slf4j
@Component
public class AuthorizationInterceptor implements HandlerInterceptor {
	
	@Autowired
	private IRedisService redisService;

	@Autowired
    private IUserService userService;



    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        log.info("uri:"+uri);
        // 不检验token的路由可以访问
        // 也就是无需登录就可访问
        Header header = new Header();
        header.setIp(HttpUtil.getRealIp(request));
        header.setVersion(request.getHeader("version"));
        header.setType(2);
        header.setPlatform(request.getHeader("platform"));
        if (uri.contains(MALLGlobalConfig.MALL_WITHOUT_TOKEN)) {
            if(!StringUtil.isNullOrEmpty(request.getHeader("token"))){
                User user = userService.getByToken(request.getHeader("token"));
                if(user != null){
                    header.setAccount(user.getPhone());
                    header.setUid(user.getUid());
                    header.setToken(request.getHeader("token"));
                    header.setPayPassword(user.getPayPassword());
                }

            }
            request.setAttribute("header", header);
            return true;
        }
        // 其他的表示需要登录才能访问
        User user = userService.getByToken(request.getHeader("token"));
        if(user == null){
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return false;
        }
        if(user.getStatus() == 2){
            JSONObject.writeJSONString(response.getWriter(),
                    MallResponseEnum.ACCOUNT_FROZEN);
            return  false;
        }
        header.setArea(user.getArea());
        header.setAccount(user.getPhone());
        header.setUid(user.getUid());
        header.setToken(request.getHeader("token"));
        header.setPayPassword(user.getPayPassword());
        request.setAttribute("header", header);
        //token过期
        String key = redisService.getKey(header.getUid()+MALLGlobalConfig.MALL_TOKEN_REDISKEY_SUFFIX);
        if(StringUtil.isNullOrEmpty(key)) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return false;
            //token无效
        }
        return true;
    }
}

