Skip to content

Commit 634615e

Browse files
committed
登录流程和演练
1 parent 51f8b15 commit 634615e

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

app.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const TOKEN = 'token'
2+
3+
App({
4+
// 对象: 小程序关闭掉
5+
globalData: {
6+
token: ''
7+
},
8+
onLaunch: function () {
9+
// 1.先从缓冲中取出token
10+
const token = wx.getStorageSync(TOKEN)
11+
12+
// 2.判断token是否有值
13+
if (token && token.length !== 0) { // 已经有token,验证token是否过期
14+
this.check_token(token) // 验证token是否过期
15+
} else { // 没有token, 进行登录操作
16+
this.login()
17+
}
18+
},
19+
check_token(token) {
20+
console.log('执行了验证token操作')
21+
wx.request({
22+
url: 'http://123.207.32.32:3000/auth',
23+
method: 'post',
24+
header: {
25+
token
26+
},
27+
success: (res) => {
28+
if (!res.data.errCode) {
29+
console.log('token有效')
30+
this.globalData.token = token;
31+
} else {
32+
this.login()
33+
}
34+
},
35+
fail: function(err) {
36+
console.log(err)
37+
}
38+
})
39+
},
40+
login() {
41+
console.log('执行了登录操作')
42+
wx.login({
43+
// code只有5分钟的有效期
44+
success: (res) => {
45+
// 1.获取code
46+
const code = res.code;
47+
48+
// 2.将code发送给我们的服务器
49+
wx.request({
50+
url: 'http://123.207.32.32:3000/login',
51+
method: 'post',
52+
data: {
53+
code
54+
},
55+
success: (res) => {
56+
// 1.取出token
57+
const token = res.data.token;
58+
59+
// 2.将token保存在globalData中
60+
this.globalData.token = token;
61+
62+
// 3.进行本地存储
63+
wx.setStorageSync(TOKEN, token)
64+
}
65+
})
66+
}
67+
})
68+
}
69+
})

project.config.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
"ignore": []
55
},
66
"setting": {
7-
"urlCheck": true,
7+
"urlCheck": false,
88
"es6": true,
99
"postcss": true,
1010
"minified": true,
1111
"newFeature": true,
12-
"autoAudits": false
12+
"autoAudits": false,
13+
"checkInvalidKey": true
1314
},
1415
"compileType": "miniprogram",
1516
"libVersion": "2.7.1",

0 commit comments

Comments
 (0)