manage.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //管理js文件 获取modeldata.js 判断是否有特殊的字段,如果有就先加载SpecialScene.js 里面有对特殊场景处理的代码 否则就直接加载main
  2. var Manage = function() {
  3. this.time = "?" + new Date().getTime();
  4. this.loadAudio();
  5. };
  6. //动态加载js文件
  7. Manage.prototype.LoadJs = function(_files, succes) {
  8. /* 已加载文件缓存列表,用于判断文件是否已加载过,若已加载则不再次加载*/
  9. var classcodes = [];
  10. var FileArray = [];
  11. if (typeof _files === "object") {
  12. FileArray = _files;
  13. } else {
  14. /*如果文件列表是字符串,则用,切分成数组*/
  15. if (typeof _files === "string") {
  16. FileArray = _files.split(",");
  17. }
  18. }
  19. if (FileArray != null && FileArray.length > 0) {
  20. var LoadedCount = 0;
  21. for (var i = 0; i < FileArray.length; i++) {
  22. loadFile(FileArray[i], function() {
  23. LoadedCount++;
  24. if (LoadedCount == FileArray.length) {
  25. try {
  26. succes();
  27. } catch (err) {
  28. console.log("err: 您未定义回调");
  29. }
  30. }
  31. });
  32. }
  33. }
  34. /*加载JS文件,url:文件路径,success:加载成功回调函数*/
  35. function loadFile(url, success) {
  36. if (!FileIsExt(classcodes, url)) {
  37. var _ThisType = GetFileType(url);
  38. var ThisType =
  39. _ThisType.indexOf("?") == -1
  40. ? _ThisType
  41. : _ThisType.substring(0, _ThisType.indexOf("?"));
  42. var fileObj = null;
  43. if (ThisType == ".js") {
  44. fileObj = document.createElement("script");
  45. fileObj.src = url;
  46. } else if (ThisType == ".css") {
  47. fileObj = document.createElement("link");
  48. fileObj.href = url;
  49. fileObj.type = "text/css";
  50. fileObj.rel = "stylesheet";
  51. } else if (ThisType == ".less") {
  52. fileObj = document.createElement("link");
  53. fileObj.href = url;
  54. fileObj.type = "text/css";
  55. fileObj.rel = "stylesheet/less";
  56. }
  57. success = success || function() {};
  58. fileObj.onload = fileObj.onreadystatechange = function() {
  59. if (
  60. !this.readyState ||
  61. "loaded" === this.readyState ||
  62. "complete" === this.readyState
  63. ) {
  64. success();
  65. classcodes.push(url);
  66. }
  67. };
  68. document.getElementsByTagName("head")[0].appendChild(fileObj);
  69. } else {
  70. success();
  71. }
  72. }
  73. /*获取文件类型,后缀名,小写*/
  74. function GetFileType(url) {
  75. if (url != null && url.length > 0) {
  76. return url.substr(url.lastIndexOf(".")).toLowerCase();
  77. }
  78. return "";
  79. }
  80. /*文件是否已加载*/
  81. function FileIsExt(FileArray, _url) {
  82. if (FileArray != null && FileArray.length > 0) {
  83. var len = FileArray.length;
  84. for (var i = 0; i < len; i++) {
  85. if (FileArray[i] == _url) {
  86. return true;
  87. }
  88. }
  89. }
  90. return false;
  91. }
  92. };
  93. Manage.prototype.loadAudio = function() {
  94. //相关:g_tourAudio \ g_playAudio
  95. g_bgAudio = new Audio();
  96. g_bgAudio.loop = true;
  97. g_bgAudio.autoplay = true;
  98. g_bgAudio.id = "bgaudio";
  99. //https://www.cnblogs.com/interdrp/p/4211883.html 部分资料
  100. g_bgAudio.load(); // iOS 9 还需要额外的 load 一下, 否则直接 play 无效
  101. var play = function() {
  102. //if(window.tourAudioSta) return;
  103. this.switchBgmState(true);
  104. document.removeEventListener("touchstart", play);
  105. document.removeEventListener("click", play);
  106. $("#player")[0].removeEventListener("touchstart", play);
  107. }.bind(this);
  108. g_bgAudio.oncanplay = () => {
  109. this.switchBgmState(true);
  110. };
  111. document.removeEventListener("WeixinJSBridgeReady", play);
  112. document.addEventListener("WeixinJSBridgeReady",play);
  113. document.addEventListener("touchstart", play); //ios需要加个事件才能播放 不能自动播放;如果还有浏览器不行,换成别的交互事件
  114. document.addEventListener("click", play);
  115. $("#player")[0].addEventListener("touchstart", play);
  116. g_bgAudio.addEventListener("ended", () => {
  117. this.switchBgmState(true);
  118. });
  119. $("#volume")
  120. .find("a")
  121. .on("click", () => {
  122. if ($("#volume img")[0].src.indexOf("btn_on.png") > -1) {
  123. this.switchBgmState(true);
  124. } else if ($("#volume img")[0].src.indexOf("btn_off.png") > -1) {
  125. this.switchBgmState(false);
  126. }
  127. });
  128. };
  129. Manage.prototype.switchBgmState = function(state) {
  130. if (!g_bgAudio || !g_bgAudio.src) return;
  131. var played = function() {
  132. console.log("begin play bgm");
  133. g_play = 1;
  134. g_playAudio = g_bgAudio;
  135. $("#volume a img").attr("src", "./static/images/Volume btn_off.png");
  136. $("#volume").attr("title", "关闭声音");
  137. g_tourAudio && g_tourAudio.pause();
  138. };
  139. var paused = function() {
  140. g_play = 0;
  141. g_playAudio == g_bgAudio && (g_playAudio = null);
  142. $("#volume a img").attr("src", "./static/images/Volume btn_on.png");
  143. $("#volume").attr("title", "打开声音");
  144. };
  145. if (state) {
  146. g_bgAudio.play();
  147. if (g_bgAudio.paused) {
  148. paused();
  149. } else {
  150. played();
  151. return true;
  152. }
  153. } else {
  154. g_bgAudio.pause();
  155. paused();
  156. }
  157. g_bgAudio.pauseByHot = false;
  158. g_bgAudio.pauseByTour = false;
  159. };
  160. Manage.prototype.dealURL = function(src, type) {
  161. //music: "///super.4dage.com/data/LYW/edit/20200928_151633415.mp3"
  162. //"https://super.4dage.com/data/LYW/edit/20200928_165319399.jpg"]
  163. if (window.isLocal && settings.localPrefix != void 0) {
  164. //本地将线上的前缀替换
  165. var oldPrefixs = [
  166. "https://super.4dage.com/",
  167. "http://super.4dage.com/",
  168. "///super.4dage.com/",
  169. ];
  170. for (let i = 0; i < oldPrefixs.length; i++) {
  171. if (src.includes(oldPrefixs[i])) {
  172. return src.replace(oldPrefixs[i], settings.localPrefix);
  173. break;
  174. }
  175. }
  176. console.error("没有找到合适的本地链接");
  177. return src;
  178. } else {
  179. //add https://
  180. var prefix = g_Prefix.replace("https://", "").replace("http://", "");
  181. if (
  182. !src.includes("http:/") &&
  183. !src.includes("https:/") &&
  184. src.includes(prefix)
  185. ) {
  186. src = "https://" + src;
  187. }
  188. return src;
  189. }
  190. };
  191. //隐藏公司Logo
  192. function showLogo() {
  193. $("#myCompany").hide();
  194. $("#loaderCoBrandName").hide();
  195. $("#title-logo").hide();
  196. $(".title-container").css("justify-content", "center");
  197. }
  198. //czj 添加随机的时间
  199. function randomTime() {
  200. return new Date();
  201. }
  202. window.loadMange = () => {
  203. if (!window.manage) {
  204. window.manage = new Manage();
  205. }
  206. };