RestActions.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. MWF.xAction = MWF.xAction || {};
  2. //MWF.require("MWF.xDesktop.Actions.RestActions", null, false);
  3. MWF.xAction.RestActions = MWF.Actions = {
  4. "actions": {},
  5. "loadedActions": {},
  6. "get": function(root){
  7. if (this.actions[root]) return this.actions[root];
  8. var actions = null;
  9. var url = o2.session.path+"/xAction/services/"+root+".json";
  10. MWF.getJSON(url, function(json){actions = json;}.bind(this), false, false, false);
  11. if (!MWF.xAction.RestActions.Action[root] && actions.clazz) MWF.require("MWF.xAction.services."+actions.clazz, null, false);
  12. if (!MWF.xAction.RestActions.Action[root]) MWF.xAction.RestActions.Action[root] = new Class({Extends: MWF.xAction.RestActions.Action});
  13. this.actions[root] = new MWF.xAction.RestActions.Action[root](root, actions);
  14. return this.actions[root];
  15. },
  16. "load": function(root){
  17. if (this.loadedActions[root]) return this.loadedActions[root];
  18. var jaxrs = null;
  19. //var url = this.getHost(root)+"/"+root+"/describe/describe.json";
  20. var url = this.getHost(root)+"/"+root+"/describe/api.json";
  21. //var url = "../o2_core/o2/xAction/temp.json";
  22. try{
  23. MWF.getJSON(url, function(json){jaxrs = json.jaxrs;}.bind(this), false, false, false);
  24. if (jaxrs){
  25. var actionObj = {};
  26. jaxrs.each(function(o){
  27. if (o.methods && o.methods.length){
  28. var actions = {};
  29. o.methods.each(function(m){
  30. var o = {"uri": "/"+m.uri};
  31. if (m.method) o.method = m.method;
  32. if (m.enctype) o.enctype = m.enctype;
  33. actions[m.name] = o;
  34. }.bind(this));
  35. actionObj[o.name] = new MWF.xAction.RestActions.Action(root, actions);
  36. //actionObj[o.name] = new MWF.xAction.RestActions.Action(root, o.methods);
  37. }
  38. }.bind(this));
  39. this.loadedActions[root] = actionObj;
  40. return actionObj;
  41. }
  42. }catch(e){}
  43. return null;
  44. },
  45. //actions: [{"action": "", "subAction": "TaskAction", "name": "list", "par": [], "body": "", "urlEncode": false, "cache": false}]
  46. async: function(actions, callback){
  47. var cbs = (o2.typeOf(callback)==="function") ? callback : callback.success;
  48. var cbf = (o2.typeOf(callback)==="function") ? null : callback.failure;
  49. var res = [];
  50. var len = actions.length;
  51. var jsons = new Array(len-1);
  52. var cb = function(){
  53. if (res.length===len) cbs.apply(this, jsons);
  54. };
  55. var _doError = function(xhr, text, error){
  56. if (xhr.status!=0){
  57. var errorText = error;
  58. if (xhr){
  59. var json = JSON.decode(xhr.responseText);
  60. if (json){
  61. errorText = json.message.trim() || "request json error";
  62. }else{
  63. errorText = "request json error: "+xhr.responseText;
  64. }
  65. }
  66. MWF.xDesktop.notice("error", {x: "right", y:"top"}, errorText);
  67. }
  68. };
  69. actions.each(function(action, i){
  70. var actionArgs = action.par || [];
  71. actionArgs.push(function(json){
  72. jsons[i] = json;
  73. res.push(true);
  74. cb();
  75. });
  76. actionArgs.push(function(xhr, text, error){
  77. res.push(false);
  78. if (!cbf){
  79. _doError(xhr, text, error);
  80. }else{
  81. cbf();
  82. }
  83. cb();
  84. });
  85. actionArgs.push(true);
  86. actionArgs.push(action.urlEncode);
  87. actionArgs.push(action.cache);
  88. action.action[action.subAction][action.name].apply(action.action[action.subAction], actionArgs);
  89. });
  90. },
  91. //actions: [{"action": "", "name": "list", "par": [], "body": "", "urlEncode": false, "cache": false}]
  92. invokeAsync2: function(actions, callback){
  93. var cbs = (o2.typeOf(callback)==="function") ? callback : callback.success;
  94. var cbf = (o2.typeOf(callback)==="function") ? null : callback.failure;
  95. var res = [];
  96. var len = actions.length;
  97. var jsons = new Array(len-1);
  98. var cb = function(){
  99. if (res.length===len) cbs.apply(this, jsons);
  100. };
  101. var _doError = function(xhr, text, error){
  102. if (xhr.status!=0){
  103. var errorText = error;
  104. if (xhr){
  105. var json = JSON.decode(xhr.responseText);
  106. if (json){
  107. errorText = json.message.trim() || "request json error";
  108. }else{
  109. errorText = "request json error: "+xhr.responseText;
  110. }
  111. }
  112. MWF.xDesktop.notice("error", {x: "right", y:"top"}, errorText);
  113. }
  114. };
  115. actions.each(function(action, i){
  116. var actionArgs = action.par || [];
  117. actionArgs.push(function(json){
  118. jsons[i] = json;
  119. res.push(true);
  120. cb();
  121. });
  122. actionArgs.push(function(xhr, text, error){
  123. res.push(false);
  124. if (!cbf){
  125. _doError(xhr, text, error);
  126. }else{
  127. cbf();
  128. }
  129. cb();
  130. });
  131. actionArgs.push(true);
  132. actionArgs.push(action.urlEncode);
  133. actionArgs.push(action.cache);
  134. action.action[action.name].apply(action.action, actionArgs);
  135. });
  136. },
  137. "getHost": function(root){
  138. var addressObj = layout.serviceAddressList[root];
  139. var address = "";
  140. var defaultPort = layout.config.app_protocol==='https' ? "443" : "80";
  141. if (addressObj){
  142. var appPort = addressObj.port || window.location.port;
  143. address = layout.config.app_protocol+"//"+(addressObj.host || window.location.hostname)+ ((!appPort || appPort.toString()===defaultPort) ? "" : ":"+appPort);
  144. }else{
  145. var host = layout.desktop.centerServer.host || window.location.hostname;
  146. var port = layout.desktop.centerServer.port || window.location.port;
  147. //var mapping = layout.getCenterUrlMapping();
  148. address = layout.config.app_protocol+"//"+host+( (!port || port.toString()===defaultPort) ? "" : ":"+port);
  149. }
  150. return address;
  151. },
  152. "invokeAsync": function(actions, callback){
  153. var len = actions.length;
  154. var parlen = arguments.length-2;
  155. var res = [];
  156. var jsons = new Array(len-1);
  157. var args = arguments;
  158. var cbs = (o2.typeOf(callback)==="function") ? callback : callback.success;
  159. var cbf = (o2.typeOf(callback)==="function") ? null : callback.failure;
  160. var cb = function(){
  161. if (res.length===len) cbs.apply(this, jsons);
  162. };
  163. var _doError = function(xhr, text, error){
  164. if (xhr.status!=0){
  165. var errorText = error;
  166. if (xhr){
  167. var json = JSON.decode(xhr.responseText);
  168. if (json){
  169. errorText = json.message.trim() || "request json error";
  170. }else{
  171. errorText = "request json error: "+xhr.responseText;
  172. }
  173. }
  174. MWF.xDesktop.notice("error", {x: "right", y:"top"}, errorText);
  175. }
  176. };
  177. actions.each(function(action, i){
  178. var par = (i<parlen) ? args[i+2] : args[parlen+1];
  179. if (par){
  180. var actionArgs = (o2.typeOf(par)==="array") ? par : [par];
  181. actionArgs.unshift(function(xhr, text, error){
  182. res.push(false);
  183. if (!cbf){
  184. _doError(xhr, text, error);
  185. }else{
  186. cbf();
  187. }
  188. cb();
  189. return true;
  190. });
  191. //actionArgs.unshift(null);
  192. actionArgs.unshift(function(json){
  193. jsons[i] = json;
  194. res.push(true);
  195. cb();
  196. });
  197. var p = action.action[action.name].apply(action.action, actionArgs);
  198. // p.catch(function(xhr, text, error){
  199. // res.push(false);
  200. // if (!cbf){
  201. // _doError(xhr, text, error);
  202. // }else{
  203. // cbf();
  204. // }
  205. // cb();
  206. // })
  207. }else{
  208. action.action[action.name](function(){
  209. jsons[i] = json;
  210. res.push(true);
  211. cb();
  212. }, function(xhr, text, error){
  213. res.push(false);
  214. if (!cbf){
  215. _doError(xhr, text, error);
  216. }else{
  217. cbf();
  218. }
  219. cb();
  220. });
  221. }
  222. });
  223. }
  224. };
  225. MWF.xAction.RestActions.Action = new Class({
  226. initialize: function(root, actions){
  227. this.action = new MWF.xDesktop.Actions.RestActions("/xAction/services/"+root+".json", root, "");
  228. this.action.actions = actions;
  229. Object.each(this.action.actions, function(service, key){
  230. if (service.uri) if (!this[key]) this.createMethod(service, key);
  231. }.bind(this));
  232. },
  233. createMethod: function(service, key){
  234. var jaxrsUri = service.uri;
  235. var re = new RegExp("\{.+?\}", "g");
  236. var replaceWords = jaxrsUri.match(re);
  237. var parameters = [];
  238. if (replaceWords) parameters = replaceWords.map(function(s){
  239. return s.substring(1,s.length-1);
  240. });
  241. this[key] = this.invokeFunction(service, parameters, key);
  242. },
  243. invokeFunction: function(service, parameters, key){
  244. //uri的参数, data(post, put), file(formData), success, failure, async
  245. return function(){
  246. var i = parameters.length-1;
  247. var n = arguments.length;
  248. var functionArguments = arguments;
  249. var parameter = {};
  250. var success, failure, async, data, file;
  251. if (typeOf(functionArguments[0])==="function"){
  252. i=-1;
  253. success = (n>++i) ? functionArguments[i] : null;
  254. failure = (n>++i) ? functionArguments[i] : null;
  255. parameters.each(function(p, x){
  256. parameter[p] = (n>++i) ? functionArguments[i] : null;
  257. });
  258. if (service.method && (service.method.toLowerCase()==="post" || service.method.toLowerCase()==="put")){
  259. if ((!service.enctype) || service.enctype.toLowerCase()!=="formdata"){
  260. data = (n>++i) ? functionArguments[i] : null;
  261. }else{
  262. data = (n>++i) ? functionArguments[i] : null;
  263. file = (n>++i) ? functionArguments[i] : null;
  264. }
  265. }
  266. async = (n>++i) ? functionArguments[i] : null;
  267. urlEncode = (n>++i) ? functionArguments[i] : true;
  268. cache = (n>++i) ? functionArguments[i] : (Browser.name != "ie");
  269. }else{
  270. parameters.each(function(p, x){
  271. parameter[p] = (n>x) ? functionArguments[x] : null;
  272. });
  273. if (service.method && (service.method.toLowerCase()==="post" || service.method.toLowerCase()==="put")){
  274. if ((!service.enctype) || service.enctype.toLowerCase()!=="formdata"){
  275. data = (n>++i) ? functionArguments[i] : null;
  276. }else{
  277. data = (n>++i) ? functionArguments[i] : null;
  278. file = (n>++i) ? functionArguments[i] : null;
  279. }
  280. }
  281. success = (n>++i) ? functionArguments[i] : null;
  282. failure = (n>++i) ? functionArguments[i] : null;
  283. async = (n>++i) ? functionArguments[i] : null;
  284. urlEncode = (n>++i) ? functionArguments[i] : true;
  285. cache = (n>++i) ? functionArguments[i] : (Browser.name != "ie");
  286. }
  287. return this.invoke(service,{"name": key, "async": async, "data": data, "file": file, "parameter": parameter, "success": success, "failure": failure, "urlEncode": urlEncode, "cache": cache});
  288. //if (!cache) debugger;
  289. //return this.action.invoke({"name": key, "async": async, "data": data, "file": file, "parameter": parameter, "success": success, "failure": failure, "urlEncode": urlEncode, "cache": cache});
  290. }.bind(this);
  291. },
  292. invoke: function(service, options){
  293. return this.action.invoke(options);
  294. }
  295. });