package com.dahe.pass.net;

import android.text.TextUtils;
import android.util.Log;

import com.dahe.mylibrary.utils.AppUtils;
import com.dahe.mylibrary.utils.BaseUtils;
import com.dahe.pass.base.App;
import com.dahe.pass.utils.SPUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 头部Interceptor
 */

public class RequestHeadInterceptor implements Interceptor {
    private static final String TAG = "RequestHeadInterceptor";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();
        if (SPUtils.hasUserInfo(App.getApp())&&!TextUtils.isEmpty(SPUtils.getUserInfo(App.getApp()).getToken())) {
//            builder.addHeader("Authorization", SPUtils.getUserInfo(App.getApp()).getToken());
            builder.addHeader("token", SPUtils.getUserInfo(App.getApp()).getToken());
        }
        Request request = builder
                .addHeader("versionCode", AppUtils.getAppVersionCode() + "")
                .addHeader("versionValue", AppUtils.getAppVersionName() + "")
                .addHeader("deviceType", "ANDROID")
                .addHeader("language", BaseUtils.getLanguage(App.getApp())+"")
                .build();
        Log.i(TAG, "intercept: " + request.headers());
        return chain.proceed(request);
    }
}
