微信小程序账号密码登录获取token过期刷新token |
您所在的位置:网站首页 › 微信小程序登录token过期 › 微信小程序账号密码登录获取token过期刷新token |
(代码是来自自做项目的一部分,可自行更改完成业务) 创建utils.js文件在文件中封装wx.request方法 // 封装的微信request方法 function request(url, data = {}, method = "POST", header = "application/x-www-form-urlencoded") { wx.showLoading({ title: '加载中···', mask: true }) //这里是判断登录不需要携带token if (url != api.BASE_URL + '/api/user/login') { header = { 'Content-Type': header, Authorization: `Bearer ${wx.getStorageSync('token')}` } } else { header = { 'Content-Type': header } } return new Promise(function (resolve, reject) { wx.request({ url: url, data: data, method: method, header: header, success: function (res) { wx.hideLoading(); if (res.statusCode == 200) { if (res.data.code == 401) {//token过期之后获取新的token getNewToken().then(() => { request(url,data,method,header).then(res =>{ resolve(res) }) }) } else { resolve(res.data); } } }, fail: function (err) { wx.hideLoading(); reject(err) } }) }) }这个方法为token过期后调用的登录方法获取token function getNewToken() {//token过期之后获取新的token return new Promise((resolve, reject)=>{ request(api.getUserPhone, { username: wx.getStorageSync('username'), password: wx.getStorageSync('password'), }, "POST").then(res => { if (res.code == 200) { wx.setStorageSync('token', res.token) resolve(); } }) }) }
|
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |