-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
84 lines (79 loc) · 2.82 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//app.js
import lcapi from './utils/lcapi';
import { wxmp } from './utils/private';
App({
onLaunch: function () {
//调用API从本地缓存中获取数据
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
},
getUserInfo: function (cb) {
var that = this;
if(this.globalData.userInfo && this.globalData.userInfo.openId && this.globalData.userInfo.objectId) {
typeof cb == "function" && cb(null, this.globalData.userInfo);
} else {
//调用登录接口
wx.login({
success: function (res) {
if (res.code) {
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + that.mpData.appId + '&secret=' + that.mpData.secret + '&js_code=' + res.code + '&grant_type=authorization_code',
success: (oiRes) => {
let openId = oiRes.data.openid;
let sessionKey = oiRes.data.session_key;
function getUserInfo(objectId) {
wx.getUserInfo({
success: uiRes => {
that.globalData.userInfo = uiRes.userInfo;
that.globalData.userInfo.openId = openId;
that.globalData.userInfo.objectId = objectId;
typeof cb == "function" && cb(null, that.globalData.userInfo);
},
fail: () => {
typeof cb == 'function' && cb(new Error('Fail to get user info'));
}
});
}
// Create user if needed
lcapi.getUserByOpenId(openId, (err, userData) => {
if (err) {
return cb(new Error('Fail to get user'));
}
if (!userData) {
// Create user
lcapi.createUser(openId, (err, newUserData) => {
if (err) {
return cb(new Error('Fail to get user'));
}
// User created
getUserInfo(newUserData.objectId);
});
} else {
// User exists
getUserInfo(userData.objectId);
}
});
},
fail: () => {
typeof cb == 'function' && cb(new Error('Fail to get OpenID: Network error'));
}
});
} else {
typeof cb == 'function' && cb(new Error('Fail to login: errMsg = ' + res.errMsg));
}
},
fail: () => {
typeof cb == 'function' && cb(new Error('Fail to login'));
}
});
}
},
globalData:{
userInfo:null
},
mpData: {
appId: wxmp.appId,
secret: wxmp.secret
}
});