-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
260 lines (257 loc) · 7.23 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//app.js
const Towxml = require('/towxml/main');
App({
onLaunch: function() {
// 展示本地存储能力
//var logs = wx.getStorageSync('logs') || []
//logs.unshift(Date.now())
//wx.setStorageSync('logs', logs)
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
this.globalData.userInfo = res.userInfo
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})
}
}
})
},
//创建一个towxml对象,供其它页面调用
towxml: new Towxml(),
globalData: {
userInfo: null,
baseUrl: "http://192.168.0.102:8888",
successCode: "0",
failCode: "1",
header: {
'Cookie': ''
},
bookHeader: {
'Cookie': '',
},
hasLogin: false,
havaWeChatInfo: false,
havaOpenid: false,
openId: "",
scene: "",
shareTicket: "",
sessionkey: "",
isShow: true
},
checkLoginStatus: function(res) {
if (res.status == '10') {
wx.hideToast();
this.showErrorModal(res.msg, '提示');
this.globalData.hasLogin = false;
wx.navigateTo({
url: '/pages/login/login'
})
return false;
} else if (res.status == '20') {
wx.hideToast();
this.showErrorModal(res.msg, '提示');
return false;
}
return true;
},
onShow: function() {
this.checkSessionYesOrNo();
},
checkSessionYesOrNo: function() {
var that = this;
wx.checkSession({
success: function() {
console.log("yes")
//session_key 未过期,并且在本生命周期一直有效
},
fail: function() {
// session_key 已经失效,需要重新执行登录流程
console.log("no")
that.getOpenId();
}
})
},
getOpenId: function() {
//设置常量,page中使用
//this.data.scene = ops.scene;
//this.data.shareTicket = ops.shareTicket;
var that = this;
var openid = ""; //openid
var nickName = ""; //姓名
var avatarUrl = ""; //头像
var province = ""; //地区
var platform = ""; //设备号
var v = ""; //手机版本号
var sex = ""; //性别
var sessionkey = "";
wx.login({
success: function(res) {
//获取设备信息
wx.getSystemInfo({
success: function(res) {
platform = res.system;
that.globalData.platform = res.system;
v = res.version;
that.globalData.v = res.version;
},
})
if (res.code) {
//获取openId
wx.request({
url: that.globalData.baseUrl + '/portal/user/getWechatInfo.do',
data: {
"jscode": res.code,
"appName": "微井大",
"platform": platform,
"v": v,
"type": "WX"
},
method: 'POST',
success: function(openIdRes) {
//console.log(that);
//console.log(openIdRes);
that.globalData.sessionkey = openIdRes.data.data.sessionKey;
openid = openIdRes.data.data.openId;
that.globalData.openId = openid;
that.globalData.havaOpenid = true;
var weChatOpenId = openid;
wx.setStorage({
key: 'weChatOpenId',
data: weChatOpenId
})
// 判断openId是否获取成功
if (openIdRes.data.data.openId != undefined) {} else {
console.info("获取用户openId失败");
}
},
fail: function(error) {
console.info("获取用户openId失败");
console.info(error);
}
})
}
},
fail: function(err) {
console.log(that);
wx.showToast({
title: '网络异常。。',
icon: 'loading',
duration: 10000
})
}
});
},
getWechatInfo: function(e) {
var that = this;
var weChatOpenId = wx.getStorageSync('weChatOpenId');
if (weChatOpenId !== null && weChatOpenId !==undefined){
that.globalData.havaOpenid = true;
}
that.globalData.openId = weChatOpenId;
var openId = weChatOpenId; //openid
var nickName = ""; //姓名
var avatarUrl = ""; //头像
var country = ""; //地区
var platform = ""; //设备号
var v = ""; //手机版本号
var gender = ""; //性别
if (e.detail.errMsg == 'getUserInfo:ok') {
//seccess
nickName = e.detail.userInfo.nickName;
avatarUrl = e.detail.userInfo.avatarUrl;
country = e.detail.userInfo.country;
gender = e.detail.userInfo.gender;
wx.setStorage({
key: 'userWechatInfo',
data: e.detail.userInfo,
})
//将信息传给后台
wx.request({
url: that.globalData.baseUrl + '/portal/user/saveWechatInfo.do',
data: {
"openId": openId,
"nickName": nickName,
"avatarUrl": avatarUrl,
"country": country,
"gender": gender,
"appname": "微井大",
"platform": platform,
"v": v
},
method: 'post',
success: function(res) {
//网络回滚
if (res.data.status == that.globalData.successCode) {
that.globalData.havaWeChatInfo = true;
}
},
fail: function(error) {
console.info("网络错误");
console.info(error);
}
})
} else {
//失败
console.info("用户拒绝授权");
wx.showModal({
title: '温馨提示',
content: '如不授权,将不能使用此功能',
success: function(res) {
if (res.confirm) {
console.log('用户点击确定')
wx.openSetting({
success: function(res) {
if (!res.authSetting["scope.userInfo"]) {
//这里是授权成功之后 填写你重新获取数据的js
//参考:
that.getLogiCallback('', function() {
callback('')
})
}
}
})
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
}
},
showErrorModal: function(content, title) {
wx.showModal({
title: title || '加载失败',
content: content || '未知错误',
showCancel: false
});
},
showLoadToast: function(title, icon, duration,succCall) {
wx.lin.showToast({
title: title,
icon: icon,
success: (res) => {
succCall()
},
complete: (res) => {
console.log(res)
}
})
},
showLoadMessage: function(type, duration, icon, content) {
wx.lin.showMessage({
type: type,
duration: duration,
icon: icon,
content: content
})
}
})