-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
217 lines (195 loc) · 5.41 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
import versionUtil from './utils/version-util';
import Global from './services/global';
const util = require('./utils/util')
const environment = require('./utils/environment');
const locationUtil = require('./utils/location/location.util')
const Hub = require("./utils/signalR")
App({
version: versionUtil.version(),
// 设置开发环境 dev: 开发环境 prod:生产环境
global: new Global('dev'),
// 环境配置信息,根据开发模式 mode 判断
config: {},
//连接Hub
hubConnect: {},
//客户端连接
connectionId: '',
isFirstLogin: true,
statusenum: require('./utils/statusenum.js'),
native: require('./utils/native.js'),
utils: require('./utils/util.js'),
onLaunch: function () {
try {
this.hubConnect = new Hub.HubConnection();
var context = wx.getStorageSync('workContext')
if (context) {
this.global.token = context.AccessToken;
this.global.userInfo = context;
this.linkHub();
}
} catch (e) {
//console.log(e)
}
//获取位置信息
wx.startLocationUpdate({
success: (res) => {
//console.log(res)
},
fail: (res) => {
//console.log(res)
wx.showToast({
title: '请转到设置,打开定位权限以便更好体验',
icon: 'none',
duration: 3000
});
}
})
wx.onLocationChange((res) => {
this.global.gcj02_location = res;
this.global.bd09_location = locationUtil.GCJ02Tobd09II({
"lon": res.longitude,
"lat": res.latitude
});
//console.log(this.global.bd09_location);
wx.stopLocationUpdate({
success: (res) => {
//console.log('位置停止成功');
},
})
});
this.config = Object.assign(environment.defaultConfig, environment[this.global.mode]);
//console.log(`=== 当前开发环境 ${this.global.mode} ===`);
// 检查更新
versionUtil.checkUpdate();
//------
},
//用户存在时建立连接
linkHub() {
var that = this
this.hubConnect.start(that.global.ipw, {});
//绑定事件
that.initEventHandle()
},
//绑定事件
initEventHandle() {
var that = this
//成功开启连接时
this.hubConnect.onOpen = res => {
//console.log('onOpen', res)
//调用服务端注册在线用户
this.hubConnect.send("Login", {
StoreId: this.global.storeId,
UserId: this.global.userInfo.Id,
Name: this.global.userInfo.UserRealName,
Avatar: this.global.userInfo.FaceImage,
MobileNumber: this.global.userInfo.MobileNumber
});
//重置状态
that.reset()
//开始心跳
that.start()
}
//连接成功时
this.hubConnect.on("UserConnected", res => {
//user
//console.log('用户加入:', res)
})
//断开连接时
this.hubConnect.on("UserDisconnected", res => {
//user
//console.log('用户退出:', res)
})
//确认登录
this.hubConnect.on("MakeLogin", connectionId => {
//console.log('MakeLogin', connectionId)
//非第一次登录时
if (!that.isFirstLogin) {
that.hubConnect.invoke("CheckLogin", that.connectionId);
} else {
that.isFirstLogin = false;
}
that.connectionId = connectionId
})
//连接关闭时
this.hubConnect.onClose = res => {
//console.log('onClose', res)
//重新连接
that.reconnect()
}
//连接出错时
this.hubConnect.onError = res => {
//console.log('onError', res)
//重新连接
that.reconnect()
}
//接收心跳消息
this.hubConnect.on("OnHeartbeat", res => {
//console.log('OnHeartbeat', res)
//回调
if (!util.isEmpty(that.global.token))
that.global.callback(res)
})
//接收服务端消息
this.hubConnect.on("Receive", res => {
//强制注销
if (res.toUpperCase() == 'SIGNOUT') {
wx.showToast({
title: '您账号权限变更请重新进入小程序',
icon: 'error',
duration: 5000
})
this.global.token = '';
this.global.userInfo = {}
setTimeout(() => {
wx.reLaunch({
url: '/pages/login/index',
complete: (res) => {}
})
}, 3000);
}
//通告
else {
wx.lin.showDialog({
type: 'alert',
title: '消息提示~',
content: res,
success: (res) => {
if (res.confirm) {}
}
})
}
})
},
//重新连接
reconnect() {
var that = this;
if (that.lockReconnect) return;
that.lockReconnect = true;
clearTimeout(that.timer)
//连接10次后不再重新连接
if (that.global.limit < 10) {
that.timer = setTimeout(() => {
//重新连接
that.linkHub();
that.lockReconnect = false;
//console.log("次数:" + that.global.limit)
}, 5000); //每隔5秒连接一次
that.global.limit = that.global.limit + 1
}
},
//重置心跳
reset: function () {
var that = this;
clearTimeout(that.global.timeoutObj);
clearTimeout(that.global.serverTimeoutObj);
return that;
},
//开始心跳
start: function () {
var that = this;
that.global.timeoutObj = setTimeout(() => {
//console.log("心跳开始发送ping");
this.hubConnect.send("SendHeartbeat", 'ping');
}, that.global.timeout);
}
});