-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
66 lines (64 loc) · 2.66 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// app.js
let com = require("./utils/util.js");
App({
com: com,
onLaunch(key, data) {
// 展示本地存储能力
const logs = wx.getStorageSync("logs") || [];
logs.unshift(Date.now());
wx.setStorageSync("logs", logs);
//获取系统信息
wx.getSystemInfo({
success: (e) => {
this.globalData.StatusBar = e.statusBarHeight;
let capsule = wx.getMenuButtonBoundingClientRect();
if (capsule) {
this.globalData.Custom = capsule;
this.globalData.CustomBar =
capsule.bottom + capsule.top - e.statusBarHeight;
} else {
this.globalData.CustomBar = e.statusBarHeight + 50;
}
},
});
// 登录
wx.login({
success(res) {
com.post("User/login", res.code, function (res) {
//是否获取到用户的openid和token
if (res.code === 0) {
//缓存会话token和用户openId
wx.setStorageSync("token", res.data.session_key);
wx.setStorageSync('openId', res.data.openid)
//用openid请求数据库,若data为null,则用户表中新建用户
com.get(
"User/queryUserByOpenId/" + res.data.openid,
{},
function (res) {
//判断是否有用户数据,若无,则新建用户
if (res.data == null) {
com.post(
"User/addUser",
{
nickName: '💜无名之辈💛',
avatarUrl: 'https://z3.ax1x.com/2021/11/09/ItIbm4.jpg',
openId: wx.getStorageSync('openId')
}, function (res) {
wx.setStorageSync('user', res.data)
}
);
} else {
//若查询到用户信息,则将返回值存入本地
wx.setStorageSync("user", res.data);
}
}
);
}
});
},
});
},
globalData: {
userInfo: null,
},
});