const apiBase = "https://meeting-api.23544.com/pc"

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,
    header: {
      'content-type': 'application/json',
      'Authorization': `Bearer ${obj.token}`
    },
    success: res => {
      callback('success', res)
    },
    fail: res => {
      callback('fail', res)
    },
  })
}