base.js 31 KB

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