-
Notifications
You must be signed in to change notification settings - Fork 101
/
app.js
190 lines (188 loc) · 6.07 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
//app.js
App({
globalData: {
domain: 'https://shellbox.airmole.cn/api',
_amap_key: '66a87160f8db2a9a76431c954b4f52a5', // 高德导航API秘钥
requestTimeout: 120 * 1000, // 网络请求最长时间120s
defaultGrayAvatar: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
openid: '',
session_key: '',
userInfo: {},
edusysUserInfo: {},
netsysUserInfo: '',
elesysUserInfo: '',
env: 'develop',
hasEdusysStorage: false
},
onLaunch: function () {
let launchInfo = wx.getLaunchOptionsSync()
const accountInfo = wx.getAccountInfoSync()
this.globalData.env = accountInfo.miniProgram.envVersion
// this.globalData.env = 'release'
if (this.globalData.env == 'release') {
this.globalData.domain = 'https://shellbox.airmole.cn/api'
} else {
this.globalData.domain = 'https://dev.shellbox.airmole.cn/api'
// this.globalData.domain = 'http://shellbox.cn/api'
}
if(launchInfo.scene != 1145){
this.getUserOpenId()
this.appUpdate()
}
this.checkHasEdusysStorage()
this.getStorageEdusysUserInfo()
this.getUserInfoFromStorage()
this.getSystemStatusBarInfo()
console.log('app.globalData:', this.globalData)
},
checkHasEdusysStorage: function () {
const edusysStorage = wx.getStorageSync('edusysUserInfo') || {}
var self = this
try {
if (edusysStorage.uid.length > 0) {
self.globalData.hasEdusysStorage = true
self.globalData.edusysUserInfo = self.autoLoginCookie(edusysStorage)
} else {
self.globalData.hasEdusysStorage = false
}
} catch (error) {
self.globalData.hasEdusysStorage = false
}
},
autoLoginCookie: function (edusysInfo) {
var self = this
const uid = edusysInfo.uid
const pwd = edusysInfo.password
const cookie = edusysInfo.cookie ? edusysInfo.cookie : ''
const openid = wx.getStorageSync('openid') || { openid: this.globalData.openid }
wx.request({
// url: `https://dev.shellbox.airmole.cn/api/edu/profile`,
url: `${self.globalData.domain}/edu/profile`,
data:{
uid: uid,
pwd: pwd,
cookie: cookie,
userFrom: 'wechat',
openid: openid.openid
},
timeout: self.globalData.requestTimeout,
method: 'POST',
success: function(res){
// console.log('eduSysProfile:', res.data)
try {
if (res.data.name.length > 1) {
res.data.password = pwd
wx.setStorage({ data: res.data, key: 'edusysUserInfo' })
self.globalData.edusysUserInfo = res.data
}
} catch (error) {
console.log('后台自动静默激活教务cookie失败', error)
}
},
fail: function(error){
console.log('后台自动静默激活教务cookie失败', error)
}
})
},
getUserInfoFromStorage: function () {
var self = this;
const userInfo = wx.getStorageSync('userInfo') || {}
self.globalData.userInfo = userInfo
},
getStorageEdusysUserInfo: function () {
var self = this;
const edusysUserInfo = wx.getStorageSync('edusysUserInfo') || {}
self.globalData.edusysUserInfo = edusysUserInfo
},
getSystemStatusBarInfo: function () {
// 获取系统状态栏信息
wx.getSystemInfo({
success: e => {
this.globalData.StatusBar = e.statusBarHeight;
let capsule = wx.getMenuButtonBoundingClientRect();
if (capsule) {
this.globalData.Custom = capsule;
this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
} else {
this.globalData.CustomBar = e.statusBarHeight + 50;
}
}
})
},
getUserOpenId: function (callback) {
var self = this
var storageOpenid = wx.getStorageSync('openid') || {}
if (storageOpenid.openid) {
wx.checkSession({
success () {
//session_key 未过期,并且在本生命周期一直有效
self.globalData.openid = storageOpenid.openid
return
},
fail () {
// session_key 已经失效,需要重新执行登录流程
self.wxLoginAndRequest()
}
})
}
if (self.globalData.openid) {
callback(null, self.globalData.openid)
} else {
this.wxLoginAndRequest()
}
},
wxLoginAndRequest: function () {
var self = this
wx.login({
success: function (data) {
wx.request({
url: self.globalData.domain + `/wechat/openid?jscode=${data.code}`,
success: function (res) {
console.log('拉取openid成功', res.data)
wx.setStorageSync('openid', res.data)
self.globalData.openid = res.data.openid
self.globalData.session_key = res.data.session_key
},
fail: function (res) {
console.log('拉取用户openid失败,将无法正常使用开放接口等服务', res)
}
})
},
fail: function (err) {
console.log('wx.login 接口调用失败,将无法正常使用开放接口等服务', err)
}
})
},
appUpdate: function (where) {
const updateManager = wx.getUpdateManager()
updateManager.onCheckForUpdate(function (res) {
// 请求完新版本信息的回调
// console.log(res)
if (where == 'userclick') {
wx.showToast({
title: '已是最新版',
icon: 'none'
})
}
})
updateManager.onUpdateReady(function () {
wx.showModal({
title: '小盒子求更新',
content: "小盒子有版本功能更新啦,建议各位小可爱重启应用体验新版本(●'◡'●)",
success: function (res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
}
}
})
})
updateManager.onUpdateFailed(function () {
// 新版本下载失败
wx.showToast({
title: '嘤嘤嘤更新失败了。可能网络不好',
duration: 1000
});
})
}
})