manage.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. window.addEventListener('themechange', (e) => {
  120. if ($("#bg-music-btn")[0].src.indexOf(`bgm_off_`) > -1) {
  121. setTimeout(() => {
  122. let theme = ''
  123. if($('#app')[0]) {
  124. theme = $('#app')[0].className.substring(5)
  125. } else {
  126. theme = 'huyangjin'
  127. }
  128. $("#bg-music-btn").attr("src", `../static/images/bgm_off_${theme}.png`);
  129. }, 0);
  130. }
  131. })
  132. $("#bg-music-btn")
  133. .on("click", () => {
  134. if ($("#bg-music-btn")[0].src.indexOf("bgm_on.png") > -1) {
  135. this.switchBgmState(true);
  136. } else if ($("#bg-music-btn")[0].src.indexOf(`bgm_off_`) > -1) {
  137. this.switchBgmState(false);
  138. }
  139. });
  140. };
  141. Manage.prototype.switchBgmState = function(state) {
  142. if (!g_bgAudio || !g_bgAudio.src) return;
  143. var played = function() {
  144. g_play = 1;
  145. g_playAudio = g_bgAudio;
  146. let theme = ''
  147. if($('#app')[0]) {
  148. theme = $('#app')[0].className.substring(5)
  149. } else {
  150. theme = 'huyangjin'
  151. }
  152. $("#bg-music-btn").attr("src", `../static/images/bgm_off_${theme}.png`);
  153. $("#bg-music-btn").attr("title", "关闭声音");
  154. g_tourAudio && g_tourAudio.pause();
  155. };
  156. var paused = function() {
  157. g_play = 0;
  158. g_playAudio == g_bgAudio && (g_playAudio = null);
  159. $("#bg-music-btn").attr("src", "../static/images/bgm_on.png");
  160. $("#bg-music-btn").attr("title", "打开声音");
  161. };
  162. if (state) {
  163. g_bgAudio.play();
  164. if (g_bgAudio.paused) {
  165. paused();
  166. } else {
  167. played();
  168. return true;
  169. }
  170. } else {
  171. g_bgAudio.pause();
  172. paused();
  173. }
  174. g_bgAudio.pauseByHot = false;
  175. g_bgAudio.pauseByTour = false;
  176. };
  177. Manage.prototype.dealURL = function(src, type) {
  178. //music: "///super.4dage.com/data/LYW/edit/20200928_151633415.mp3"
  179. //"https://super.4dage.com/data/LYW/edit/20200928_165319399.jpg"]
  180. if (window.isLocal && settings.localPrefix != void 0) {
  181. //本地将线上的前缀替换
  182. var oldPrefixs = [
  183. "https://super.4dage.com/",
  184. "http://super.4dage.com/",
  185. "///super.4dage.com/",
  186. ];
  187. for (let i = 0; i < oldPrefixs.length; i++) {
  188. if (src.includes(oldPrefixs[i])) {
  189. return src.replace(oldPrefixs[i], settings.localPrefix);
  190. break;
  191. }
  192. }
  193. console.error("没有找到合适的本地链接");
  194. return src;
  195. } else {
  196. //add https://
  197. var prefix = g_Prefix.replace("https://", "").replace("http://", "");
  198. if (
  199. !src.includes("http:/") &&
  200. !src.includes("https:/") &&
  201. src.includes(prefix)
  202. ) {
  203. src = "https://" + src;
  204. }
  205. return src;
  206. }
  207. };
  208. //隐藏公司Logo
  209. function showLogo() {
  210. $("#myCompany").hide();
  211. $("#loaderCoBrandName").hide();
  212. $("#title-logo").hide();
  213. $(".title-container").css("justify-content", "center");
  214. }
  215. //czj 添加随机的时间
  216. function randomTime() {
  217. return new Date();
  218. }
  219. window.loadMange = () => {
  220. if (!window.manage) {
  221. window.manage = new Manage();
  222. }
  223. };