wx.request 微信小程序請(qǐng)求上傳封裝

wx.request 微信小程序請(qǐng)求上傳封裝

目錄
image.png

utils下新建文件wx-prototype.js:

const host = 'https://***'
const alert = title =>{
  wx.showToast({
    title: title,
    icon: 'none'
  })
}
const getRequestOptions = options => {
  const url = host + options.url
  const header = {
    api_token: wx.getStorageSync('api_token'),
    "Content-Type": "application/json"
  }
  options.url = host + options.url
//loading 傳false表示不展現(xiàn)請(qǐng)求彈窗,傳文字展示所傳文字,不傳展示默認(rèn)
  options.loading === false ? null : typeof options.loading === 'string' ? wx.showLoading({
    title: loading,
  }): wx.showLoading({
      title: '請(qǐng)稍后...',
    })
  return Object.assign(options, { url, header });
}


const get = options => {
  options.method = 'get'
  return new Promise((resolve, reject)=>{
    request(getRequestOptions(options), resolve, reject)
  })
}

const post = options => {
  options.method = 'post'
  return new Promise((resolve, reject)=>{
    request(getRequestOptions(options), resolve, reject)
  })
}

const request = (options, resolve, reject) => {
  options.data = options.data || {}
  //后端同學(xué)非要把token放請(qǐng)求參數(shù)里面,沒得辦法??
  options.data.api_token = wx.getStorageSync('api_token'),
    wx.request({
      ...options,
      success: (res) => {
        wx.hideLoading()
        wx.stopPullDownRefresh()
        const data = res.data
        if (data.status == 1) {
          wx.setStorageSync('isLoginPage', false)
          resolve(data)
        } else if (data.status == 400) {
          wx.setStorageSync('isLoginPage', false)
          alert('數(shù)據(jù)不存在')
        } else if (data.status == 401) {
          alert('沒有權(quán)限')
          const isLoginPage = wx.getStorageSync('isLoginPage')
          if (isLoginPage) return;
          setTimeout(_ => {
            //防止一個(gè)頁面有兩個(gè)請(qǐng)求時(shí),跳轉(zhuǎn)兩次登錄頁
            wx.setStorageSync('isLoginPage', true)
            wx.redirectTo({
              url: '/pages/login/login',
            })
            wx.setStorageSync('isLoginPage', false)
          }, 2000)
        } else {
          alert(data.message || '請(qǐng)求錯(cuò)誤')
        }
      },
      fail: (error) => {
        wx.hideLoading()
        wx.stopPullDownRefresh()
        alert(JSON.stringify(error))
        reject(error)
      }
    })
}

// 這里比較懶,直接掛到wx上了
wx.$get = get
wx.$post = post
wx.$upload = upload

wx.$alert = alert

app.js里面:

image.png

使用方法:

Page({
  data: {
      data: {
      list: [],
      page: 1,
     total: 0
   },
  onLoad: function (options) {
    this.getData()
  },
  getData() {
    wx.$get({
      url: '/list',
      data: {
        group: 'system',
        page: this.data.page
      }
    }).then(res => {
      const data = res.data
      const list = this.data.list
      list.push(...data.data)
      this.setData({
        list: list
      })
    })
  },
  onReachBottom() {
     this.data.page += 1
     this.getData()
  }
},

下一篇,微信小程序上傳至騰訊云:http://m.itdecent.cn/p/168aed3265ab

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容