// const url = 'https://gpttest.malls.iformall.com/C'
const url = 'https://gpt.malls.iformall.com/C'

class request {
  constructor(address) {
    if (address) {
      this.address = address;
    }
  }
  /**
   * 配置
   */
  /**
   * 请求路径前缀
   */
  address = url;
  /**
   * 请求头
   */
  headers = {
    "Content-Type": "application/json;charset=UTF-8",
    token: "",
    tenantId: ""
  };
  /**
   * @description 设置请求头
   * @param {*} data token , tenantId
   */
  setHead(token, tenantId) {
    this.headers.token = token;
    this.headers.tenantId = tenantId;
  }

  /**
   * @description 小程序appId
   * @returns {string} wxedb9fb309728a9f1
   */
  appId = 'wxedb9fb309728a9f1';

  /**
   * @description get
   * @param {*}param url & param
   * @method  Request.get({url: url}).then(res=>{}).catch(err=>{})
   */
  get({ url, data, headers }) {
    headers = { ...this.headers, ...headers };
    url = `${this.address}${url}`;
    return new Promise((resolve, reject) => {
      wx.request({
        url: url,
        header: headers,
        data: data,
        method: "Get",
        success: res => {
          this.responseMap(res, resolve, reject);
        },
        fail: err => {
          console.log(err)
          if (err.errMsg == 'request:fail timeout') {
            err.errMsg = '请求超时，请检查您的网络设置！'
          } else if (err.errMsg == 'request:fail') {
            err.errMsg = '未检查到您的网络，请检查您的网络设置！'
          } else if (err.errMsg == 'request:fail request connect error') {
            err.errMsg = '连接失败'
          }
          reject(err);
        }
      });
    });
  }

  /**
   * @description post
   * @param {*}param url & param
   * @method  Request.post({url: url,data: {}}).then(res=>{}).catch(err=>{})
   */
  post({ url, data, headers }) {
    headers = { ...this.headers, ...headers };
    url = `${this.address}${url}`;
    return new Promise((resolve, reject) => {
      wx.request({
        url: url,
        header: headers,
        data: data,
        method: "POST",
        success: res => {
          this.responseMap(res, resolve, reject);
        },
        fail: err => {
          if (err.errMsg == 'request:fail timeout') {
            err.errMsg = '请求超时，请检查您的网络设置！'
          } else if (err.errMsg == 'request:fail') {
            err.errMsg = '未检查到您的网络，请检查您的网络设置！'
          } else if (err.errMsg == 'request:fail request connect error') {
            err.errMsg = '连接失败'
          }
          reject(err);
        },
        complete: res => { }
      });
    });
  }

  /**
   * 过滤 请求信息 
   * @param {*} res
   * @param {*} resolve
   * @param {*} reject
   */
  responseMap = (res, resolve, reject) => {
    // 网络状态码200
    if (res.statusCode == 200) {
      // 服务器code 200 成功
      if (res.data.code == 200) {
        resolve(res.data);
      } else if (res.data.code == 1052) {
        wx.reLaunch({
          url: '/pages/index/index'
        })
      } else {
        reject(res.data);
      }
    } else {
      console.log("请求出错：", res);
      reject(res.data);
    }
  };
  /** 日志 */
  log(url, body, headers) { }
}
module.exports = new request();
