package com.rdiot.yx485.net

import com.drake.net.interceptor.RequestInterceptor
import com.drake.net.request.BaseRequest
import com.rdiot.yx485.base.LocalData


/** 请求拦截器, 一般用于添加全局参数 */


class MyRequestInterceptor : RequestInterceptor {
    /** 本方法每次请求发起都会调用, 这里添加的参数可以是动态参数 */
    override fun interceptor(request: BaseRequest) {
        // 仅请求动作没有添加时才会添加默认请求头
        if (request.headers()["X-YX-APP"] == null) {
            val jsonStr = LocalData.yxBean.toBase64()
            request.addHeader("X-YX-APP", jsonStr)
        }

        LocalData.loginData?.let { loginData ->
            val token = "${loginData.tokenType} ${loginData.accessToken}"
//            LogUtils.e("Token:$token")
            if (LocalData.makeTokenErr) {
                request.addHeader("Authorization", "${token}1")
                LocalData.makeTokenErr = false
            } else {
                request.addHeader("Authorization", token)
            }
        }

    }
}