var app = getApp(); //引入全局app.js，我们可以在globalData中定义一些公用的数据，比如baseUrl、token

const request = function (url = "", options) {
  const baseUrl = "http://61.164.207.46:8000/yos/rest/index" + url;
  // const baseUrl = "http://122.226.136.204:8082/yos/rest/index" + url;
  console.log(baseUrl)
  options.data.accesstoken = wx.getStorageSync('userMsg').token
  return new Promise((resolve, reject) => {
    wx.request({
      url: baseUrl,
      method: options.method,
      data: options.method == "GET" ? options.data : JSON.stringify(options.data),
      // header这里根据业务情况自行选择需要还是不需要
      header: {
        "Content-Type": 'application/json;charset=UTF-8'
      },
      success: (res) => {
        if (res.data.code == -1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          resolve(res.data)
        } else if (res.data.code == 0) {
          wx.showToast({
            title: res.data.msg,
            duration: 3000,
            icon: 'none'
          })
          resolve(res.data)
        } else {
          resolve(res.data)
        }
      },
      fail: (err) => {
        reject(err)
      }
    })
  })
}

module.exports = {
  get(url, data) {
    return request(url, {
      method: "GET",
      data
    })
  },
  //封装post方法
  post(url, data) {
    return request(url, {
      method: "POST",
      data
    })
  }
}