InitByJsonData.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. //默认值设置
  2. defaultinput = {
  3. server: "http://172.23.12.228:8080",
  4. author: base64.encode("meta.admin:admin"),
  5. istesting: true,
  6. classname: "/cncc/action/test",
  7. data: null,
  8. datafiledir: "/opt/cncc/ITIL/change_main",
  9. datafilename: "",
  10. jsfilename: "/script/cncc/ITIL/测试.js",
  11. clsoption: "with ttl=10 day, version=true, nickname='test'",
  12. fieldmap: {id: "jid"},
  13. fields: {idUnique: "string,key"},
  14. mustfield: "id,idUnique",
  15. };
  16. inputmapping = {
  17. "change": {
  18. classname: "/cncc/action/change_main",
  19. datafiledir: "/opt/cncc/ITIL/change_main",
  20. jsfilename: "/script/cncc/ITIL/变更单.js",
  21. clsoption: "with ttl=10 day, version=true, nickname='change_main'",
  22. },
  23. "event": {
  24. classname: "/cncc/action/event",
  25. datafiledir: "/opt/cncc/ITIL/event",
  26. jsfilename: "/script/cncc/ITIL/事件单.js",
  27. clsoption: "with ttl=10 day, version=true, nickname='event'",
  28. },
  29. "problem": {
  30. classname: "/cncc/action/problem",
  31. datafiledir: "/opt/cncc/ITIL/problem",
  32. jsfilename: "/script/cncc/ITIL/问题单.js",
  33. clsoption: "with ttl=10 day, version=true, nickname='problem'",
  34. },
  35. "baobei": {
  36. classname: "/cncc/action/baobei",
  37. datafiledir: "/opt/cncc/ITIL/baobei",
  38. jsfilename: "/script/cncc/ITIL/报备.js",
  39. clsoption: "with ttl=10 day, version=true, nickname='baobei'",
  40. },
  41. "shebeishangxiadian": {
  42. classname: "/cncc/action/shebeishangxiadian",
  43. datafiledir: "/opt/cncc/ITIL/shebeishangxiadian",
  44. jsfilename: "/script/cncc/ITIL/设备上下电.js",
  45. clsoption: "with ttl=10 day, version=true, nickname='shebeishangxiadian'",
  46. },
  47. "shujuhuoqu": {
  48. classname: "/cncc/action/shujuhuoqu",
  49. datafiledir: "/opt/cncc/ITIL/shujuhuoqu",
  50. jsfilename: "/script/cncc/ITIL/数据获取.js",
  51. clsoption: "with ttl=10 day, version=true, nickname='shujuhuoqu'",
  52. },
  53. "yanlian": {
  54. classname: "/cncc/action/yanlian",
  55. datafiledir: "/opt/cncc/ITIL/yanlian",
  56. jsfilename: "/script/cncc/ITIL/应急演练.js",
  57. clsoption: "with ttl=10 day, version=true, nickname='yanlian'",
  58. },
  59. "project": {
  60. classname: "/cncc/action/project",
  61. datafiledir: "/opt/cncc/ITIL/project",
  62. jsfilename: "/script/cncc/ITIL/项目实施或巡检.js",
  63. clsoption: "with ttl=10 day, version=true, nickname='project'",
  64. }
  65. };
  66. // 输入输出参数格式化
  67. input = INPUT;
  68. output = {};
  69. try {
  70. input = decodeURIComponent(input);
  71. } catch(e) {}
  72. try {
  73. input = base64.decode(input);
  74. } catch(e) {}
  75. try {
  76. input = JSON.parse(input);
  77. input = JSON.parse(input);
  78. } catch(e) {}
  79. // 通用函数
  80. function isempty(o) {
  81. if (o) {
  82. for (var k in o){
  83. return false;
  84. }
  85. }
  86. return true;
  87. }
  88. // 主执行阶段
  89. function main() {
  90. output.info = {};
  91. output.info.p0 = {name: "确认参数"};
  92. p = inputmapping[input.name];
  93. if (p) {
  94. for (var k in p) {
  95. defaultinput[k] = p[k];
  96. }
  97. }
  98. server = input.server;
  99. if (!server) {
  100. server = defaultinput.server;
  101. }
  102. author = input.author;
  103. if (!author) {
  104. author = defaultinput.author;
  105. }
  106. istesting = input.istesting;
  107. if (!istesting) {
  108. istesting = defaultinput.istesting;
  109. }
  110. classname = input.classname;
  111. if (!classname) {
  112. classname = defaultinput.classname;
  113. }
  114. if (classname[0] != "/") {
  115. throw ("classname必须以 / 开头");
  116. }
  117. if (classname.substring(0,8) == "/matrix/") {
  118. throw ("classname不能以 /matrix/ 开头");
  119. }
  120. data = input.data;
  121. if (isempty(data)) {
  122. data = defaultinput.data;
  123. if (isempty(data)) {
  124. datafilename = input.datafilename;
  125. if (!datafilename) {
  126. datafilename = defaultinput.datafilename;
  127. }
  128. if (datafilename) {
  129. try {
  130. data = dfs.read(datafilename);
  131. data = JSON.parse(data);
  132. } catch(e) {}
  133. }
  134. }
  135. if (isempty(data)) {
  136. datafiledir = input.datafiledir;
  137. if (!datafiledir) {
  138. if (classname == defaultinput.classname) {
  139. datafiledir = defaultinput.datafiledir;
  140. } else {
  141. datafiledir = "/opt" + classname;
  142. }
  143. }
  144. // 读取所有文件,综合数据
  145. data = {};
  146. files = dfs.readdir(datafiledir);
  147. for (i = 0; i < files.length; i++) {
  148. try {
  149. adat = dfs.read(files[i].fullname);
  150. adat = JSON.parse(adat);
  151. if (typeof(adat) != "object") {
  152. throw("只接收JSON对象数据");
  153. }
  154. for (var k in adat) {
  155. if (!data[k]) {
  156. data[k] = adat[k];
  157. } else if (adat[k]) {
  158. if (typeof(adat[k]) == "object") {
  159. if (JSON.stringify(adat[k]).length > JSON.stringify(data[k]).length) {
  160. data[k] = adat[k];
  161. }
  162. } else if (typeof(adat[k]) == "string") {
  163. if (typeof(data[k]) != "string") {
  164. data[k] = adat[k];
  165. } else if (adat[k].length > data[k].length) {
  166. data[k] = adat[k];
  167. }
  168. } else if (typeof(adat[k]) == "number") {
  169. if (typeof(data[k]) == "number") {
  170. if ((""+adat[k]).indexOf(".")>=0) {
  171. data[k] = adat[k];
  172. }
  173. }
  174. } else if (typeof(adat[k]) == "boolean") {
  175. if (typeof(data[k]) != "boolean") {
  176. data[k] = ""+adat[k];
  177. }
  178. }
  179. }
  180. }
  181. } catch(e) {}
  182. }
  183. }
  184. }
  185. jsfilename = input.jsfilename;
  186. if (!jsfilename) {
  187. jsfilename = defaultinput.jsfilename;
  188. }
  189. clsoption = input.clsoption;
  190. if (!clsoption) {
  191. clsoption = defaultinput.clsoption;
  192. }
  193. fieldmap = input.fieldmap;
  194. if (!fieldmap) {
  195. fieldmap = defaultinput.fieldmap;
  196. }
  197. mustfield = input.mustfield;
  198. if (!mustfield) {
  199. mustfield = defaultinput.mustfield;
  200. }
  201. fields = input.fields;
  202. if (!fields) {
  203. fields = defaultinput.fields;
  204. }
  205. reqinput = {
  206. istesting: istesting,
  207. classname: classname,
  208. jsfilename: jsfilename,
  209. clsoption: clsoption,
  210. fieldmap: fieldmap,
  211. fields: fields,
  212. mustfield: mustfield,
  213. data: data,
  214. };
  215. reqinput = encodeURIComponent(JSON.stringify(reqinput));
  216. output.info.p1 = {name:"生成类"};
  217. http.do("POST",
  218. server+"/script/exec/js?filepath=/matrix/utils/AlterClassByJson.js",
  219. {
  220. "Authorization": "Basic "+author,
  221. "Content-Type": "application/x-www-form-urlencoded",
  222. "Data-Type": "json"
  223. },
  224. 'input='+reqinput,
  225. function(response){
  226. // success func
  227. ret = response.data;
  228. if (ret.message) {
  229. output.info.p1.result = ret.message;
  230. } else {
  231. output.info.p1.result = ret;
  232. }
  233. },
  234. function(response){
  235. // error func
  236. output.error = response.data;
  237. });
  238. if (output.error || output.info.p1.result.error) {
  239. return;
  240. }
  241. output.info.p2 = {name:"生成脚本"};
  242. http.do("POST",
  243. server+"/script/exec/js?filepath=/matrix/utils/JsonImporterGen.js&input="+reqinput,
  244. {"Authorization": "Basic "+author},
  245. '',
  246. function(response){
  247. // success func
  248. ret = response.data;
  249. if (ret.message) {
  250. output.info.p2.result = ret.message;
  251. } else {
  252. output.info.p2.result = ret;
  253. }
  254. },
  255. function(response){
  256. // error func
  257. output.error = response.data;
  258. });
  259. if (output.error || output.info.p2.result.error) {
  260. return;
  261. }
  262. output.info.p2x = {name:"激活脚本"};
  263. calljsfp = encodeURIComponent(jsfilename.replace(/\/script/, ""));
  264. http.do("POST",
  265. server+"/fs/tolocal/script"+calljsfp+"?issys=true",
  266. {"Authorization": "Basic "+author},
  267. '',
  268. function(response){
  269. // success func
  270. ret = response.data;
  271. if (ret.message) {
  272. output.info.p2x.result = ret.message;
  273. } else {
  274. output.info.p2x.result = ret;
  275. }
  276. },
  277. function(response){
  278. // error func
  279. output.error = response.data;
  280. });
  281. if (output.error || output.info.p2x.result.error) {
  282. return;
  283. }
  284. output.info.p3 = {name:"插入数据"};
  285. reqinput = encodeURIComponent(base64.encode(JSON.stringify(data)));
  286. calljsfp = encodeURIComponent(jsfilename.replace(/\/script/, ""));
  287. http.do("POST",
  288. server+"/script/exec/js?filepath="+calljsfp+"&input="+reqinput,
  289. {"Authorization": "Basic "+author},
  290. '',
  291. function(response){
  292. // success func
  293. ret = response.data;
  294. if (ret.message) {
  295. output.info.p3.result = ret.message;
  296. } else {
  297. output.info.p3.result = ret;
  298. }
  299. },
  300. function(response){
  301. // error func
  302. output.error = response.data;
  303. });
  304. if (output.error || output.info.p3.result.error) {
  305. return;
  306. }
  307. output.info.p4 = {name:"完成"};
  308. }
  309. try {
  310. main();
  311. } catch(e) {
  312. if (typeof(e) == "object") {
  313. output.error = e;
  314. } else if (typeof(e) == "string") {
  315. output.error = "错误:" + e;
  316. } else {
  317. output.error = JSON.stringify(e);
  318. }
  319. }
  320. // 返回输出信息
  321. OUTPUT = output;