-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
49 lines (46 loc) · 1.29 KB
/
app.js
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
// app.js
import {
getLoginCode,
sendCodeToServer,
checkToken,
checkSession,
} from "./service/api_login";
import { TOKEN_KEY } from "./constants/token-const";
App({
globalData: {
screenWidth: 0,
screenHeight: 0,
statusBarHeight: 0,
},
onLaunch: async function () {
// 1.请求设备信息
const info = wx.getSystemInfoSync();
console.log(info.model);
this.globalData.screenWidth = info.screenWidth;
this.globalData.screenHeight = info.screenHeight;
this.globalData.statusBarHeight = info.statusBarHeight;
// 2.让用户默认进行登录
this.handleLogin();
// 3.获取用户的信息
},
handleLogin: async function () {
// 先检查token是否过期
const token = wx.getStorageSync(TOKEN_KEY);
if (token) {
const checkResult = await checkToken(token);
const isSessionExpire = await checkSession();
if (checkResult.errorCode || !isSessionExpire) {
this.loginAction();
}
}
},
loginAction: async function () {
// 1.获取微信随机给的code
const code = await getLoginCode();
// 2.携带code交给自己的服务器交换token
const result = await sendCodeToServer(code);
const token = result.token;
// 3.储存在Storage里面
wx.setStorageSync(TOKEN_KEY, token);
},
});