base_loader.js 8.3 KB

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