package com.chinaweal.aiccs.outside.interceptor;

import com.chinaweal.aiccs.common.constant.BaseDataConstant;
import com.chinaweal.aiccs.outside.api.SOAServiceApi;
import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.interceptor.Interceptor;
import com.dtflys.forest.reflection.ForestMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class DataCenterInterceptor implements Interceptor {
    @Resource
    private SOAServiceApi soaServiceApi;

    /**
     * 该方法在被调用时，并在beforeExecute前被调用
     *
     * @Param request Forest请求对象
     * @Param args 方法被调用时传入的参数数组
     */
    @Override
    public void onInvokeMethod(ForestRequest req, ForestMethod method, Object[] args) {
        String url = req.getUrl();
        if (url.contains("getToken")) {
            return;
        }
        Map<String, Object> query = new HashMap<>();
        query.put("login", BaseDataConstant.CHANGRUAN_USER);
        //注入token
        Map<String, Object> result = (Map<String, Object>) soaServiceApi.getToken(query).get("result");
        Map<String, Object> data = (Map<String, Object>) result.get("data");
//        把令牌赋值请求连接上面去
        req.getHeaders().setHeader("gwssitoken", (String) data.get("token"));
    }
}
