base_loader.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. //修改支持x-token
  91. var uri = new URI(window.location.href);
  92. var options = uri.get("data");
  93. if (options["x-token"]) {
  94. Cookie.write("x-token", options["x-token"]);
  95. }
  96. layout.sessionPromise = new Promise(function(resolve, reject){
  97. o2.Actions.get("x_organization_assemble_authentication").getAuthentication(function (json) {
  98. if (resolve) resolve(json.data);
  99. }.bind(this), function (xhr, text, error) {
  100. if (reject) reject({"xhr": xhr, "text": text, "error": error});
  101. }.bind(this));
  102. });
  103. layout.sessionPromise.then(function(data){
  104. //已经登录
  105. layout.user = data;
  106. layout.session = layout.session || {};
  107. layout.session.user = data;
  108. layout.session.token = data.token;
  109. layout.desktop.session = layout.session;
  110. //_loadApp();
  111. }, function(){
  112. //允许匿名访问
  113. if (layout.anonymous) {
  114. var data = { name: "anonymous", roleList: [] };
  115. layout.user = data;
  116. layout.session = layout.session || {};
  117. layout.session.user = data;
  118. layout.session.token = data.token;
  119. layout.desktop.session = layout.session;
  120. //_loadApp();
  121. } else {
  122. _loadProgressBar(true);
  123. if (layout.yqwx) {
  124. layout.openLoginQywx();
  125. } else {
  126. layout.openLogin();
  127. }
  128. }
  129. });
  130. _loadApp();
  131. layout.openLogin = function () {
  132. layout.desktop.type = "app";
  133. layout.app = null;
  134. var content = $("appContent") || $("layout");
  135. if (content) content.empty();
  136. layout.authentication = new o2.xDesktop.Authentication({
  137. "style": "flat",
  138. "onLogin": _load.bind(layout)
  139. });
  140. layout.authentication.loadLogin(document.body);
  141. var loadingNode = $("browser_loading");
  142. if (loadingNode) loadingNode.fade("out");
  143. };
  144. layout.openLoginQywx = function () {
  145. console.log("开始login。。。。。。。。。。。。。");
  146. var uri = locate.href.toURI();
  147. console.log("执行单点。。。。。。。。。。");
  148. var action = new MWF.xDesktop.Actions.RestActions("", "x_organization_assemble_authentication", "");
  149. action.getActions = function (actionCallback) {
  150. this.actions = { "sso": { "uri": "/jaxrs/qiyeweixin/code/{code}", "method": "GET" } };
  151. if (actionCallback) actionCallback();
  152. };
  153. action.invoke({
  154. "name": "sso", "async": true, "parameter": { "code": uri.getData("code") }, "success": function (json) {
  155. console.log("单点成功。");
  156. console.log(json);
  157. //基础数据。。。。
  158. layout.session.user = json.data;
  159. //
  160. _load();
  161. }.bind(this), "failure": function (xhr, text, error) {
  162. var n = document.getElementById("loaddingArea");
  163. if (n) { n.destroy(); }
  164. document.id("layout").set("html", "<div>企业微信单点异常!</div>");
  165. }.bind(this)
  166. });
  167. };
  168. };
  169. //异步载入必要模块
  170. layout.config = null;
  171. var configLoaded = false;
  172. var lpLoaded = false;
  173. var commonLoaded = false;
  174. var lp = o2.session.path + "/lp/" + o2.language + ".js";
  175. if (o2.session.isDebugger && (o2.session.isMobile || layout.mobile)) o2.load("../o2_lib/eruda/eruda.js");
  176. var loadModuls = function () {
  177. _loadProgressBar();
  178. lpLoaded = true;
  179. var modules = ["o2.xDesktop.$all"];
  180. o2.require(modules, {
  181. "onSuccess": function () {
  182. commonLoaded = true;
  183. if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () { _load(); });
  184. },
  185. "onEvery": function () {
  186. _loadProgressBar();
  187. }
  188. });
  189. };
  190. if (!o2.LP){
  191. o2.load(lp, loadModuls);
  192. }else{
  193. loadModuls();
  194. }
  195. o2.getJSON("../x_desktop/res/config/config.json", function (config) {
  196. _loadProgressBar();
  197. layout.config = config;
  198. configLoaded = true;
  199. if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () { _load(); });
  200. });
  201. });