记录封装的wx.request

/**
 * 封装微信的request
 */
function request(url, data = {}, method = 'GET') {
  return new Promise((resolve, reject) => {
    uni.request({
      url: url,
      data: data,
      method: method,
      header: {
        'Content-Type': 'application/json',
        'X-Access-Token': uni.getStorageSync('token'),
        'req_type': 'xcx'
      },
      success(res) {
        if (res.statusCode === 200) {
          if (res.data.code !== 200) {
            uni.hideLoading()
            uni.showModal({
              title: '温馨提示',
              content: res.data.message || '抱歉!出错了',
              showCancel: false
            })
          }
          resolve(res.data)
        } else {
          if (res.statusCode === 401) {
            uni.navigateTo({
              url: '/pages/authorization/authorization?scene=tokenInvalid',
            })
          }
          uni.hideLoading()
          reject(res)
        }
      },
      fail(err) {
        reject(err)
        uni.hideLoading()
        console.log('failed')
      }
    })
  })
}

export function get(url, data = {}) {
  return request(url, data, 'GET')
}

export function customDelete(url, data = {}) {
  return request(url, data, 'DELETE')
}

export function post(url, data = {}) {
  return request(url, data, 'POST')
}

export function put(url, data = {}) {
  return request(url, data, 'PUT')
}

你可能感兴趣的:(前端,javascript,wx.request,微信小程序)