-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
217 lines (203 loc) · 6.01 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//app.js
var md5 = require('utils/md5.js')
var wxTools = require('utils/wxTools.js')
var loginInfo = {};
App({
setConfig: {
url: 'https://mp.mlbrgw.com',
hb_appid: 'hb_wzwd123',
hb_appsecret: 'MXWDh0mzTfreeoE6ffe123'
},
onLaunch: function () {
var that = this;
},
onShow: function (options){
this.onShareTicket(options)
//判断用户是否已经授权获取用户信息
this.isAuthUserInfo(null);
},
globalData: {
userInfo: null,
token: '',
timer: null,
excAmount: null, //可兑换的挑战次数
revive_money: null, //复活卡金额
toAdvs: true,
title: '究竟谁是答题王, 等你来挑战! !',
iv:'',
encryptedData:'',
getinfo: false,
code:'',
isAuthUserInfo:false
},
getSign: function () {
var timestamp = Math.round(new Date().getTime() / 1000);
var sign = md5.md5(this.setConfig.hb_appid + this.setConfig.hb_appsecret + timestamp);
sign = md5.md5(sign + this.setConfig.hb_appsecret);
return { appid: this.setConfig.hb_appid, timestamp: timestamp, sign: sign };
},
//判断是否已经授权获取用户信息
isAuthUserInfo: function (page){
var that = this;
// console.log(page);
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
this.globalData.getinfo = true;
if(page)return true;
that.userLogin();
}else{
this.globalData.getinfo = false;
if (page){
wx.hideLoading();
console.log('显示授权');
page.setData({
showGetinfo: true
});
}
}
}
});
},
//登录
userLogin: function () {
console.log(this.globalData.token);
if (this.globalData.token){
return false;
}
var that = this;
//判断用户是否已经授权获取用户信息 end
wx.getUserInfo({
success: res => {
// console.log(res);
// 可以将 res 发送给后台解码出 unionId
var infoUser = '';
that.globalData.userInfo = res;
//用户信息入库
that.login(res, "", this)
}
})
},
login:function(res,callback,page){
console.log("登录..");
var that = this;
var infoUser = res.userInfo;
console.log(infoUser);
console.log(that.globalData.userInfo);
that.globalData.userInfo = res.userInfo;
var url = that.setConfig.url + '/index.php?g=User&m=login&a=dologin';
//获取登录code
wx.login({
success: function (res) {
console.log(res.code);
if (res.code) {
var data = {
user_name: infoUser.nickName,
nick_name: infoUser.nickName,
head_img: infoUser.avatarUrl,
sex: infoUser.gender,
coutry: infoUser.country,
city: infoUser.city,
province: infoUser.province,
code: res.code,
encryptedData: that.globalData.userInfo.encryptedData,
iv: that.globalData.userInfo.iv,
}
if (callback) callback();
that.request(url, data,(res) => {
// console.log("成功")
if (res.data.code != 20000) {
wx.showToast({
title: res.data.msg,
icon: 'loading',
mask: true,
duration: 1500
})
if (res.data.code == 40500) {
wx.showToast({
title: res.data.msg,
icon: 'loading',
mask: true,
duration: 1500
})
}
return false;
}
if (res.data.token) {
that.globalData.token = res.data.token;
if (that.tokenReadyCallback) {
console.log('tokenReadyCallback')
that.tokenReadyCallback();
}
// 所以此处加入 callback 以防止这种情况 获取慢
if (that.userInfoReadyCallback) {
console.log('userInfoReadyCallback')
that.userInfoReadyCallback(res)
}
}
}, that);
}
}
})
},
request: function (url, data, cb, page){
var signData = this.getSign();
data.sign = signData.sign;
data.timestamp = signData.timestamp;
wxTools.wxRequest({url: url, data: data, method: 'post'}, cb, page, 0);
},
//提交
postLogin: function (url, data, callback = function () { }) {
var that = this;
var signData = this.getSign();
data.sign = signData.sign;
data.timestamp = signData.timestamp;
//发起网络请求
wx.request({
url: url,
data: data,
method: 'POST',
header: { 'content-type': 'application/x-www-form-urlencoded' },
success: function (res) {
if (res.data.code != 20000) {
wx.showToast({
title: res.data.msg,
icon: 'loading',
mask: true,
duration: 1500
})
if (res.data.code == 40500) { callback(res); }
return false;
}
if (res.data.token) {
that.globalData.token = res.data.token;
if (that.tokenReadyCallback) {
that.tokenReadyCallback();
}
}
//console.log(that.globalData)
callback(res);
}
})
},
onShareTicket: function (options) {
console.log("shareTicket: "+options.shareTicket);
var that = this;
// console.log('111:'+options)
// console.log(options)
if (options.shareTicket) {
var shareTickets = options.shareTicket
wx.getShareInfo({
shareTicket: shareTickets,
success: function (res) { //是群聊
that.globalData.encryptedData = res.encryptedData;
that.globalData.iv = res.iv;
if (that.allDataReadyCallback && that.globalData.token){
console.log('onShareTicket.allDataReadyCallback')
that.allDataReadyCallback();
}
}
})
}
}
})