package com.hc.web.aspect;

import com.hc.core.utils.exception.ResponseException;
import com.hc.core.utils.redis.RedisUtil;
import com.hc.core.utils.tool.Verification;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author 10696
 * @since 2020/12/22 9:01
 */

@Aspect
@Component
@Slf4j
public class AppUserAspect {

    private final RedisUtil redisUtil;

    public AppUserAspect(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }

    @Pointcut("execution (public * com.hc.web.controller.browser.app.*.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.app.AppUserController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.app.AppNewsTagController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.app.AppUserTeacherController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.app.AppClassConfigController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.app.AppClassCourseController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.app.AppBannerController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.app.AppQrAccessoryController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.app.AppUserController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.app.AppCourseController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.app.AppCourseTypeController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.app.AppNewsController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.app.AppAreaDistrictController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.app.AppWxPayJsApiController.*(..))"
    )

    public void verify() {

    }

    @Before("verify()")
    public void doVerify() {

        //获取请求信息

        ServletRequestAttributes attributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();

        //从请求头中获取token
        String token = request.getHeader("token");

        if (!Objects.isNull(token)) {

            //身份识别 已取消

            //获取redis中的token 查看token是否过期
            if (Verification.verificationString((String) redisUtil.get(token))) {
                log.info("app 请求-token正常");
            } else {
                log.info("app 请求-redis中的token已过期，请重新登录");
                throw new ResponseException(-1, "front token is expired");
            }

        } else {
            log.info("app request-token is null");
            throw new ResponseException(-1, "app request-token is null");
        }
    }
}
