RestActions.js 13 KB

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