class request {
    constructor() {
     this._baseUrl = 'https://xzjl-api.windymuse.cn';
     this._token = wx.getStorageSync('token');
     this._header = {'Authorization': this._token}
    }

    /**
     * 文件上传封装请求
     */
    uploadRequest(url, data, header = this._header) {
        return this.uploadAll(url, data, header)
    }
    
    /**
     * GET类型的网络请求
     */
    getRequest(url, data, header = this._header) {
     return this.requestAll(url, data, header, 'GET')
    }
    
    /**
     * DELETE类型的网络请求
     */
    deleteRequest(url, data, header = this._header) {
     return this.requestAll(url, data, header, 'DELETE')
    }
    
    /**
     * PUT类型的网络请求
     */
    putRequest(url, data, header = this._header) {
     return this.requestAll(url, data, header, 'PUT')
    }
    
    /**
     * POST类型的网络请求
     */
    postRequest(url, data, header = this._header) {
     return this.requestAll(url, data, header, 'POST')
    }

        /**
     * POST类型的网络请求
     */
    patchRequest(url, data, header = this._header) {
        return this.requestAll(url, data, header, 'PATCH')
       }

       
    uploadAll(url, data, header){
        console.log('到这个位置了。。。。。')
        console.log(data)
        return new Promise((resolve, reject) => {
            console.log('输出以下。。。。。')
            console.log(data.file)
            wx.uploadFile({
                method:'POST',
                url: this._baseUrl + url,
                name:'',
                filePath:'',
                header: {'Authorization': wx.getStorageSync('token')},
                formData: {
                    type:data.type,
                    file:data.file
                },
                success: (res => {
                    if(res.data.code==401){
                        wx.setStorageSync('token', null)
                     wx.showToast({
                         title:'Token无效或过期，请重新登录！',
                         icon: 'none',
                         duration: 5000
                     })
                     wx.navigateTo({
                         url: '/pages/index/index'
                       })
                       resolve(res)
         
                  }
                  
                 if (res.statusCode === 200) {
                  //200: 服务端业务处理正常结束
                  if(res.data.code==401){
                     wx.showToast({
                         title:'Token无效或过期，请重新登录！',
                         icon: 'none',
                         duration: 5000
                     })
                     wx.navigateTo({
                         url: '/pages/index/index'
                       })
         
                  }
                  resolve(res)
                 } else {
                  //其它错误，提示用户错误信息
                  reject(res)
                 }
                }),
                fail: (res => {
                 reject(res)
                })
              })
        })



        

    }
    
    /**
     * 网络请求
     */
    requestAll(url, data, header, method) {
     return new Promise((resolve, reject) => {
      wx.request({
       url: this._baseUrl + url,
       data: data,
       header: {'Authorization': wx.getStorageSync('token')},
       method: method,
       success: (res => {
           if(res.data.code==401){
               wx.setStorageSync('token', null)
            wx.showToast({
                title:'Token无效或过期，请重新登录！',
                icon: 'none',
                duration: 5000
            })
            wx.navigateTo({
                url: '/pages/index/index'
              })
              resolve(res)

         }
         
        if (res.statusCode === 200) {
         //200: 服务端业务处理正常结束
         if(res.data.code==401){
            wx.showToast({
                title:'Token无效或过期，请重新登录！',
                icon: 'none',
                duration: 5000
            })
            wx.navigateTo({
                url: '/pages/index/index'
              })

         }
         resolve(res)
        } else {
         //其它错误，提示用户错误信息
         reject(res)
        }
       }),
       fail: (res => {
        reject(res)
       })
      })
     })
    }
   }
    
   export default request