-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlealone-rpc.js
342 lines (322 loc) · 13.2 KB
/
lealone-rpc.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
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
336
337
338
339
340
341
342
const LealoneRpcClient = (function() {
const L = {
sockjsUrl: "/_lealone_sockjs_",
serviceUrl: "/service",
serviceNames: [],
serviceProxyObjects: [],
cache: [],
getService(serviceName) {
if(this.serviceProxyObjects[serviceName] != undefined)
return this.serviceProxyObjects[serviceName];
var object = { serviceName: serviceName };
this.serviceNames.push(serviceName);
this.serviceProxyObjects[serviceName] = this.getProxyObject(object);
return this.serviceProxyObjects[serviceName];
},
getProxyObject(object) {
let that = this;
let missingMethod = this.missingMethod;
const proxyObject = new Proxy(object, {
get(object, property) {
if (Reflect.has(object, property)) {
return Reflect.get(object, property);
} else {
return (...args) => Reflect.apply(missingMethod, that, [object, property, ...args]);
}
}
});
return proxyObject;
},
missingMethod(object, method, ...args) {
this.call(object, method, ...args);
},
toUnderscore(str) {
return str.replace(/([A-Z])/g, function(v) { return '_' + v.toLowerCase(); });
},
toCamel(str) {
return str.replace(/\_(\w)/g, function(all, letter){
return letter.toUpperCase();
});
},
getKey(serviceName, methodName) {
return serviceName + "." + this.toUnderscore(methodName);
},
call(object, methodName) {
var methodArgs = [];
var key = this.getKey(object.serviceName, methodName);
this.cache[key] = {};
var length = arguments.length;
if(typeof arguments[length - 1] == 'function') {
this.cache[key]["callback"] = arguments[length - 1];
length--;
}
if(length > 2) {
for(var j = 2; j < length; j++) {
if(arguments[j].onServiceException) {
this.cache[key]["onServiceException"] = arguments[j].onServiceException;
this.cache[key]["serviceObject"] = arguments[j];
continue;
}
methodArgs.push(arguments[j]);
}
}
this.sendRequest(object.serviceName, methodName, methodArgs);
},
call4(serviceContext, service, methodName, arguments) {
if(service.hooks != undefined) {
let hook = service.hooks[this.toCamel(methodName)];
if(hook != undefined) {
let beforeHook = hook["before"];
if(beforeHook != undefined) {
let ret = beforeHook.apply(serviceContext, arguments);
if(ret === false)
return
}
}
}
var methodArgs = [];
var key = this.getKey(service.serviceName, methodName);
this.cache[key] = {};
this.cache[key]["onServiceException"] = serviceContext.onServiceException;
this.cache[key]["serviceObject"] = serviceContext;
this.cache[key]["hooks"] = service.hooks;
var argumentCount = arguments.length;
if(typeof arguments[argumentCount - 1] == 'function') {
this.cache[key]["callback"] = arguments[argumentCount - 1];
argumentCount--;
}
// 过滤掉事件对象
if(argumentCount > 0 && arguments[argumentCount - 1].defaultPrevented != undefined) {
argumentCount--;
}
var names = service.methodInfo[methodName];
var columnCount = names.length;
var length = columnCount > argumentCount ? argumentCount : columnCount;
var columnIndex = 0;
if(serviceContext.gid == lealone.page && methodName == lealone.methodName) {
length = lealone.params.length;
for(var i = 0; i < length; i++) {
methodArgs.push(lealone.params[i]);
columnIndex++;
}
}
else if(argumentCount > 0) {
for(var i = 0; i < length; i++) {
methodArgs.push(arguments[i]);
columnIndex++;
}
}
if(columnIndex < columnCount) {
for(var i = columnIndex; i < columnCount; i++) {
// 存在相应字段时才加
if(serviceContext[names[i]] != undefined)
methodArgs.push(serviceContext[names[i]]);
else
methodArgs.push('');
}
}
this.sendRequest(service.serviceName, methodName, methodArgs);
},
callService(serviceContext, serviceName, methodName, methodArgs) {
var service = this.serviceProxyObjects[serviceName];
this.call4(serviceContext, service, methodName, methodArgs);
},
loadServices(callback) {
var object = { serviceName: "lealone_system_service" };
var systemService = this.getProxyObject(object);
systemService.loadServices(this.serviceNames.join(","), services => {
for(var i = 0; i < services.length; i++) {
var serviceInfo = services[i];
var service = this.getService(serviceInfo.serviceName);
var parameterNames = [];
service.methodInfo = {};
var funBody = "return {"
for(var m = 0; m < serviceInfo.serviceMethods.length; m++) {
var serviceMethodInfo = serviceInfo.serviceMethods[m];
funBody += serviceMethodInfo.methodName + "(){ Lealone.callService(this, '"
+ serviceInfo.serviceName + "', '"+ serviceMethodInfo.methodName + "', arguments)},";
service.methodInfo[serviceMethodInfo.methodName] = serviceMethodInfo.parameterNames;
for(var p = 0; p < serviceMethodInfo.parameterNames.length; p++) {
parameterNames.push(serviceMethodInfo.parameterNames[p]);
}
}
funBody += " }";
var fun = new Function(funBody);
service.parameterNames = parameterNames;
service.methods = fun();
for(var m in service.methods) {
service[m] = service.methods[m];
}
}
if(callback != undefined)
callback(services);
});
},
initSockJS() {
var that = this;
var sockjs = new SockJS(this.sockjsUrl); // {"transports":"xhr_streaming"}
that.sockjs = sockjs;
sockjs.onopen = function() {
that.sockjsReady = true;
if(that.penddingMsgs) {
for(var i = 0; i < that.penddingMsgs.length; i++) {
sockjs.send(that.penddingMsgs[i]);
}
that.penddingMsgs = [];
}
};
sockjs.onmessage = function(e) {
that.handleResponse(e);
};
sockjs.onclose = function() {
console.log("SockJS close");
};
},
sendRequest(serviceName, methodName, methodArgs) {
if(window.axios != undefined) {
var underscoreMethodName = this.toUnderscore(methodName);
var url = this.serviceUrl + "/" + serviceName + "/" + underscoreMethodName;
axios.post(url, { methodArgs : JSON.stringify(methodArgs) })
.then(response => {
this.handleResponse(response);
})
.catch(error => {
this.handleError(serviceName, methodName, error.message);
});
} else if(window.SockJS != undefined) {
if(!this.sockjs) {
this.initSockJS();
}
// 格式: type;serviceName.methodName;[arg1,arg2,...argn]
var msg = "1;" + serviceName + "." + methodName + ";" + JSON.stringify(methodArgs);
if(this.sockjsReady)
this.sockjs.send(msg);
else {
if(!this.penddingMsgs) {
this.penddingMsgs = [];
}
this.penddingMsgs.push(msg);
}
} else {
this.handleError(serviceName, methodName, "axios or sockjs not found");
}
},
handleResponse(response) {
var a = [];
if(Array.isArray(response.data))
a = response.data;
else
a = JSON.parse(response.data);
var type = a[0];
var serviceAndMethodName = a[1].split(".");
var serviceName = serviceAndMethodName[0];
var methodName = serviceAndMethodName[1];
var service = this.cache[a[1]];
if(!service) {
console.log("not found service name: "+ serviceName);
return;
}
var result = a[2];
switch(type) {
case 2: // 正常返回
// 如果有回调就执行它
if(service["callback"]) {
try {
result = JSON.parse(result); // 尝试转换成json对象
} catch(e) {
}
service["callback"](result);
}
else if(service["serviceObject"]) {
try {
var serviceObject = service["serviceObject"];
var isJson = true;
try {
result = JSON.parse(result); // 尝试转换成json对象
} catch(e) {
isJson = false;
}
let hooks = service["hooks"];
if(hooks != undefined) {
let hook = hooks[this.toCamel(methodName)];
if(hook != undefined) {
let handleHook = hook["handle"];
//手动处理结果
if(handleHook != undefined) {
handleHook.call(serviceObject, result);
return;
}
}
}
//自动关联字段
if(isJson) {
var isInitMethod = serviceObject.services != undefined && serviceObject.services[1] === true;
for(var key in result) {
if(isInitMethod) {
if(serviceObject.$data)
serviceObject.$data[key] = result[key];
else
serviceObject[key] = result[key];
}
else if(serviceObject[key] != undefined) {
serviceObject[key] = result[key];
}
}
}
if(hooks != undefined) {
let hook = hooks[this.toCamel(methodName)];
if(hook != undefined) {
let afterHook = hook["after"];
if(afterHook != undefined) {
afterHook.call(serviceObject, result);
}
}
}
} catch(e) {
console.log(e);
}
}
break;
case 3: // error
var msg = "failed to call service: " + a[1] + ", backend error: " + result;
this.handleError(serviceName, methodName, msg);
break;
default:
var msg = "unknown response type: " + type + ", response data: " + response.data;
this.handleError(serviceName, methodName, msg);
}
},
handleError(serviceName, methodName, msg) {
var key = this.getKey(serviceName, methodName);
var service = this.cache[key];
if(!service) {
console.error(msg);
} else {
if(service["onServiceException"])
service["onServiceException"](msg);
else {
let hooks = service["hooks"];
if(hooks != undefined) {
let hook = hooks[this.toCamel(methodName)];
if(hook != undefined) {
let errorHook = hook["error"];
if(errorHook != undefined) {
errorHook.call(service["serviceObject"], msg);
return;
}
}
}
console.error(msg)
}
}
},
};
return {
setSockjsUrl: function(url) { L.sockjsUrl = url },
setServiceUrl: function(url) { L.serviceUrl = url },
getService: function() { return L.getService.apply(L, arguments) },
loadServices: function() { return L.loadServices.apply(L, arguments) },
callService: function() { return L.callService.apply(L, arguments) },
};
})();
if(!window.lealone) window.lealone = LealoneRpcClient;