qywxStartProcess.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. layout = window.layout || {};
  2. layout.desktop = layout;
  3. layout.desktop.type = "app";
  4. var locate = window.location;
  5. layout.protocol = locate.protocol;
  6. layout.inBrowser = true;
  7. layout.session = layout.session || {};
  8. layout.debugger = (locate.href.toString().indexOf("debugger") !== -1);
  9. o2.xApplication = o2.xApplication || {};
  10. o2.xDesktop = o2.xDesktop || {};
  11. o2.xDesktop.requireApp = function (module, clazz, callback, async) {
  12. o2.requireApp(module, clazz, callback, async);
  13. };
  14. (function (layout) {
  15. layout.readys = [];
  16. layout.addReady = function () {
  17. for (var i = 0; i < arguments.length; i++) {
  18. if (o2.typeOf(arguments[i]) === "function") layout.readys.push(arguments[i]);
  19. }
  20. };
  21. var _requireApp = function (appNames, callback, clazzName) {
  22. var appPath = appNames.split(".");
  23. var baseObject = o2.xApplication;
  24. appPath.each(function (path, i) {
  25. if (i < (appPath.length - 1)) {
  26. baseObject[path] = baseObject[path] || {};
  27. } else {
  28. baseObject[path] = baseObject[path] || { "options": Object.clone(o2.xApplication.Common.options) };
  29. }
  30. baseObject = baseObject[path];
  31. }.bind(this));
  32. if (!baseObject.options) baseObject.options = Object.clone(o2.xApplication.Common.options);
  33. var _lpLoaded = false;
  34. o2.xDesktop.requireApp(appNames, "lp." + o2.language, {
  35. "failure": function () {
  36. o2.xDesktop.requireApp(appNames, "lp.zh-cn", null, false);
  37. }.bind(this)
  38. }, false);
  39. o2.xDesktop.requireApp(appNames, clazzName, function () {
  40. if (callback) callback(baseObject);
  41. });
  42. };
  43. var _createNewApplication = function (e, appNamespace, appName, options, statusObj, inBrowser, taskitem, notCurrent) {
  44. if (options) { options.event = e; } else { options = { "event": e }; }
  45. var app = new appNamespace["Main"](layout.desktop, options);
  46. app.desktop = layout.desktop;
  47. app.status = statusObj;
  48. app.inBrowser = !!(inBrowser || layout.inBrowser);
  49. if (layout.desktop.type === "layout") {
  50. app.appId = (options.appId) ? options.appId : ((appNamespace.options.multitask) ? appName + "-" + (new o2.widget.UUID()) : appName);
  51. app.options.appId = app.appId;
  52. if (!taskitem) taskitem = layout.desktop.createTaskItem(app);
  53. app.taskitem = taskitem;
  54. app.taskitem.app = app;
  55. app.isLoadApplication = true;
  56. app.load(!notCurrent);
  57. if (!layout.desktop.apps) layout.desktop.apps = {};
  58. if (layout.desktop.apps[app.appId]) {
  59. var tmpApp = layout.desktop.apps[app.appId];
  60. } else {
  61. layout.desktop.apps[app.appId] = app;
  62. }
  63. layout.desktop.appArr.push(app);
  64. layout.desktop.appCurrentList.push(app);
  65. if (!notCurrent) layout.desktop.currentApp = app;
  66. //app.taskitem = new MWF.xDesktop.Layout.Taskitem(app, this);
  67. } else {
  68. app.load(true);
  69. layout.app = app;
  70. }
  71. var mask = document.getElementById("appContentMask");
  72. if (mask) mask.destroy();
  73. };
  74. layout.openApplication = function (e, appNames, options, statusObj, inBrowser, taskitem, notCurrent) {
  75. var appPath = appNames.split(".");
  76. var appName = appPath[appPath.length - 1];
  77. _requireApp(appNames, function (appNamespace) {
  78. var appId = (options && options.appId) ? options.appId : ((appNamespace.options.multitask) ? "" : appName);
  79. //if (appId && layout.desktop.apps && layout.desktop.apps[appId] && layout.desktop.apps[appId].window){
  80. if (appId && layout.desktop.apps && layout.desktop.apps[appId]) {
  81. layout.desktop.apps[appId].setCurrent();
  82. } else {
  83. if (options) options.appId = appId;
  84. if (appNamespace.loading && appNamespace.loading.then){
  85. appNamespace.loading.then(function(){
  86. _createNewApplication(e, appNamespace, appName, (options || { "appId": appId }), statusObj, inBrowser, taskitem, notCurrent);
  87. });
  88. }else{
  89. _createNewApplication(e, appNamespace, appName, (options || { "appId": appId }), statusObj, inBrowser, taskitem, notCurrent);
  90. }
  91. }
  92. }.bind(this));
  93. };
  94. layout.startProcess = function (appId, pId, redirect) {
  95. MWF.Actions.get("x_processplatform_assemble_surface").getProcessByName(pId, appId, function (json) {
  96. if (json.data) {
  97. MWF.xDesktop.requireApp("process.TaskCenter", "ProcessStarter", function () {
  98. var starter = new MWF.xApplication.process.TaskCenter.ProcessStarter(json.data, layout.app, {
  99. "workData": {},
  100. "identity": null,
  101. "latest": false,
  102. "onStarted": function (data, title, processName) {
  103. if (data.work){
  104. layout.startProcessDraft(data, title, processName, redirect);
  105. }else{
  106. layout.startProcessInstance(data, title, processName, redirect);
  107. }
  108. }.bind(this)
  109. });
  110. var mask = document.getElementById("loaddingArea");
  111. if (mask) mask.destroy();
  112. starter.load();
  113. }.bind(this));
  114. }
  115. }.bind(this));
  116. };
  117. layout.startProcessDraft = function(data, title, processName, redirect){
  118. o2.require("o2.widget.UUID", function () {
  119. var work = data.work;
  120. var options = {"draft": work, "appId": "process.Work"+(new o2.widget.UUID).toString(), "desktopReload": false};
  121. //先修改当前url为配置的门户地址
  122. if (redirect) {
  123. history.replaceState(null, "startProcess", redirect);
  124. } else {
  125. history.replaceState(null, "startProcess", "../x_desktop/appMobile.html?app=process.TaskCenter");
  126. }
  127. // layout.openApplication(null, "process.Work", options);
  128. layout.openWorkIn(options);
  129. });
  130. };
  131. layout.startProcessInstance = function(data, title, processName, redirect){
  132. var currentTask = [];
  133. data.each(function (work) {
  134. if (work.currentTaskIndex != -1) currentTask.push(work.taskList[work.currentTaskIndex].work);
  135. }.bind(this));
  136. if (currentTask.length == 1) {
  137. var options = { "workId": currentTask[0], "appId": currentTask[0] };
  138. //先修改当前url为配置的门户地址
  139. if (redirect) {
  140. history.replaceState(null, "startProcess", redirect);
  141. } else {
  142. history.replaceState(null, "startProcess", "../x_desktop/appMobile.html?app=process.TaskCenter");
  143. }
  144. // layout.openApplication(null, "process.Work", options);
  145. layout.openWorkIn(options);
  146. } else { }
  147. };
  148. layout.openWorkIn = function(options){
  149. o2.requireApp("Common", "", function() {
  150. var uri = new URI(window.location.href);
  151. var redirectlink = uri.getData("redirectlink");
  152. if (!redirectlink) {
  153. redirectlink = encodeURIComponent(locate.pathname + locate.search);
  154. } else {
  155. redirectlink = encodeURIComponent(redirectlink);
  156. }
  157. var appName="process.Work", m_status=null;
  158. options.redirectlink = redirectlink;
  159. layout.app = null;//创建工作界面
  160. layout.openApplication(null, appName, options, m_status);
  161. }, true);
  162. };
  163. layout.notice = function (content, type, target, where, offset) {
  164. if (!where) where = { "x": "right", "y": "top" };
  165. if (!target) target = this.content;
  166. if (!type) type = "ok";
  167. var noticeTarget = target || $(document.body);
  168. var off = offset;
  169. if (!off) {
  170. off = {
  171. x: 10,
  172. y: where.y.toString().toLowerCase() == "bottom" ? 10 : 10
  173. };
  174. }
  175. new mBox.Notice({
  176. type: type,
  177. position: where,
  178. move: false,
  179. target: noticeTarget,
  180. delayClose: (type == "error") ? 10000 : 5000,
  181. offset: off,
  182. content: content
  183. });
  184. };
  185. })(layout);
  186. o2.addReady(function () {
  187. //兼容方法
  188. Element.implement({
  189. "makeLnk": function (options) { }
  190. });
  191. layout.desktop.addEvent = function (type, e, d) {
  192. window.addEvent(type, e, d);
  193. };
  194. layout.desktop.addEvents = function (e) {
  195. window.addEvents(e);
  196. };
  197. var loadingNode = $("loaddingArea");
  198. var loadeds = 0;
  199. var loadCount = 16;
  200. var size = document.body.getSize();
  201. var _closeLoadingNode = function () {
  202. if (loadingNode) {
  203. loadingNode.destroy();
  204. loadingNode = null;
  205. }
  206. };
  207. var _loadProgressBar = function (complete) {
  208. if (loadingNode) {
  209. if (complete) {
  210. loadingNode.setStyles({ "width": "" + size.x + "px" });
  211. window.setTimeout(_closeLoadingNode, 500);
  212. } else {
  213. loadeds++;
  214. var p = (loadeds / loadCount) * size.x;
  215. loadingNode.setStyles({ "width": "" + p + "px" });
  216. //loadingNode.set('morph', {duration: 100}).morph({"width": ""+p+"px"});
  217. if (loadeds >= loadCount) window.setTimeout(_closeLoadingNode, 500);
  218. }
  219. }
  220. };
  221. //异步载入必要模块
  222. layout.config = null;
  223. var configLoaded = false;
  224. var lpLoaded = false;
  225. var commonLoaded = false;
  226. var lp = o2.session.path + "/lp/" + o2.language + ".js";
  227. o2.load(lp, function () {
  228. _loadProgressBar();
  229. lpLoaded = true;
  230. if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () { _load(); });
  231. });
  232. var modules = ["o2.xDesktop.Common", "o2.xDesktop.Actions.RestActions", "o2.xAction.RestActions"];
  233. o2.require(modules, {
  234. "onSuccess": function () {
  235. commonLoaded = true;
  236. if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () { _load(); });
  237. },
  238. "onEvery": function () {
  239. _loadProgressBar();
  240. }
  241. });
  242. o2.getJSON("../x_desktop/res/config/config.json", function (config) {
  243. _loadProgressBar();
  244. if (config.proxyCenterEnable){
  245. if (o2.typeOf(config.center)==="array"){
  246. config.center.forEach(function(c){
  247. c.port = window.location.port || 80;
  248. });
  249. }else{
  250. config.port = window.location.port || 80;
  251. }
  252. }
  253. layout.config = config;
  254. o2.tokenName = config.tokenName || "x-token";
  255. configLoaded = true
  256. if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () { _load(); });
  257. });
  258. var _getDistribute = function (callback) {
  259. if (layout.config.app_protocol === "auto") {
  260. layout.config.app_protocol = window.location.protocol;
  261. }
  262. o2.xDesktop.getServiceAddress(layout.config, function (service, center) {
  263. layout.serviceAddressList = service;
  264. layout.centerServer = center;
  265. _loadProgressBar();
  266. if (callback) callback();
  267. }.bind(this));
  268. };
  269. var _load = function () {
  270. var _loadApp = function (json) {
  271. //用户已经登录
  272. layout.user = json.data;
  273. //layout.session = {};
  274. layout.session.user = json.data;
  275. layout.session.token = json.data.token;
  276. layout.desktop.session = layout.session;
  277. (function (layout) {
  278. var _loadResource = function (callback) {
  279. var isLoadedA = false;
  280. var isLoadedB = false;
  281. //var isLoadedC = false;
  282. var modules = [
  283. "o2.xDesktop.Dialog",
  284. "o2.xDesktop.UserData",
  285. "o2.xDesktop.Access",
  286. "o2.widget.UUID",
  287. "o2.xDesktop.Menu",
  288. "o2.xDesktop.Authentication",
  289. // "o2.xDesktop.shortcut",
  290. "o2.widget.PinYin",
  291. "o2.xDesktop.Access"
  292. // "o2.xDesktop.MessageMobile"
  293. ];
  294. //o2.xDesktop.requireApp("Common", "", null, false);
  295. var _check = function () { if (isLoadedA && isLoadedB) if (callback) callback(); };
  296. o2.load(["../o2_lib/mootools/plugin/mBox.min.js"], function () { _loadProgressBar(); isLoadedA = true; _check(); });
  297. o2.require("o2.widget.Common", function () {
  298. _loadProgressBar();
  299. o2.require(modules, {
  300. "onSuccess": function () {
  301. o2.requireApp("Common", "", function () { _loadProgressBar(); isLoadedB = true; _check(); })
  302. },
  303. "onEvery": function () {
  304. _loadProgressBar();
  305. }
  306. });
  307. });
  308. };
  309. var _loadContent = function () {
  310. _loadResource(function () {
  311. _loadProgressBar(true);
  312. // while (layout.readys && layout.readys.length) {
  313. // layout.readys.shift().apply(window);
  314. // }
  315. var uri = locate.href.toURI();
  316. var redirect = uri.getData("redirect");
  317. var processId = uri.getData("processId");
  318. var applicationId = uri.getData("appId");
  319. layout.content = $(document.body);
  320. layout.app = layout;
  321. layout.startProcess(applicationId, processId, redirect);
  322. });
  323. };
  324. _loadContent();
  325. })(layout);
  326. };
  327. // 是否ip
  328. var _isIp = function(ip) {
  329. var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
  330. return reg.test(ip);
  331. };
  332. //修改支持x-token
  333. var uri = new URI(window.location.href);
  334. var options = uri.get("data");
  335. // if (options[o2.tokenName]) {
  336. // // 删除
  337. // Cookie.dispose(o2.tokenName);
  338. // // 写入
  339. // var host = window.location.hostname; // 域名
  340. // var domain = null;
  341. // if (_isIp(host)) {
  342. // domain = host;
  343. // }else {
  344. // if (host.indexOf(".") > 0) {
  345. // domain = host.substring(host.indexOf(".")); // 上级域名 如 .o2oa.net
  346. // }
  347. // }
  348. // if (domain) {
  349. // Cookie.write(o2.tokenName, options[o2.tokenName], {domain: domain, path:"/"});
  350. // }else {
  351. // Cookie.write(o2.tokenName, options[o2.tokenName]);
  352. // }
  353. // }
  354. //先判断用户是否登录
  355. o2.Actions.get("x_organization_assemble_authentication").getAuthentication(function (json) {
  356. //已经登录
  357. _loadProgressBar();
  358. _loadApp(json);
  359. }.bind(this), function (json) {
  360. _loadProgressBar();
  361. var _loadResource = function (callback) {
  362. var isLoadedA = false;
  363. var isLoadedB = false;
  364. //var isLoadedC = false;
  365. //var lp = o2.session.path+"/lp/"+o2.language+".js";
  366. var modules = [
  367. "o2.xDesktop.Dialog",
  368. "o2.xDesktop.UserData",
  369. "o2.xDesktop.Access",
  370. "o2.widget.UUID",
  371. "o2.xDesktop.Menu",
  372. //"o2.xDesktop.shortcut",
  373. "o2.widget.PinYin",
  374. "o2.xDesktop.Access",
  375. //"o2.xDesktop.MessageMobile"
  376. ];
  377. //o2.xDesktop.requireApp("Common", "", null, false);
  378. var _check = function () { if (isLoadedA && isLoadedB) if (callback) callback(); };
  379. o2.load(["../o2_lib/mootools/plugin/mBox.min.js"], function () { _loadProgressBar(); isLoadedA = true; _check(); });
  380. o2.require("o2.widget.Common", function () {
  381. _loadProgressBar();
  382. o2.require(modules, {
  383. "onSuccess": function () {
  384. o2.requireApp("Common", "", function () { isLoadedB = true; _check(); })
  385. },
  386. "onEvery": function () {
  387. _loadProgressBar();
  388. }
  389. });
  390. });
  391. };
  392. _loadResource(function () {
  393. _loadProgressBar(true);
  394. layout.openLoginQywx();
  395. });
  396. });
  397. layout.openLoginQywx = function () {
  398. var uri = locate.href.toURI();
  399. MWF.require("MWF.xDesktop.Actions.RestActions", function () {
  400. var action = new MWF.xDesktop.Actions.RestActions("", "x_organization_assemble_authentication", "");
  401. action.getActions = function (actionCallback) {
  402. this.actions = { "sso": { "uri": "/jaxrs/qiyeweixin/code/{code}", "method": "GET" } };
  403. if (actionCallback) actionCallback();
  404. };
  405. action.invoke({
  406. "name": "sso", "async": true, "parameter": { "code": uri.getData("code") }, "success": function (json) {
  407. //基础数据。。。。
  408. layout.session.user = json.data;
  409. //
  410. _load();
  411. }.bind(this), "failure": function (xhr, text, error) {
  412. var n = document.getElementById("loaddingArea");
  413. if (n) { n.destroy(); }
  414. document.id("layout").set("html", "<div>企业微信单点异常!</div>")
  415. }.bind(this)
  416. });
  417. });
  418. };
  419. };
  420. });