manage.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.addEventListener(
  112. "WeixinJSBridgeReady",
  113. () => {
  114. this.switchBgmState(true);
  115. },
  116. false
  117. );
  118. document.addEventListener("touchstart", play); //ios需要加个事件才能播放 不能自动播放;如果还有浏览器不行,换成别的交互事件
  119. document.addEventListener("click", play);
  120. $("#player")[0].addEventListener("touchstart", play);
  121. g_bgAudio.addEventListener("ended", () => {
  122. this.switchBgmState(true);
  123. });
  124. $("#volume")
  125. .find("a")
  126. .on("click", () => {
  127. if ($("#volume img")[0].src.indexOf("btn_on.png") > -1) {
  128. this.switchBgmState(true);
  129. } else if ($("#volume img")[0].src.indexOf("btn_off.png") > -1) {
  130. this.switchBgmState(false);
  131. }
  132. });
  133. };
  134. Manage.prototype.switchBgmState = function(state) {
  135. if (!g_bgAudio || !g_bgAudio.src) return;
  136. var played = function() {
  137. console.log("begin play bgm");
  138. g_play = 1;
  139. g_playAudio = g_bgAudio;
  140. $("#volume a img").attr("src", "./images/Volume btn_off.png");
  141. $("#volume").attr("title", "关闭声音");
  142. g_tourAudio && g_tourAudio.pause();
  143. };
  144. var paused = function() {
  145. g_play = 0;
  146. g_playAudio == g_bgAudio && (g_playAudio = null);
  147. $("#volume a img").attr("src", "./images/Volume btn_on.png");
  148. $("#volume").attr("title", "打开声音");
  149. };
  150. if (state) {
  151. g_bgAudio.play();
  152. if (g_bgAudio.paused) {
  153. paused();
  154. } else {
  155. played();
  156. return true;
  157. }
  158. } else {
  159. g_bgAudio.pause();
  160. paused();
  161. }
  162. g_bgAudio.pauseByHot = false;
  163. g_bgAudio.pauseByTour = false;
  164. };
  165. Manage.prototype.dealURL = function(src, type) {
  166. //music: "///super.4dage.com/data/LYW/edit/20200928_151633415.mp3"
  167. //"https://super.4dage.com/data/LYW/edit/20200928_165319399.jpg"]
  168. if (window.isLocal && settings.localPrefix != void 0) {
  169. //本地将线上的前缀替换
  170. var oldPrefixs = [
  171. "https://super.4dage.com/",
  172. "http://super.4dage.com/",
  173. "///super.4dage.com/",
  174. ];
  175. for (let i = 0; i < oldPrefixs.length; i++) {
  176. if (src.includes(oldPrefixs[i])) {
  177. return src.replace(oldPrefixs[i], settings.localPrefix);
  178. break;
  179. }
  180. }
  181. console.error("没有找到合适的本地链接");
  182. return src;
  183. } else {
  184. //add https://
  185. var prefix = g_Prefix.replace("https://", "").replace("http://", "");
  186. if (
  187. !src.includes("http:/") &&
  188. !src.includes("https:/") &&
  189. src.includes(prefix)
  190. ) {
  191. src = "https://" + src;
  192. }
  193. return src;
  194. }
  195. };
  196. //隐藏公司Logo
  197. function showLogo() {
  198. $("#myCompany").hide();
  199. $("#loaderCoBrandName").hide();
  200. $("#title-logo").hide();
  201. $(".title-container").css("justify-content", "center");
  202. }
  203. //czj 添加随机的时间
  204. function randomTime() {
  205. return new Date();
  206. }
  207. window.loadMange = () => {
  208. window.manage = new Manage();
  209. };