微信小程序cookie过期实现用户重新登录并再次发起业务请求

//获取当前允许环境
const env = wx.getAccountInfoSync().miniProgram.envVersion;
if (!env) {
   console.error("获取运行环境失败!")
}
//设置运行环境地址
const api = {
   // 开发版
   develop: "http://124.221.134.93:8088",
   // 体验版
   trial: "http://124.221.134.93:8088",
   // 正式版
   release: "http://124.221.134.93:8088",
}
const baseURL = api[env];


const http = ({
   url,
   data,
   method = 'POST',
}) => {
   const cookie = wx.getStorageSync('cookie');
   const csrfToken = wx.getStorageSync('csrfToken');
   const header = {
      "content-type": 'application/json',
      'cookie': cookie,
      'X-CSRF-Token': csrfToken
   }
   method = (method || 'POST').toLocaleLowerCase();
   return new Promise((resolve, reject) => {
      wx.request({
         url: baseURL + url,
         method,
         data,
         dataType: "json",
         header,
         timeout: 7000, //请求超时时间
         success: (res) => {
            console.log("响应结果", res)
            if (res.statusCode >= 200 && res.statusCode < 300) {
               resolve(res.data);
            } else if (res.statusCode === 401) { //token过期或者token出错了
               getNewToken().then(res=>{
               //成功获取新的cookie后,再次发起业务请求,并返回结果
                 resolve(http({method, url, data}))
               });
            } else {
               wx.showToast({
                  title: res.data.message,
                  icon: "none",
                  duration: 2000
               })
            }
         },
         fail: (err) => { //请求失败
            wx.showModal({
               title: '服务器出错',
               content: '服务器出错了,请稍后再试!',
               showCancel: false,
            })
            reject(err);
         },
         complete: (res) => {
            // wx.hideLoading()
         }
      })
   });
}
function getNewToken () {
   return new Promise((resolve, reject) => {
      
            const {
               username,
               password
            } = wx.getStorageSync('loginInfo')
            wx.request({
               url: baseURL + '/signin', //在user/login的接口中,后台返回用户的信息并且带有token
               method: 'post',
               header:{
                  "content-type": 'application/json'
               },
               data: {
                  username,
                  password,
               },
               success(res) {
                  if (res.statusCode == 200) { //这里相当于用户又一次登录,并且得到了一个新的cookie并把它写入了 storage本地缓存中
                    //设置新的cookie逻辑代码
                    
                     resolve(res); 
                  }
               },
               fail(err) {
                  console.log('请求出错啦' + err)
                  reject()
               }
            })
         }
   })
}

module.exports = http;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值