RestActions.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. address = layout.config.app_protocol+"//"+addressObj.host+(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. address = layout.config.app_protocol+"//"+host+(port=="80" ? "" : ":"+port);
  145. }
  146. return address;
  147. },
  148. "invokeAsync": function(actions, callback){
  149. var len = actions.length;
  150. var parlen = arguments.length-2;
  151. var res = [];
  152. var jsons = new Array(len-1);
  153. var args = arguments;
  154. var cbs = (o2.typeOf(callback)==="function") ? callback : callback.success;
  155. var cbf = (o2.typeOf(callback)==="function") ? null : callback.failure;
  156. var cb = function(){
  157. if (res.length===len) cbs.apply(this, jsons);
  158. };
  159. var _doError = function(xhr, text, error){
  160. if (xhr.status!=0){
  161. var errorText = error;
  162. if (xhr){
  163. var json = JSON.decode(xhr.responseText);
  164. if (json){
  165. errorText = json.message.trim() || "request json error";
  166. }else{
  167. errorText = "request json error: "+xhr.responseText;
  168. }
  169. }
  170. MWF.xDesktop.notice("error", {x: "right", y:"top"}, errorText);
  171. }
  172. };
  173. actions.each(function(action, i){
  174. var par = (i<parlen) ? args[i+2] : args[parlen+1];
  175. if (par){
  176. var actionArgs = (o2.typeOf(par)==="array") ? par : [par];
  177. actionArgs.unshift(function(xhr, text, error){
  178. res.push(false);
  179. if (!cbf){
  180. _doError(xhr, text, error);
  181. }else{
  182. cbf();
  183. }
  184. cb();
  185. });
  186. actionArgs.unshift(function(json){
  187. jsons[i] = json;
  188. res.push(true);
  189. cb();
  190. });
  191. action.action[action.name].apply(action.action, actionArgs);
  192. }else{
  193. action.action[action.name](function(){
  194. jsons[i] = json;
  195. res.push(true);
  196. cb();
  197. }, function(xhr, text, error){
  198. res.push(false);
  199. if (!cbf){
  200. _doError(xhr, text, error);
  201. }else{
  202. cbf();
  203. }
  204. cb();
  205. });
  206. }
  207. });
  208. }
  209. };
  210. MWF.xAction.RestActions.Action = new Class({
  211. initialize: function(root, actions){
  212. this.action = new MWF.xDesktop.Actions.RestActions("/xAction/services/"+root+".json", root, "");
  213. this.action.actions = actions;
  214. Object.each(this.action.actions, function(service, key){
  215. if (service.uri) if (!this[key]) this.createMethod(service, key);
  216. }.bind(this));
  217. },
  218. createMethod: function(service, key){
  219. var jaxrsUri = service.uri;
  220. var re = new RegExp("\{.+?\}", "g");
  221. var replaceWords = jaxrsUri.match(re);
  222. var parameters = [];
  223. if (replaceWords) parameters = replaceWords.map(function(s){
  224. return s.substring(1,s.length-1);
  225. });
  226. this[key] = this.invokeFunction(service, parameters, key);
  227. },
  228. invokeFunction: function(service, parameters, key){
  229. //uri的参数, data(post, put), file(formData), success, failure, async
  230. return function(){
  231. var i = parameters.length-1;
  232. var n = arguments.length;
  233. var functionArguments = arguments;
  234. var parameter = {};
  235. var success, failure, async, data, file;
  236. if (typeOf(functionArguments[0])==="function"){
  237. i=-1;
  238. success = (n>++i) ? functionArguments[i] : null;
  239. failure = (n>++i) ? functionArguments[i] : null;
  240. parameters.each(function(p, x){
  241. parameter[p] = (n>++i) ? functionArguments[i] : null;
  242. });
  243. if (service.method && (service.method.toLowerCase()==="post" || service.method.toLowerCase()==="put")){
  244. if ((!service.enctype) || service.enctype.toLowerCase()!=="formdata"){
  245. data = (n>++i) ? functionArguments[i] : null;
  246. }else{
  247. data = (n>++i) ? functionArguments[i] : null;
  248. file = (n>++i) ? functionArguments[i] : null;
  249. }
  250. }
  251. async = (n>++i) ? functionArguments[i] : null;
  252. urlEncode = (n>++i) ? functionArguments[i] : true;
  253. cache = (n>++i) ? functionArguments[i] : (Browser.name != "ie");
  254. }else{
  255. parameters.each(function(p, x){
  256. parameter[p] = (n>x) ? functionArguments[x] : 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. success = (n>++i) ? functionArguments[i] : null;
  267. failure = (n>++i) ? functionArguments[i] : null;
  268. async = (n>++i) ? functionArguments[i] : null;
  269. urlEncode = (n>++i) ? functionArguments[i] : true;
  270. cache = (n>++i) ? functionArguments[i] : (Browser.name != "ie");
  271. }
  272. return this.invoke(service,{"name": key, "async": async, "data": data, "file": file, "parameter": parameter, "success": success, "failure": failure, "urlEncode": urlEncode, "cache": cache});
  273. //if (!cache) debugger;
  274. //return this.action.invoke({"name": key, "async": async, "data": data, "file": file, "parameter": parameter, "success": success, "failure": failure, "urlEncode": urlEncode, "cache": cache});
  275. }.bind(this);
  276. },
  277. invoke: function(service, options){
  278. return this.action.invoke(options);
  279. }
  280. });
  281. Date.implement({
  282. "getFromServer": function(){
  283. var d;
  284. o2.Actions.get("x_program_center").echo(function(json){
  285. d = Date.parse(json.data.serverTime);
  286. }, null, false);
  287. return d;
  288. }
  289. });