RestActions.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. MWF.xAction = MWF.xAction || {};
  2. MWF.require("MWF.xDesktop.Actions.RestActions", null, false);
  3. MWF.xAction.RestActions = MWF.Actions = {
  4. "actions": {},
  5. "get": function(root){
  6. if (this.actions[root]) return this.actions[root];
  7. var actions = null;
  8. var url = o2.session.path+"/xAction/services/"+root+".json";
  9. MWF.getJSON(url, function(json){actions = json;}.bind(this), false, false, false);
  10. if (!MWF.xAction.RestActions.Action[root] && actions.clazz) MWF.require("MWF.xAction.services."+actions.clazz, null, false);
  11. if (!MWF.xAction.RestActions.Action[root]) MWF.xAction.RestActions.Action[root] = new Class({Extends: MWF.xAction.RestActions.Action});
  12. this.actions[root] = new MWF.xAction.RestActions.Action[root](root, actions);
  13. return this.actions[root];
  14. },
  15. "getHost": function(root){
  16. var addressObj = layout.desktop.serviceAddressList[root];
  17. var address = "";
  18. if (addressObj){
  19. address = layout.config.app_protocol+"//"+addressObj.host+(addressObj.port==80 ? "" : ":"+addressObj.port);
  20. }else{
  21. var host = layout.desktop.centerServer.host || window.location.hostname;
  22. var port = layout.desktop.centerServer.port;
  23. address = layout.config.app_protocol+"//"+host+(port=="80" ? "" : ":"+port);
  24. }
  25. return address;
  26. },
  27. "invokeAsync": function(actions, callback){
  28. var len = actions.length;
  29. var parlen = arguments.length-2;
  30. var res = [];
  31. var jsons = new Array(len-1);
  32. var args = arguments;
  33. var cbs = (o2.typeOf(callback)==="function") ? callback : callback.success;
  34. var cbf = (o2.typeOf(callback)==="function") ? null : callback.failure;
  35. var cb = function(){
  36. if (res.length===len) cbs.apply(this, jsons);
  37. };
  38. var _doError = function(xhr, text, error){
  39. if (xhr.status!=0){
  40. var errorText = error;
  41. if (xhr){
  42. var json = JSON.decode(xhr.responseText);
  43. if (json){
  44. errorText = json.message.trim() || "request json error";
  45. }else{
  46. errorText = "request json error: "+xhr.responseText;
  47. }
  48. }
  49. MWF.xDesktop.notice("error", {x: "right", y:"top"}, errorText);
  50. }
  51. };
  52. debugger;
  53. actions.each(function(action, i){
  54. var par = (i<parlen) ? args[i+2] : args[parlen+1];
  55. if (par){
  56. var actionArgs = (o2.typeOf(par)==="array") ? par : [par];
  57. actionArgs.unshift(function(xhr, text, error){
  58. res.push(false);
  59. if (!cbf){
  60. _doError(xhr, text, error);
  61. }else{
  62. cbf();
  63. }
  64. cb();
  65. });
  66. actionArgs.unshift(function(json){
  67. jsons[i] = json;
  68. res.push(true);
  69. cb();
  70. });
  71. action.action[action.name].apply(action.action, actionArgs);
  72. }else{
  73. action.action[action.name](function(){
  74. jsons[i] = json;
  75. res.push(true);
  76. cb();
  77. }, function(xhr, text, error){
  78. res.push(false);
  79. if (!cbf){
  80. _doError(xhr, text, error);
  81. }else{
  82. cbf();
  83. }
  84. cb();
  85. });
  86. }
  87. });
  88. }
  89. };
  90. MWF.xAction.RestActions.Action = new Class({
  91. initialize: function(root, actions){
  92. this.action = new MWF.xDesktop.Actions.RestActions("/xAction/services/"+root+".json", root, "");
  93. this.action.actions = actions;
  94. Object.each(this.action.actions, function(service, key){
  95. if (service.uri) if (!this[key]) this.createMethod(service, key);
  96. }.bind(this));
  97. },
  98. createMethod: function(service, key){
  99. var jaxrsUri = service.uri;
  100. var re = new RegExp("\{.+?\}", "g");
  101. var replaceWords = jaxrsUri.match(re);
  102. var parameters = [];
  103. if (replaceWords) parameters = replaceWords.map(function(s){
  104. return s.substring(1,s.length-1);
  105. });
  106. this[key] = this.invokeFunction(service, parameters, key);
  107. },
  108. invokeFunction: function(service, parameters, key){
  109. //uri的参数, data(post, put), file(formData), success, failure, async
  110. return function(){
  111. var i = parameters.length-1;
  112. var n = arguments.length;
  113. var functionArguments = arguments;
  114. var parameter = {};
  115. var success, failure, async, data, file;
  116. if (typeOf(functionArguments[0])==="function"){
  117. i=-1;
  118. success = (n>++i) ? functionArguments[i] : null;
  119. failure = (n>++i) ? functionArguments[i] : null;
  120. parameters.each(function(p, x){
  121. parameter[p] = (n>++i) ? functionArguments[i] : null;
  122. });
  123. if (service.method && (service.method.toLowerCase()==="post" || service.method.toLowerCase()==="put")){
  124. if ((!service.enctype) || service.enctype.toLowerCase()!=="formdata"){
  125. data = (n>++i) ? functionArguments[i] : null;
  126. }else{
  127. data = (n>++i) ? functionArguments[i] : null;
  128. file = (n>++i) ? functionArguments[i] : null;
  129. }
  130. }
  131. async = (n>++i) ? functionArguments[i] : null;
  132. urlEncode = (n>++i) ? functionArguments[i] : true;
  133. cache = (n>++i) ? functionArguments[i] : false;
  134. }else{
  135. parameters.each(function(p, x){
  136. parameter[p] = (n>x) ? functionArguments[x] : null;
  137. });
  138. if (service.method && (service.method.toLowerCase()==="post" || service.method.toLowerCase()==="put")){
  139. if ((!service.enctype) || service.enctype.toLowerCase()!=="formdata"){
  140. data = (n>++i) ? functionArguments[i] : null;
  141. }else{
  142. data = (n>++i) ? functionArguments[i] : null;
  143. file = (n>++i) ? functionArguments[i] : null;
  144. }
  145. }
  146. success = (n>++i) ? functionArguments[i] : null;
  147. failure = (n>++i) ? functionArguments[i] : null;
  148. async = (n>++i) ? functionArguments[i] : null;
  149. urlEncode = (n>++i) ? functionArguments[i] : true;
  150. cache = (n>++i) ? functionArguments[i] : false;
  151. }
  152. return this.action.invoke({"name": key, "async": async, "data": data, "file": file, "parameter": parameter, "success": success, "failure": failure, "urlEncode": urlEncode, "cache": cache});
  153. }.bind(this);
  154. }
  155. });