RestActions.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. debugger;
  92. var cbs = (o2.typeOf(callback)==="function") ? callback : callback.success;
  93. var cbf = (o2.typeOf(callback)==="function") ? null : callback.failure;
  94. var res = [];
  95. var len = actions.length;
  96. var jsons = new Array(len-1);
  97. var cb = function(){
  98. if (res.length===len) cbs.apply(this, jsons);
  99. };
  100. var _doError = function(xhr, text, error){
  101. if (xhr.status!=0){
  102. var errorText = error;
  103. if (xhr){
  104. var json = JSON.decode(xhr.responseText);
  105. if (json){
  106. errorText = json.message.trim() || "request json error";
  107. }else{
  108. errorText = "request json error: "+xhr.responseText;
  109. }
  110. }
  111. MWF.xDesktop.notice("error", {x: "right", y:"top"}, errorText);
  112. }
  113. };
  114. actions.each(function(action, i){
  115. var actionArgs = action.par || [];
  116. actionArgs.push(function(json){
  117. jsons[i] = json;
  118. res.push(true);
  119. cb();
  120. });
  121. actionArgs.push(function(xhr, text, error){
  122. res.push(false);
  123. if (!cbf){
  124. _doError(xhr, text, error);
  125. }else{
  126. cbf();
  127. }
  128. cb();
  129. });
  130. actionArgs.push(true);
  131. actionArgs.push(action.urlEncode);
  132. actionArgs.push(action.cache);
  133. action.action[action.name].apply(action.action, actionArgs);
  134. });
  135. },
  136. "getHost": function(root){
  137. var addressObj = layout.serviceAddressList[root];
  138. var address = "";
  139. if (addressObj){
  140. //var mapping = layout.getAppUrlMapping();
  141. address = layout.config.app_protocol+"//"+(addressObj.host || window.location.hostname)+(addressObj.port==80 ? "" : ":"+addressObj.port);
  142. }else{
  143. var host = layout.desktop.centerServer.host || window.location.hostname;
  144. var port = layout.desktop.centerServer.port;
  145. //var mapping = layout.getCenterUrlMapping();
  146. address = layout.config.app_protocol+"//"+host+(port=="80" ? "" : ":"+port);
  147. }
  148. return address;
  149. },
  150. "invokeAsync": function(actions, callback){
  151. var len = actions.length;
  152. var parlen = arguments.length-2;
  153. var res = [];
  154. var jsons = new Array(len-1);
  155. var args = arguments;
  156. var cbs = (o2.typeOf(callback)==="function") ? callback : callback.success;
  157. var cbf = (o2.typeOf(callback)==="function") ? null : callback.failure;
  158. var cb = function(){
  159. if (res.length===len) cbs.apply(this, jsons);
  160. };
  161. var _doError = function(xhr, text, error){
  162. if (xhr.status!=0){
  163. var errorText = error;
  164. if (xhr){
  165. var json = JSON.decode(xhr.responseText);
  166. if (json){
  167. errorText = json.message.trim() || "request json error";
  168. }else{
  169. errorText = "request json error: "+xhr.responseText;
  170. }
  171. }
  172. MWF.xDesktop.notice("error", {x: "right", y:"top"}, errorText);
  173. }
  174. };
  175. actions.each(function(action, i){
  176. var par = (i<parlen) ? args[i+2] : args[parlen+1];
  177. if (par){
  178. var actionArgs = (o2.typeOf(par)==="array") ? par : [par];
  179. actionArgs.unshift(function(xhr, text, error){
  180. res.push(false);
  181. if (!cbf){
  182. _doError(xhr, text, error);
  183. }else{
  184. cbf();
  185. }
  186. cb();
  187. });
  188. actionArgs.unshift(function(json){
  189. jsons[i] = json;
  190. res.push(true);
  191. cb();
  192. });
  193. action.action[action.name].apply(action.action, actionArgs);
  194. }else{
  195. action.action[action.name](function(){
  196. jsons[i] = json;
  197. res.push(true);
  198. cb();
  199. }, function(xhr, text, error){
  200. res.push(false);
  201. if (!cbf){
  202. _doError(xhr, text, error);
  203. }else{
  204. cbf();
  205. }
  206. cb();
  207. });
  208. }
  209. });
  210. }
  211. };
  212. MWF.xAction.RestActions.Action = new Class({
  213. initialize: function(root, actions){
  214. this.action = new MWF.xDesktop.Actions.RestActions("/xAction/services/"+root+".json", root, "");
  215. this.action.actions = actions;
  216. Object.each(this.action.actions, function(service, key){
  217. if (service.uri) if (!this[key]) this.createMethod(service, key);
  218. }.bind(this));
  219. },
  220. createMethod: function(service, key){
  221. var jaxrsUri = service.uri;
  222. var re = new RegExp("\{.+?\}", "g");
  223. var replaceWords = jaxrsUri.match(re);
  224. var parameters = [];
  225. if (replaceWords) parameters = replaceWords.map(function(s){
  226. return s.substring(1,s.length-1);
  227. });
  228. this[key] = this.invokeFunction(service, parameters, key);
  229. },
  230. invokeFunction: function(service, parameters, key){
  231. //uri的参数, data(post, put), file(formData), success, failure, async
  232. return function(){
  233. var i = parameters.length-1;
  234. var n = arguments.length;
  235. var functionArguments = arguments;
  236. var parameter = {};
  237. var success, failure, async, data, file;
  238. if (typeOf(functionArguments[0])==="function"){
  239. i=-1;
  240. success = (n>++i) ? functionArguments[i] : null;
  241. failure = (n>++i) ? functionArguments[i] : null;
  242. parameters.each(function(p, x){
  243. parameter[p] = (n>++i) ? functionArguments[i] : null;
  244. });
  245. if (service.method && (service.method.toLowerCase()==="post" || service.method.toLowerCase()==="put")){
  246. if ((!service.enctype) || service.enctype.toLowerCase()!=="formdata"){
  247. data = (n>++i) ? functionArguments[i] : null;
  248. }else{
  249. data = (n>++i) ? functionArguments[i] : null;
  250. file = (n>++i) ? functionArguments[i] : null;
  251. }
  252. }
  253. async = (n>++i) ? functionArguments[i] : null;
  254. urlEncode = (n>++i) ? functionArguments[i] : true;
  255. cache = (n>++i) ? functionArguments[i] : (Browser.name != "ie");
  256. }else{
  257. parameters.each(function(p, x){
  258. parameter[p] = (n>x) ? functionArguments[x] : null;
  259. });
  260. if (service.method && (service.method.toLowerCase()==="post" || service.method.toLowerCase()==="put")){
  261. if ((!service.enctype) || service.enctype.toLowerCase()!=="formdata"){
  262. data = (n>++i) ? functionArguments[i] : null;
  263. }else{
  264. data = (n>++i) ? functionArguments[i] : null;
  265. file = (n>++i) ? functionArguments[i] : null;
  266. }
  267. }
  268. success = (n>++i) ? functionArguments[i] : null;
  269. failure = (n>++i) ? functionArguments[i] : null;
  270. async = (n>++i) ? functionArguments[i] : null;
  271. urlEncode = (n>++i) ? functionArguments[i] : true;
  272. cache = (n>++i) ? functionArguments[i] : (Browser.name != "ie");
  273. }
  274. return this.invoke(service,{"name": key, "async": async, "data": data, "file": file, "parameter": parameter, "success": success, "failure": failure, "urlEncode": urlEncode, "cache": cache});
  275. //if (!cache) debugger;
  276. //return this.action.invoke({"name": key, "async": async, "data": data, "file": file, "parameter": parameter, "success": success, "failure": failure, "urlEncode": urlEncode, "cache": cache});
  277. }.bind(this);
  278. },
  279. invoke: function(service, options){
  280. return this.action.invoke(options);
  281. }
  282. });