package com.zfire.mall.manager.config.aop;



import com.zfire.mall.manager.constant.Constant;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.utils.CheckResult;
import com.zfire.mall.manager.utils.JwtUtils;
import com.zfire.mall.manager.utils.RedisUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class SignAspect {
    @Autowired
    RedisUtil redisUtil;

    /**
     * 切入点
     */
    @Pointcut(value = "@annotation(com.zfire.mall.manager.annotation.Sign)") //对应的sign注解类
    private void pointcut() {
    }



    @Around("pointcut()")
    public Object Around(ProceedingJoinPoint pjp) throws Throwable {
        String ipAddress = getIpAddress();

        //后台管理的校验
        if(StringUtils.isEmpty(ipAddress)){
            throw new RemoteServiceException(ResponseHelper.ResponseCode_AUTH_ERROR, "请求不合法");
        }

        if (!ipAddress.equals(Constant.POST_URL_THREE) && !ipAddress.equals(Constant.POST_URL_TWO) && !ipAddress.equals(Constant.POST_URL_ONE))
            throw new RemoteServiceException(ResponseHelper.ResponseCode_AUTH_ERROR, "请求不合法");

        return pjp.proceed();
    }


    private static final String UN_KNOWN = "unKnown";
    private static String getIpAddress() {
        if (ObjectUtils.isEmpty(RequestContextHolder.getRequestAttributes())) {
            return null;
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        String xip = request.getHeader("X-Real-IP");
        String xFor = request.getHeader("X-Forwarded-For");
        if(!org.apache.commons.lang3.StringUtils.isEmpty(xFor) && !UN_KNOWN.equalsIgnoreCase(xFor)){
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = xFor.indexOf(",");
            if(index != -1){
                return xFor.substring(0,index);
            }else{
                return xFor;
            }
        }
        xFor = xip;
        if(!org.apache.commons.lang3.StringUtils.isEmpty(xFor) && !UN_KNOWN.equalsIgnoreCase(xFor)){
            return xFor;
        }
        if (org.apache.commons.lang3.StringUtils.isEmpty(xFor) || UN_KNOWN.equalsIgnoreCase(xFor)) {
            xFor = request.getHeader("Proxy-Client-IP");
        }
        if (org.apache.commons.lang3.StringUtils.isEmpty(xFor) || UN_KNOWN.equalsIgnoreCase(xFor)) {
            xFor = request.getHeader("WL-Proxy-Client-IP");
        }
        if (org.apache.commons.lang3.StringUtils.isEmpty(xFor) || UN_KNOWN.equalsIgnoreCase(xFor)) {
            xFor = request.getHeader("HTTP_CLIENT_IP");
        }
        if (org.apache.commons.lang3.StringUtils.isEmpty(xFor) || UN_KNOWN.equalsIgnoreCase(xFor)) {
            xFor = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (org.apache.commons.lang3.StringUtils.isEmpty(xFor) || UN_KNOWN.equalsIgnoreCase(xFor)) {
            xFor = request.getRemoteAddr();
        }
        return xFor;
    }



}
