Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
75 lines (47 loc) · 1.56 KB
/
Copy pathapp.js
File metadata and controls
75 lines (47 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import './utils/toPromise'
import Store from './reducers/index'
import SERVER from "./server/index"
App({
Store,
onLaunch() {
wx.showLoading({ title: 'loading...', mask: true })
let session_key;
try {
session_key = wx.getStorageSync(SERVER.SESSION_KEY)
} catch (e) {
console.log('[获取登录key-value失败' + JSON.stringify(e))
}
if (!session_key) {
console.log('token为空,获取token并登录');
this._login()
} else {
console.log('token存在,进行验证 -> ' + session_key);
this.getUserInfo();
}
},
getUserInfo() {// 根据当前用户token获取用户信息,失败做重新登录处理
SERVER.getCurrentUserInfo().then(response => {
console.log('token验证成功,并获取用户信息 -> ' + JSON.stringify(response.data))
Store.dispatch({ type: "MODIFY_USER", data: response.data })
wx.hideLoading()
}, error => {
console.log('token验证出错,移除token后重新登陆 -> ' + wx.getStorageSync(SERVER.SESSION_KEY));
wx.removeStorage({ key: SERVER.SESSION_KEY })
this._login()
})
},
_login() {// 登录/注册,并获取当前用户信息
wx.login({
complete: (res) => {
if (res.code) {
SERVER.login(res.code).then(response => {
const { data } = response.data
console.log('登陆成功,记录新token -> ' + data.sessionKey)
wx.setStorageSync(SERVER.SESSION_KEY, data.sessionKey)
this.getUserInfo();
})
}
}
})
}
})