package thyyxxk.webserver.utils;

import com.auth0.jwt.JWT;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

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

/**
 * @author dj
 */
public class TokenUtil {
    private static String weComAddressBookToken = "";
    private static String weComClockInToken = "";
    private static String weComSendMsgToken = "";
    private static String[] lisDockSign = new String[2];

    public static String getTokenUserId() {
        try {
            String token = Objects.requireNonNull(getRequest()).getHeader("token");
            return JWT.decode(token).getAudience().get(0);
        } catch (Exception e) {
            return "99999";
        }
    }

    private static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        return requestAttributes == null ? null : requestAttributes.getRequest();
    }

    public static String getWeComAddressBookToken() {
        return weComAddressBookToken;
    }

    public static void setWeComAddressBookToken(String token) {
        weComAddressBookToken = token;
    }

    public static String getWeComClockInToken() {
        return weComClockInToken;
    }

    public static void setWeComClockInToken(String token) {
        weComClockInToken = token;
    }

    public static String getWeComSendMsgToken() {
        return weComSendMsgToken;
    }

    public static void setWeComSendMsgToken(String token) {
        weComSendMsgToken = token;
    }

    public static String[] getLisDockSign() {
        return lisDockSign;
    }

    public static void setLisDockSign(String[] sign) {
        lisDockSign = sign;
    }
}
