import { httpConfig } from './httpConfig';
const { wechatAuthorizationToken, wechatIpDeault, wechatUrlAddress } = httpConfig;

export const $http = {
    UrlEncode: (obj) => {
        if (!obj || Object.prototype.toString.call(obj) !== '[object Object]') {
            return '';
        }
        let params = [];
        for (let key in obj) {
            params.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]));
        }
        return params.join('&');
    },
    post: (type, Nothis, Objson) => {
        // let allIpDeault = Objson.namespace === 'wechat' ? wechatIpDeault : (Objson.namespace === 'HIS' ? HISIpDeault : wechatIpDeault);
        // let allUrlAddress = Objson.namespace === 'wechat' ? wechatUrlAddress : (Objson.namespace === 'HIS' ? HISUrlAddress : wechatUrlAddress);
        // let allAuthorizationToken = Objson.namespace === 'wechat' ? wechatAuthorizationToken : (Objson.namespace === 'HIS' ? HISAuthorizationToken : wechatAuthorizationToken);

        let allIpDeault = wechatIpDeault;
        let allUrlAddress = wechatUrlAddress;
        let allAuthorizationToken = wechatAuthorizationToken;

        let userData = localStorage.getItem("userData") ? JSON.parse(localStorage.getItem("userData")) : {};
        let weChatParams = sessionStorage.getItem("weChatParams") ? JSON.parse(sessionStorage.getItem("weChatParams")) : {};
        let formData = Object.assign({}, Objson.data);
        let httpUrlAddress; //完整地址
        formData.HISFlag = (Objson.namespace === 'HIS' || Objson.namespace === 'Y') ? 'Y' : 'N';
        formData.UPPFlag = Objson.namespace === 'UPP' ? 'Y' : 'N';
        formData.code = Objson.code;
        formData.session = [{
            "hisHospID":  userData&&userData.hisHospID ? userData.hisHospID : "",
            "hospID": userData&&userData.hospID ? userData.hospID : '',
            "hospDesc": userData&&userData.hospDesc ? userData.hospDesc : "",
            "openID": weChatParams&&weChatParams.openID ? weChatParams.openID : "",
            "pubAccCode": weChatParams&&weChatParams.pubAccCode ? weChatParams.pubAccCode : "",
            "hisPatID": userData&&userData.hisPatID ? userData.hisPatID : "",
            "hisPatNo": userData&&userData.hisPatNo ? userData.hisPatNo : "",
            "patID": userData&&userData.patID ? userData.patID : "",
            "patDesc": userData&&userData.patDesc ? userData.patDesc : "",
            "idCardNo": userData&&userData.idCardNo ? userData.idCardNo : "",
            "imUserID": userData&&userData.imUserID ? userData.imUserID : "",
        }];

        httpUrlAddress = allIpDeault + allUrlAddress;
        // fetch('http://' + httpUrlAddress, {
        fetch(httpUrlAddress, {
            method: 'post',
            mode: 'cors',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                "Authorization": allAuthorizationToken,
                "Access-Control-Allow-Origin": allIpDeault
            },
            body: JSON.stringify(formData)
        }).then((res) => {
            if (res.status !== 200) {
                throw res.status
            } else {
                let Data;
                Data = res.json()
                return Data
            }
        }).then((res) => {
            Objson.success.call(Nothis, res)
        }).catch((error) => {
            if (Objson.error) {
                Objson.error.call(Nothis, error)
            }
        });
    },
}
