登录适配;编写token刷新机制;架构完善;

This commit is contained in:
Saturneric 2021-03-08 01:09:11 +08:00
parent fab8c33bd2
commit 6ecb37eea2
10 changed files with 213 additions and 9 deletions

0
api/document.js Normal file
View File

0
api/user.js Normal file
View File

33
app.js
View File

@ -1,4 +1,6 @@
// app.js // app.js
import request from './utils/request.js'
App({ App({
onLaunch() { onLaunch() {
// 展示本地存储能力 // 展示本地存储能力
@ -6,12 +8,35 @@ App({
logs.unshift(Date.now()) logs.unshift(Date.now())
wx.setStorageSync('logs', logs) wx.setStorageSync('logs', logs)
// 登录
wx.login({ wx.login({
success: res => { success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId // 发送 res.code 到后台换取 openId, sessionKey, unionId
request({
url: '/wx/user/' + this.globalData.appID + '/login',
method: 'get',
data: {
code: res.code
}
}).then(res => {
console.log(res)
this.globalData.login = true
// 设置登录时间
this.globalData.loginTime = Date.now()
// 设置登录信息
const userInfo = this.globalData.userInfo
userInfo.openid = res.openid
userInfo.postmark = res.postmark
userInfo.token = res.token
}).then(res => {
}).catch(err => {
console.log(err)
})
} }
}) })
// 获取用户信息 // 获取用户信息
wx.getSetting({ wx.getSetting({
success: res => { success: res => {
@ -34,6 +59,10 @@ App({
}) })
}, },
globalData: { globalData: {
userInfo: null userInfo: {},
loginTime: null,
login: false,
appID: 'wxb915ee2a665fcb6c',
baseURL: 'http://localhost:8088'
} }
}) })

View File

@ -1,13 +1,14 @@
{ {
"pages":[ "pages": [
"pages/index/index", "pages/index/index",
"pages/logs/logs" "pages/logs/logs",
"pages/bind-document/bind-document"
], ],
"window":{ "window": {
"backgroundTextStyle":"light", "backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff", "navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "Weixin", "navigationBarTitleText": "Weixin",
"navigationBarTextStyle":"black" "navigationBarTextStyle": "black"
}, },
"style": "v2", "style": "v2",
"sitemapLocation": "sitemap.json" "sitemapLocation": "sitemap.json"

View File

@ -0,0 +1,66 @@
// pages/bind-document.js
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})

View File

@ -0,0 +1,3 @@
{
"usingComponents": {}
}

View File

@ -0,0 +1,2 @@
<!--pages/bind-document.wxml-->
<text>pages/bind-document.wxml</text>

View File

@ -0,0 +1 @@
/* pages/bind-document.wxss */

View File

@ -4,7 +4,7 @@
"ignore": [] "ignore": []
}, },
"setting": { "setting": {
"urlCheck": true, "urlCheck": false,
"es6": true, "es6": true,
"enhance": false, "enhance": false,
"postcss": true, "postcss": true,

102
utils/request.js Normal file
View File

@ -0,0 +1,102 @@
// 正在队列上的请求
const pendings = []
let refreshing = false
// 刷新token
const refresh_token = app => {
return new Promise((resolve, reject) => {
do_request(app, {
url: '/auth/token',
method: 'get'
}).then(res => {
// 设置登录信息
const userInfo = app.globalData.userInfo
userInfo.token = res.token
resolve(true)
}).catch(err => {
console.log(err)
// 清空登录信息
app.globalData.login = false
app.globalData.userInfo = {}
app.globalData.loginTime = null
refreshing = false
reject()
}).then(() => {
// 停止刷新状态
refreshing = false;
// 执行所有的代发请求
pendings.forEach(element => {
do_request(app, element)
})
})
})
}
// 实际操作函数
const do_request = (app, options) => {
return new Promise((resolve, reject) => {
const { data, method, url } = options
// 设置请求头
let header_options = {
'timestamp': Date.now(),
'X-Requested-With': 'XMLHttpRequest'
}
// 检查登录状态
if(app.globalData.login) {
}
options.url = app.globalData.baseURL + url
if(data && method === 'post') {
header_options['Content-Type'] = 'application/x-www-form-urlencoded'
}
wx.request({
header: {
...header_options
},
...options,
success: function(res) {
const data = res.data
if(data.status >= 200 && data.status < 300) {
resolve(data.data)
} else {
reject(data.data)
}
},
fail: function(res) {
reject(res.data)
}
})
})
}
// 对外接口
const request = options => {
return new Promise((resolve, reject) => {
const app = getApp()
// 获得登录时间
const loginTime = app.globalData.loginTime
// 检查是否达到刷新周期
if(loginTime != null && loginTime + 1000 * 60 < Date.now()) {
app.globalData.loginTime = null
refreshing = true
refresh_token(app)
}
if(refreshing) {
pendings.push(options)
} else {
do_request(app, options).then(res => {
resolve(res)
}).catch(err => {
reject(err)
})
}
})
}
export default request