base_loader.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. if (!layout.isReady) {
  2. o2.addReady(function () {
  3. //兼容方法
  4. if (window.Element) {
  5. Element.implement({
  6. "makeLnk": function (options) {
  7. }
  8. });
  9. }
  10. layout.desktop.addEvent = function (type, e, d) {
  11. window.addEvent(type, e, d);
  12. };
  13. layout.desktop.addEvents = function (e) {
  14. window.addEvents(e);
  15. };
  16. var loadingNode = (window.$) ? $("loaddingArea") : null;
  17. var loadeds = 0;
  18. var loadCount = 4;
  19. var size = (window.document && document.body) ? document.body.getSize() : null;
  20. var _closeLoadingNode = function () {
  21. if (loadingNode) {
  22. loadingNode.destroy();
  23. loadingNode = null;
  24. }
  25. };
  26. var _loadProgressBar = function (complete) {
  27. if (loadingNode) {
  28. if (complete) {
  29. loadingNode.setStyles({"width": "" + size.x + "px"});
  30. //loadingNode.set('morph', {duration: 100}).morph({"width": ""+size.x+"px"});
  31. window.setTimeout(_closeLoadingNode, 500);
  32. } else {
  33. loadeds++;
  34. var p = (loadeds / loadCount) * size.x;
  35. loadingNode.setStyles({"width": "" + p + "px"});
  36. //loadingNode.set('morph', {duration: 100}).morph({"width": ""+p+"px"});
  37. if (loadeds >= loadCount) window.setTimeout(_closeLoadingNode, 500);
  38. }
  39. }
  40. };
  41. var _setLayoutService = function (service, center) {
  42. layout.serviceAddressList = service;
  43. layout.centerServer = center;
  44. layout.desktop.serviceAddressList = service;
  45. layout.desktop.centerServer = center;
  46. };
  47. var _getDistribute = function (callback) {
  48. if (layout.config.app_protocol === "auto") {
  49. layout.config.app_protocol = window.location.protocol;
  50. }
  51. if (layout.config.configMapping && (layout.config.configMapping[window.location.host] || layout.config.configMapping[window.location.hostname])) {
  52. var mapping = layout.config.configMapping[window.location.host] || layout.config.configMapping[window.location.hostname];
  53. if (mapping.servers) {
  54. layout.serviceAddressList = mapping.servers;
  55. layout.desktop.serviceAddressList = mapping.servers;
  56. if (mapping.center) center = (o2.typeOf(mapping.center) === "array") ? mapping.center[0] : mapping.center;
  57. layout.centerServer = center;
  58. layout.desktop.centerServer = center;
  59. if (callback) callback();
  60. } else {
  61. if (mapping.center) layout.config.center = (o2.typeOf(mapping.center) === "array") ? mapping.center : [mapping.center];
  62. o2.xDesktop.getServiceAddress(layout.config, function (service, center) {
  63. _setLayoutService(service, center);
  64. _loadProgressBar();
  65. if (callback) callback();
  66. }.bind(this));
  67. }
  68. } else {
  69. o2.xDesktop.getServiceAddress(layout.config, function (service, center) {
  70. _setLayoutService(service, center);
  71. _loadProgressBar();
  72. if (callback) callback();
  73. }.bind(this));
  74. }
  75. };
  76. var _load = function () {
  77. var _loadApp = function (json) {
  78. //用户已经登录
  79. if (json) {
  80. layout.user = json.data;
  81. layout.session = layout.session || {};
  82. layout.session.user = json.data;
  83. layout.session.token = json.data.token;
  84. layout.desktop.session = layout.session;
  85. }
  86. _loadProgressBar(true);
  87. layout.isReady = true;
  88. while (layout.readys && layout.readys.length) {
  89. layout.readys.shift().apply(window);
  90. }
  91. };
  92. // 是否ip
  93. var _isIp = function (ip) {
  94. 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])$/
  95. return reg.test(ip);
  96. };
  97. //修改支持x-token
  98. var uri = new URI(window.location.href);
  99. var options = uri.get("data");
  100. if (options[o2.tokenName]) {
  101. // 删除
  102. // Cookie.dispose(o2.tokenName);
  103. // // 写入
  104. // var host = window.location.hostname; // 域名
  105. // var domain = null;
  106. // if (_isIp(host)) {
  107. // domain = host;
  108. // } else {
  109. // if (host.indexOf(".") > 0) {
  110. // domain = host.substring(host.indexOf(".")); // 上级域名 如 .o2oa.net
  111. // }
  112. // }
  113. // if (domain) {
  114. // Cookie.write(o2.tokenName, options[o2.tokenName], {domain: domain, path: "/"});
  115. // } else {
  116. // Cookie.write(o2.tokenName, options[o2.tokenName]);
  117. // }
  118. if (window.layout) {
  119. if (!layout.session) layout.session = {};
  120. layout.session.token = options[o2.tokenName];
  121. }
  122. if (layout.config && layout.config.sessionStorageEnable && window.sessionStorage) window.sessionStorage.setItem("o2LayoutSessionToken", options[o2.tokenName]);
  123. }
  124. layout.sessionPromise = new Promise(function (resolve, reject) {
  125. o2.Actions.get("x_organization_assemble_authentication").getAuthentication(function (json) {
  126. if (json.data.language && (json.data.language !== o2.languageName)) {
  127. o2.language = json.data.language.toLowerCase();
  128. o2.languageName = json.data.language;
  129. var lp = "../x_desktop/js/base_lp_" + o2.language + ((o2.session.isDebugger) ? "" : ".min") + ".js?v="+o2.version.v;;
  130. o2.load(lp, {"reload": true}, function () {
  131. if (resolve) resolve(json.data);
  132. });
  133. } else {
  134. if (resolve) resolve(json.data);
  135. }
  136. }.bind(this), function (xhr, text, error) {
  137. if (reject) reject({"xhr": xhr, "text": text, "error": error});
  138. }.bind(this));
  139. });
  140. layout.sessionPromise.then(function (data) {
  141. //已经登录
  142. layout.user = data;
  143. layout.session = layout.session || {};
  144. layout.session.user = data;
  145. layout.session.token = data.token;
  146. layout.desktop.session = layout.session;
  147. //_loadApp();
  148. }, function () {
  149. //允许匿名访问
  150. if (layout.anonymous) {
  151. var data = {name: "anonymous", roleList: []};
  152. layout.user = data;
  153. layout.session = layout.session || {};
  154. layout.session.user = data;
  155. layout.session.token = data.token;
  156. layout.desktop.session = layout.session;
  157. //_loadApp();
  158. } else {
  159. _loadProgressBar(true);
  160. if (layout.yqwx) {
  161. layout.openLoginQywx();
  162. } else {
  163. layout.openLogin();
  164. }
  165. }
  166. });
  167. _loadApp();
  168. layout.openLogin = function () {
  169. layout.desktop.type = "app";
  170. layout.app = null;
  171. var content = $("appContent") || $("layout");
  172. if (content) content.empty();
  173. layout.authentication = new o2.xDesktop.Authentication({
  174. "style": "flat",
  175. "onLogin": _load.bind(layout)
  176. });
  177. layout.authentication.loadLogin(document.body);
  178. var loadingNode = $("browser_loading");
  179. if (loadingNode) loadingNode.fade("out");
  180. };
  181. layout.openLoginQywx = function () {
  182. //console.log("开始login。。。。。。。。。。。。。");
  183. var uri = locate.href.toURI();
  184. //console.log("执行单点。。。。。。。。。。");
  185. var action = new MWF.xDesktop.Actions.RestActions("", "x_organization_assemble_authentication", "");
  186. action.getActions = function (actionCallback) {
  187. this.actions = {"sso": {"uri": "/jaxrs/qiyeweixin/code/{code}", "method": "GET"}};
  188. if (actionCallback) actionCallback();
  189. };
  190. action.invoke({
  191. "name": "sso",
  192. "async": true,
  193. "parameter": {"code": uri.getData("code")},
  194. "success": function (json) {
  195. //console.log("单点成功。");
  196. //console.log(json);
  197. //基础数据。。。。
  198. layout.session.user = json.data;
  199. //
  200. _load();
  201. }.bind(this),
  202. "failure": function (xhr, text, error) {
  203. var n = document.getElementById("loaddingArea");
  204. if (n) {
  205. n.destroy();
  206. }
  207. document.id("layout").set("html", "<div>企业微信单点异常!</div>");
  208. }.bind(this)
  209. });
  210. };
  211. };
  212. //异步载入必要模块
  213. layout.config = null;
  214. var configLoaded = false;
  215. var lpLoaded = false;
  216. var commonLoaded = false;
  217. //var lp = o2.session.path + "/lp/" + o2.language + ".js";
  218. // var lp = "../x_desktop/js/base_lp_" + o2.language + ((o2.session.isDebugger) ? "" : ".min") + ".js?v="+o2.version.v;;
  219. if (o2.session.isDebugger && (o2.session.isMobile || layout.mobile)) o2.load("../o2_lib/eruda/eruda.js");
  220. var loadAllModules = function(error){
  221. _loadProgressBar();
  222. lpLoaded = true;
  223. var modules = ["o2.xDesktop.$all"];
  224. o2.require(modules, {
  225. "onSuccess": function () {
  226. if (o2.xDesktop.getServiceAddress){
  227. commonLoaded = true;
  228. if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () {
  229. _load();
  230. });
  231. }else{
  232. if (error) error();
  233. }
  234. },
  235. "onFailure": function(){ if (error) error();},
  236. "onEvery": function () {
  237. _loadProgressBar();
  238. }
  239. });
  240. }
  241. var loadO2Modules = function(){
  242. _loadProgressBar();
  243. lpLoaded = true;
  244. var o2modules = ['o2.widget.Common',
  245. 'o2.widget.Dialog',
  246. 'o2.widget.UUID',
  247. 'o2.xDesktop.Common',
  248. 'o2.xDesktop.Actions.RestActions',
  249. 'o2.xAction.RestActions',
  250. 'o2.xDesktop.Access',
  251. 'o2.xDesktop.Dialog',
  252. 'o2.xDesktop.Menu',
  253. 'o2.xDesktop.UserData',
  254. 'o2.xDesktop.Authentication',
  255. 'o2.xDesktop.Dialog',
  256. 'o2.xDesktop.Window'];
  257. o2.require(o2modules, {
  258. "onSuccess": function () {
  259. var appmodules = [['Template', 'MPopupForm'], ['Common', 'Main']];
  260. o2.requireApp(appmodules, "", {
  261. "onSuccess": function(){
  262. if (o2.xDesktop.getServiceAddress){
  263. commonLoaded = true;
  264. if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () {
  265. _load();
  266. });
  267. }
  268. },
  269. "onEvery": function () {
  270. _loadProgressBar();
  271. }
  272. });
  273. },
  274. "onEvery": function () {
  275. _loadProgressBar();
  276. }
  277. });
  278. }
  279. var loadModuls = function () {
  280. loadAllModules(loadO2Modules);
  281. };
  282. o2.getJSON("../x_desktop/res/config/config.json", function (config) {
  283. var supportedLanguages = Object.keys(config.supportedLanguages);
  284. if (supportedLanguages.indexOf(o2.language) === -1){
  285. o2.language = o2.language.substring(0, o2.language.indexOf('-'));
  286. }
  287. if (supportedLanguages.indexOf(o2.language) === -1) o2.language = "zh-cn";
  288. if (!o2.LP) {
  289. var lp = "../x_desktop/js/base_lp_" + o2.language + ((o2.session.isDebugger) ? "" : ".min") + ".js?v="+o2.version.v;
  290. o2.load(lp, function(m){
  291. if (!m.length){
  292. var lp = "../o2_core/o2/lp/" + o2.language + ((o2.session.isDebugger) ? "" : ".min") + ".js?v="+o2.version.v;
  293. o2.load(lp,loadModuls);
  294. }else{
  295. loadModuls();
  296. }
  297. });
  298. } else {
  299. loadModuls();
  300. }
  301. _loadProgressBar();
  302. if (config.proxyCenterEnable){
  303. if (o2.typeOf(config.center)==="array"){
  304. config.center.forEach(function(c){
  305. c.port = window.location.port || 80;
  306. });
  307. }else{
  308. config.port = window.location.port || 80;
  309. }
  310. }
  311. layout.config = config;
  312. o2.tokenName = config.tokenName || "x-token";
  313. if( !layout.mobile )document.title = layout.config.systemTitle || layout.config.title;
  314. configLoaded = true;
  315. if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () {
  316. _load();
  317. });
  318. });
  319. });
  320. }