gamerpc 8.0.8 → 9.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -12
- package/auth3.0.md +189 -0
- package/docs/2026.07.25.md +209 -0
- package/index.js +68 -1
- package/package.json +33 -21
- package/src/authConn.js +591 -0
- package/src/gameConn.js +10 -0
- package/src/index.js +106 -645
- package/src/remote.js +637 -0
- package/src/utils/secp256k1.js +79 -101
- package/src/utils/socket.io.min.js +7 -0
- package/src/utils/util.js +157 -85
- package/test/game/auth.js +1 -1
- package/test/game/crm.js +1 -1
- package/test/test.html +47 -29
- package/test/utils/secp256k1.js +444 -0
- package/webpack.config.js +41 -13
- package/.babelrc +0 -16
- package/test/game/kow.js +0 -43
- package/test/game/mgr.js +0 -80
package/src/remote.js
ADDED
|
@@ -0,0 +1,637 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remote - 远程连接器基类 (基于 V8.0.8 + HTTPS 支持)
|
|
3
|
+
*
|
|
4
|
+
* 根据创建参数,可分别支持 WS、Socket、HTTP 三种常用通讯模式,
|
|
5
|
+
* 支持 Notify、JSONP、Watching 等报文通讯模式。
|
|
6
|
+
* 支持 LBS 重定向功能和 HTTPS 协议。
|
|
7
|
+
*/
|
|
8
|
+
const { stringify, NotifyType, extendObj, CommStatus, clone, ReturnCodeName, ReturnCode, CommMode, io } = require('./utils/util');
|
|
9
|
+
const EventEmitter = require('events').EventEmitter;
|
|
10
|
+
const Indicator = require('./utils/Indicator');
|
|
11
|
+
|
|
12
|
+
class Remote {
|
|
13
|
+
constructor(options) {
|
|
14
|
+
this.rpcMode = CommMode.post;
|
|
15
|
+
this.loginMode = Indicator.inst();
|
|
16
|
+
this.configOri = options; //读取并保存初始配置,不会修改
|
|
17
|
+
this.config = clone(this.configOri); //复制一份配置信息,有可能修改
|
|
18
|
+
this.notifyHandles = {};
|
|
19
|
+
this.userInfo = {};
|
|
20
|
+
//事件管理器
|
|
21
|
+
this.events = new EventEmitter();
|
|
22
|
+
//状态管理器
|
|
23
|
+
this.status = Indicator.inst(options.status);
|
|
24
|
+
|
|
25
|
+
//两阶段登录时,用户输入验证码时提交此事件
|
|
26
|
+
this.events.on('authcode', async code => {
|
|
27
|
+
console.log('gameconn: commit authcode', code);
|
|
28
|
+
await this.setSign(code).login();
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 修改通讯模式
|
|
34
|
+
* @param {*} $mode 通讯模式
|
|
35
|
+
* @param {*} cb 建立连接时的回调
|
|
36
|
+
*/
|
|
37
|
+
setmode($mode) {
|
|
38
|
+
this.rpcMode = $mode;
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 创建通讯连接组件
|
|
44
|
+
* 支持直接 IP:Port 或通过 url 字段指定完整地址
|
|
45
|
+
*/
|
|
46
|
+
async createSocket() {
|
|
47
|
+
let port = this.config.webserver.port;
|
|
48
|
+
let host = this.config.webserver.host;
|
|
49
|
+
let url = this.config.webserver.url;
|
|
50
|
+
|
|
51
|
+
this.close();
|
|
52
|
+
|
|
53
|
+
if(!url) {
|
|
54
|
+
this.socket = io(`${this.config.UrlHead}://${host}:${port}`, {'force new connection': true});
|
|
55
|
+
} else {
|
|
56
|
+
this.socket = io(`${this.config.UrlHead}://${url}`, {'force new connection': true});
|
|
57
|
+
}
|
|
58
|
+
this.socket.on('notify', ret => {//监听推送消息
|
|
59
|
+
if(this.notifyHandles[ret.type]) {
|
|
60
|
+
this.notifyHandles[ret.type](ret.info);
|
|
61
|
+
}
|
|
62
|
+
else if(!!this.notifyHandles['0']){
|
|
63
|
+
this.notifyHandles['0'](ret.info);
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
.on('disconnect', ()=>{//断线重连
|
|
67
|
+
console.log('gameconn: socket disconnected');
|
|
68
|
+
this.events.emit('comm', {status: 'disconnect'});
|
|
69
|
+
this.socket.needConnect = true;
|
|
70
|
+
setTimeout(()=>{
|
|
71
|
+
if(!!this.socket.needConnect) {
|
|
72
|
+
this.socket.needConnect = false;
|
|
73
|
+
this.socket.connect();
|
|
74
|
+
}
|
|
75
|
+
}, 1500);
|
|
76
|
+
}).on('connect', () => { //连接消息
|
|
77
|
+
console.log('gameconn: socket connected');
|
|
78
|
+
this.events.emit('comm', {status: 'connect'});
|
|
79
|
+
}).on('error', () => {
|
|
80
|
+
this.events.emit('comm', {status: 'error'});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
let self = this;
|
|
84
|
+
let prom = new Promise(resolve => {
|
|
85
|
+
self.events.on('comm', async msg => {
|
|
86
|
+
if(msg.status == 'connect') {
|
|
87
|
+
resolve();
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
});
|
|
91
|
+
return prom;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 获取签名数据集
|
|
96
|
+
*/
|
|
97
|
+
async getSign() {
|
|
98
|
+
console.log('gameconn: getSign');
|
|
99
|
+
let router = this.userInfo.openid.split('.')[0];
|
|
100
|
+
let msg = await this.getRequest({}, router);
|
|
101
|
+
|
|
102
|
+
if(!msg) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
this.userInfo.auth = msg;
|
|
107
|
+
|
|
108
|
+
console.log('gameconn: getSign', msg);
|
|
109
|
+
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 设置验证码
|
|
115
|
+
* @param {*} code
|
|
116
|
+
*/
|
|
117
|
+
setSign(code) {
|
|
118
|
+
if(!!this.userInfo) {
|
|
119
|
+
if(this.loginMode.check(CommStatus.reqSign)) {
|
|
120
|
+
this.status.set(CommStatus.signCode);
|
|
121
|
+
this.userInfo.auth = this.userInfo.auth || {};
|
|
122
|
+
this.userInfo.auth.captcha = code;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* 登录并获得令牌
|
|
130
|
+
*/
|
|
131
|
+
async getToken() {
|
|
132
|
+
if(!this.userInfo
|
|
133
|
+
|| (this.loginMode.check(CommStatus.reqLb) && !this.status.check(CommStatus.lb))
|
|
134
|
+
|| (this.loginMode.check(CommStatus.reqSign) && !this.status.check(CommStatus.signCode)
|
|
135
|
+
|| this.status.check(CommStatus.logined))
|
|
136
|
+
) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
console.log('gameconn: getToken');
|
|
141
|
+
let msg = await this.fetching({
|
|
142
|
+
'func': 'login.UserLogin',
|
|
143
|
+
"oemInfo": this.userInfo,
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
if(!!msg) {
|
|
147
|
+
console.log('gameconn: getToken', msg.code);
|
|
148
|
+
if(msg.code == ReturnCode.Success && !!msg.data) {
|
|
149
|
+
if(typeof msg.data == 'object') {
|
|
150
|
+
extendObj(this.userInfo, msg.data);
|
|
151
|
+
}
|
|
152
|
+
this.status.set(CommStatus.logined);
|
|
153
|
+
this.events.emit('logined', {code:0, data:{currentAuthority: this.userInfo.currentAuthority}});
|
|
154
|
+
|
|
155
|
+
return true;
|
|
156
|
+
} else {
|
|
157
|
+
this.events.emit('logined', {code: msg.code});
|
|
158
|
+
}
|
|
159
|
+
} else {
|
|
160
|
+
console.log('gameconn: getToken failed');
|
|
161
|
+
this.events.emit('logined', {code:-1});
|
|
162
|
+
}
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* 清空先前的缓存状态
|
|
168
|
+
*/
|
|
169
|
+
clearCache() {
|
|
170
|
+
if(this.userInfo) {
|
|
171
|
+
this.userInfo.token = null;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* 执行负载均衡流程
|
|
177
|
+
* @param {*} ui
|
|
178
|
+
*/
|
|
179
|
+
async setLB(force) {
|
|
180
|
+
if(!this.userInfo) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if(!force && this.status.check(CommStatus.lb)) {
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
this.clearCache();
|
|
189
|
+
this.status.init();
|
|
190
|
+
|
|
191
|
+
console.log('gameconn: lb');
|
|
192
|
+
let msg = await this.locate(this.configOri.webserver.host, this.configOri.webserver.port, this.configOri.webserver.url).getRequest({"func": "lb.getServerInfo", "oemInfo":{"domain": this.userInfo.domain, "openid": this.userInfo.openid}});
|
|
193
|
+
|
|
194
|
+
if(!!msg && msg.code == ReturnCode.Success) {
|
|
195
|
+
console.log('gameconn: lb', msg.data);
|
|
196
|
+
this.status.set(CommStatus.lb);
|
|
197
|
+
this.locate(msg.data.ip, msg.data.port, msg.data.url);
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
console.log('gameconn: lb failed');
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* 登录流程
|
|
207
|
+
* @param {Object} options
|
|
208
|
+
* @param {Boolean} options.force 强制登录
|
|
209
|
+
*/
|
|
210
|
+
async login(options) {
|
|
211
|
+
options = options || {};
|
|
212
|
+
if(options.force) {
|
|
213
|
+
this.clearCache();
|
|
214
|
+
this.status.init();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if(options.domain) {
|
|
218
|
+
let authmode = options.openid.split('.')[0];
|
|
219
|
+
switch(authmode) {
|
|
220
|
+
case 'bxs': {
|
|
221
|
+
this.setUserInfo({
|
|
222
|
+
domain: options.domain,
|
|
223
|
+
openid: options.openid,
|
|
224
|
+
openkey: options.openkey,
|
|
225
|
+
auth: options.auth,
|
|
226
|
+
}, CommStatus.reqLb);
|
|
227
|
+
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
case 'authwx': {
|
|
232
|
+
this.setUserInfo({
|
|
233
|
+
domain: options.domain,
|
|
234
|
+
openkey: options.openkey,
|
|
235
|
+
}, CommStatus.reqLb | CommStatus.reqOpenId);
|
|
236
|
+
|
|
237
|
+
break;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
case 'auth2step': {
|
|
241
|
+
this.setUserInfo({
|
|
242
|
+
domain: options.domain,
|
|
243
|
+
openid: options.openid,
|
|
244
|
+
openkey: options.openkey,
|
|
245
|
+
addrType: options.addrType,
|
|
246
|
+
address: options.address,
|
|
247
|
+
}, CommStatus.reqLb | CommStatus.reqSign);
|
|
248
|
+
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
case 'authpwd': {
|
|
253
|
+
this.setUserInfo({
|
|
254
|
+
domain: options.domain,
|
|
255
|
+
openid: options.openid,
|
|
256
|
+
openkey: options.openkey,
|
|
257
|
+
}, CommStatus.reqLb);
|
|
258
|
+
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
default: {
|
|
263
|
+
this.setUserInfo({
|
|
264
|
+
domain: options.domain || 'official',
|
|
265
|
+
openid: options.openid,
|
|
266
|
+
openkey: options.openkey,
|
|
267
|
+
});
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if(this.status.check(CommStatus.logined)) {
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if(!this.userInfo) {
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if(this.loginMode.check(CommStatus.reqLb)) {
|
|
282
|
+
if(!this.status.check(CommStatus.lb)) {
|
|
283
|
+
if(!(await this.setLB())) {
|
|
284
|
+
throw(new Error('lb error'));
|
|
285
|
+
} else {
|
|
286
|
+
this.status.set(CommStatus.lb);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if(this.loginMode.check(CommStatus.reqSign)) {
|
|
292
|
+
if(!this.status.check(CommStatus.sign)) {
|
|
293
|
+
if(!(await this.getSign())) {
|
|
294
|
+
throw(new Error('get sign error'));
|
|
295
|
+
} else {
|
|
296
|
+
this.status.set(CommStatus.sign);
|
|
297
|
+
return true;
|
|
298
|
+
}
|
|
299
|
+
} else {
|
|
300
|
+
return await this.getToken();
|
|
301
|
+
}
|
|
302
|
+
} else {
|
|
303
|
+
return await this.getToken();
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* 设置用户信息
|
|
309
|
+
* @param {Object} ui {openid}
|
|
310
|
+
* @param {Number} st 登录流程描述符
|
|
311
|
+
*/
|
|
312
|
+
setUserInfo(ui, st) {
|
|
313
|
+
this.userInfo = this.userInfo || {};
|
|
314
|
+
|
|
315
|
+
this.clearCache();
|
|
316
|
+
|
|
317
|
+
if(!!st) {
|
|
318
|
+
this.loginMode.init(st);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
extendObj(this.userInfo, ui);
|
|
322
|
+
|
|
323
|
+
return this;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* 设置服务端推送报文的监控句柄,支持链式调用
|
|
328
|
+
* @param cb 回调
|
|
329
|
+
* @param etype
|
|
330
|
+
* @returns {Remote}
|
|
331
|
+
*/
|
|
332
|
+
watch(cb, etype = '0') {
|
|
333
|
+
this.notifyHandles[etype] = cb;
|
|
334
|
+
return this;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* 移除监控句柄
|
|
338
|
+
* @param {*} etype
|
|
339
|
+
*/
|
|
340
|
+
unWatch(etype) {
|
|
341
|
+
if(!!this.notifyHandles[etype]) {
|
|
342
|
+
delete this.notifyHandles[etype];
|
|
343
|
+
}
|
|
344
|
+
return this;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* 判断返回值是否成功
|
|
349
|
+
* @param msg 网络报文
|
|
350
|
+
* @param out 强制打印日志
|
|
351
|
+
* @returns {*}
|
|
352
|
+
*/
|
|
353
|
+
isSuccess(msg, out=false) {
|
|
354
|
+
if(!msg) {
|
|
355
|
+
return false;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
msg.msg = ReturnCodeName[msg.code];
|
|
359
|
+
|
|
360
|
+
if((msg.code != ReturnCode.Success) || out){
|
|
361
|
+
this.log(msg);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
return msg.code == ReturnCode.Success;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* 直接打印各种对象
|
|
369
|
+
* @param val
|
|
370
|
+
*/
|
|
371
|
+
log(val){
|
|
372
|
+
if(!val){
|
|
373
|
+
console.log('undefined');
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if(!!val.code){
|
|
378
|
+
val.msg = ReturnCodeName[val.code];
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
switch(typeof val){
|
|
382
|
+
case 'number':
|
|
383
|
+
case 'string':
|
|
384
|
+
case 'boolean':
|
|
385
|
+
console.log(val);
|
|
386
|
+
break;
|
|
387
|
+
case 'function':
|
|
388
|
+
console.log(val());
|
|
389
|
+
break;
|
|
390
|
+
case 'undefined':
|
|
391
|
+
console.log('err: undefined');
|
|
392
|
+
break;
|
|
393
|
+
default:
|
|
394
|
+
console.log(JSON.stringify(val));
|
|
395
|
+
break;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
get newone(){
|
|
400
|
+
return new Remote(this.configOri).setmode(this.rpcMode);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
get new(){
|
|
404
|
+
return new Remote(this.configOri).setmode(this.rpcMode);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* 等待指定时长
|
|
409
|
+
* @param {Number} time 等待时长(毫秒)
|
|
410
|
+
*/
|
|
411
|
+
async wait (time) {
|
|
412
|
+
await (async (time) => {return new Promise(resolve => {setTimeout(resolve, time);});})(time);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* 为了提供node下的兼容性而添加的属性设定函数
|
|
417
|
+
* @param {*} fn
|
|
418
|
+
*/
|
|
419
|
+
setFetch(fn) {
|
|
420
|
+
this.fetch = fn;
|
|
421
|
+
return this;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* 向服务端提交请求,默认JSONP模式
|
|
426
|
+
* @param params 命令参数,JSON对象
|
|
427
|
+
* @param callback 回调函数
|
|
428
|
+
* @returns {*}
|
|
429
|
+
*/
|
|
430
|
+
async fetching(params) {
|
|
431
|
+
this.parseParams(params);
|
|
432
|
+
|
|
433
|
+
if(this.loginMode.check(CommStatus.reqLb) && !this.status.check(CommStatus.lb)) {
|
|
434
|
+
await this.setLB();
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
switch(this.rpcMode) {
|
|
438
|
+
case CommMode.ws:
|
|
439
|
+
if(!this.socket) {
|
|
440
|
+
await this.createSocket();
|
|
441
|
+
}
|
|
442
|
+
return new Promise((resolve, reject) => {
|
|
443
|
+
this.socket.emit('req', params, msg => {
|
|
444
|
+
resolve(msg);
|
|
445
|
+
});
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
case CommMode.get:
|
|
449
|
+
return this.getRequest(params);
|
|
450
|
+
|
|
451
|
+
case CommMode.post:
|
|
452
|
+
return this.postRequest(params);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
return Promise.reject();
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* 设定远程服务器地址
|
|
460
|
+
* @param host
|
|
461
|
+
* @param port
|
|
462
|
+
* @param url 完整 URL 地址(用于 HTTPS 代理等场景)
|
|
463
|
+
* @returns {Remote}
|
|
464
|
+
*/
|
|
465
|
+
locate(host, port, url) {
|
|
466
|
+
this.config.webserver.host = host;
|
|
467
|
+
this.config.webserver.port = port;
|
|
468
|
+
this.config.webserver.url = url;
|
|
469
|
+
|
|
470
|
+
return this;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* 关闭长连接
|
|
475
|
+
*/
|
|
476
|
+
close() {
|
|
477
|
+
if(this.socket){
|
|
478
|
+
this.socket.removeAllListeners();
|
|
479
|
+
this.socket.disconnect();
|
|
480
|
+
this.socket = null;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
return this;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* 彻底清除连接器历史数据,包括通讯状态、运行数据缓存,只保留最原始的配置信息
|
|
488
|
+
*/
|
|
489
|
+
init() {
|
|
490
|
+
this.close();
|
|
491
|
+
|
|
492
|
+
this.config = clone(this.configOri);
|
|
493
|
+
this.userInfo = {};
|
|
494
|
+
this.status.init();
|
|
495
|
+
this.loginMode.init();
|
|
496
|
+
|
|
497
|
+
return this;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* 参数调整
|
|
502
|
+
* @param params
|
|
503
|
+
*/
|
|
504
|
+
parseParams(params) {
|
|
505
|
+
params.func = !!params.func ? params.func : 'index.login';
|
|
506
|
+
let arr = params.func.split('.');
|
|
507
|
+
if(arr.length > 1) {
|
|
508
|
+
params.control = arr[0];
|
|
509
|
+
params.action = arr[1];
|
|
510
|
+
} else {
|
|
511
|
+
params.action = arr[0];
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
params.oemInfo = {
|
|
515
|
+
domain: this.userInfo.domain,
|
|
516
|
+
openid: this.userInfo.openid,
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
if(this.userInfo.openkey) {
|
|
520
|
+
params.oemInfo.openkey = this.userInfo.openkey;
|
|
521
|
+
}
|
|
522
|
+
if(this.userInfo.token) {
|
|
523
|
+
params.oemInfo.token = this.userInfo.token;
|
|
524
|
+
}
|
|
525
|
+
if(this.userInfo.addrType) {
|
|
526
|
+
params.oemInfo.addrType = this.userInfo.addrType;
|
|
527
|
+
}
|
|
528
|
+
if(this.userInfo.address) {
|
|
529
|
+
params.oemInfo.address = this.userInfo.address;
|
|
530
|
+
}
|
|
531
|
+
if(this.userInfo.auth) {
|
|
532
|
+
params.oemInfo.auth = this.userInfo.auth;
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* 以 GET 方式访问远程API
|
|
538
|
+
* @param {*} url 远程地址
|
|
539
|
+
*/
|
|
540
|
+
async get(url) {
|
|
541
|
+
const newOptions = { json: true, method: 'GET', mode: 'cors', };
|
|
542
|
+
|
|
543
|
+
newOptions.headers = {
|
|
544
|
+
Accept: 'application/json',
|
|
545
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
546
|
+
};
|
|
547
|
+
|
|
548
|
+
if(this.fetch) {
|
|
549
|
+
let ret = await this.fetch(url, newOptions);
|
|
550
|
+
return await ret.json();
|
|
551
|
+
}
|
|
552
|
+
else {
|
|
553
|
+
let ret = await fetch(url, newOptions);
|
|
554
|
+
return await ret.json();
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* 以 POST 方式访问远程API
|
|
560
|
+
* @param {*} url 远程地址
|
|
561
|
+
* @param {*} options body
|
|
562
|
+
*/
|
|
563
|
+
async post(url, options) {
|
|
564
|
+
const newOptions = { json: true, method: 'POST', mode: 'cors', body: JSON.stringify(options) };
|
|
565
|
+
|
|
566
|
+
newOptions.headers = {
|
|
567
|
+
Accept: 'application/json',
|
|
568
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
569
|
+
};
|
|
570
|
+
|
|
571
|
+
try {
|
|
572
|
+
if(this.fetch) {
|
|
573
|
+
let ret = await this.fetch(url, newOptions);
|
|
574
|
+
return await ret.json();
|
|
575
|
+
}
|
|
576
|
+
else {
|
|
577
|
+
let ret = await fetch(url, newOptions);
|
|
578
|
+
return await ret.json();
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
catch(e) {
|
|
582
|
+
console.error(e);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* (内部函数)发起基于Http协议的GET RPC请求
|
|
588
|
+
* @param params
|
|
589
|
+
*/
|
|
590
|
+
async getRequest(params, authControl) {
|
|
591
|
+
this.parseParams(params);
|
|
592
|
+
|
|
593
|
+
let url = null;
|
|
594
|
+
if(!this.config.webserver.url) {
|
|
595
|
+
let port = !!params.port ? params.port : this.config.webserver.port;
|
|
596
|
+
url = !!authControl ? `${this.config.UrlHead}://${this.config.webserver.host}:${port}/${authControl}` : `${this.config.UrlHead}://${this.config.webserver.host}:${port}/index.html`;
|
|
597
|
+
} else {
|
|
598
|
+
url = !!authControl ? `${this.config.UrlHead}://${this.config.webserver.url}/${authControl}` : `${this.config.UrlHead}://${this.config.webserver.url}/index.html`;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
url += "?" + Object.keys(params).reduce((ret, next)=>{
|
|
602
|
+
if(ret != '') {
|
|
603
|
+
ret += '&';
|
|
604
|
+
}
|
|
605
|
+
return ret + next + "=" + ((typeof params[next]) == "object" ? encodeURIComponent(JSON.stringify(params[next])) : encodeURIComponent(params[next]));
|
|
606
|
+
}, '');
|
|
607
|
+
|
|
608
|
+
return this.get(url);
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* (内部函数)发起基于Http协议的POST RPC请求
|
|
613
|
+
* @param params
|
|
614
|
+
*/
|
|
615
|
+
async postRequest(params, authControl) {
|
|
616
|
+
this.parseParams(params);
|
|
617
|
+
|
|
618
|
+
let url = null;
|
|
619
|
+
if(!this.config.webserver.url) {
|
|
620
|
+
let port = !!params.port ? params.port : this.config.webserver.port;
|
|
621
|
+
url = !!authControl ? `${this.config.UrlHead}://${this.config.webserver.host}:${port}/${authControl}` : `${this.config.UrlHead}://${this.config.webserver.host}:${port}/index.html`;
|
|
622
|
+
} else {
|
|
623
|
+
url = !!authControl ? `${this.config.UrlHead}://${this.config.webserver.url}/${authControl}` : `${this.config.UrlHead}://${this.config.webserver.url}/index.html`;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
return this.post(url, params);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// 静态常量挂载(精简版,仅保留游戏云需要的)
|
|
631
|
+
Remote.prototype.CommMode = CommMode;
|
|
632
|
+
Remote.prototype.stringify = stringify;
|
|
633
|
+
Remote.prototype.CommStatus = CommStatus;
|
|
634
|
+
Remote.prototype.ReturnCode = ReturnCode;
|
|
635
|
+
Remote.prototype.NotifyType = NotifyType;
|
|
636
|
+
|
|
637
|
+
module.exports = Remote;
|