laya.Alipaymini.js 69 KB

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