import httpConfig from './httpConfig.js';
import Aes from './AesTwo.js';
import Cache from './cache.js';
const $http = {
  post: (type, Nothis, Objson) => {
    let fetchUrl = httpConfig.ipDeault + httpConfig.urlAddress;
    let formData = Object.assign({}, Objson.data);
    formData.code = Objson.code;
    formData.HISFlag = Objson.namespace ? Objson.namespace : "N";
    formData.UPPFlag = Objson.namespace === 'UPP' ? 'Y' : 'N';
    let userData = Cache.get('userData');
    formData.session = [{
      ...userData,
    }];
    let bodyData = JSON.stringify(formData);
    if (fetchUrl.indexOf("Encrypt") > -1) {
      bodyData = Aes.Encrypt(JSON.stringify(formData));
    }
    uni.request({
      url: fetchUrl,
      data: bodyData,
      method: 'post',
      header: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        "Authorization": httpConfig.authorizationToken
      },
      success: function (res) {
		Objson.callback && Objson.callback.call(Nothis, bodyData)
        if (+res.statusCode === 200) {
          let data = res.data;
          if (fetchUrl.indexOf("Encrypt") > -1) {
            data = JSON.parse(Aes.Decrypt(data));
          }
          Objson.success.call(Nothis, data)
        } else {
          uni.showToast({
            title: res.errMsg,
            icon: 'none',
          })
		  const obj = {
		  	error: res.errMsg,
		  	inParams: bodyData
		  }
		  Objson.error.call(Nothis, obj)
        }
      },
      fail: function (error) {
        uni.showToast({
          title: error,
          icon: 'none',
        })
		const obj = {
			error: error,
			inParams: bodyData
		}
        Objson.error.call(Nothis, obj)
      },
      complete: function(data) {
        typeof Objson.complete == 'function' && Objson.complete.call(Nothis, data)
      }
    })
  }
}

export default $http