laya.wxmini.js 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  1. window.wxMiniGame = function (exports, Laya) {
  2. 'use strict';
  3. class MiniFileMgr {
  4. static isLocalNativeFile(url) {
  5. for (var i = 0, sz = MiniAdpter.nativefiles.length; i < sz; i++) {
  6. if (url.indexOf(MiniAdpter.nativefiles[i]) != -1)
  7. return true;
  8. }
  9. return false;
  10. }
  11. static isNetFile(url) {
  12. return (url.indexOf("http://") != -1 || url.indexOf("https://") != -1) && url.indexOf(MiniAdpter.window.wx.env.USER_DATA_PATH) == -1;
  13. }
  14. static getFileInfo(fileUrl) {
  15. var fileNativePath = fileUrl;
  16. var fileObj = MiniFileMgr.fakeObj[fileNativePath];
  17. if (fileObj == null)
  18. return null;
  19. else
  20. return fileObj;
  21. }
  22. static read(filePath, encoding = "utf8", callBack = null, readyUrl = "", isSaveFile = false, fileType = "") {
  23. var fileUrl;
  24. if (readyUrl != "" && (readyUrl.indexOf("http://") != -1 || readyUrl.indexOf("https://") != -1)) {
  25. fileUrl = MiniFileMgr.getFileNativePath(filePath);
  26. }
  27. else {
  28. fileUrl = filePath;
  29. }
  30. fileUrl = Laya.URL.getAdptedFilePath(fileUrl);
  31. MiniFileMgr.fs.readFile({ filePath: fileUrl, encoding: encoding, success: function (data) {
  32. callBack != null && callBack.runWith([0, data]);
  33. }, fail: function (data) {
  34. if (data && readyUrl != "")
  35. MiniFileMgr.downFiles(readyUrl, encoding, callBack, readyUrl, isSaveFile, fileType);
  36. else
  37. callBack != null && callBack.runWith([1]);
  38. } });
  39. }
  40. static isFile(url) {
  41. let stat;
  42. try {
  43. stat = MiniFileMgr.fs.statSync(url);
  44. }
  45. catch (err) {
  46. return false;
  47. }
  48. return stat.isFile();
  49. }
  50. static downFiles(fileUrl, encoding = "utf8", callBack = null, readyUrl = "", isSaveFile = false, fileType = "", isAutoClear = true) {
  51. var downloadTask = MiniFileMgr.down({ url: fileUrl, success: function (data) {
  52. if (data.statusCode === 200)
  53. MiniFileMgr.readFile(data.tempFilePath, encoding, callBack, readyUrl, isSaveFile, fileType, isAutoClear);
  54. else if (data.statusCode === 403) {
  55. callBack != null && callBack.runWith([0, fileUrl]);
  56. }
  57. else {
  58. callBack != null && callBack.runWith([1, data]);
  59. }
  60. }, fail: function (data) {
  61. callBack != null && callBack.runWith([1, data]);
  62. } });
  63. downloadTask.onProgressUpdate(function (data) {
  64. callBack != null && callBack.runWith([2, data.progress]);
  65. });
  66. }
  67. static readFile(filePath, encoding = "utf8", callBack = null, readyUrl = "", isSaveFile = false, fileType = "", isAutoClear = true) {
  68. filePath = Laya.URL.getAdptedFilePath(filePath);
  69. MiniFileMgr.fs.readFile({ filePath: filePath, encoding: encoding, success: function (data) {
  70. if ((filePath.indexOf("http://") != -1 || filePath.indexOf("https://") != -1) && filePath.indexOf(MiniAdpter.window.wx.env.USER_DATA_PATH) == -1) {
  71. if (MiniAdpter.AutoCacheDownFile || isSaveFile) {
  72. callBack != null && callBack.runWith([0, data]);
  73. MiniFileMgr.copyTOCache(filePath, readyUrl, null, encoding, isAutoClear);
  74. }
  75. else
  76. callBack != null && callBack.runWith([0, data]);
  77. }
  78. else
  79. callBack != null && callBack.runWith([0, data]);
  80. }, fail: function (data) {
  81. if (data)
  82. callBack != null && callBack.runWith([1, data]);
  83. } });
  84. }
  85. static downOtherFiles(fileUrl, callBack = null, readyUrl = "", isSaveFile = false, isAutoClear = true) {
  86. MiniFileMgr.down({ url: fileUrl, success: function (data) {
  87. if (data.statusCode === 200) {
  88. if ((MiniAdpter.autoCacheFile || isSaveFile) && readyUrl.indexOf("qlogo.cn") == -1 && readyUrl.indexOf(".php") == -1) {
  89. callBack != null && callBack.runWith([0, data.tempFilePath]);
  90. MiniFileMgr.copyTOCache(data.tempFilePath, readyUrl, null, "", isAutoClear);
  91. }
  92. else
  93. callBack != null && callBack.runWith([0, data.tempFilePath]);
  94. }
  95. else {
  96. callBack != null && callBack.runWith([1, data]);
  97. }
  98. }, fail: function (data) {
  99. callBack != null && callBack.runWith([1, data]);
  100. } });
  101. }
  102. static copyFile(src, dest, complete = null) {
  103. MiniFileMgr.fs.copyFile({
  104. srcPath: src,
  105. destPath: dest,
  106. success: function () {
  107. complete && complete.runWith(0);
  108. },
  109. fail: function (err) {
  110. complete && complete.runWith([1, err]);
  111. }
  112. });
  113. }
  114. static downLoadFile(fileUrl, fileType = "", callBack = null, encoding = "utf8") {
  115. if (window.navigator.userAgent.indexOf('MiniGame') < 0) {
  116. Laya.Laya.loader.load(fileUrl, callBack);
  117. }
  118. else {
  119. if (fileType == Laya.Loader.IMAGE || fileType == Laya.Loader.SOUND)
  120. MiniFileMgr.downOtherFiles(fileUrl, callBack, fileUrl, true, false);
  121. else
  122. MiniFileMgr.downFiles(fileUrl, encoding, callBack, fileUrl, true, fileType, false);
  123. }
  124. }
  125. static copyTOCache(tempFilePath, readyUrl, callBack, encoding = "", isAutoClear = true) {
  126. var temp = tempFilePath.split("/");
  127. var tempFileName = temp[temp.length - 1];
  128. var fileurlkey = readyUrl;
  129. var fileObj = MiniFileMgr.getFileInfo(readyUrl);
  130. var saveFilePath = MiniFileMgr.getFileNativePath(tempFileName);
  131. MiniFileMgr.fakeObj[fileurlkey] = { md5: tempFileName, readyUrl: readyUrl, size: 0, times: Laya.Browser.now(), encoding: encoding, tempFilePath: tempFilePath };
  132. var totalSize = MiniAdpter.sizeLimit;
  133. var chaSize = 4 * 1024 * 1024;
  134. var fileUseSize = MiniFileMgr.getCacheUseSize();
  135. if (fileObj) {
  136. if (fileObj.readyUrl != readyUrl) {
  137. MiniFileMgr.fs.getFileInfo({
  138. filePath: tempFilePath,
  139. success: function (data) {
  140. if ((isAutoClear && (fileUseSize + chaSize + data.size) >= totalSize)) {
  141. if (data.size > MiniAdpter.minClearSize)
  142. MiniAdpter.minClearSize = data.size;
  143. MiniFileMgr.onClearCacheRes();
  144. }
  145. MiniFileMgr.deleteFile(tempFilePath, readyUrl, callBack, encoding, data.size);
  146. },
  147. fail: function (data) {
  148. callBack != null && callBack.runWith([1, data]);
  149. }
  150. });
  151. }
  152. else
  153. callBack != null && callBack.runWith([0]);
  154. }
  155. else {
  156. MiniFileMgr.fs.getFileInfo({
  157. filePath: tempFilePath,
  158. success: function (data) {
  159. if ((isAutoClear && (fileUseSize + chaSize + data.size) >= totalSize)) {
  160. if (data.size > MiniAdpter.minClearSize)
  161. MiniAdpter.minClearSize = data.size;
  162. MiniFileMgr.onClearCacheRes();
  163. }
  164. MiniFileMgr.fs.copyFile({ srcPath: tempFilePath, destPath: saveFilePath, success: function (data2) {
  165. MiniFileMgr.onSaveFile(readyUrl, tempFileName, true, encoding, callBack, data.size);
  166. }, fail: function (data) {
  167. callBack != null && callBack.runWith([1, data]);
  168. } });
  169. },
  170. fail: function (data) {
  171. callBack != null && callBack.runWith([1, data]);
  172. }
  173. });
  174. }
  175. }
  176. static onClearCacheRes() {
  177. var memSize = MiniAdpter.minClearSize;
  178. var tempFileListArr = [];
  179. for (var key in MiniFileMgr.filesListObj) {
  180. if (key != "fileUsedSize")
  181. tempFileListArr.push(MiniFileMgr.filesListObj[key]);
  182. }
  183. MiniFileMgr.sortOn(tempFileListArr, "times", MiniFileMgr.NUMERIC);
  184. var clearSize = 0;
  185. for (var i = 1, sz = tempFileListArr.length; i < sz; i++) {
  186. var fileObj = tempFileListArr[i];
  187. if (clearSize >= memSize)
  188. break;
  189. clearSize += fileObj.size;
  190. MiniFileMgr.deleteFile("", fileObj.readyUrl);
  191. }
  192. }
  193. static sortOn(array, name, options = 0) {
  194. if (options == MiniFileMgr.NUMERIC)
  195. return array.sort(function (a, b) { return a[name] - b[name]; });
  196. if (options == (MiniFileMgr.NUMERIC | MiniFileMgr.DESCENDING))
  197. return array.sort(function (a, b) { return b[name] - a[name]; });
  198. return array.sort(function (a, b) { return a[name] - b[name]; });
  199. }
  200. static getFileNativePath(fileName) {
  201. return MiniFileMgr.fileNativeDir + "/" + fileName;
  202. }
  203. static deleteFile(tempFileName, readyUrl = "", callBack = null, encoding = "", fileSize = 0) {
  204. var fileObj = MiniFileMgr.getFileInfo(readyUrl);
  205. var deleteFileUrl = MiniFileMgr.getFileNativePath(fileObj.md5);
  206. MiniFileMgr.fs.unlink({ filePath: deleteFileUrl, success: function (data) {
  207. if (tempFileName != "") {
  208. var saveFilePath = MiniFileMgr.getFileNativePath(tempFileName);
  209. MiniFileMgr.fs.copyFile({ srcPath: tempFileName, destPath: saveFilePath, success: function (data) {
  210. MiniFileMgr.onSaveFile(readyUrl, tempFileName, true, encoding, callBack, fileSize);
  211. }, fail: function (data) {
  212. callBack != null && callBack.runWith([1, data]);
  213. } });
  214. }
  215. else {
  216. MiniFileMgr.onSaveFile(readyUrl, tempFileName, false, encoding, callBack, fileSize);
  217. }
  218. }, fail: function (data) {
  219. callBack != null && callBack.runWith([1, data]);
  220. } });
  221. }
  222. static deleteAll() {
  223. var tempFileListArr = [];
  224. for (var key in MiniFileMgr.filesListObj) {
  225. if (key != "fileUsedSize")
  226. tempFileListArr.push(MiniFileMgr.filesListObj[key]);
  227. }
  228. for (var i = 1, sz = tempFileListArr.length; i < sz; i++) {
  229. var fileObj = tempFileListArr[i];
  230. MiniFileMgr.deleteFile("", fileObj.readyUrl);
  231. }
  232. if (MiniFileMgr.filesListObj && MiniFileMgr.filesListObj.fileUsedSize) {
  233. MiniFileMgr.filesListObj.fileUsedSize = 0;
  234. }
  235. MiniFileMgr.writeFilesList("", JSON.stringify({}), false);
  236. }
  237. static onSaveFile(readyUrl, md5Name, isAdd = true, encoding = "", callBack = null, fileSize = 0) {
  238. var fileurlkey = readyUrl;
  239. if (MiniFileMgr.filesListObj['fileUsedSize'] == null)
  240. MiniFileMgr.filesListObj['fileUsedSize'] = 0;
  241. if (isAdd) {
  242. var fileNativeName = MiniFileMgr.getFileNativePath(md5Name);
  243. MiniFileMgr.filesListObj[fileurlkey] = { md5: md5Name, readyUrl: readyUrl, size: fileSize, times: Laya.Browser.now(), encoding: encoding, tempFilePath: fileNativeName };
  244. MiniFileMgr.filesListObj['fileUsedSize'] = parseInt(MiniFileMgr.filesListObj['fileUsedSize']) + fileSize;
  245. MiniFileMgr.writeFilesList(fileurlkey, JSON.stringify(MiniFileMgr.filesListObj), true);
  246. callBack != null && callBack.runWith([0]);
  247. }
  248. else {
  249. if (MiniFileMgr.filesListObj[fileurlkey]) {
  250. var deletefileSize = parseInt(MiniFileMgr.filesListObj[fileurlkey].size);
  251. MiniFileMgr.filesListObj['fileUsedSize'] = parseInt(MiniFileMgr.filesListObj['fileUsedSize']) - deletefileSize;
  252. if (MiniFileMgr.fakeObj[fileurlkey].md5 == MiniFileMgr.filesListObj[fileurlkey].md5) {
  253. delete MiniFileMgr.fakeObj[fileurlkey];
  254. }
  255. delete MiniFileMgr.filesListObj[fileurlkey];
  256. MiniFileMgr.writeFilesList(fileurlkey, JSON.stringify(MiniFileMgr.filesListObj), false);
  257. callBack != null && callBack.runWith([0]);
  258. }
  259. }
  260. }
  261. static writeFilesList(fileurlkey, filesListStr, isAdd) {
  262. var listFilesPath = MiniFileMgr.fileNativeDir + "/" + MiniFileMgr.fileListName;
  263. MiniFileMgr.fs.writeFile({ filePath: listFilesPath, encoding: 'utf8', data: filesListStr, success: function (data) {
  264. }, fail: function (data) {
  265. } });
  266. if (!MiniAdpter.isZiYu && MiniAdpter.isPosMsgYu) {
  267. MiniAdpter.window.wx.postMessage({ url: fileurlkey, data: MiniFileMgr.filesListObj[fileurlkey], isLoad: "filenative", isAdd: isAdd });
  268. }
  269. }
  270. static getCacheUseSize() {
  271. if (MiniFileMgr.filesListObj && MiniFileMgr.filesListObj['fileUsedSize'])
  272. return MiniFileMgr.filesListObj['fileUsedSize'];
  273. return 0;
  274. }
  275. static getCacheList(dirPath, cb) {
  276. let stat;
  277. try {
  278. stat = MiniFileMgr.fs.statSync(dirPath);
  279. }
  280. catch (err) {
  281. stat = null;
  282. }
  283. if (stat) {
  284. MiniFileMgr.readSync(MiniFileMgr.fileListName, "utf8", cb);
  285. }
  286. else {
  287. MiniFileMgr.fs.mkdirSync(dirPath, true);
  288. cb && cb.runWith([1]);
  289. }
  290. }
  291. static existDir(dirPath, callBack) {
  292. MiniFileMgr.fs.mkdir({ dirPath: dirPath, success: function (data) {
  293. callBack != null && callBack.runWith([0, { data: JSON.stringify({}) }]);
  294. }, fail: function (data) {
  295. if (data.errMsg.indexOf("file already exists") != -1)
  296. MiniFileMgr.readSync(MiniFileMgr.fileListName, "utf8", callBack);
  297. else
  298. callBack != null && callBack.runWith([1, data]);
  299. } });
  300. }
  301. static readSync(filePath, encoding = "utf8", callBack = null, readyUrl = "") {
  302. var fileUrl = MiniFileMgr.getFileNativePath(filePath);
  303. var filesListStr;
  304. try {
  305. filesListStr = MiniFileMgr.fs.readFileSync(fileUrl, encoding);
  306. callBack != null && callBack.runWith([0, { data: filesListStr }]);
  307. }
  308. catch (error) {
  309. callBack != null && callBack.runWith([1]);
  310. }
  311. }
  312. static setNativeFileDir(value) {
  313. MiniFileMgr.fileNativeDir = MiniAdpter.window.wx.env.USER_DATA_PATH + value;
  314. }
  315. }
  316. MiniFileMgr.fs = window.wx.getFileSystemManager();
  317. MiniFileMgr.down = window.wx.downloadFile;
  318. MiniFileMgr.filesListObj = {};
  319. MiniFileMgr.fakeObj = {};
  320. MiniFileMgr.fileListName = "layaairfiles.txt";
  321. MiniFileMgr.ziyuFileData = {};
  322. MiniFileMgr.ziyuFileTextureData = {};
  323. MiniFileMgr.loadPath = "";
  324. MiniFileMgr.DESCENDING = 2;
  325. MiniFileMgr.NUMERIC = 16;
  326. class MiniSoundChannel extends Laya.SoundChannel {
  327. constructor(sound) {
  328. super();
  329. this._sound = sound;
  330. this._audio = sound._sound;
  331. this._onCanplay = this.onCanPlay.bind(this);
  332. this._onError = this.onError.bind(this);
  333. this._onEnd = this.__onEnd.bind(this);
  334. this.addEventListener();
  335. }
  336. addEventListener() {
  337. this._audio.onError(this._onError);
  338. this._audio.onCanplay(this._onCanplay);
  339. }
  340. offEventListener() {
  341. this._audio.offError(this._onError);
  342. this._audio.offCanplay(this._onCanplay);
  343. this._audio.offEnded(this._onEnd);
  344. }
  345. onError(error) {
  346. console.log("-----1---------------minisound-----url:", this.url);
  347. console.log(error);
  348. this.event(Laya.Event.ERROR);
  349. if (!this._audio)
  350. return;
  351. this._sound.dispose();
  352. this.offEventListener();
  353. this._sound = this._audio = null;
  354. }
  355. onCanPlay() {
  356. if (!this._audio)
  357. return;
  358. this.event(Laya.Event.COMPLETE);
  359. this.offEventListener();
  360. this._audio.onEnded(this._onEnd);
  361. if (!this.isStopped) {
  362. this.play();
  363. }
  364. else {
  365. this.stop();
  366. }
  367. }
  368. __onEnd() {
  369. if (this.loops == 1) {
  370. if (this.completeHandler) {
  371. Laya.Laya.systemTimer.once(10, this, this.__runComplete, [this.completeHandler], false);
  372. this.completeHandler = null;
  373. }
  374. this.stop();
  375. this.event(Laya.Event.COMPLETE);
  376. return;
  377. }
  378. if (this.loops > 0) {
  379. this.loops--;
  380. }
  381. this.startTime = 0;
  382. this.play();
  383. }
  384. play() {
  385. this.isStopped = false;
  386. Laya.SoundManager.addChannel(this);
  387. if (!this._audio)
  388. return;
  389. this._audio.play();
  390. }
  391. set startTime(time) {
  392. if (!this._audio)
  393. return;
  394. this._audio.startTime = time;
  395. }
  396. set autoplay(value) {
  397. if (!this._audio)
  398. return;
  399. this._audio.autoplay = value;
  400. }
  401. get autoplay() {
  402. if (!this._audio)
  403. return false;
  404. return this._audio.autoplay;
  405. }
  406. get position() {
  407. if (!this._audio)
  408. return 0;
  409. return this._audio.currentTime;
  410. }
  411. get duration() {
  412. if (!this._audio)
  413. return 0;
  414. return this._audio.duration;
  415. }
  416. stop() {
  417. super.stop();
  418. this.isStopped = true;
  419. Laya.SoundManager.removeChannel(this);
  420. this.completeHandler = null;
  421. if (!this._audio)
  422. return;
  423. this._audio.stop();
  424. if (!this.loop) {
  425. this.offEventListener();
  426. this._sound.dispose();
  427. this._sound = null;
  428. this._audio = null;
  429. }
  430. }
  431. pause() {
  432. this.isStopped = true;
  433. if (!this._audio)
  434. return;
  435. this._audio.pause();
  436. }
  437. get loop() {
  438. if (!this._audio)
  439. return false;
  440. return this._audio.loop;
  441. }
  442. set loop(value) {
  443. if (!this._audio)
  444. return;
  445. this._audio.loop = value;
  446. }
  447. resume() {
  448. this.isStopped = false;
  449. Laya.SoundManager.addChannel(this);
  450. if (!this._audio)
  451. return;
  452. this._audio.play();
  453. }
  454. set volume(v) {
  455. if (!this._audio)
  456. return;
  457. this._audio.volume = v;
  458. }
  459. get volume() {
  460. if (!this._audio)
  461. return 0;
  462. return this._audio.volume;
  463. }
  464. }
  465. class MiniSound extends Laya.EventDispatcher {
  466. constructor() {
  467. super();
  468. this.loaded = false;
  469. this._sound = MiniSound._createSound();
  470. }
  471. static _createSound() {
  472. MiniSound._id++;
  473. return MiniAdpter.window.wx.createInnerAudioContext();
  474. }
  475. load(url) {
  476. if (!MiniFileMgr.isLocalNativeFile(url)) {
  477. url = Laya.URL.formatURL(url);
  478. }
  479. else {
  480. if (MiniFileMgr.isNetFile(url)) {
  481. if (MiniFileMgr.loadPath != "") {
  482. url = url.split(MiniFileMgr.loadPath)[1];
  483. }
  484. else {
  485. var tempStr = Laya.URL.rootPath != "" ? Laya.URL.rootPath : Laya.URL._basePath;
  486. if (tempStr != "")
  487. url = url.split(tempStr)[1];
  488. }
  489. }
  490. }
  491. this.url = url;
  492. this.readyUrl = url;
  493. if (MiniAdpter.autoCacheFile && MiniFileMgr.getFileInfo(url)) {
  494. this.onDownLoadCallBack(url, 0);
  495. }
  496. else {
  497. if (!MiniAdpter.autoCacheFile) {
  498. this.onDownLoadCallBack(url, 0);
  499. }
  500. else {
  501. if (MiniFileMgr.isLocalNativeFile(url)) {
  502. if (MiniAdpter.subNativeFiles && MiniAdpter.subNativeheads.length == 0) {
  503. for (var key in MiniAdpter.subNativeFiles) {
  504. var tempArr = MiniAdpter.subNativeFiles[key];
  505. MiniAdpter.subNativeheads = MiniAdpter.subNativeheads.concat(tempArr);
  506. for (let i = 0; i < tempArr.length; i++) {
  507. MiniAdpter.subMaps[tempArr[i]] = key + "/" + tempArr[i];
  508. }
  509. }
  510. }
  511. if (MiniAdpter.subNativeFiles && url.indexOf("/") != -1) {
  512. var curfileHead = url.split("/")[0] + "/";
  513. if (curfileHead && MiniAdpter.subNativeheads.indexOf(curfileHead) != -1) {
  514. var newfileHead = MiniAdpter.subMaps[curfileHead];
  515. url = url.replace(curfileHead, newfileHead);
  516. }
  517. }
  518. this.onDownLoadCallBack(url, 0);
  519. }
  520. else {
  521. if (MiniFileMgr.isNetFile(url)) {
  522. MiniFileMgr.downOtherFiles(url, Laya.Handler.create(this, this.onDownLoadCallBack, [url]), url);
  523. }
  524. else {
  525. this.onDownLoadCallBack(url, 0);
  526. }
  527. }
  528. }
  529. }
  530. }
  531. onDownLoadCallBack(sourceUrl, errorCode, tempFilePath = null) {
  532. if (!errorCode && this._sound) {
  533. var fileNativeUrl;
  534. if (MiniAdpter.autoCacheFile) {
  535. if (!tempFilePath) {
  536. if (MiniFileMgr.isLocalNativeFile(sourceUrl)) {
  537. var tempStr = Laya.URL.rootPath != "" ? Laya.URL.rootPath : Laya.URL._basePath;
  538. var tempUrl = sourceUrl;
  539. if (tempStr != "" && (sourceUrl.indexOf("http://") != -1 || sourceUrl.indexOf("https://") != -1))
  540. fileNativeUrl = sourceUrl.split(tempStr)[1];
  541. if (!fileNativeUrl) {
  542. fileNativeUrl = tempUrl;
  543. }
  544. }
  545. else {
  546. var fileObj = MiniFileMgr.getFileInfo(sourceUrl);
  547. if (fileObj && fileObj.md5) {
  548. fileNativeUrl = fileObj.tempFilePath || MiniFileMgr.getFileNativePath(fileObj.md5);
  549. }
  550. else {
  551. fileNativeUrl = sourceUrl;
  552. }
  553. }
  554. }
  555. else {
  556. fileNativeUrl = tempFilePath;
  557. }
  558. this._sound.src = this.readyUrl = fileNativeUrl;
  559. }
  560. else {
  561. this._sound.src = this.readyUrl = sourceUrl;
  562. }
  563. }
  564. else {
  565. this.event(Laya.Event.ERROR);
  566. }
  567. }
  568. play(startTime = 0, loops = 0) {
  569. if (!this.url)
  570. return null;
  571. var channel = new MiniSoundChannel(this);
  572. channel.url = this.url;
  573. channel.loops = loops;
  574. channel.loop = (loops === 0 ? true : false);
  575. channel.startTime = startTime;
  576. channel.isStopped = false;
  577. Laya.SoundManager.addChannel(channel);
  578. return channel;
  579. }
  580. get duration() {
  581. return this._sound.duration;
  582. }
  583. dispose() {
  584. if (this._sound) {
  585. this._sound.destroy();
  586. this._sound = null;
  587. this.readyUrl = this.url = null;
  588. }
  589. }
  590. }
  591. MiniSound._id = 0;
  592. class MiniInput {
  593. constructor() {
  594. }
  595. static _createInputElement() {
  596. Laya.Input['_initInput'](Laya.Input['area'] = Laya.Browser.createElement("textarea"));
  597. Laya.Input['_initInput'](Laya.Input['input'] = Laya.Browser.createElement("input"));
  598. Laya.Input['inputContainer'] = Laya.Browser.createElement("div");
  599. Laya.Input['inputContainer'].style.position = "absolute";
  600. Laya.Input['inputContainer'].style.zIndex = 1E5;
  601. Laya.Browser.container.appendChild(Laya.Input['inputContainer']);
  602. Laya.Laya.stage.on("resize", null, MiniInput._onStageResize);
  603. MiniAdpter.window.wx.onWindowResize && MiniAdpter.window.wx.onWindowResize(function (res) {
  604. });
  605. Laya.SoundManager._soundClass = MiniSound;
  606. Laya.SoundManager._musicClass = MiniSound;
  607. var model = MiniAdpter.systemInfo.model;
  608. var system = MiniAdpter.systemInfo.system;
  609. if (model.indexOf("iPhone") != -1) {
  610. Laya.Browser.onIPhone = true;
  611. Laya.Browser.onIOS = true;
  612. Laya.Browser.onIPad = true;
  613. Laya.Browser.onAndroid = false;
  614. }
  615. if (system.indexOf("Android") != -1 || system.indexOf("Adr") != -1) {
  616. Laya.Browser.onAndroid = true;
  617. Laya.Browser.onIPhone = false;
  618. Laya.Browser.onIOS = false;
  619. Laya.Browser.onIPad = false;
  620. }
  621. }
  622. static _onStageResize() {
  623. var ts = Laya.Laya.stage._canvasTransform.identity();
  624. ts.scale((Laya.Browser.width / Laya.Render.canvas.width / Laya.Browser.pixelRatio), Laya.Browser.height / Laya.Render.canvas.height / Laya.Browser.pixelRatio);
  625. }
  626. static wxinputFocus(e) {
  627. var _inputTarget = Laya.Input['inputElement'].target;
  628. if (_inputTarget && !_inputTarget.editable) {
  629. return;
  630. }
  631. MiniAdpter.window.wx.offKeyboardConfirm();
  632. MiniAdpter.window.wx.offKeyboardInput();
  633. MiniAdpter.window.wx.showKeyboard({ defaultValue: _inputTarget.text, maxLength: _inputTarget.maxChars, multiple: _inputTarget.multiline, confirmHold: true, confirmType: _inputTarget["confirmType"] || 'done', success: function (res) {
  634. }, fail: function (res) {
  635. } });
  636. MiniAdpter.window.wx.onKeyboardConfirm(function (res) {
  637. var str = res ? res.value : "";
  638. if (_inputTarget._restrictPattern) {
  639. str = str.replace(/\u2006|\x27/g, "");
  640. if (_inputTarget._restrictPattern.test(str)) {
  641. str = str.replace(_inputTarget._restrictPattern, "");
  642. }
  643. }
  644. _inputTarget.text = str;
  645. _inputTarget.event(Laya.Event.INPUT);
  646. MiniInput.inputEnter();
  647. _inputTarget.event("confirm");
  648. });
  649. MiniAdpter.window.wx.onKeyboardInput(function (res) {
  650. var str = res ? res.value : "";
  651. if (!_inputTarget.multiline) {
  652. if (str.indexOf("\n") != -1) {
  653. MiniInput.inputEnter();
  654. return;
  655. }
  656. }
  657. if (_inputTarget._restrictPattern) {
  658. str = str.replace(/\u2006|\x27/g, "");
  659. if (_inputTarget._restrictPattern.test(str)) {
  660. str = str.replace(_inputTarget._restrictPattern, "");
  661. }
  662. }
  663. _inputTarget.text = str;
  664. _inputTarget.event(Laya.Event.INPUT);
  665. });
  666. }
  667. static inputEnter() {
  668. Laya.Input['inputElement'].target.focus = false;
  669. }
  670. static wxinputblur() {
  671. MiniInput.hideKeyboard();
  672. }
  673. static hideKeyboard() {
  674. MiniAdpter.window.wx.offKeyboardConfirm();
  675. MiniAdpter.window.wx.offKeyboardInput();
  676. MiniAdpter.window.wx.hideKeyboard({ success: function (res) {
  677. console.log('隐藏键盘');
  678. }, fail: function (res) {
  679. console.log("隐藏键盘出错:" + (res ? res.errMsg : ""));
  680. } });
  681. }
  682. }
  683. class MiniLoader extends Laya.EventDispatcher {
  684. constructor() {
  685. super();
  686. }
  687. _loadResourceFilter(type, url) {
  688. var thisLoader = this;
  689. this.sourceUrl = Laya.URL.formatURL(url);
  690. if (MiniFileMgr.isNetFile(url)) {
  691. if (MiniFileMgr.loadPath != "") {
  692. url = url.split(MiniFileMgr.loadPath)[1];
  693. }
  694. else {
  695. var tempStr = Laya.URL.rootPath != "" ? Laya.URL.rootPath : Laya.URL._basePath;
  696. var tempUrl = url;
  697. if (tempStr != "")
  698. url = url.split(tempStr)[1];
  699. if (!url) {
  700. url = tempUrl;
  701. }
  702. }
  703. }
  704. if (MiniAdpter.subNativeFiles && MiniAdpter.subNativeheads.length == 0) {
  705. for (var key in MiniAdpter.subNativeFiles) {
  706. var tempArr = MiniAdpter.subNativeFiles[key];
  707. MiniAdpter.subNativeheads = MiniAdpter.subNativeheads.concat(tempArr);
  708. for (var aa = 0; aa < tempArr.length; aa++) {
  709. MiniAdpter.subMaps[tempArr[aa]] = key + "/" + tempArr[aa];
  710. }
  711. }
  712. }
  713. if (MiniAdpter.subNativeFiles && url.indexOf("/") != -1) {
  714. var curfileHead = url.split("/")[0] + "/";
  715. if (curfileHead && MiniAdpter.subNativeheads.indexOf(curfileHead) != -1) {
  716. var newfileHead = MiniAdpter.subMaps[curfileHead];
  717. url = url.replace(curfileHead, newfileHead);
  718. }
  719. }
  720. switch (type) {
  721. case Laya.Loader.IMAGE:
  722. case "htmlimage":
  723. case "nativeimage":
  724. MiniLoader._transformImgUrl(url, type, thisLoader);
  725. break;
  726. case Laya.Loader.SOUND:
  727. thisLoader._loadSound(url);
  728. break;
  729. default:
  730. thisLoader._loadResource(type, url);
  731. }
  732. }
  733. _loadSound(url) {
  734. var thisLoader = this;
  735. if (!MiniAdpter.autoCacheFile) {
  736. MiniLoader.onDownLoadCallBack(url, thisLoader, 0);
  737. }
  738. else {
  739. var tempurl = Laya.URL.formatURL(url);
  740. if (!MiniFileMgr.isLocalNativeFile(url) && !MiniFileMgr.getFileInfo(tempurl)) {
  741. if (MiniFileMgr.isNetFile(tempurl)) {
  742. MiniFileMgr.downOtherFiles(tempurl, Laya.Handler.create(MiniLoader, MiniLoader.onDownLoadCallBack, [tempurl, thisLoader]), tempurl);
  743. }
  744. else {
  745. MiniLoader.onDownLoadCallBack(url, thisLoader, 0);
  746. }
  747. }
  748. else {
  749. MiniLoader.onDownLoadCallBack(url, thisLoader, 0);
  750. }
  751. }
  752. }
  753. static onDownLoadCallBack(sourceUrl, thisLoader, errorCode, tempFilePath = null) {
  754. if (!errorCode) {
  755. var fileNativeUrl;
  756. if (MiniAdpter.autoCacheFile) {
  757. if (!tempFilePath) {
  758. if (MiniFileMgr.isLocalNativeFile(sourceUrl)) {
  759. fileNativeUrl = sourceUrl;
  760. }
  761. else {
  762. var fileObj = MiniFileMgr.getFileInfo(sourceUrl);
  763. if (fileObj && fileObj.md5) {
  764. fileNativeUrl = fileObj.tempFilePath || MiniFileMgr.getFileNativePath(fileObj.md5);
  765. }
  766. else {
  767. fileNativeUrl = sourceUrl;
  768. }
  769. }
  770. }
  771. else {
  772. fileNativeUrl = tempFilePath;
  773. }
  774. }
  775. else {
  776. fileNativeUrl = Laya.URL.formatURL(sourceUrl);
  777. }
  778. sourceUrl = fileNativeUrl;
  779. var sound = (new Laya.SoundManager._soundClass());
  780. sound.load(sourceUrl);
  781. thisLoader.onLoaded(sound);
  782. }
  783. else {
  784. thisLoader.event(Laya.Event.ERROR, "Load sound failed");
  785. }
  786. }
  787. complete(data) {
  788. if (data instanceof Laya.Resource) {
  789. data._setCreateURL(this.sourceUrl);
  790. }
  791. else if ((data instanceof Laya.Texture) && (data.bitmap instanceof Laya.Resource)) {
  792. data.bitmap._setCreateURL(this.sourceUrl);
  793. }
  794. this.originComplete(data);
  795. }
  796. _loadHttpRequestWhat(url, contentType) {
  797. var thisLoader = this;
  798. var encoding = MiniAdpter.getUrlEncode(url, contentType);
  799. if (Laya.Loader.preLoadedMap[url])
  800. thisLoader.onLoaded(Laya.Loader.preLoadedMap[url]);
  801. else {
  802. var tempurl = Laya.URL.formatURL(url);
  803. if (!MiniAdpter.AutoCacheDownFile) {
  804. if (MiniFileMgr.isNetFile(tempurl)) {
  805. thisLoader._loadHttpRequest(tempurl, contentType, thisLoader, thisLoader.onLoaded, thisLoader, thisLoader.onProgress, thisLoader, thisLoader.onError);
  806. }
  807. else
  808. MiniFileMgr.readFile(url, encoding, new Laya.Handler(MiniLoader, MiniLoader.onReadNativeCallBack, [url, contentType, thisLoader]), url);
  809. }
  810. else {
  811. if (!MiniFileMgr.isLocalNativeFile(url) && !MiniFileMgr.getFileInfo(tempurl)) {
  812. if (MiniFileMgr.isNetFile(tempurl)) {
  813. MiniFileMgr.downFiles(tempurl, encoding, new Laya.Handler(MiniLoader, MiniLoader.onReadNativeCallBack, [url, contentType, thisLoader]), tempurl, true);
  814. }
  815. else {
  816. MiniFileMgr.readFile(url, encoding, new Laya.Handler(MiniLoader, MiniLoader.onReadNativeCallBack, [url, contentType, thisLoader]), url);
  817. }
  818. }
  819. else {
  820. var tempUrl = url;
  821. var fileObj = MiniFileMgr.getFileInfo(tempurl);
  822. if (fileObj && fileObj.md5) {
  823. tempUrl = fileObj.tempFilePath || MiniFileMgr.getFileNativePath(fileObj.md5);
  824. }
  825. MiniFileMgr.readFile(tempUrl, encoding, new Laya.Handler(MiniLoader, MiniLoader.onReadNativeCallBack, [url, contentType, thisLoader]), url);
  826. }
  827. }
  828. }
  829. }
  830. static onReadNativeCallBack(url, type = null, thisLoader = null, errorCode = 0, data = null) {
  831. if (!errorCode) {
  832. var tempData;
  833. if (type == Laya.Loader.JSON || type == Laya.Loader.ATLAS || type == Laya.Loader.PREFAB || type == Laya.Loader.PLF) {
  834. tempData = MiniAdpter.getJson(data.data);
  835. }
  836. else if (type == Laya.Loader.XML) {
  837. tempData = Laya.Utils.parseXMLFromString(data.data);
  838. }
  839. else {
  840. tempData = data.data;
  841. }
  842. if (!MiniAdpter.isZiYu && MiniAdpter.isPosMsgYu && type != Laya.Loader.BUFFER) {
  843. MiniAdpter.window.wx.postMessage({ url: url, data: tempData, isLoad: "filedata" });
  844. }
  845. thisLoader.onLoaded(tempData);
  846. }
  847. else if (errorCode == 1) {
  848. thisLoader._loadHttpRequest(url, type, thisLoader, thisLoader.onLoaded, thisLoader, thisLoader.onProgress, thisLoader, thisLoader.onError);
  849. }
  850. }
  851. static _transformImgUrl(url, type, thisLoader) {
  852. if (MiniAdpter.isZiYu || MiniFileMgr.isLocalNativeFile(url)) {
  853. thisLoader._loadImage(url, false);
  854. return;
  855. }
  856. if (!MiniAdpter.autoCacheFile) {
  857. thisLoader._loadImage(url);
  858. }
  859. else {
  860. var tempUrl = Laya.URL.formatURL(url);
  861. if (!MiniFileMgr.isLocalNativeFile(url) && !MiniFileMgr.getFileInfo(tempUrl)) {
  862. if (MiniFileMgr.isNetFile(tempUrl)) {
  863. MiniFileMgr.downOtherFiles(tempUrl, new Laya.Handler(MiniLoader, MiniLoader.onDownImgCallBack, [url, thisLoader]), tempUrl);
  864. }
  865. else {
  866. MiniLoader.onCreateImage(url, thisLoader, true);
  867. }
  868. }
  869. else {
  870. MiniLoader.onCreateImage(url, thisLoader);
  871. }
  872. }
  873. }
  874. static onDownImgCallBack(sourceUrl, thisLoader, errorCode, tempFilePath = "") {
  875. if (!errorCode)
  876. MiniLoader.onCreateImage(sourceUrl, thisLoader, false, tempFilePath);
  877. else {
  878. thisLoader.onError(null);
  879. }
  880. }
  881. static onCreateImage(sourceUrl, thisLoader, isLocal = false, tempFilePath = "") {
  882. var fileNativeUrl;
  883. if (MiniAdpter.autoCacheFile) {
  884. if (!isLocal) {
  885. if (tempFilePath != "") {
  886. fileNativeUrl = tempFilePath;
  887. }
  888. else {
  889. var fileObj = MiniFileMgr.getFileInfo(Laya.URL.formatURL(sourceUrl));
  890. fileNativeUrl = fileObj.tempFilePath || MiniFileMgr.getFileNativePath(fileObj.md5);
  891. }
  892. }
  893. else if (MiniAdpter.isZiYu) {
  894. var tempUrl = Laya.URL.formatURL(sourceUrl);
  895. if (MiniFileMgr.ziyuFileTextureData[tempUrl]) {
  896. fileNativeUrl = MiniFileMgr.ziyuFileTextureData[tempUrl];
  897. }
  898. else
  899. fileNativeUrl = sourceUrl;
  900. }
  901. else
  902. fileNativeUrl = sourceUrl;
  903. }
  904. else {
  905. if (!isLocal)
  906. fileNativeUrl = tempFilePath;
  907. else
  908. fileNativeUrl = sourceUrl;
  909. }
  910. thisLoader._loadImage(fileNativeUrl, false);
  911. }
  912. }
  913. class MiniLocalStorage {
  914. constructor() {
  915. }
  916. static __init__() {
  917. MiniLocalStorage.items = MiniLocalStorage;
  918. }
  919. static setItem(key, value) {
  920. try {
  921. MiniAdpter.window.wx.setStorageSync(key, value);
  922. }
  923. catch (error) {
  924. MiniAdpter.window.wx.setStorage({
  925. key: key,
  926. data: value
  927. });
  928. }
  929. }
  930. static getItem(key) {
  931. return MiniAdpter.window.wx.getStorageSync(key);
  932. }
  933. static setJSON(key, value) {
  934. MiniLocalStorage.setItem(key, value);
  935. }
  936. static getJSON(key) {
  937. return MiniLocalStorage.getItem(key);
  938. }
  939. static removeItem(key) {
  940. MiniAdpter.window.wx.removeStorageSync(key);
  941. }
  942. static clear() {
  943. MiniAdpter.window.wx.clearStorageSync();
  944. }
  945. static getStorageInfoSync() {
  946. try {
  947. var res = MiniAdpter.window.wx.getStorageInfoSync();
  948. console.log(res.keys);
  949. console.log(res.currentSize);
  950. console.log(res.limitSize);
  951. return res;
  952. }
  953. catch (e) {
  954. }
  955. return null;
  956. }
  957. }
  958. MiniLocalStorage.support = true;
  959. class MiniAdpter {
  960. static getJson(data) {
  961. return JSON.parse(data);
  962. }
  963. static enable() {
  964. MiniAdpter.init(Laya.Laya.isWXPosMsg, Laya.Laya.isWXOpenDataContext);
  965. }
  966. static init(isPosMsg = false, isSon = false) {
  967. if (MiniAdpter._inited)
  968. return;
  969. MiniAdpter._inited = true;
  970. MiniAdpter.window = window;
  971. if (!MiniAdpter.window.hasOwnProperty("wx"))
  972. return;
  973. if (MiniAdpter.window.navigator.userAgent.indexOf('MiniGame') < 0)
  974. return;
  975. MiniAdpter.isZiYu = isSon;
  976. MiniAdpter.isPosMsgYu = isPosMsg;
  977. MiniAdpter.EnvConfig = {};
  978. if (!MiniAdpter.isZiYu) {
  979. MiniFileMgr.setNativeFileDir("/layaairGame");
  980. MiniFileMgr.getCacheList(MiniFileMgr.fileNativeDir, Laya.Handler.create(MiniAdpter, MiniAdpter.onMkdirCallBack));
  981. }
  982. MiniAdpter.systemInfo = MiniAdpter.window.wx.getSystemInfoSync();
  983. MiniAdpter.window.focus = function () {
  984. };
  985. Laya.Laya['_getUrlPath'] = function () {
  986. return "";
  987. };
  988. MiniAdpter.window.logtime = function (str) {
  989. };
  990. MiniAdpter.window.alertTimeLog = function (str) {
  991. };
  992. MiniAdpter.window.resetShareInfo = function () {
  993. };
  994. MiniAdpter.window.CanvasRenderingContext2D = function () {
  995. };
  996. MiniAdpter.window.CanvasRenderingContext2D.prototype = MiniAdpter.window.wx.createCanvas().getContext('2d').__proto__;
  997. MiniAdpter.window.document.body.appendChild = function () {
  998. };
  999. MiniAdpter.EnvConfig.pixelRatioInt = 0;
  1000. Laya.Browser["_pixelRatio"] = MiniAdpter.pixelRatio();
  1001. MiniAdpter._preCreateElement = Laya.Browser.createElement;
  1002. Laya.Browser["createElement"] = MiniAdpter.createElement;
  1003. Laya.RunDriver.createShaderCondition = MiniAdpter.createShaderCondition;
  1004. Laya.Utils['parseXMLFromString'] = MiniAdpter.parseXMLFromString;
  1005. Laya.Input['_createInputElement'] = MiniInput['_createInputElement'];
  1006. Laya.Loader.prototype._loadResourceFilter = MiniLoader.prototype._loadResourceFilter;
  1007. Laya.Loader.prototype.originComplete = Laya.Loader.prototype.complete;
  1008. Laya.Loader.prototype.complete = MiniLoader.prototype.complete;
  1009. Laya.Loader.prototype._loadSound = MiniLoader.prototype._loadSound;
  1010. Laya.Loader.prototype._loadHttpRequestWhat = MiniLoader.prototype._loadHttpRequestWhat;
  1011. Laya.LocalStorage._baseClass = MiniLocalStorage;
  1012. MiniLocalStorage.__init__();
  1013. MiniAdpter.window.wx.onMessage && MiniAdpter.window.wx.onMessage(MiniAdpter._onMessage);
  1014. }
  1015. static _onMessage(data) {
  1016. switch (data.type) {
  1017. case "changeMatrix":
  1018. Laya.Laya.stage.transform.identity();
  1019. Laya.Laya.stage._width = data.w;
  1020. Laya.Laya.stage._height = data.h;
  1021. Laya.Laya.stage._canvasTransform = new Laya.Matrix(data.a, data.b, data.c, data.d, data.tx, data.ty);
  1022. break;
  1023. case "display":
  1024. Laya.Laya.stage.frameRate = data.rate || Laya.Stage.FRAME_FAST;
  1025. break;
  1026. case "undisplay":
  1027. Laya.Laya.stage.frameRate = Laya.Stage.FRAME_SLEEP;
  1028. break;
  1029. }
  1030. if (data['isLoad'] == "opendatacontext") {
  1031. if (data.url) {
  1032. MiniFileMgr.ziyuFileData[data.url] = data.atlasdata;
  1033. MiniFileMgr.ziyuFileTextureData[data.imgReadyUrl] = data.imgNativeUrl;
  1034. }
  1035. }
  1036. else if (data['isLoad'] == "openJsondatacontext") {
  1037. if (data.url) {
  1038. MiniFileMgr.ziyuFileData[data.url] = data.atlasdata;
  1039. }
  1040. }
  1041. else if (data['isLoad'] == "openJsondatacontextPic") {
  1042. MiniFileMgr.ziyuFileTextureData[data.imgReadyUrl] = data.imgNativeUrl;
  1043. }
  1044. }
  1045. static getUrlEncode(url, type) {
  1046. if (type == "arraybuffer")
  1047. return "";
  1048. return "utf8";
  1049. }
  1050. static downLoadFile(fileUrl, fileType = "", callBack = null, encoding = "utf8") {
  1051. var fileObj = MiniFileMgr.getFileInfo(fileUrl);
  1052. if (!fileObj)
  1053. MiniFileMgr.downLoadFile(fileUrl, fileType, callBack, encoding);
  1054. else {
  1055. callBack != null && callBack.runWith([0]);
  1056. }
  1057. }
  1058. static remove(fileUrl, callBack = null) {
  1059. MiniFileMgr.deleteFile("", fileUrl, callBack, "", 0);
  1060. }
  1061. static removeAll() {
  1062. MiniFileMgr.deleteAll();
  1063. }
  1064. static hasNativeFile(fileUrl) {
  1065. return MiniFileMgr.isLocalNativeFile(fileUrl);
  1066. }
  1067. static getFileInfo(fileUrl) {
  1068. return MiniFileMgr.getFileInfo(fileUrl);
  1069. }
  1070. static getFileList() {
  1071. return MiniFileMgr.filesListObj;
  1072. }
  1073. static exitMiniProgram() {
  1074. MiniAdpter.window["wx"].exitMiniProgram();
  1075. }
  1076. static onMkdirCallBack(errorCode, data) {
  1077. if (!errorCode) {
  1078. MiniFileMgr.filesListObj = JSON.parse(data.data);
  1079. MiniFileMgr.fakeObj = JSON.parse(data.data);
  1080. }
  1081. else {
  1082. MiniFileMgr.fakeObj = {};
  1083. MiniFileMgr.filesListObj = {};
  1084. }
  1085. let files = MiniFileMgr.fs.readdirSync(MiniFileMgr.fileNativeDir);
  1086. if (!files.length)
  1087. return;
  1088. var tempMd5ListObj = {};
  1089. var fileObj;
  1090. for (let key in MiniFileMgr.filesListObj) {
  1091. if (key != "fileUsedSize") {
  1092. fileObj = MiniFileMgr.filesListObj[key];
  1093. tempMd5ListObj[fileObj.md5] = fileObj.readyUrl;
  1094. }
  1095. }
  1096. var fileName;
  1097. for (let i = 0, sz = files.length; i < sz; i++) {
  1098. fileName = files[i];
  1099. if (fileName == MiniFileMgr.fileListName)
  1100. continue;
  1101. if (!tempMd5ListObj[fileName]) {
  1102. let deleteFileUrl = MiniFileMgr.getFileNativePath(fileName);
  1103. MiniFileMgr.fs.unlink({
  1104. filePath: deleteFileUrl,
  1105. success: function (data) {
  1106. console.log("删除无引用的磁盘文件:" + fileName);
  1107. }
  1108. });
  1109. }
  1110. delete tempMd5ListObj[fileName];
  1111. }
  1112. for (let key in tempMd5ListObj) {
  1113. delete MiniFileMgr.filesListObj[tempMd5ListObj[key]];
  1114. delete MiniFileMgr.fakeObj[tempMd5ListObj[key]];
  1115. console.log("删除错误记录:", tempMd5ListObj[key]);
  1116. }
  1117. }
  1118. static pixelRatio() {
  1119. if (!MiniAdpter.EnvConfig.pixelRatioInt) {
  1120. try {
  1121. MiniAdpter.EnvConfig.pixelRatioInt = MiniAdpter.systemInfo.pixelRatio;
  1122. return MiniAdpter.systemInfo.pixelRatio;
  1123. }
  1124. catch (error) {
  1125. }
  1126. }
  1127. return MiniAdpter.EnvConfig.pixelRatioInt;
  1128. }
  1129. static createElement(type) {
  1130. if (type == "canvas") {
  1131. var _source;
  1132. if (MiniAdpter.idx == 1) {
  1133. if (MiniAdpter.isZiYu) {
  1134. _source = MiniAdpter.window.sharedCanvas;
  1135. _source.style = {};
  1136. }
  1137. else {
  1138. _source = MiniAdpter.window.canvas;
  1139. }
  1140. }
  1141. else {
  1142. _source = MiniAdpter.window.wx.createCanvas();
  1143. }
  1144. MiniAdpter.idx++;
  1145. return _source;
  1146. }
  1147. else if (type == "textarea" || type == "input") {
  1148. return MiniAdpter.onCreateInput(type);
  1149. }
  1150. else if (type == "div") {
  1151. var node = MiniAdpter._preCreateElement(type);
  1152. node.contains = function (value) {
  1153. return null;
  1154. };
  1155. node.removeChild = function (value) {
  1156. };
  1157. return node;
  1158. }
  1159. else {
  1160. return MiniAdpter._preCreateElement(type);
  1161. }
  1162. }
  1163. static onCreateInput(type) {
  1164. var node = MiniAdpter._preCreateElement(type);
  1165. node.focus = MiniInput.wxinputFocus;
  1166. node.blur = MiniInput.wxinputblur;
  1167. node.style = {};
  1168. node.value = 0;
  1169. node.parentElement = {};
  1170. node.placeholder = {};
  1171. node.type = {};
  1172. node.setColor = function (value) {
  1173. };
  1174. node.setType = function (value) {
  1175. };
  1176. node.setFontFace = function (value) {
  1177. };
  1178. node.addEventListener = function (value) {
  1179. };
  1180. node.contains = function (value) {
  1181. return null;
  1182. };
  1183. node.removeChild = function (value) {
  1184. };
  1185. return node;
  1186. }
  1187. static createShaderCondition(conditionScript) {
  1188. var func = function () {
  1189. return this[conditionScript.replace("this.", "")];
  1190. };
  1191. return func;
  1192. }
  1193. static sendAtlasToOpenDataContext(url) {
  1194. if (!MiniAdpter.isZiYu) {
  1195. var atlasJson = Laya.Loader.getRes(Laya.URL.formatURL(url));
  1196. if (atlasJson) {
  1197. var textureArr = atlasJson.meta.image.split(",");
  1198. if (atlasJson.meta && atlasJson.meta.image) {
  1199. var toloadPics = atlasJson.meta.image.split(",");
  1200. var split = url.indexOf("/") >= 0 ? "/" : "\\";
  1201. var idx = url.lastIndexOf(split);
  1202. var folderPath = idx >= 0 ? url.substr(0, idx + 1) : "";
  1203. for (var i = 0, len = toloadPics.length; i < len; i++) {
  1204. toloadPics[i] = folderPath + toloadPics[i];
  1205. }
  1206. }
  1207. else {
  1208. toloadPics = [url.replace(".json", ".png")];
  1209. }
  1210. for (i = 0; i < toloadPics.length; i++) {
  1211. var tempAtlasPngUrl = toloadPics[i];
  1212. MiniAdpter.postInfoToContext(Laya.Laya.URL.formatURL(url), Laya.Laya.URL.formatURL(tempAtlasPngUrl), atlasJson);
  1213. }
  1214. }
  1215. else {
  1216. throw "传递的url没有获取到对应的图集数据信息,请确保图集已经过!";
  1217. }
  1218. }
  1219. }
  1220. static postInfoToContext(url, atlaspngUrl, atlasJson) {
  1221. var postData = { "frames": atlasJson.frames, "meta": atlasJson.meta };
  1222. var textureUrl = atlaspngUrl;
  1223. var fileObj = MiniFileMgr.getFileInfo(Laya.URL.formatURL(atlaspngUrl));
  1224. if (fileObj) {
  1225. var fileNativeUrl = fileObj.tempFilePath || MiniFileMgr.getFileNativePath(fileObj.md5);
  1226. }
  1227. else {
  1228. fileNativeUrl = textureUrl;
  1229. }
  1230. if (fileNativeUrl) {
  1231. MiniAdpter.window.wx.postMessage({ url: url, atlasdata: postData, imgNativeUrl: fileNativeUrl, imgReadyUrl: textureUrl, isLoad: "opendatacontext" });
  1232. }
  1233. else {
  1234. throw "获取图集的磁盘url路径不存在!";
  1235. }
  1236. }
  1237. static sendSinglePicToOpenDataContext(url) {
  1238. var tempTextureUrl = Laya.URL.formatURL(url);
  1239. var fileObj = MiniFileMgr.getFileInfo(tempTextureUrl);
  1240. if (fileObj) {
  1241. var fileNativeUrl = fileObj.tempFilePath || MiniFileMgr.getFileNativePath(fileObj.md5);
  1242. url = tempTextureUrl;
  1243. }
  1244. else {
  1245. fileNativeUrl = url;
  1246. }
  1247. if (fileNativeUrl) {
  1248. url = Laya.Laya.URL.formatURL(url);
  1249. MiniAdpter.window.wx.postMessage({ url: url, imgNativeUrl: fileNativeUrl, imgReadyUrl: url, isLoad: "openJsondatacontextPic" });
  1250. }
  1251. else {
  1252. throw "获取图集的磁盘url路径不存在!";
  1253. }
  1254. }
  1255. static sendJsonDataToDataContext(url) {
  1256. if (!MiniAdpter.isZiYu) {
  1257. url = Laya.Laya.URL.formatURL(url);
  1258. var atlasJson = Laya.Loader.getRes(url);
  1259. if (atlasJson) {
  1260. MiniAdpter.window.wx.postMessage({ url: url, atlasdata: atlasJson, isLoad: "openJsondatacontext" });
  1261. }
  1262. else {
  1263. throw "传递的url没有获取到对应的图集数据信息,请确保图集已经过!";
  1264. }
  1265. }
  1266. }
  1267. }
  1268. MiniAdpter._inited = false;
  1269. MiniAdpter.autoCacheFile = true;
  1270. MiniAdpter.minClearSize = (5 * 1024 * 1024);
  1271. MiniAdpter.sizeLimit = (200 * 1024 * 1024);
  1272. MiniAdpter.nativefiles = ["layaNativeDir", "wxlocal"];
  1273. MiniAdpter.subNativeFiles = [];
  1274. MiniAdpter.subNativeheads = [];
  1275. MiniAdpter.subMaps = [];
  1276. MiniAdpter.AutoCacheDownFile = false;
  1277. MiniAdpter.parseXMLFromString = function (value) {
  1278. var rst;
  1279. value = value.replace(/>\s+</g, '><');
  1280. try {
  1281. rst = (new MiniAdpter.window.Parser.DOMParser()).parseFromString(value, 'text/xml');
  1282. }
  1283. catch (error) {
  1284. throw "需要引入xml解析库文件";
  1285. }
  1286. return rst;
  1287. };
  1288. MiniAdpter.idx = 1;
  1289. class MiniAccelerator extends Laya.EventDispatcher {
  1290. constructor() {
  1291. super();
  1292. }
  1293. static __init__() {
  1294. try {
  1295. var Acc;
  1296. Acc = Laya.Accelerator;
  1297. if (!Acc)
  1298. return;
  1299. Acc["prototype"]["on"] = MiniAccelerator["prototype"]["on"];
  1300. Acc["prototype"]["off"] = MiniAccelerator["prototype"]["off"];
  1301. }
  1302. catch (e) {
  1303. }
  1304. }
  1305. static startListen(callBack) {
  1306. MiniAccelerator._callBack = callBack;
  1307. if (MiniAccelerator._isListening)
  1308. return;
  1309. MiniAccelerator._isListening = true;
  1310. try {
  1311. MiniAdpter.window.wx.onAccelerometerChange(MiniAccelerator.onAccelerometerChange);
  1312. }
  1313. catch (e) { }
  1314. }
  1315. static stopListen() {
  1316. MiniAccelerator._isListening = false;
  1317. try {
  1318. MiniAdpter.window.wx.stopAccelerometer({});
  1319. }
  1320. catch (e) { }
  1321. }
  1322. static onAccelerometerChange(res) {
  1323. var e;
  1324. e = {};
  1325. e.acceleration = res;
  1326. e.accelerationIncludingGravity = res;
  1327. e.rotationRate = {};
  1328. if (MiniAccelerator._callBack != null) {
  1329. MiniAccelerator._callBack(e);
  1330. }
  1331. }
  1332. on(type, caller, listener, args = null) {
  1333. super.on(type, caller, listener, args);
  1334. MiniAccelerator.startListen(this["onDeviceOrientationChange"]);
  1335. return this;
  1336. }
  1337. off(type, caller, listener, onceOnly = false) {
  1338. if (!this.hasListener(type))
  1339. MiniAccelerator.stopListen();
  1340. return super.off(type, caller, listener, onceOnly);
  1341. }
  1342. }
  1343. MiniAccelerator._isListening = false;
  1344. class MiniLocation {
  1345. constructor() {
  1346. }
  1347. static __init__() {
  1348. MiniAdpter.window.navigator.geolocation.getCurrentPosition = MiniLocation.getCurrentPosition;
  1349. MiniAdpter.window.navigator.geolocation.watchPosition = MiniLocation.watchPosition;
  1350. MiniAdpter.window.navigator.geolocation.clearWatch = MiniLocation.clearWatch;
  1351. }
  1352. static getCurrentPosition(success = null, error = null, options = null) {
  1353. var paramO;
  1354. paramO = {};
  1355. paramO.success = getSuccess;
  1356. paramO.fail = error;
  1357. MiniAdpter.window.wx.getLocation(paramO);
  1358. function getSuccess(res) {
  1359. if (success != null) {
  1360. success(res);
  1361. }
  1362. }
  1363. }
  1364. static watchPosition(success = null, error = null, options = null) {
  1365. MiniLocation._curID++;
  1366. var curWatchO;
  1367. curWatchO = {};
  1368. curWatchO.success = success;
  1369. curWatchO.error = error;
  1370. MiniLocation._watchDic[MiniLocation._curID] = curWatchO;
  1371. Laya.Laya.systemTimer.loop(1000, null, MiniLocation._myLoop);
  1372. return MiniLocation._curID;
  1373. }
  1374. static clearWatch(id) {
  1375. delete MiniLocation._watchDic[id];
  1376. if (!MiniLocation._hasWatch()) {
  1377. Laya.Laya.systemTimer.clear(null, MiniLocation._myLoop);
  1378. }
  1379. }
  1380. static _hasWatch() {
  1381. var key;
  1382. for (key in MiniLocation._watchDic) {
  1383. if (MiniLocation._watchDic[key])
  1384. return true;
  1385. }
  1386. return false;
  1387. }
  1388. static _myLoop() {
  1389. MiniLocation.getCurrentPosition(MiniLocation._mySuccess, MiniLocation._myError);
  1390. }
  1391. static _mySuccess(res) {
  1392. var rst = {};
  1393. rst.coords = res;
  1394. rst.timestamp = Laya.Browser.now();
  1395. var key;
  1396. for (key in MiniLocation._watchDic) {
  1397. if (MiniLocation._watchDic[key].success) {
  1398. MiniLocation._watchDic[key].success(rst);
  1399. }
  1400. }
  1401. }
  1402. static _myError(res) {
  1403. var key;
  1404. for (key in MiniLocation._watchDic) {
  1405. if (MiniLocation._watchDic[key].error) {
  1406. MiniLocation._watchDic[key].error(res);
  1407. }
  1408. }
  1409. }
  1410. }
  1411. MiniLocation._watchDic = {};
  1412. MiniLocation._curID = 0;
  1413. class MiniVideo {
  1414. constructor(width = 320, height = 240) {
  1415. this.videoend = false;
  1416. this.videourl = "";
  1417. this.videoElement = MiniAdpter.window.wx.createVideo({ width: width, height: height, autoplay: true });
  1418. }
  1419. static __init__() {
  1420. }
  1421. on(eventType, ths, callBack) {
  1422. if (eventType == "loadedmetadata") {
  1423. this.onPlayFunc = callBack.bind(ths);
  1424. this.videoElement.onPlay = this.onPlayFunction.bind(this);
  1425. }
  1426. else if (eventType == "ended") {
  1427. this.onEndedFunC = callBack.bind(ths);
  1428. this.videoElement.onEnded = this.onEndedFunction.bind(this);
  1429. }
  1430. this.videoElement.onTimeUpdate = this.onTimeUpdateFunc.bind(this);
  1431. }
  1432. onTimeUpdateFunc(data) {
  1433. this.position = data.position;
  1434. this._duration = data.duration;
  1435. }
  1436. get duration() {
  1437. return this._duration;
  1438. }
  1439. onPlayFunction() {
  1440. if (this.videoElement)
  1441. this.videoElement.readyState = 200;
  1442. console.log("=====视频加载完成========");
  1443. this.onPlayFunc != null && this.onPlayFunc();
  1444. }
  1445. onEndedFunction() {
  1446. if (!this.videoElement)
  1447. return;
  1448. this.videoend = true;
  1449. console.log("=====视频播放完毕========");
  1450. this.onEndedFunC != null && this.onEndedFunC();
  1451. }
  1452. off(eventType, ths, callBack) {
  1453. if (eventType == "loadedmetadata") {
  1454. this.onPlayFunc = callBack.bind(ths);
  1455. this.videoElement.offPlay = this.onPlayFunction.bind(this);
  1456. }
  1457. else if (eventType == "ended") {
  1458. this.onEndedFunC = callBack.bind(ths);
  1459. this.videoElement.offEnded = this.onEndedFunction.bind(this);
  1460. }
  1461. }
  1462. load(url) {
  1463. if (!this.videoElement)
  1464. return;
  1465. this.videoElement.src = url;
  1466. }
  1467. play() {
  1468. if (!this.videoElement)
  1469. return;
  1470. this.videoend = false;
  1471. this.videoElement.play();
  1472. }
  1473. pause() {
  1474. if (!this.videoElement)
  1475. return;
  1476. this.videoend = true;
  1477. this.videoElement.pause();
  1478. }
  1479. get currentTime() {
  1480. if (!this.videoElement)
  1481. return 0;
  1482. return this.videoElement.initialTime;
  1483. }
  1484. set currentTime(value) {
  1485. if (!this.videoElement)
  1486. return;
  1487. this.videoElement.initialTime = value;
  1488. }
  1489. get videoWidth() {
  1490. if (!this.videoElement)
  1491. return 0;
  1492. return this.videoElement.width;
  1493. }
  1494. get videoHeight() {
  1495. if (!this.videoElement)
  1496. return 0;
  1497. return this.videoElement.height;
  1498. }
  1499. get ended() {
  1500. return this.videoend;
  1501. }
  1502. get loop() {
  1503. if (!this.videoElement)
  1504. return false;
  1505. return this.videoElement.loop;
  1506. }
  1507. set loop(value) {
  1508. if (!this.videoElement)
  1509. return;
  1510. this.videoElement.loop = value;
  1511. }
  1512. get playbackRate() {
  1513. if (!this.videoElement)
  1514. return 0;
  1515. return this.videoElement.playbackRate;
  1516. }
  1517. set playbackRate(value) {
  1518. if (!this.videoElement)
  1519. return;
  1520. this.videoElement.playbackRate = value;
  1521. }
  1522. get muted() {
  1523. if (!this.videoElement)
  1524. return false;
  1525. return this.videoElement.muted;
  1526. }
  1527. set muted(value) {
  1528. if (!this.videoElement)
  1529. return;
  1530. this.videoElement.muted = value;
  1531. }
  1532. get paused() {
  1533. if (!this.videoElement)
  1534. return false;
  1535. return this.videoElement.paused;
  1536. }
  1537. size(width, height) {
  1538. if (!this.videoElement)
  1539. return;
  1540. this.videoElement.width = width;
  1541. this.videoElement.height = height;
  1542. }
  1543. get x() {
  1544. if (!this.videoElement)
  1545. return 0;
  1546. return this.videoElement.x;
  1547. }
  1548. set x(value) {
  1549. if (!this.videoElement)
  1550. return;
  1551. this.videoElement.x = value;
  1552. }
  1553. get y() {
  1554. if (!this.videoElement)
  1555. return 0;
  1556. return this.videoElement.y;
  1557. }
  1558. set y(value) {
  1559. if (!this.videoElement)
  1560. return;
  1561. this.videoElement.y = value;
  1562. }
  1563. get currentSrc() {
  1564. return this.videoElement.src;
  1565. }
  1566. destroy() {
  1567. if (this.videoElement)
  1568. this.videoElement.destroy();
  1569. this.videoElement = null;
  1570. this.onEndedFunC = null;
  1571. this.onPlayFunc = null;
  1572. this.videoend = false;
  1573. this.videourl = null;
  1574. }
  1575. reload() {
  1576. if (!this.videoElement)
  1577. return;
  1578. this.videoElement.src = this.videourl;
  1579. }
  1580. }
  1581. exports.MiniAccelerator = MiniAccelerator;
  1582. exports.MiniAdpter = MiniAdpter;
  1583. exports.MiniFileMgr = MiniFileMgr;
  1584. exports.MiniInput = MiniInput;
  1585. exports.MiniLoader = MiniLoader;
  1586. exports.MiniLocalStorage = MiniLocalStorage;
  1587. exports.MiniLocation = MiniLocation;
  1588. exports.MiniSound = MiniSound;
  1589. exports.MiniSoundChannel = MiniSoundChannel;
  1590. exports.MiniVideo = MiniVideo;
  1591. }