package com.bbitcn.silk.utils.network;

import com.bbitcn.silk.utils.MMKVUtil;
import com.bbitcn.silk.utils.global.Global;

import org.xutils.http.RequestParams;

/**
 * @Author：BinJianXin
 * @Date：2022/5/30/9:34
 * @Declaration：
 */
public class XHttpManager {
    private static XHttpManager xHttpManager;

    public static XHttpManager getInstance() {
        if (xHttpManager == null) {
            synchronized (XHttpManager.class) {
                if (xHttpManager == null) {
                    xHttpManager = new XHttpManager();
                }
            }
        }
        return xHttpManager;
    }

    /**
     * 获得网络请求头
     *
     * @param url 地址
     */
    public RequestParams getRequestParams(String url) {
        return getRequestParams(url, true, false, 0, 0);
    }

    /**
     * 获得网络请求头
     *
     * @param url       地址
     * @param needToken 是否需要token
     */
    public RequestParams getRequestParams(String url, boolean needToken) {
        return getRequestParams(url, needToken, false, 0, 0);
    }
    public RequestParams getRequestParams(String url, boolean needToken, boolean needPage, int page, int limit) {
        RequestParams params = new RequestParams(url);
        params.setReadTimeout(10000);
        params.setConnectTimeout(10000);
        params.addHeader("Content-Type", "application/json,charset=UTF-8");
        params.setUseCookie(false);
        params.setAsJsonContent(true);
        if (needToken) {
            params.addHeader("Authorization", MMKVUtil.get(Global.TOKEN));
        }
        if (needPage) {
            //NeedPage:是否需要分页，Page:第几页，Limit:每页数目，OrderBy:排序
            params.addHeader("Page-Info", "{\"NeedPage\":true,\"Page\":" + page + ",\"Limit\":" + limit + ",\"OrderBy\":\"\"}");
        }
        return params;
    }
}
