//设置域名
const dev_baseUrl = "https://test.hz-hanghui.com:18890/yx-fyzd"; //测试
// const dev_baseUrl = "http://192.168.11.19:9100/yx-fyzd"; //本地
// const pro_baseUrl = "http://192.168.77.19:8081/hanghui-server-platform"; //正式
const pro_baseUrl = "https://tx.hz-hanghui.com:8088/hanghui-server-platform"; //正式

//是否是正式环境
const env = true;
const baseUrl = env ? pro_baseUrl : dev_baseUrl;

//https请求
const request = e => {
  let token = my.getStorageSync({
    key: 'token'
  }).data;
  return new Promise(function (resolve, reject) {
    my.request({
      url: e.url.startsWith('http') ? e.url : (env ?
        pro_baseUrl + e.url :
        dev_baseUrl + e.url),
      data: e.data,
      method: e.method,
      headers: {
        "content-type": e.type,
        "Authorization": token ? token.token : '',
      },
      timeout: e.timeout ? e.timeout : 5000,
      success: res => {
        if (res.data.code == 200) {
          resolve(res.data);
        } else {
          my.hideLoading();
          my.showToast({
            content: e.errorTip + (res.data.msg ? res.data.msg : ''),
            duration: 3000
          });
          reject(res.data);
        }
      },
      fail: error => {
        my.hideLoading();
        my.showToast({
          content: e.errorTip + (error.errmsg ? error.errmsg : ''),
          duration: 3000,
        });
        reject(error.errorMessage);
      }
    });
  });
};
// 三要素获取接口
const threeElementsRequest = e => {
  return new Promise(function (resolve, reject) {
    my.request({
      url: e.url,
      data: e.data,
      method: e.method,
      headers: {
        "content-type": e.type
      },
      timeout: 25000,
      success: res => {
        if (res.data.errcode == 0 || res.data.code == 200) {
          resolve(res.data);
        } else {
          my.hideLoading();
          my.showToast({
            content: e.errorTip + (res.data.msg ? res.data.msg : ''),
            duration: 3000
          });
          reject(res.data);
        }
      },
      fail: error => {
        my.hideLoading();
        my.showToast({
          content: e.errorTip + (error.errmsg ? error.errmsg : ''),
          duration: 3000,
        });
        reject(error.errorMessage);
      }
    });
  });
};

module.exports = {
  env: env,
  baseUrl,
  dev_baseUrl: dev_baseUrl,
  pro_baseUrl: pro_baseUrl,
  request: request,
  threeElementsRequest: threeElementsRequest,
};
