Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
92 lines (89 loc) · 2.52 KB
/
Copy pathapp.js
File metadata and controls
92 lines (89 loc) · 2.52 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//app.js
var util = require('/utils/url.js');
var networks = util.network
var userInfo = [];
App({
onLaunch: function () {
wx.clearStorage()
//调用API从本地缓存中获取数据
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
this.getUserInfo();
wx.login({
success: function (res) {
if (res.code) {
//发起网络请求
wx.request({
url: networks + 'getopid.php',
data: {
code: res.code
},
method: "POST",
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function (res) {
wx.setStorage({
key: 'openid',
data: res.data,
})
var openid = res.data
wx.getUserInfo({
success: function (res2) {
var name = res2.userInfo.nickName //昵称
var pic = res2.userInfo.avatarUrl //头像
var sex = res2.userInfo.gender //性别
wx.request({
url: networks + 'member.php',
data: {
name: name,
pic: pic,
openid: openid,
sex: sex
},
method: "POST",
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function (res) {
console.log(res)
wx.setStorageSync('uid', res.data.uid)// 将用户id存进缓存
}
})
},
fail: function () {
wx.clearStorage()
}
})
}
});
} else {
console.log('获取用户登录态失败!' + res.errMsg)
}
}
});
},
getUserInfo: function (cb) {
var that = this
if (this.globalData.userInfo) {
typeof cb == "function" && cb(this.globalData.userInfo)
} else {
//调用登录接口
wx.login({
success: function () {
wx.getUserInfo({
success: function (res) {
that.globalData.userInfo = res.userInfo
typeof cb == "function" && cb(that.globalData.userInfo)
}
})
}
})
}
},
globalData: {
userInfo: null,
zt:4
}
})