laya.qqmini.js 69 KB

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