base.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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. layout.anonymous = (locate.href.toString().indexOf("anonymous") !== -1);
  10. o2.xApplication = o2.xApplication || {};
  11. o2.xDesktop = o2.xDesktop || {};
  12. o2.xDesktop.requireApp = function (module, clazz, callback, async) {
  13. o2.requireApp(module, clazz, callback, async);
  14. };
  15. (function (layout) {
  16. layout.readys = [];
  17. layout.addReady = function () {
  18. for (var i = 0; i < arguments.length; i++) {
  19. if (o2.typeOf(arguments[i]) === "function") layout.readys.push(arguments[i]);
  20. }
  21. };
  22. var _requireApp = function (appNames, callback, clazzName) {
  23. var appPath = appNames.split(".");
  24. var baseObject = o2.xApplication;
  25. appPath.each(function (path, i) {
  26. if (i < (appPath.length - 1)) {
  27. baseObject[path] = baseObject[path] || {};
  28. } else {
  29. baseObject[path] = baseObject[path] || { "options": Object.clone(o2.xApplication.Common.options) };
  30. }
  31. baseObject = baseObject[path];
  32. }.bind(this));
  33. if (!baseObject.options) baseObject.options = Object.clone(o2.xApplication.Common.options);
  34. var _lpLoaded = false;
  35. o2.xDesktop.requireApp(appNames, "lp." + o2.language, {
  36. "onFailure": function () {
  37. o2.xDesktop.requireApp(appNames, "lp.zh-cn", null, false);
  38. }.bind(this)
  39. }, false);
  40. o2.xDesktop.requireApp(appNames, clazzName, function () {
  41. if (callback) callback(baseObject);
  42. });
  43. };
  44. var _createNewApplication = function (e, appNamespace, appName, options, statusObj, inBrowser, taskitem, notCurrent) {
  45. if (options) { options.event = e; } else { options = { "event": e }; }
  46. var app = new appNamespace["Main"](layout.desktop, options);
  47. app.desktop = layout.desktop;
  48. app.status = statusObj;
  49. app.inBrowser = !!(inBrowser || layout.inBrowser);
  50. if (layout.desktop.type === "layout") {
  51. app.appId = (options.appId) ? options.appId : ((appNamespace.options.multitask) ? appName + "-" + (new o2.widget.UUID()) : appName);
  52. app.options.appId = app.appId;
  53. if (layout.desktop.createTaskItem){
  54. if (!taskitem) taskitem = layout.desktop.createTaskItem(app);
  55. app.taskitem = taskitem;
  56. app.taskitem.app = app;
  57. }
  58. app.isLoadApplication = true;
  59. app.load(!notCurrent);
  60. if (!layout.desktop.apps) layout.desktop.apps = {};
  61. if (layout.desktop.apps[app.appId]) {
  62. var tmpApp = layout.desktop.apps[app.appId];
  63. } else {
  64. layout.desktop.apps[app.appId] = app;
  65. }
  66. //if (layout.desktop.appArr){
  67. layout.desktop.appArr.push(app);
  68. layout.desktop.appCurrentList.push(app);
  69. if (!notCurrent) layout.desktop.currentApp = app;
  70. //}
  71. //app.taskitem = new MWF.xDesktop.Layout.Taskitem(app, this);
  72. } else {
  73. app.load(true);
  74. layout.app = app;
  75. }
  76. var mask = document.getElementById("appContentMask");
  77. if (mask) mask.destroy();
  78. };
  79. var _openWorkAndroid = function (options) {
  80. if (window.o2android && window.o2android.openO2Work) {
  81. if (options.workId) {
  82. window.o2android.openO2Work(options.workId, "", options.title || options.docTitle || "");
  83. } else if (options.workCompletedId) {
  84. window.o2android.openO2Work("", options.workCompletedId, options.title || options.docTitle || "");
  85. }
  86. return true;
  87. }
  88. return false;
  89. };
  90. var _openWorkIOS = function (options) {
  91. if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2Work) {
  92. if (options.workId) {
  93. window.webkit.messageHandlers.openO2Work.postMessage({
  94. "work": options.workId,
  95. "workCompleted": "",
  96. "title": options.title || options.docTitle || ""
  97. });
  98. } else if (options.workCompletedId) {
  99. window.webkit.messageHandlers.openO2Work.postMessage({
  100. "work": "",
  101. "workCompleted": options.workCompletedId,
  102. "title": options.title || options.docTitle || ""
  103. });
  104. }
  105. return true;
  106. }
  107. return false;
  108. };
  109. var _openWorkHTML = function (options) {
  110. // var uri = new URI(window.location.href);
  111. // var redirectlink = uri.getData("redirectlink");
  112. // if (!redirectlink) {
  113. // redirectlink = encodeURIComponent(locate.pathname + locate.search);
  114. // } else {
  115. // redirectlink = encodeURIComponent(redirectlink);
  116. // }
  117. // if (options.workId) {
  118. // window.location = o2.filterUrl("../x_desktop/workmobilewithaction.html?workid=" + options.workId + ((layout.debugger) ? "&debugger" : "") + "&redirectlink=" + redirectlink);
  119. // } else if (options.workCompletedId) {
  120. // window.location = o2.filterUrl("../x_desktop/workmobilewithaction.html?workcompletedid=" + options.workCompletedId + ((layout.debugger) ? "&debugger" : "") + "&redirectlink=" + redirectlink);
  121. // }
  122. var uri = new URI(window.location.href);
  123. var redirectlink = uri.getData("redirectlink");
  124. if (!redirectlink) {
  125. redirectlink = encodeURIComponent(locate.pathname + locate.search);
  126. } else {
  127. redirectlink = encodeURIComponent(redirectlink);
  128. }
  129. var docurl = "../x_desktop/workmobilewithaction.html".toURI();
  130. if (options.draft){
  131. var par = "draft="+encodeURIComponent(JSON.stringify(options.draft));
  132. docurl = "../x_desktop/workmobilewithaction.html?" + par;
  133. }else{
  134. docurl = docurl.setData(options).toString();
  135. }
  136. var job = (options.jobid || options.jobId || options.job);
  137. if (job) docurl += ((docurl.indexOf("?")!=-1) ? "&" : "?") + "jobid="+job;
  138. docurl += ((redirectlink) ? "&redirectlink=" + redirectlink : "");
  139. docurl +=((layout.debugger) ? "&debugger" : "");
  140. window.location = o2.filterUrl(docurl);
  141. };
  142. var _openWork = function (options) {
  143. if (!_openWorkAndroid(options)) if (!_openWorkIOS(options)) _openWorkHTML(options);
  144. };
  145. var _openDocument = function (appNames, options, statusObj) {
  146. var title = typeOf(options) === "object" ? (options.docTitle || options.title) : "";
  147. title = title || "";
  148. var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
  149. if (window.o2android && window.o2android.openO2CmsDocument) {
  150. window.o2android.openO2CmsDocument(options.documentId, title);
  151. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2CmsDocument) {
  152. window.webkit.messageHandlers.openO2CmsDocument.postMessage({ "docId": options.documentId, "docTitle": title });
  153. } else {
  154. window.location = o2.filterUrl("../x_desktop/appMobile.html?" + par + ((layout.debugger) ? "&debugger" : ""));
  155. }
  156. };
  157. var _openCms = function (appNames, options, statusObj) {
  158. var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
  159. if (window.o2android && window.o2android.openO2CmsApplication) {
  160. window.o2android.openO2CmsApplication(options.columnId, options.title || "");
  161. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2CmsApplication) {
  162. window.webkit.messageHandlers.openO2CmsApplication.postMessage(options.columnId);
  163. } else {
  164. window.location = o2.filterUrl("../x_desktop/appMobile.html?" + par + ((layout.debugger) ? "&debugger" : ""));
  165. }
  166. };
  167. var _openMeeting = function (appNames, options, statusObj) {
  168. var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
  169. if (window.o2android && window.o2android.openO2Meeting) {
  170. window.o2android.openO2Meeting("");
  171. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2Meeting) {
  172. window.webkit.messageHandlers.openO2Meeting.postMessage("");
  173. } else {
  174. window.location = o2.filterUrl("../x_desktop/appMobile.html?" + par + ((layout.debugger) ? "&debugger" : ""));
  175. }
  176. };
  177. var _openCalendar = function (appNames, options, statusObj) {
  178. var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
  179. if (window.o2android && window.o2android.openO2Calendar) {
  180. window.o2android.openO2Calendar("");
  181. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2Calendar) {
  182. window.webkit.messageHandlers.openO2Calendar.postMessage("");
  183. } else {
  184. window.location = o2.filterUrl("../x_desktop/appMobile.html?" + par + ((layout.debugger) ? "&debugger" : ""));
  185. }
  186. };
  187. var _openTaskCenter = function (appNames, options, statusObj) {
  188. var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
  189. var tab = ((options && options.navi) ? options.navi : "task").toLowerCase();
  190. if (tab === "done") tab = "taskCompleted";
  191. if (tab === "readed") tab = "readCompleted";
  192. if (window.o2android && window.o2android.openO2WorkSpace) {
  193. window.o2android.openO2WorkSpace(tab);
  194. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.openO2WorkSpace) {
  195. window.webkit.messageHandlers.openO2WorkSpace.postMessage(tab);
  196. } else {
  197. window.location = o2.filterUrl("../x_desktop/appMobile.html?" + par + ((layout.debugger) ? "&debugger" : ""));
  198. }
  199. };
  200. var _openApplicationMobile = function (appNames, options, statusObj) {
  201. switch (appNames) {
  202. case "process.Work":
  203. _openWork(options);
  204. break;
  205. case "cms.Document":
  206. _openDocument(appNames, options, statusObj);
  207. break;
  208. case "cms.Module":
  209. _openCms(appNames, options, statusObj);
  210. break;
  211. case "Meeting":
  212. _openMeeting(appNames, options, statusObj);
  213. break;
  214. case "Calendar":
  215. _openCalendar(appNames, options, statusObj);
  216. break;
  217. case "process.TaskCenter":
  218. _openTaskCenter(appNames, options, statusObj);
  219. break;
  220. default:
  221. var uri = new URI(window.location.href);
  222. var optionsStr = uri.getData("option");
  223. var statusStr = uri.getData("status");
  224. window.location = o2.filterUrl("../x_desktop/appMobile.html?app=" + appNames + "&option=" + (optionsStr || "") + "&status=" + (statusStr || "") + ((layout.debugger) ? "&debugger" : ""));
  225. }
  226. };
  227. var _openApplicationPC = function (appNames, options, statusObj) {
  228. if (options) delete options.docTitle;
  229. var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
  230. switch (appNames) {
  231. case "process.Work":
  232. var url = "../x_desktop/work.html".toURI();
  233. if (options.draft){
  234. url = "../x_desktop/app.html?" + par;
  235. }else{
  236. url = url.setData(options).toString();
  237. }
  238. // if (options.workId) {
  239. // url += "?workid=" + options.workId+"&"+par;
  240. // //window.location = o2.filterUrl("../x_desktop/workmobilewithaction.html?workid=" + options.workId + ((layout.debugger) ? "&debugger" : "") + "&redirectlink=" + redirectlink);
  241. // } else if (options.workCompletedId) {
  242. // url += "?workcompletedid=" + options.workCompletedId+"&"+par;;
  243. // //window.location = o2.filterUrl("../x_desktop/workmobilewithaction.html?workcompletedid=" + options.workCompletedId + ((layout.debugger) ? "&debugger" : "") + "&redirectlink=" + redirectlink);
  244. // } else if (options.draftId){
  245. // url += "?draftId=" + options.draftId+"&"+par;;
  246. // } else if (options.draft) {
  247. // url = "../x_desktop/app.html?" + par;
  248. // }
  249. var job = (options.jobid || options.jobId || options.job);
  250. if (job) url += ((url.indexOf("?")!=-1) ? "&" : "?") + "jobid="+job;
  251. url +=((layout.debugger) ? "&debugger" : "");
  252. if (layout.app.$openWithSelf) {
  253. return window.location = o2.filterUrl(url);
  254. } else {
  255. return window.open(o2.filterUrl(url), par);
  256. }
  257. break;
  258. case "cms.Document":
  259. // _openDocument(appNames, options, statusObj);
  260. var url = "../x_desktop/cmsdoc.html".toURI();
  261. url = url.setData(options).toString();
  262. url +=((layout.debugger) ? "&debugger" : "");
  263. if (layout.app.$openWithSelf) {
  264. return window.location = o2.filterUrl(url);
  265. } else {
  266. return window.open(o2.filterUrl(url), par);
  267. }
  268. break;
  269. // case "cms.Module":
  270. // _openCms(appNames, options, statusObj);
  271. // break;
  272. // case "Meeting":
  273. // _openMeeting(appNames, options, statusObj);
  274. // break;
  275. // case "Calendar":
  276. // _openCalendar(appNames, options, statusObj);
  277. // break;
  278. // case "process.TaskCenter":
  279. // _openTaskCenter(appNames, options, statusObj);
  280. // break;
  281. default:
  282. //var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
  283. if (layout.app.$openWithSelf) {
  284. return window.location = o2.filterUrl("../x_desktop/app.html?" + par + ((layout.debugger) ? "&debugger" : ""));
  285. } else {
  286. return window.open(o2.filterUrl("../x_desktop/app.html?" + par + ((layout.debugger) ? "&debugger" : "")), par);
  287. }
  288. }
  289. };
  290. layout.openApplication = function (e, appNames, options, statusObj, inBrowser, taskitem, notCurrent) {
  291. if (appNames.substring(0, 4) === "@url") {
  292. var url = appNames.replace(/\@url\:/i, "");
  293. var a = new Element("a", { "href": url, "target": "_blank" });
  294. a.click();
  295. a.destroy();
  296. a = null;
  297. return true;
  298. }
  299. if (layout.app) {
  300. if (layout.mobile) {
  301. _openApplicationMobile(appNames, options, statusObj);
  302. } else {
  303. return _openApplicationPC(appNames, options, statusObj);
  304. // var par = "app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent((statusObj) ? JSON.encode(statusObj) : "") + "&option=" + encodeURIComponent((options) ? JSON.encode(options) : "");
  305. //
  306. // if (layout.app.$openWithSelf) {
  307. // return window.location = o2.filterUrl("../x_desktop/app.html?" + par + ((layout.debugger) ? "&debugger" : ""));
  308. // } else {
  309. // return window.open(o2.filterUrl("../x_desktop/app.html?" + par + ((layout.debugger) ? "&debugger" : "")), par);
  310. // }
  311. }
  312. } else {
  313. var appPath = appNames.split(".");
  314. var appName = appPath[appPath.length - 1];
  315. _requireApp(appNames, function (appNamespace) {
  316. var appId = (options && options.appId) ? options.appId : ((appNamespace.options.multitask) ? "" : appName);
  317. //if (appId && layout.desktop.apps && layout.desktop.apps[appId] && layout.desktop.apps[appId].window){
  318. if (appId && layout.desktop.apps && layout.desktop.apps[appId]) {
  319. layout.desktop.apps[appId].setCurrent();
  320. } else {
  321. if (options) options.appId = appId;
  322. _createNewApplication(e, appNamespace, appName, (options || { "appId": appId }), statusObj, inBrowser, taskitem, notCurrent);
  323. }
  324. }.bind(this));
  325. }
  326. };
  327. layout.refreshApp = function (app) {
  328. var status = app.recordStatus();
  329. var uri = new URI(window.location.href);
  330. var appNames = uri.getData("app");
  331. var optionsStr = uri.getData("option");
  332. var statusStr = uri.getData("status");
  333. if (status) statusStr = JSON.encode(status);
  334. var port = uri.get("port");
  335. window.location = o2.filterUrl(uri.get("scheme") + "://" + uri.get("host") + ((port) ? ":" + port + "/" : "") + uri.get("directory ") + "?app=" + encodeURIComponent(appNames) + "&status=" + encodeURIComponent(statusStr) + "&option=" + encodeURIComponent((optionsStr) ? JSON.encode(optionsStr) : "") + ((layout.debugger) ? "&debugger" : ""));
  336. };
  337. layout.load = function (appNames, options, statusObj) {
  338. // layout.message = new o2.xDesktop.MessageMobile();
  339. // layout.message.load();
  340. layout.apps = [];
  341. layout.node = $("layout");
  342. var appName = appNames, m_status = statusObj, option = options;
  343. var topWindow = window.opener;
  344. if (topWindow) {
  345. try {
  346. if (!appName) appName = topWindow.layout.desktop.openBrowserApp;
  347. if (!m_status) m_status = topWindow.layout.desktop.openBrowserStatus;
  348. if (!option) option = topWindow.layout.desktop.openBrowserOption;
  349. } catch (e) { }
  350. }
  351. layout.openApplication(null, appName, option || {}, m_status);
  352. };
  353. // layout.getAppUrlMapping = function(url){
  354. // return this.getUrlMapping(url, "app");
  355. // };
  356. // layout.getCenterUrlMapping = function(url){
  357. // return this.getUrlMapping(url,"center");
  358. // };
  359. // layout.getWebUrlMapping = function(url){
  360. // return this.getUrlMapping(url, "web");
  361. // };
  362. // layout.getUrlMapping = function(url, type){
  363. // var urlContextMapping = layout.config.urlMapping;
  364. // if (urlContextMapping && urlContextMapping.app){
  365. // var href = url || window.location.href.toString();
  366. //
  367. // for (var k in urlContextMapping[type]){
  368. // var regex = new RegExp(k);
  369. // if (regex.test(href)){
  370. // return urlContextMapping[type][k];
  371. // }
  372. // }
  373. // }
  374. // return "";
  375. // };
  376. })(layout);
  377. o2.addReady(function () {
  378. // layout.desktop = new (new Class({Implements: [Options, Events]}))();
  379. // layout.desktop.openApplication = layout.openApplication;
  380. // layout.desktop.refreshApp = layout.refreshApp;
  381. // layout.desktop.load = layout.load;
  382. //兼容方法
  383. if (window.Element){
  384. Element.implement({
  385. "makeLnk": function (options) { }
  386. });
  387. }
  388. layout.desktop.addEvent = function (type, e, d) {
  389. window.addEvent(type, e, d);
  390. };
  391. layout.desktop.addEvents = function (e) {
  392. window.addEvents(e);
  393. };
  394. var loadingNode = (window.$) ? $("loaddingArea") : null;
  395. var loadeds = 0;
  396. var loadCount = 4;
  397. var size = (window.document && document.body) ? document.body.getSize() : null;
  398. var _closeLoadingNode = function () {
  399. if (loadingNode) {
  400. loadingNode.destroy();
  401. loadingNode = null;
  402. }
  403. };
  404. var _loadProgressBar = function (complete) {
  405. if (loadingNode) {
  406. if (complete) {
  407. loadingNode.setStyles({ "width": "" + size.x + "px" });
  408. //loadingNode.set('morph', {duration: 100}).morph({"width": ""+size.x+"px"});
  409. window.setTimeout(_closeLoadingNode, 500);
  410. } else {
  411. loadeds++;
  412. var p = (loadeds / loadCount) * size.x;
  413. loadingNode.setStyles({ "width": "" + p + "px" });
  414. //loadingNode.set('morph', {duration: 100}).morph({"width": ""+p+"px"});
  415. if (loadeds >= loadCount) window.setTimeout(_closeLoadingNode, 500);
  416. }
  417. }
  418. };
  419. var _setLayoutService = function(service, center){
  420. layout.serviceAddressList = service;
  421. layout.centerServer = center;
  422. layout.desktop.serviceAddressList = service;
  423. layout.desktop.centerServer = center;
  424. };
  425. var _getDistribute = function (callback) {
  426. if (layout.config.app_protocol === "auto") {
  427. layout.config.app_protocol = window.location.protocol;
  428. }
  429. if (layout.config.configMapping && (layout.config.configMapping[window.location.host] || layout.config.configMapping[window.location.hostname])){
  430. var mapping = layout.config.configMapping[window.location.host] || layout.config.configMapping[window.location.hostname];
  431. if (mapping.servers){
  432. layout.serviceAddressList = mapping.servers;
  433. layout.desktop.serviceAddressList = mapping.servers;
  434. if (mapping.center) center = (o2.typeOf(mapping.center)==="array") ? mapping.center[0] : mapping.center;
  435. layout.centerServer = center;
  436. layout.desktop.centerServer = center;
  437. if (callback) callback();
  438. }else{
  439. if (mapping.center) layout.config.center = (o2.typeOf(mapping.center)==="array") ? mapping.center : [mapping.center];
  440. o2.xDesktop.getServiceAddress(layout.config, function (service, center) {
  441. _setLayoutService(service, center);
  442. _loadProgressBar();
  443. if (callback) callback();
  444. }.bind(this));
  445. }
  446. }else{
  447. o2.xDesktop.getServiceAddress(layout.config, function (service, center) {
  448. _setLayoutService(service, center);
  449. _loadProgressBar();
  450. if (callback) callback();
  451. }.bind(this));
  452. }
  453. };
  454. var _load = function () {
  455. var _loadApp = function (json) {
  456. //用户已经登录
  457. if (json){
  458. layout.user = json.data;
  459. layout.session = layout.session || {};
  460. layout.session.user = json.data;
  461. layout.session.token = json.data.token;
  462. layout.desktop.session = layout.session;
  463. }
  464. _loadProgressBar(true);
  465. while (layout.readys && layout.readys.length) {
  466. console.log("load app ...")
  467. layout.readys.shift().apply(window);
  468. }
  469. };
  470. //修改支持x-token
  471. var uri = new URI(window.location.href);
  472. var options = uri.get("data");
  473. if (options["x-token"]) {
  474. Cookie.write("x-token", options["x-token"]);
  475. }
  476. layout.sessionPromise = new Promise(function(resolve, reject){
  477. o2.Actions.get("x_organization_assemble_authentication").getAuthentication(function (json) {
  478. if (resolve) resolve(json.data);
  479. }.bind(this), function (xhr, text, error) {
  480. if (reject) reject({"xhr": xhr, "text": text, "error": error});
  481. }.bind(this));
  482. });
  483. // layout.sessionPromise = {
  484. // "resolveList": [],
  485. // "rejectList": [],
  486. // "init": function(resolve, reject){
  487. // if (resolve) this.resolveList.push(resolve);
  488. // if (reject) this.rejectList.push(reject);
  489. // this.status = "pending";
  490. // this.resolveReturn = this;
  491. //
  492. // //先判断用户是否登录
  493. // console.log("layout.sessionPromise.init")
  494. // o2.Actions.get("x_organization_assemble_authentication").getAuthentication(function (json) {
  495. // this.status = "fulfilled";
  496. // this.resolveReturn = json.data;
  497. // this.runResolve(this.resolveReturn);
  498. // }.bind(this), function (xhr, text, error) {
  499. // this.status = "rejected";
  500. // this.resolveReturn = {"xhr": xhr, "text": text, "error": error};
  501. // this.runReject(this.resolveReturn);
  502. // }.bind(this));
  503. // },
  504. // "runResolve": function(json){
  505. // while (this.resolveList.length){
  506. // var r = this.resolveList.shift()(this.resolveReturn);
  507. // if (r) this.resolveReturn = r;
  508. // }
  509. // },
  510. // "runReject": function(json){
  511. // while (this.rejectList.length){
  512. // var r = this.rejectList.shift()(json);
  513. // if (r) this.resolveReturn = r;
  514. // }
  515. // },
  516. // "then": function(resolve, reject){
  517. // if (resolve) this.resolveList.push(resolve);
  518. // if (reject) this.rejectList.push(reject);
  519. // switch (this.status){
  520. // case "fulfilled":
  521. // this.runResolve();
  522. // break;
  523. // case "rejected":
  524. // this.runReject();
  525. // break;
  526. // default:
  527. // //nothing
  528. // }
  529. // return this;
  530. // }
  531. // }
  532. layout.sessionPromise.then(function(data){
  533. //已经登录
  534. layout.user = data;
  535. layout.session = layout.session || {};
  536. layout.session.user = data;
  537. layout.session.token = data.token;
  538. layout.desktop.session = layout.session;
  539. //_loadApp();
  540. }, function(){
  541. //允许匿名访问
  542. if (layout.anonymous) {
  543. var data = { name: "anonymous", roleList: [] };
  544. layout.user = data;
  545. layout.session = layout.session || {};
  546. layout.session.user = data;
  547. layout.session.token = data.token;
  548. layout.desktop.session = layout.session;
  549. //_loadApp();
  550. } else {
  551. _loadProgressBar(true);
  552. if (layout.yqwx) {
  553. layout.openLoginQywx();
  554. } else {
  555. layout.openLogin();
  556. }
  557. }
  558. });
  559. _loadApp();
  560. // //先判断用户是否登录
  561. // o2.Actions.get("x_organization_assemble_authentication").getAuthentication(function (json) {
  562. // //已经登录
  563. // //_loadProgressBar();
  564. //
  565. // layout.user = json.data;
  566. // layout.session = layout.session || {};
  567. // layout.session.user = json.data;
  568. // layout.session.token = json.data.token;
  569. // layout.desktop.session = layout.session;
  570. //
  571. // //_loadApp(json);
  572. // }.bind(this), function (json) {
  573. // //_loadProgressBar();
  574. // //允许匿名访问
  575. // if (layout.anonymous) {
  576. // //_loadProgressBar(true);
  577. // // _loadApp({
  578. // // data : {
  579. // // user: "anonymous",
  580. // // session: {
  581. // // user: {
  582. // // name: "anonymous",
  583. // // roleList: []
  584. // // }
  585. // // }
  586. // // }
  587. // // });
  588. // } else {
  589. // _loadProgressBar(true);
  590. // if (layout.yqwx) {
  591. // layout.openLoginQywx();
  592. // } else {
  593. // layout.openLogin();
  594. // }
  595. // }
  596. // });
  597. layout.openLogin = function () {
  598. layout.desktop.type = "app";
  599. layout.app = null;
  600. var content = $("appContent") || $("layout");
  601. if (content) content.empty();
  602. layout.authentication = new o2.xDesktop.Authentication({
  603. "style": "flat",
  604. "onLogin": _load.bind(layout)
  605. });
  606. layout.authentication.loadLogin(document.body);
  607. var loadingNode = $("browser_loading");
  608. if (loadingNode) loadingNode.fade("out");
  609. };
  610. layout.openLoginQywx = function () {
  611. console.log("开始login。。。。。。。。。。。。。");
  612. var uri = locate.href.toURI();
  613. console.log("执行单点。。。。。。。。。。");
  614. var action = new MWF.xDesktop.Actions.RestActions("", "x_organization_assemble_authentication", "");
  615. action.getActions = function (actionCallback) {
  616. this.actions = { "sso": { "uri": "/jaxrs/qiyeweixin/code/{code}", "method": "GET" } };
  617. if (actionCallback) actionCallback();
  618. };
  619. action.invoke({
  620. "name": "sso", "async": true, "parameter": { "code": uri.getData("code") }, "success": function (json) {
  621. console.log("单点成功。");
  622. console.log(json);
  623. //基础数据。。。。
  624. layout.session.user = json.data;
  625. //
  626. _load();
  627. }.bind(this), "failure": function (xhr, text, error) {
  628. var n = document.getElementById("loaddingArea");
  629. if (n) { n.destroy(); }
  630. document.id("layout").set("html", "<div>企业微信单点异常!</div>")
  631. }.bind(this)
  632. });
  633. };
  634. };
  635. //异步载入必要模块
  636. layout.config = null;
  637. var configLoaded = false;
  638. var lpLoaded = false;
  639. var commonLoaded = false;
  640. var lp = o2.session.path + "/lp/" + o2.language + ".js";
  641. if (o2.session.isDebugger && (o2.session.isMobile || layout.mobile)) o2.load("../o2_lib/eruda/eruda.js");
  642. debugger;
  643. var loadModuls = function () {
  644. _loadProgressBar();
  645. lpLoaded = true;
  646. var modules = ["o2.xDesktop.$all"];
  647. o2.require(modules, {
  648. "onSuccess": function () {
  649. commonLoaded = true;
  650. if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () { _load(); });
  651. },
  652. "onEvery": function () {
  653. _loadProgressBar();
  654. }
  655. });
  656. }
  657. if (!o2.LP){
  658. o2.load(lp, loadModuls);
  659. }else{
  660. loadModuls();
  661. }
  662. o2.getJSON("../x_desktop/res/config/config.json", function (config) {
  663. _loadProgressBar();
  664. layout.config = config;
  665. configLoaded = true;
  666. if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () { _load(); });
  667. });
  668. });