function request(url, method, data) {
  let promise = new Promise((resolve, reject) => {
    wx.showNavigationBarLoading();
    wx.request({
      url: getApp().globalData.host + url,
      method: method,
      data: data,
      header:{
        'Authorization': 'JWT ' + getApp().globalData.token
      },
      success: (res => {
        if (res.data.code >= 200 && res.data.code < 400) {
          resolve(res.data);
        }else if(res.data.code == 401){
          getApp().onLaunch()
          wx.switchTab({
            url: '/pages/main/main',
          })
        }
        else {
          var msg = '请求错误'
          if(res.data.msg){
            msg = res.data.msg
          }
          if (msg.indexOf('该操作的权限')!=-1){
            msg = '权限不足或账户过期,请联系课程顾问'
          }
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 1000
          })
        } 
        
      }),
      fail: (res => {
        wx.showToast({
          title: '请求出错',
          icon: 'none',
          duration: 1500
        })
        console.log(res)
        reject('网络出错');
      }),
      complete: function () {
        wx.hideNavigationBarLoading();
      }

    })
  })
  return promise;
}

function requesta(url, method, data) {
  let promise = new Promise((resolve, reject) => {
    wx.showNavigationBarLoading();
    wx.request({
      url: getApp().globalData.host + url,
      method: method,
      data: data,
      header:{
        'Authorization': 'JWT ' + getApp().globalData.admintoken
      },
      success: (res => {
        if (res.data.code >= 200 && res.data.code < 400) {
          resolve(res.data);
        }else if(res.data.code == 401){

        }
        else {
          var msg = '请求错误'
          if(res.data.msg){
            msg = res.data.msg
          }
          if (msg.indexOf('该操作的权限')!=-1){
            msg = '权限不足或账户过期,请联系课程顾问'
          }
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 1000
          })
        } 
        
      }),
      fail: (res => {
        wx.showToast({
          title: '请求出错',
          icon: 'none',
          duration: 1500
        })
        console.log(res)
        reject('网络出错');
      }),
      complete: function () {
        wx.hideNavigationBarLoading();
      }

    })
  })
  return promise;
}

module.exports = {
  request: request, 
  requesta: requesta
}