package com.njzscloud.supervisory.gps.tuqiang.jimi.support;

import cn.hutool.extra.spring.SpringUtil;
import com.njzscloud.common.http.constant.HttpMethod;
import com.njzscloud.common.http.interceptor.RequestInterceptor;
import com.njzscloud.supervisory.config.JimiProperties;

public class JimiRequestInterceptor implements RequestInterceptor {
    private final String appKey;
    private final String appSecret;

    public JimiRequestInterceptor() {
        JimiProperties jimiProperties = SpringUtil.getBean(JimiProperties.class);
        this.appSecret = jimiProperties.getAppSecret();
        this.appKey = jimiProperties.getAppKey();
    }

    @Override
    public Object[] process(HttpMethod method, String url, Object[] args) {
        if (args == null || args.length == 0) {
            return args;
        }
        if (JimiParam.class.isAssignableFrom(args[0].getClass())) {
            JimiParam jimiParam = (JimiParam) args[0];
            jimiParam.setApp_key(appKey);
            String sign = JimiSignUtil.sign(jimiParam, appSecret, jimiParam.getSign_method());
            jimiParam.setSign(sign);
        }
        return args;
    }
}
