app.js
5.67 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
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
const restapi = require('/utils/restapi.js')
//app.js
import util from './utils/util.js'
App({
// 当小程序初始化完成时,会触发 onLaunch(全局只触发一次)
onLaunch: function(option) {
/**挂载到wx对象 */
wx.$util = util
var self = this;
// 获取用户登录场景值
console.log("onLaunch.option", option)
self.globalData.app = this;
self.globalData.scene = option.scene;
self.globalData.appId = wx.getAccountInfoSync().miniProgram.appId;
/**
* 获取自定义导航栏信息
*/
let padding = 6
let ios = true
wx.getSystemInfo({
success(res) {
console.log(res)
if (res.platform == "android") {
padding = 8
ios = false
}
/**兼容全屏手机 */
if (res.screenHeight - res.windowHeight > 72) {
self.globalData.isFullSucreen = true
}
self.globalData.systemInfo = {
statusBarPadding: padding,
ios: ios,
statusBarHeight: res.statusBarHeight,
phoneHeight: res.screenHeight - res.statusBarHeight
}
}
})
// 打开调试
// wx.setEnableDebug({
// enableDebug: true
// })
},
// 当小程序启动,或从后台进入前台显示,会触发
onShow: function(option) {
let self = this;
self.globalData.sceneData = option;
self.globalData.scene = option.scene;
},
onHide: function() {
let that = this
that.globalData.clearMarqueeTime = true
},
login: function() {
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
var self = this;
self.globalData.code = res.code;
restapi.jscodeLogin(res.code, self.globalData.appId, function(response) {
console.log(response)
switch (response.returnCode) {
case '0': // 获取微信用户信息成功
self.globalData.userInfo = response.data;
if (response.data.uuid) { // 已获取手机号
// 登录成功回调
self.loginSuccessCallback(response.data);
} else { // 未获取手机号,授权获取
wx.navigateTo({
url: '/pages/login/index/index'
});
}
break;
case '-1':
// 获取微信用户信息为空,授权获取
if (response.errId == '200205') {
self.getUserInfo();
}
break;
default:
wx.showToast({
title: response.message
})
}
});
}
})
},
getUserInfo: function() {
var self = this;
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
self.userAuth();
} else {
wx.navigateTo({
url: '/pages/index/index'
});
}
},
fail: res => {
console.log(res);
}
});
},
userAuth: function() {
var self = this;
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
withCredentials: true,
lang: 'zh_CN',
success: res => {
wx.checkSession({
success() {
//session_key 未过期,并且在本生命周期一直有效
self.authUserInfo(res);
},
fail() {
// session_key 已经失效,需要重新执行登录流程
wx.login({
success: response => {
appInstance.globalData.code = response['code'];
self.authUserInfo(res);
}
})
}
})
},
fail: res => {
console.log(res);
}
});
},
authUserInfo: function(wechatAuthSetting) {
var self = this;
wechatAuthSetting['code'] = self.globalData.code;
wechatAuthSetting['appId'] = self.globalData.appId;
console.log("wechatAuthSetting", wechatAuthSetting)
restapi.authUserInfo(wechatAuthSetting, function(response) {
console.log("authUserInfo", response)
switch (response.returnCode) {
case '0':
self.globalData.userInfo = response.data;
if (response.data.uuid) { // 已获取手机号
// 登录成功回调
self.loginSuccessCallback(response.data);
} else { // 未获取手机号,授权获取
wx.navigateTo({
url: '/pages/login/index/index'
});
}
break;
default:
wx.showToast({
title: response.message
})
}
}, function() {
console.log("authUserInfo request fail!")
});
},
/**
* 提示框
*/
showMessage: function(title) {
wx.showToast({
title: title,
icon: 'none',
duration: 2000
})
},
/**
* 登录成功回调
*/
loginSuccessCallback: function(userInfo) {
console.log(userInfo);
this.globalData.userInfo = userInfo;
this.globalData.uuid = userInfo.uuid;
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(userInfo)
}
},
globalData: {
clearMarqueeTime:false,
scene: null,
app: null,
userInfo: null,
code: null,
uuid: null,
unionId: '',
authUserInfo: false,
authPhone: false,
curUserId:'',
loginSuccessCallback: function(userInfo) {
this.app.loginSuccessCallback(userInfo);
},
login: function() {
this.app.login();
},
systemInfo: null,
mapKey: '',// 先去除掉腾讯的,后续需要再改造成高德
sceneData: null,
appId: null,
isFullSucreen:false
}
})