小程序设置请求超时提醒

167 阅读1分钟

小程序设置请求超时.............................

const BaseUrl = "https://www.cn"
const post = {
    post ( url, data ) {
      //  数据请求出来前显示的加载动画
       wx.showLoading({
         title: '正在加载中...',
         mask:true//请求时是否开启白色蒙版,以防点击穿透
       })
        return new Promise((resolve, reject) => {
            wx.request({
            url: BaseUrl + url, //将传入的网址与baseurl拼接
            data:data || {}, //有data就传入data,没有设置为空
            method:"POST", //请求的类型
            header: {//设置请求头
              'content-Type': 'application/json',
              'Cookie':wx.getStorageSync("key"),//读取本地存储的token
              },
            success (res) {//成功时的回调
              if(res.data == "{code:9006}"){
                wx.showModal({
                  title: '提示',
                  showCancel: false,
                  content: '登入失效,请重新登入',
                  success(res) {
                    if (res.confirm) {//confirm为true时代表用户点击了按钮
                      wx.reLaunch({
                        url: '/pages/log/log'
                      });
                    }
                  }
                })
              }else if(res.data == "{code:1903}"){
                wx.showToast({
                  title: '请求超时,请重新尝试',
                  icon:"none",
                  mask:true,
                })
              }
              resolve(res)
            },
            fail () {//失败时的回调
              wx.hideLoading();
              wx.showToast({
              title: '请求超时,请重试',
              icon:"none"
            })

            },
            reject(){

            },
            complete () {//接口调用结束的回调函数(调用成功、失败都会执行)
              //取消加载动画
              wx.hideLoading({
                mask:false
              })
              
            }
          })
    })
    },
  }
  export default post

新建了个群,欢迎大家一起进群讨论459358760