import Sign from "./Sign";
const tagInfo = "/utils/net/Http.ts";
import to from "await-to-js";
import request from '../request';
import { Session } from '/@/utils/storage';
const Http = {

    async post(url: string, param: object = {}): Promise<any> {
        param = this.combineData(param);
        console.log(tagInfo, "get param", param);

        let [err, res] = await to(request.post(url, param));
        if (err) {
            return { code: 999, data: err, msg: "请求失败" };
        }
        return res;
    },
    async get(url: string, param: object = {}): Promise<any> {
        //param = sign(param);
        let [err, res] = await to(request.get(url, { params: this.combineData(param) }));
        if (err) {
            return { code: 999, data: err, msg: "请求失败" };
        }
        return res;
    },
    combineData(param: any = {}): any {
        // 在请求发送之前做一些处理
        // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
        // config.headers['X-Token'] = token;
        if (Session.get('token')) {
            param.token = `${Session.get('token')}`;
        }
        param = Sign(param);
        console.log(tagInfo, "http:param", param);
        return param;
    }
}

export default Http;
