// const apiBase = "http://192.168.2.9:5192"
const apiBase = "https://meeting-api.23544.com/pc"
// const apiBase = "https://meeting-api.23544.com/test29"
export const Request = (params) => {
  return new Promise((resolve, reject) => {
    wx.getStorage({
      key: "user",
      success(res) {
        requestMethods({
          params,
          token: res.data.token
        }, (status, res) => {
          if (status === 'success') {
            resolve(res.data)
          } else {
            reject(res)
          }
        })
      },
      fail() {
        requestMethods({
          params,
          token: ''
        }, (status, res) => {
          if (status === 'success') {
            resolve(res.data)
          } else {
            reject(res)
          }
        })
      }
    })
  })
}

function requestMethods(obj, callback) {
  wx.request({
    ...obj.params,
    url: apiBase + obj.params.url,
    timeout: 10000,
    header: {
      'content-type': 'application/json',
      'Authorization': `Bearer ${obj.token}`
    },
    success: res => {
      callback('success', res)
    },
    fail: res => {
      if (res.errno === 5) {
        wx.showToast({
          icon: 'error',
          title: '网络连接超时，请检查网络状态',
          mask: true
        })
      }
      callback('fail', res)
    },
  })
}