app.js
9.2 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
const restapi = require('/utils/restapi.js')
const utilapi = require('/utils/util.js')
let app_timer = null
import router from './router/index.js'
import * as Sentry from './utils/sentry-miniapp.wx.min'
// 先屏蔽不上sentry功能
// import * as Sentry from 'sentry-miniapp'
const accountInfo = wx.getAccountInfoSync()
// console.log(Sentry)
// 初始化 Sentry 生产环境再开启
accountInfo.miniProgram.envVersion == 'release' &&
Sentry.init({
dsn: 'https://bcc13d39aab94cba95ce03b65035b5a5@sentry.glsx.com.cn/25'
})
//app.js
App({
// 当小程序初始化完成时,会触发 onLaunch(全局只触发一次)
onLaunch: function (option) {
wx.$router = router
var self = this
// 获取用户登录场景值
// console.log('onLaunch.option', option)
// 初始化本地会话信息
self.initSession()
self.globalData.app = this
self.globalData.scene = option.scene
self.globalData.appId = wx.getAccountInfoSync().miniProgram.appId
self.globalData.initAppInfo(option.query.merchantId | option.query.m, function (res) {
// console.log('App onLaunch', res)
})
/**
* 获取自定义导航栏信息
*/
let padding = 6
let ios = true
wx.getSystemInfo({
success(res) {
// console.log(res)
if (res.platform == 'android') {
padding = 8
ios = false
}
self.globalData.systemInfo = {
statusBarPadding: padding,
ios: ios,
statusBarHeight: res.statusBarHeight,
phoneHeight: res.screenHeight - res.statusBarHeight
}
}
})
// 打开调试
// wx.setEnableDebug({
// enableDebug: true
// })
},
/**
* 初始化本地会话
*/
initSession: function () {
var sessionId = wx.getStorageSync(this.globalData.sessionKey)
if (!sessionId) {
sessionId = utilapi.sessionId()
// 从cookie中获取sessionId存储到本地
// console.log('sessionId:', sessionId)
wx.setStorageSync(this.globalData.sessionKey, sessionId)
}
},
initAppInfo: function (merchantId, callback) {
let self = this
if (merchantId == 0) {
merchantId = ''
}
// console.log('initAppInfo merchantId:', merchantId)
restapi.getAppInfo(merchantId, function (res) {
console.log('getAppInfo', res)
if (res.returnCode == 0) {
self.globalData.extConfig = res.data
wx.setStorageSync('EXT_CONFIG', res.data)
if (callback) {
callback(res.data)
}
}
})
},
// 当小程序启动,或从后台进入前台显示,会触发
onShow: function (option) {
let self = this
// console.log('onShow.option ', option)
self.globalData.sceneData = option
self.globalData.scene = option.scene
self.globalData.initAppInfo(option.query.merchantId | option.query.m, function (res) {
// console.log('App onShow', res)
})
//获取转发场景下携带的参数
if (option.shareTicket) {
let shareInfo = wx.getShareInfo(option.shareTicket)
console.log('shareInfo', shareInfo)
}
//我的活动栏,首次进入小程序点击“我的活动”(小程序删除后再使用也算首次),显示使用提示“如何使用做活动”,yes:显示,no:不显示
let showDoActivity = wx.getStorageSync('showDoActivity')
if (!showDoActivity) {
wx.setStorageSync('showDoActivity', 'yes')
}
},
onHide: function () {
let that = this
that.globalData.clearMarqueeTime = true
if (app_timer) clearInterval(app_timer)
},
login: function () {
// debugger
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
var self = this
self.globalData.code = res.code
console.log('login', res)
// restapi.jsTestLogin(res.code, self.globalData.appId, function (response) {
restapi.jscodeLogin(res.code, self.globalData.appId, function (response) {
console.log('response', response)
switch (response.returnCode) {
case '0': // 获取微信用户信息成功
self.globalData.userInfo = response.data
if (response.data.accountId) {
// 已获取手机号
// 登录成功回调
self.loginSuccessCallback(response.data)
} else {
// 未获取手机号,授权获取
wx.navigateTo({
url: '/pages/auth/phone/phone'
})
}
break
case '-1':
// 获取微信用户信息为空,授权获取
if (response.errId == '200205') {
self.userAuth()
}
break
default:
wx.showToast({
title: response.message
})
}
})
}
})
},
userAuth: function () {
var self = this
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserProfile({
desc: '用于完善会员资料',
lang: 'zh_CN',
success: res => {
wx.checkSession({
success() {
//session_key 未过期,并且在本生命周期一直有效
self.authLogin(res)
},
fail() {
// session_key 已经失效,需要重新执行登录流程
wx.login({
success: response => {
appInstance.globalData.code = response['code']
self.authLogin(res)
}
})
}
})
},
fail: res => {
console.log(res)
}
})
},
authLogin: function (wechatAuthSetting) {
var self = this
wechatAuthSetting['code'] = self.globalData.code
wechatAuthSetting['appId'] = self.globalData.appId
wechatAuthSetting['merchantId'] = self.globalData.extConfig.merchantId
console.log('wechatAuthSetting', wechatAuthSetting)
restapi.authLogin(
wechatAuthSetting,
function (response) {
console.log('authLogin', response)
switch (response.returnCode) {
case '0':
self.globalData.userInfo = response.data
if (response.data.accountId) {
// 已获取手机号
// 登录成功回调
self.loginSuccessCallback(response.data)
} else {
// 未获取手机号,授权获取
wx.navigateTo({
url: '/pages/auth/phone/phone'
})
}
break
default:
wx.showToast({
title: response.message
})
}
},
function () {
console.log('authLogin request fail!')
}
)
},
/**
* 提示框
*/
showMessage: function (title) {
wx.showToast({
title: title,
icon: 'none',
duration: 2000
})
},
getCustomerMessageNew(accountId) {
if (!accountId) {
return
}
restapi.getCustomerMessageNew(
{
accountId: accountId,
sendType: 'B'
},
res => {
console.log(res)
if (res.returnCode == 0) {
if (res.data.length > 0) {
wx.showTabBarRedDot({
index: 1
})
}
}
}
)
},
/**
* 登录成功回调
*/
loginSuccessCallback: function (userInfo) {
console.log(userInfo)
this.globalData.userInfo = userInfo
this.globalData.accountId = userInfo.accountId
// this.getCustomerMessageNew(userInfo.accountId)
// if (!app_timer) {
// app_timer = setInterval(() => {
// this.getCustomerMessageNew(userInfo.accountId)
// }, 1000 * 600)
// }
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(userInfo)
}
},
globalData: {
sessionKey: 'DJ_S_',
clearMarqueeTime: false,
extConfig: {
appId: null,
appName: '我的智慧门店',
merchantId: null,
merchantName: ''
},
scene: null,
app: null,
userInfo: null,
code: null,
accountId: null,
unionId: '',
authUserInfo: false,
authPhone: false,
curUserId: '',
nobindAppear: false,
deviceName: '',
smartCarAppList: [],
deviceStatusVO: {},
myMileageVO: {},
// 精品交付单需要的用户手机号
phoneNumber: '',
loginSuccessCallback: function (userInfo) {
this.app.loginSuccessCallback(userInfo)
},
login: function () {
this.app.login()
},
selectedDevice: {
userId: '',
typeNmae: '',
typeId: ''
},
systemInfo: null,
//我的活动栏需要的用户id
systemUserId: null,
employeeId: null,
smartCarMapKey: 'ZEJBZ-LVNRG-EXMQH-I74N6-EHNIS-WHF6E',
sceneData: null,
appId: null,
// 初始化应用信息
initAppInfo: function (merchantId, callback) {
var extConfig = wx.getStorageSync('EXT_CONFIG')
if (extConfig) {
this.extConfig = extConfig
if (merchantId && extConfig.merchantId != merchantId) {
this.app.initAppInfo(merchantId, callback)
} else if (extConfig.merchantId) {
callback(extConfig)
}
} else {
this.app.initAppInfo(merchantId | null, callback)
}
}
}
})