微信小程序> 微信小程序token过期时间后重新获取-微信小程序过期恢复步骤-微信小程序怎么找

微信小程序token过期时间后重新获取-微信小程序过期恢复步骤-微信小程序怎么找

浏览量:5593 时间: 来源:前端阿龙
1.

首先先封装个公用的js

classConfig{constructor(){}}Config.restUrl'https://wx.knowdao.com';export{Config};2.

再从另一个封装的共用的调token的js里面传入这个模块

import{Config}from'config.js';classToken{constructor(){console.log(this)this.verifyUrlConfig.restUrl+'/api/validate/token';this.tokenUrlConfig.restUrl+'/api/login/get_openid';}verify(){vartokenwx.getStorageSync('token');console.log(token)if(!token){this.getTokenFromServer(token);}else{this._verifyFromServer(token);}}//携带令牌去服务器校验令牌_verifyFromServer(token){console.log(11212)varthatthis;constaccountwx.getStorageSync("account")//console.log(token)//console.log(wx.getStorageSync("token"))//console.log(account)consttokennwx.getStorageSync("token")wx.request({url:that.verifyUrl,method:'POST',data:{token:tokenn,account:account},success:function(res){console.log(res)returnfalsevarvalidres.data.isValid;if(!valid){that.getTokenFromServer();}}})}//从服务器获取tokengetTokenFromServer(callback){varthatthis;wx.login({success:function(res){letcoderes.codewx.getUserInfo({lang:"zh_CN",success:res{letuserInfores.userInfowx.request({url:that.tokenUrl,method:'POST',data:{code:code,nickname:userInfo.nickName,gender:userInfo.gender,avatarUrl:userInfo.avatarUrl,city:userInfo.city},header:{"content-type":"application/x-www-form-urlencoded",'content-type':'application/json'},success:function(res){console.log(res)wx.setStorageSync('token',res.data.data.token);wx.setStorageSync('account',res.data.data.account);wx.setStorageSync('userid',res.data.data.user_id);}})}})}})}}export{Token};3.

最后在app.js中调用

import{Token}from'utils/token-model.js';varscence0;App({onLaunch:function(){vartokennewToken();token.verify();//token.changlink("wss://wx.knowdao.com/system");},onShow:function(){},onHide:function(){this.globalData.scence1wx.setStorageSync('scence',this.globalData.scence)},globalData:{account:'',stroge:0,openid:0,userInfo:null,times:null,urls:'https://wx.knowdao.com',urlst:'http://test.knowdao.com',token:''}})

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎