laya.wxmini.js 62 KB

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