base_loader.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. o2.addReady(function () {
  2. //兼容方法
  3. if (window.Element){
  4. Element.implement({
  5. "makeLnk": function (options) { }
  6. });
  7. }
  8. layout.desktop.addEvent = function (type, e, d) {
  9. window.addEvent(type, e, d);
  10. };
  11. layout.desktop.addEvents = function (e) {
  12. window.addEvents(e);
  13. };
  14. var loadingNode = (window.$) ? $("loaddingArea") : null;
  15. var loadeds = 0;
  16. var loadCount = 4;
  17. var size = (window.document && document.body) ? document.body.getSize() : null;
  18. var _closeLoadingNode = function () {
  19. if (loadingNode) {
  20. loadingNode.destroy();
  21. loadingNode = null;
  22. }
  23. };
  24. var _loadProgressBar = function (complete) {
  25. if (loadingNode) {
  26. if (complete) {
  27. loadingNode.setStyles({ "width": "" + size.x + "px" });
  28. //loadingNode.set('morph', {duration: 100}).morph({"width": ""+size.x+"px"});
  29. window.setTimeout(_closeLoadingNode, 500);
  30. } else {
  31. loadeds++;
  32. var p = (loadeds / loadCount) * size.x;
  33. loadingNode.setStyles({ "width": "" + p + "px" });
  34. //loadingNode.set('morph', {duration: 100}).morph({"width": ""+p+"px"});
  35. if (loadeds >= loadCount) window.setTimeout(_closeLoadingNode, 500);
  36. }
  37. }
  38. };
  39. var _setLayoutService = function(service, center){
  40. layout.serviceAddressList = service;
  41. layout.centerServer = center;
  42. layout.desktop.serviceAddressList = service;
  43. layout.desktop.centerServer = center;
  44. };
  45. var _getDistribute = function (callback) {
  46. if (layout.config.app_protocol === "auto") {
  47. layout.config.app_protocol = window.location.protocol;
  48. }
  49. if (layout.config.configMapping && (layout.config.configMapping[window.location.host] || layout.config.configMapping[window.location.hostname])){
  50. var mapping = layout.config.configMapping[window.location.host] || layout.config.configMapping[window.location.hostname];
  51. if (mapping.servers){
  52. layout.serviceAddressList = mapping.servers;
  53. layout.desktop.serviceAddressList = mapping.servers;
  54. if (mapping.center) center = (o2.typeOf(mapping.center)==="array") ? mapping.center[0] : mapping.center;
  55. layout.centerServer = center;
  56. layout.desktop.centerServer = center;
  57. if (callback) callback();
  58. }else{
  59. if (mapping.center) layout.config.center = (o2.typeOf(mapping.center)==="array") ? mapping.center : [mapping.center];
  60. o2.xDesktop.getServiceAddress(layout.config, function (service, center) {
  61. _setLayoutService(service, center);
  62. _loadProgressBar();
  63. if (callback) callback();
  64. }.bind(this));
  65. }
  66. }else{
  67. o2.xDesktop.getServiceAddress(layout.config, function (service, center) {
  68. _setLayoutService(service, center);
  69. _loadProgressBar();
  70. if (callback) callback();
  71. }.bind(this));
  72. }
  73. };
  74. var _load = function () {
  75. var _loadApp = function (json) {
  76. //用户已经登录
  77. if (json){
  78. layout.user = json.data;
  79. layout.session = layout.session || {};
  80. layout.session.user = json.data;
  81. layout.session.token = json.data.token;
  82. layout.desktop.session = layout.session;
  83. }
  84. _loadProgressBar(true);
  85. layout.isReady = true;
  86. while (layout.readys && layout.readys.length) {
  87. layout.readys.shift().apply(window);
  88. }
  89. };
  90. // 是否ip
  91. var _isIp = function(ip) {
  92. 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])$/
  93. return reg.test(ip);
  94. };
  95. //修改支持x-token
  96. var uri = new URI(window.location.href);
  97. var options = uri.get("data");
  98. if (options["x-token"]) {
  99. // 删除
  100. Cookie.dispose("x-token");
  101. // 写入
  102. var host = window.location.host; // 域名
  103. var domain = null;
  104. if (_isIp(host)) {
  105. domain = host;
  106. }else {
  107. if (host.indexOf(".") > 0) {
  108. domain = host.substring(host.indexOf(".")); // 上级域名 如 .o2oa.net
  109. }
  110. }
  111. if (domain) {
  112. Cookie.write("x-token", options["x-token"], {domain: domain, path:"/"});
  113. }else {
  114. Cookie.write("x-token", options["x-token"]);
  115. }
  116. }
  117. layout.sessionPromise = new Promise(function(resolve, reject){
  118. o2.Actions.get("x_organization_assemble_authentication").getAuthentication(function (json) {
  119. if (resolve) resolve(json.data);
  120. }.bind(this), function (xhr, text, error) {
  121. if (reject) reject({"xhr": xhr, "text": text, "error": error});
  122. }.bind(this));
  123. });
  124. layout.sessionPromise.then(function(data){
  125. //已经登录
  126. layout.user = data;
  127. layout.session = layout.session || {};
  128. layout.session.user = data;
  129. layout.session.token = data.token;
  130. layout.desktop.session = layout.session;
  131. //_loadApp();
  132. }, function(){
  133. //允许匿名访问
  134. if (layout.anonymous) {
  135. var data = { name: "anonymous", roleList: [] };
  136. layout.user = data;
  137. layout.session = layout.session || {};
  138. layout.session.user = data;
  139. layout.session.token = data.token;
  140. layout.desktop.session = layout.session;
  141. //_loadApp();
  142. } else {
  143. _loadProgressBar(true);
  144. if (layout.yqwx) {
  145. layout.openLoginQywx();
  146. } else {
  147. layout.openLogin();
  148. }
  149. }
  150. });
  151. _loadApp();
  152. layout.openLogin = function () {
  153. layout.desktop.type = "app";
  154. layout.app = null;
  155. var content = $("appContent") || $("layout");
  156. if (content) content.empty();
  157. layout.authentication = new o2.xDesktop.Authentication({
  158. "style": "flat",
  159. "onLogin": _load.bind(layout)
  160. });
  161. layout.authentication.loadLogin(document.body);
  162. var loadingNode = $("browser_loading");
  163. if (loadingNode) loadingNode.fade("out");
  164. };
  165. layout.openLoginQywx = function () {
  166. console.log("开始login。。。。。。。。。。。。。");
  167. var uri = locate.href.toURI();
  168. console.log("执行单点。。。。。。。。。。");
  169. var action = new MWF.xDesktop.Actions.RestActions("", "x_organization_assemble_authentication", "");
  170. action.getActions = function (actionCallback) {
  171. this.actions = { "sso": { "uri": "/jaxrs/qiyeweixin/code/{code}", "method": "GET" } };
  172. if (actionCallback) actionCallback();
  173. };
  174. action.invoke({
  175. "name": "sso", "async": true, "parameter": { "code": uri.getData("code") }, "success": function (json) {
  176. console.log("单点成功。");
  177. console.log(json);
  178. //基础数据。。。。
  179. layout.session.user = json.data;
  180. //
  181. _load();
  182. }.bind(this), "failure": function (xhr, text, error) {
  183. var n = document.getElementById("loaddingArea");
  184. if (n) { n.destroy(); }
  185. document.id("layout").set("html", "<div>企业微信单点异常!</div>");
  186. }.bind(this)
  187. });
  188. };
  189. };
  190. //异步载入必要模块
  191. layout.config = null;
  192. var configLoaded = false;
  193. var lpLoaded = false;
  194. var commonLoaded = false;
  195. var lp = o2.session.path + "/lp/" + o2.language + ".js";
  196. if (o2.session.isDebugger && (o2.session.isMobile || layout.mobile)) o2.load("../o2_lib/eruda/eruda.js");
  197. var loadModuls = function () {
  198. _loadProgressBar();
  199. lpLoaded = true;
  200. var modules = ["o2.xDesktop.$all"];
  201. o2.require(modules, {
  202. "onSuccess": function () {
  203. commonLoaded = true;
  204. if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () { _load(); });
  205. },
  206. "onEvery": function () {
  207. _loadProgressBar();
  208. }
  209. });
  210. };
  211. if (!o2.LP){
  212. o2.load(lp, loadModuls);
  213. }else{
  214. loadModuls();
  215. }
  216. o2.getJSON("../x_desktop/res/config/config.json", function (config) {
  217. _loadProgressBar();
  218. layout.config = config;
  219. configLoaded = true;
  220. if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () { _load(); });
  221. });
  222. });