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
|
Page({
data: {
nickName: "微信账号登录" ,
avatarUrl: "./user-unlogin.png" ,
},
onLoad: function () {
var that = this ;
var nickName = that.data.nickName ;
var avatarUrl = that.data.avatarUrl;
var db= "no" ;
wx.getUserInfo({
success: function (res) {
that.data.nickName = res.userInfo.nickName,
that.data.avatarUrl = res.userInfo.avatarUrl,
that.setData({
nickName: that.data.nickName,
avatarUrl: that.data.avatarUrl,
}),
that.setData({
db: "ok" // 设置变量db,只有成功获取用户信息后才写入数据库
})
if (db = "ok" ) {
var name, url;
wx.request({
url: ‘https://.../getuserinfo.php‘ ,//写自己的服务器
header: {
"Content-Type" : "application/x-www-form-urlencoded"
},
method: "POST" ,
data: {
name: res.userInfo.nickName,
url: res.userInfo.avatarUrl,
},
success: function () {
console.log( "success" )
},
fail: function () {
console.log( "fail" )
}
})
}
},
fail: function (res) {
that.data.nickName = "未授权无法获取用户信息" ,
that.setData({
nickName: that.data.nickName
})
}
})
},
onShow: function () {
}
})
|