laya.Alipaymini.js 69 KB

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