forked from wangtaoceo/zhouyi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
106 lines (100 loc) · 3.41 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//app.jsvar的作用于是函数作用于,而let是块级别(大括号括起来的内容)const声明的变量只可以在声明时赋值,不可随意修改,这是最大的特点。
App({
data: {
loginData: null,
sign: "",
mobile: "",
username: "",
mid: "",
sharecode: "",
authStatic: false,
loginStatic: false,
authSuccess: false
},
onLaunch: function () {
let that = this;
let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
that.data.kid = extConfig.kid ? extConfig.kid : '123';
//that.data.kid = 123; //123 464
wx.setStorageSync('kid', that.data.kid); //that.data.kid
this.getAuth();
},
getAuth(cb) {
// var that = this;
const apiurl = 'https://friend-guess.playonwechat.com/';
let kid = wx.getStorageSync("kid");
wx.login({
success: function (res) {
console.log(res);
if (res.code) {
//发起网络请求
wx.request({
url: apiurl + 'api/auth?code=' + res.code + '&operator_id=' + kid,
data: {
code: res.code
},
success: function (res) {
console.log(res);
console.log("sign:", res.data.data.sign);
var sign = res.data.data.sign;
try {
wx.setStorageSync('sign', sign);
wx.getUserInfo({
success: function (res) {
console.log("微信信息");
// console.log(res);
let userData = {};
let userInfo = res.userInfo
let nickName = userInfo.nickName
let avatarUrl = userInfo.avatarUrl
let gender = userInfo.gender //性别 0:未知、1:男、2:女
let province = userInfo.province
let city = userInfo.city
let country = userInfo.country;
wx.setStorageSync('userInfo', userInfo);//缓存存用户信息
userData = {
nickName: nickName,
avatarUrl: avatarUrl,
gender: gender,
province: province,
city: city,
country: country
};
wx.request({
url: apiurl + '/api/save-user-info?sign=' + sign + '&operator_id=' + kid,
method: 'POST',
data: {
info: userData
},
success: function (res) {
console.log("保存信息", res);
// that.data.authSuccess = true;
typeof cb == "function" && cb();
}
})
},
fail: function () {
console.log("用户拒绝授权");
wx.openSetting({
success: (res) => {
console.log(res);
}
})
},
})
} catch (e) {
console.log("回话异常:" + e);
}
},
})
} else {
console.log('获取用户登录态失败!' + res.errMsg);
}
}
});
},
globalData: {
userInfo: null,
sign: ""
}
})