ttGame.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. /**
  2. * 广告
  3. */
  4. var ttGame;
  5. /**
  6. * 广告
  7. */
  8. (function (ttGame) {
  9. class BaseADManager {
  10. constructor(bannerAdID, rewardedAdID, interstitialAdID) {
  11. this.STATE_LOADED = 1; //1(01)加载完成
  12. this.STATE_SHOW = 2; //2(10)显示
  13. this.STATE_EXPOSURE = 3; //3(11)曝光(加载完成并显示)
  14. this._ids = { bannerAdID, rewardedAdID, interstitialAdID };
  15. this._adState = { bannerAdState: 0, rewardedAdState: 0, interstitialAdState: 0 };
  16. this.createBannerAd(bannerAdID);
  17. this.createRewardedVideoAd(rewardedAdID);
  18. this.createInterstitialAd(interstitialAdID);
  19. }
  20. //↓↓ Banner广告 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
  21. /** 创建Banner广告 */
  22. createBannerAd(bannerAdID) {
  23. if (!tt.createBannerAd)
  24. return;
  25. let sysInfo = platform.systemInfo.info;
  26. // 创建 Banner 广告实例,提前初始化
  27. let ad = tt.createBannerAd({
  28. adUnitId: bannerAdID,
  29. adIntervals: 30,
  30. style: {
  31. left: 0,
  32. top: sysInfo.windowHeight,
  33. width: this._bannerAdWidth ? this._bannerAdWidth : sysInfo.windowWidth
  34. // height:90//字节广告只有设置宽度,无高度设置
  35. }
  36. });
  37. if (!ad)
  38. return;
  39. ad.onResize(this.onBannerResize.bind(this));
  40. ad.onError(this.onBannerError.bind(this));
  41. ad.onLoad(this.onBannerLoaded.bind(this));
  42. this._bannerAd = ad;
  43. }
  44. onBannerResize(size) {
  45. log("Banner size:", size);
  46. let sysInfo = platform.systemInfo.info;
  47. this._bannerAd.style.left = (sysInfo.windowWidth - size.width) >> 1; //居中
  48. this._bannerAd.style.top = sysInfo.windowHeight - size.height; //底部
  49. }
  50. /** banner报错 */
  51. onBannerError(err) {
  52. log("Banner广告报错:", err);
  53. }
  54. /** banner加载完成 */
  55. onBannerLoaded() {
  56. // log("Banner onLoad:", windowHeight, ad.style.height)
  57. // ad.style.top = windowHeight - ad.style.height;//y坐标,按广告真实高度定
  58. this.addBannerState(this.STATE_LOADED);
  59. core.Manager.wtsdk.sendAdLog(this._ids.bannerAdID, core.AdLogType.REQUEST);
  60. }
  61. /** 添加状态(加载完或显示),并查看是否曝光(加载完且显示) */
  62. addBannerState(state) {
  63. this._adState.bannerAdState |= state;
  64. if (this._adState.bannerAdState == this.STATE_EXPOSURE) {
  65. core.Manager.wtsdk.sendAdLog(this._ids.bannerAdID, core.AdLogType.EXPOSURE);
  66. }
  67. }
  68. /** 显示 banner 广告 */
  69. showBanner() {
  70. if (this._bannerAd) {
  71. this._bannerAd.show();
  72. this.addBannerState(this.STATE_SHOW);
  73. }
  74. }
  75. /** 隐藏 banner 广告 */
  76. hideBanner() {
  77. if (this._bannerAd)
  78. this._bannerAd.hide();
  79. this._adState.bannerAdState = 0; //重置状态
  80. }
  81. //↑↑ Banner广告 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
  82. //↓↓ 激励视频广告 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
  83. /** 创建激励视频广告 */
  84. createRewardedVideoAd(rewardedAdID) {
  85. if (!tt.createRewardedVideoAd)
  86. return;
  87. this._rewardedVideoAd = tt.createRewardedVideoAd({ adUnitId: rewardedAdID });
  88. log("创建激励视频广告", this._rewardedVideoAd);
  89. if (!this._rewardedVideoAd)
  90. return;
  91. this._rewardedVideoAd.onError(this.onRewardedVideoAdError.bind(this));
  92. this._rewardedVideoAd.onClose(this.onRewardedVideoAdClosed.bind(this));
  93. this._rewardedVideoAd.onLoad(this.onRewardedVideoAdLoaded.bind(this));
  94. }
  95. /** 激励视频广告报错 */
  96. onRewardedVideoAdError(err) {
  97. log("激励视频出错", err);
  98. core.Manager.eDispatcher.event(core.Event.REWARDED_AD_ERROR, err);
  99. }
  100. /** 激励视频广告关闭 */
  101. onRewardedVideoAdClosed(res) {
  102. log("激励视频关闭", res);
  103. this._adState.rewardedAdState = 0; //重置状态
  104. if (res && res.isEnded)
  105. core.Manager.wtsdk.sendAdLog(this._ids.interstitialAdID, core.AdLogType.FINISH); //完成激励视频观看
  106. core.Manager.eDispatcher.event(core.Event.REWARDED_AD_CLOSE, res && res.isEnded);
  107. }
  108. /** 激励视频广告加载完成 */
  109. onRewardedVideoAdLoaded(res) {
  110. log("激励视频加载", res);
  111. this.addRewardedState(this.STATE_LOADED);
  112. core.Manager.wtsdk.sendAdLog(this._ids.rewardedAdID, core.AdLogType.REQUEST);
  113. core.Manager.eDispatcher.event(core.Event.REWARDED_AD_ONLOAD, res);
  114. }
  115. /** 添加状态(加载完或显示),并查看是否曝光(加载完且显示) */
  116. addRewardedState(state) {
  117. this._adState.rewardedAdState |= state;
  118. if (this._adState.rewardedAdState == this.STATE_EXPOSURE) {
  119. core.Manager.wtsdk.sendAdLog(this._ids.rewardedAdID, core.AdLogType.EXPOSURE);
  120. }
  121. }
  122. /**
  123. * 显示激励广告
  124. */
  125. showRewardedVideoAd() {
  126. if (!this._rewardedVideoAd)
  127. return;
  128. // let ad = this._rewardedVideoAd;
  129. // // 用户触发广告后,显示激励视频广告
  130. // ad.show().catch(() => {
  131. // // 失败,手动加载一次
  132. // ad.load()
  133. // .then(() => ad.show())
  134. // .catch(err =>
  135. // {
  136. // log('激励视频 广告显示失败');
  137. // core.Manager.eDispatcher.event(core.Event.REWARDED_AD_ERROW, err);
  138. // })
  139. // })
  140. let ad = this._rewardedVideoAd;
  141. // this._rewardedVideoAd.load().then(() => {
  142. // ad.show();
  143. // });
  144. ad.show();
  145. this.addRewardedState(this.STATE_SHOW);
  146. }
  147. //↑↑ 激励视频广告 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
  148. //↓↓ 插屏广告 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
  149. /** 创建插屏广告 */
  150. createInterstitialAd(interstitialAdID) {
  151. // // 定义插屏广告
  152. // // 创建插屏广告实例,提前初始化
  153. if (!tt.createInterstitialAd)
  154. return;
  155. this._interstitialAd = tt.createInterstitialAd({ adUnitId: interstitialAdID });
  156. log("创建插屏广告", this._rewardedVideoAd);
  157. if (!this._interstitialAd)
  158. return;
  159. this._interstitialAd.onError(this.onInterstitialAdError.bind(this));
  160. this._interstitialAd.onClose(this.onInterstitialAdClosed.bind(this));
  161. this._interstitialAd.onLoad(this.onInterstitialAdLoaded.bind(this));
  162. }
  163. /** 插屏广告报错 */
  164. onInterstitialAdError(err) {
  165. log("插屏广告出错", err);
  166. core.Manager.eDispatcher.event(core.Event.INTERSTITIAL_AD_ERROR, err);
  167. }
  168. /** 插屏广告关闭 */
  169. onInterstitialAdClosed(res) {
  170. log("插屏广告关闭", res);
  171. this._adState.interstitialAdState = 0; //重置状态
  172. core.Manager.eDispatcher.event(core.Event.INTERSTITIAL_AD_CLOSE, res);
  173. }
  174. /** 插屏广告加载完成 */
  175. onInterstitialAdLoaded(res) {
  176. log("插屏广告加载", res);
  177. this.addInterstitialState(this.STATE_LOADED);
  178. core.Manager.wtsdk.sendAdLog(this._ids.interstitialAdID, core.AdLogType.REQUEST);
  179. core.Manager.eDispatcher.event(core.Event.INTERSTITIAL_AD_ONLOAD, res);
  180. }
  181. /** 添加状态(加载完或显示),并查看是否曝光(加载完且显示) */
  182. addInterstitialState(state) {
  183. this._adState.interstitialAdState |= state;
  184. if (this._adState.interstitialAdState == this.STATE_EXPOSURE) {
  185. core.Manager.wtsdk.sendAdLog(this._ids.interstitialAdID, core.AdLogType.EXPOSURE);
  186. }
  187. }
  188. /**
  189. * 显示插屏广告
  190. */
  191. showInterstitialAd() {
  192. // 在适合的场景显示插屏广告
  193. if (!this._interstitialAd)
  194. return;
  195. let ad = this._interstitialAd;
  196. this._interstitialAd.load().then(() => {
  197. ad.show();
  198. this.addInterstitialState(this.STATE_SHOW);
  199. }).catch((err) => {
  200. log("显示插屏广告err:", err);
  201. });
  202. ;
  203. }
  204. }
  205. ttGame.BaseADManager = BaseADManager;
  206. })(ttGame || (ttGame = {}));
  207. /// <reference path="./base/BaseADManager.ts" />
  208. var ttGame;
  209. /// <reference path="./base/BaseADManager.ts" />
  210. (function (ttGame) {
  211. class ADManager extends ttGame.BaseADManager {
  212. constructor() {
  213. super("5ned9cusvp93gaecf5", "548if3698jfe98cl0c", "pdjoqnq1i9537a7o1o"); //广告id
  214. this._bannerAdWidth = Math.floor(tt.getSystemInfoSync().windowWidth / 3); //不取默认屏幕宽度,则设置banner宽度
  215. }
  216. //重写设置banner位置
  217. onBannerResize(size) {
  218. let windowWidth = tt.getSystemInfoSync().windowWidth;
  219. let windowHeight = tt.getSystemInfoSync().windowHeight;
  220. this._bannerAd.style.left = windowWidth - size.width; //居右
  221. this._bannerAd.style.top = windowHeight - size.height; //底部
  222. }
  223. }
  224. ttGame.ADManager = ADManager;
  225. })(ttGame || (ttGame = {}));
  226. /// <reference path="../ADManager.ts" />
  227. var ttGame;
  228. (function (ttGame) {
  229. class BasePlatform extends core.BasePlatform {
  230. constructor() {
  231. super();
  232. }
  233. /** 初始化监听事件 */
  234. initEvent() {
  235. tt.onShow(this.onShow.bind(this)); //监听小游戏回到前台的事件
  236. tt.onHide(this.onHide.bind(this)); //监听小游戏隐藏到后台事件,锁屏、按 HOME 键退到桌面等操作会触发此事件。
  237. }
  238. /**
  239. * 回到前台的事件处理函数
  240. */
  241. onShow(data) {
  242. Laya.timer.resume();
  243. // Laya.stage.renderingEnabled=true//恢复渲染
  244. // Laya.updateTimer.resume() //恢复onUpdate
  245. // Laya.physicsTimer.resume() //恢复物理
  246. // Laya.systemTimer.resume();
  247. core.Manager.wtsdk.sendStart({ scene: this.startScene });
  248. core.Manager.eDispatcher.event(core.Event.ON_SHOW, data);
  249. }
  250. /**
  251. * 小游戏隐藏到后台事件处理函数。
  252. */
  253. onHide(data) {
  254. Laya.timer.pause();
  255. // Laya.stage.renderingEnabled=false//停止渲染
  256. // Laya.updateTimer.pause() //停止onUpdate
  257. // Laya.physicsTimer.pause() //停止物理
  258. // Laya.systemTimer.pause();
  259. core.Manager.wtsdk.sendExit({ scene: this.startScene });
  260. core.Manager.eDispatcher.event(core.Event.ON_HIDE, data);
  261. }
  262. /**
  263. * 初始化(登陆、获取用户信息、设置默认分享)
  264. */
  265. init() {
  266. this.systemInfo = new ttGame.SystemInfo();
  267. core.Manager.baseInfo.app_name = this.systemInfo.appName; //设置应用名
  268. core.Manager.baseInfo.device = this.systemInfo.device; //设置设备
  269. core.Manager.baseInfo.platform_id = core.PlatformType.ID_TTGAME; //设置平台id(1-字节)
  270. // // core.Manager.wtsdk.sendVersion();//发送游戏版本到后台
  271. // //请求php,游戏初始化和配置数据
  272. // core.Manager.wtsdk.getInitData(Laya.Handler.create(this, (data) => (core.Manager.config.data = data.result)));
  273. // //请求php,获取游戏列表
  274. // core.Manager.wtsdk.getGameList(Laya.Handler.create(core.Manager, core.Manager.setGameList));
  275. this.openDataContext = tt.getOpenDataContext(); //获取开放数据域
  276. this.ad = new ttGame.ADManager(); //初始化广告
  277. this.initEvent(); //初始化监听事件
  278. this.sendRes(); //资源加载完成后,使用接口将图集透传到子域
  279. this.initRecorder(); //初始化录屏器
  280. this.setDefaultShare(); //设置默认分享
  281. this.login(); //登陆
  282. // tt.setUserGroup({ groupId: this.GROUP_ID });//设置排行分组
  283. }
  284. /**
  285. * 登陆 - 获取Code
  286. */
  287. login() {
  288. tt.login({
  289. force: false,
  290. success: this.loginComplete.bind(this, true),
  291. fail: this.loginComplete.bind(this, false)
  292. // complete:this.loginComplete.bind(this)
  293. });
  294. }
  295. /**
  296. * 登陆完成回调
  297. */
  298. loginComplete(isSucc, data) {
  299. // {errMsg: "login:ok", anonymousCode: "535ed5c9f15679e2", code: "6a4bfc20dc513b4e", isLogin: true}
  300. // 登陆失败时,后台要求anonymousCode、code发个空值
  301. if (!isSucc) {
  302. data.anonymousCode = null;
  303. data.code = null;
  304. }
  305. data.app_name = core.Manager.baseInfo.app_name;
  306. data.game_id = core.Manager.baseInfo.game_id;
  307. data.platform_id = core.Manager.baseInfo.platform_id;
  308. data.device = this.systemInfo.device;
  309. data.scene = this.startScene;
  310. data.version = BaseConfig.version;
  311. core.Manager.wtsdk.getUserInfo(data, Laya.Handler.create(this, this.httpUserInfoCallback)); //向后台请求用户信息
  312. this.getAccessToken();
  313. }
  314. /** 获取AccessToken接口 */
  315. getAccessToken(callBack) {
  316. core.Manager.wtsdk.getAccessToken(Laya.Handler.create(this, (data) => {
  317. // "code": 200,
  318. // "result": {
  319. // "data": {
  320. // "access_token": "87be87ff0c68ecb79a970e6582b30ab1e90b1118f83bdd27d7f0d985f84ba8b1eda5b32488f9d84060bca6665b96c98937410dddd992c24664c4e863c45a31ea1d6d164dc8b59638fafbc97e31497",
  321. // "expires_in": 1606742584
  322. // },
  323. // "appId": "tt29575feab66d7a22"
  324. // },
  325. // "msg": "操作成功"
  326. if (data.code) {
  327. this.appID = data.result.appId;
  328. this.accessToken = data.result.data.access_token;
  329. }
  330. if (callBack)
  331. callBack.runWith(data);
  332. }));
  333. }
  334. //向后台请求用户信息
  335. httpUserInfoCallback(data) {
  336. if (data && data.code == 200) {
  337. core.Manager.baseInfo.token = data.result.id;
  338. core.Manager.selfInfo.updateAll(data.result);
  339. this.checkSetting(); //获取用户信息
  340. }
  341. else
  342. log(data);
  343. }
  344. //向后台发送平台用户信息
  345. setUserInfo(userInfo) {
  346. //如果能获取到字节个人信息,则发送更新用户信息
  347. if (userInfo)
  348. core.Manager.wtsdk.modifyUserInfo({ userInfo: userInfo, member_id: core.Manager.selfInfo.id }, Laya.Handler.create(this, this.httpModifyUserInfoCallback));
  349. else {
  350. core.Manager.selfInfo.isAuthorized = false; //未授权
  351. this.sendUserInfo(core.Manager.selfInfo); //无需更新信息,则直接发送用户信息到开放域
  352. }
  353. }
  354. httpModifyUserInfoCallback(data) {
  355. // log(data);
  356. if (data && data.code == 200) {
  357. core.Manager.selfInfo.update(data.result);
  358. core.Manager.selfInfo.isAuthorized = true; //已授权
  359. this.sendUserInfo(core.Manager.selfInfo); //发送用户信息到开放域
  360. }
  361. else
  362. log(data);
  363. }
  364. /**
  365. * 查看是否已授权
  366. */
  367. checkSetting() {
  368. tt.getSetting({
  369. success: (res => {
  370. log("tt.getSetting:", res);
  371. if (!res.authSetting['scope.userInfo']) {
  372. this.authorizeUserInfo(); //未授权,显示授权按钮
  373. }
  374. else {
  375. this.getUserInfo(); //已授权,取用户信息
  376. }
  377. if (!res.authSetting['scope.screenRecord']) {
  378. this.authorizeScreenRecord(); //未授权录屏
  379. }
  380. else {
  381. core.Manager.selfInfo.isAuthorizedScreenRecord = true; //已授权录屏
  382. }
  383. }).bind(this)
  384. });
  385. }
  386. //发出用户信息授权请求
  387. authorizeUserInfo() {
  388. tt.authorize({
  389. scope: "scope.userInfo",
  390. success: (res => {
  391. this.getUserInfo(); //已授权用户信息
  392. log("tt.authorize UserInfo succ", res);
  393. }).bind(this),
  394. fail: (res => {
  395. this.setUserInfo(null);
  396. log("tt.authorize UserInfo fail", res);
  397. }).bind(this),
  398. complete: (res => { log("tt.authorize UserInfo complete", res); }).bind(this)
  399. });
  400. }
  401. //发出录屏授权请求
  402. authorizeScreenRecord() {
  403. tt.authorize({
  404. success: (res => {
  405. core.Manager.selfInfo.isAuthorizedScreenRecord = true; //已授权录屏
  406. log("tt.authorize screenRecord succ", res);
  407. }).bind(this),
  408. fail: (res => { log("tt.authorize screenRecord fail", res); }).bind(this),
  409. complete: (res => { log("tt.authorize screenRecord complete", res); }).bind(this)
  410. });
  411. }
  412. /**
  413. * 获取用户信息
  414. */
  415. getUserInfo() {
  416. // 已授权 - 直接获取
  417. tt.getUserInfo({
  418. withCredentials: true,
  419. success: (res => {
  420. log("getUserInfo:", res);
  421. this.setUserInfo(res.userInfo); //设置用户信息
  422. }).bind(this),
  423. fail: (res => {
  424. log("getUserInfo fail:", res);
  425. this.setUserInfo(null);
  426. }).bind(this),
  427. });
  428. }
  429. /** 透传资源到子域 */
  430. sendRes() { }
  431. /** 获取启动场景(php用到) */
  432. get startScene() {
  433. let options = tt.getLaunchOptionsSync();
  434. return options && options.scene ? options.scene : "";
  435. }
  436. /**
  437. * 设置默认分享(微信小游戏小程序不再支持分享回调https://developers.weixin.qq.com/community/develop/doc/0000447a5b431807af57249a551408)
  438. */
  439. setDefaultShare() {
  440. // tt.showShareMenu({
  441. // withShareTicket: true,
  442. // menus: ['shareAppMessage', 'shareTimeline'],
  443. // success: (res?: any) => { log("showShareMenu success", res) },
  444. // fail: (res?: any) => { log("showShareMenu fail", res) },
  445. // complete: (res?: any) => { log("showShareMenu complete", res) }
  446. // })
  447. tt.onShareAppMessage((() => {
  448. if (this._sharing)
  449. return;
  450. let channel = "";
  451. let data = this.getShareData();
  452. core.ObjectUtil.addKeys(data, {
  453. channel: channel, success: this.shareSuccess.bind(this), fail: this.shareFail.bind(this), complete: this.shareComplete.bind(this)
  454. });
  455. return data;
  456. }).bind(this));
  457. }
  458. /** 关注 */
  459. attention() {
  460. tt.openAwemeUserProfile(); //跳转个人抖音号主页,用户可以选择关注/取消关注抖音号
  461. }
  462. /** 收藏 */
  463. addToFavorite() {
  464. tt.showFavoriteGuide();
  465. // tt.showFavoriteGuide({
  466. // type: "bar", //小游戏中只能使用 tip 否 引导组件类型,可以是 bar(底部弹窗)、tip(顶部气泡)
  467. // content: "一键添加到我的小程序", //弹窗文案,最多显示 12 个字符
  468. // position: "bottom", //小游戏中该字段无意义 否 弹窗类型为 bar 时的位置参数,可以是 bottom(贴近底部)、overtab(悬于页面 tab 区域上方)
  469. // success(res) { log("引导组件展示成功"); },
  470. // fail(res) { log("引导组件展示失败"); },
  471. // complete(res){}
  472. // });
  473. // tt.onFavoriteStateChange(callback);
  474. }
  475. /** 获取视频列表页面(字节用) */
  476. getVideoList() {
  477. tt.request({
  478. url: "https://gate.snssdk.com/developer/api/get_top_video_ids_by_like",
  479. method: "POST",
  480. data: {
  481. app_id: this.appID,
  482. number_of_top: 100,
  483. tag: "rankTag1",
  484. access_token: this.accessToken,
  485. },
  486. success: (res) => {
  487. log("视频排行榜信息", res);
  488. if (res.data.err_no == -15003)
  489. this.getAccessToken(Laya.Handler.create(this, this.getVideoList));
  490. else {
  491. core.Manager.eDispatcher.event(core.Event.VIDEO_LIST, res.data.err_no == 0 ? res.data : []);
  492. }
  493. // 从res中获取所需视频信息(videoId数组索引与返回数据数组索引一一对应)
  494. },
  495. });
  496. }
  497. /** 跳转到分享的视频播放页面 */
  498. navigateToVideoView(videoId) {
  499. tt.navigateToVideoView({
  500. videoId: videoId,
  501. success: (res) => { log("done"); },
  502. fail: (err) => {
  503. if (err.errCode === 1006) {
  504. tt.showToast({
  505. title: "something wrong with your network",
  506. });
  507. }
  508. },
  509. });
  510. }
  511. /**
  512. * 主动分享
  513. */
  514. share(title) {
  515. if (this._sharing)
  516. return;
  517. let channel = "";
  518. let data = this.getShareData(title);
  519. core.ObjectUtil.addKeys(data, {
  520. channel: channel, success: this.shareSuccess.bind(this, channel), fail: this.shareFail.bind(this, channel), complete: this.shareComplete.bind(this, channel)
  521. });
  522. tt.shareAppMessage(data);
  523. }
  524. /** 分享的数据(需要重写) */
  525. getShareData(title) {
  526. return { success: this.shareSuccess.bind(this), fail: this.shareFail.bind(this), complete: this.shareComplete.bind(this) };
  527. }
  528. /** 视频分享 */
  529. shareVideo(videoPath) {
  530. if (this._sharing)
  531. return;
  532. let channel = "video";
  533. let data = this.getVideoShareData(videoPath);
  534. core.ObjectUtil.addKeys(data, {
  535. channel: channel, success: this.shareSuccess.bind(this, channel), fail: this.shareFail.bind(this, channel), complete: this.shareComplete.bind(this, channel)
  536. });
  537. tt.shareAppMessage(data);
  538. //php统计用
  539. core.Manager.wtsdk.sendRecord();
  540. }
  541. /** 视频分享的数据(需要重写) */
  542. getVideoShareData(videoPath) {
  543. let channel = "video";
  544. return { channel: channel, success: this.shareSuccess.bind(this, channel), fail: this.shareFail.bind(this, channel), complete: this.shareComplete.bind(this, channel) };
  545. }
  546. //分享成功回调
  547. shareSuccess(channel, data) {
  548. //php统计用
  549. if (channel == "video")
  550. core.Manager.wtsdk.sendRecordShare();
  551. else
  552. core.Manager.wtsdk.sendShare();
  553. data.channel = channel;
  554. core.Manager.eDispatcher.event(core.Event.SHARE_SUCCESS, data);
  555. log("分享成功", data);
  556. }
  557. //分享失败回调
  558. shareFail(channel, data) {
  559. data.channel = channel;
  560. core.Manager.eDispatcher.event(core.Event.SHARE_FAIL, data);
  561. log("分享失败", data);
  562. }
  563. //分享完成回调
  564. shareComplete(channel, data) {
  565. data.channel = channel;
  566. this._sharing = false;
  567. core.Manager.eDispatcher.event(core.Event.SHARE_COMPLETE, data);
  568. log("分享完成", data);
  569. }
  570. //↑↑分享 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
  571. /** 获取本地缓存数据 */
  572. getStorageSync(key) {
  573. return tt.getStorageSync(key);
  574. }
  575. /** 保存数据本地缓存 */
  576. setStorageSync(key, data) {
  577. return tt.setStorageSync(key, data);
  578. }
  579. /**
  580. * 向开放数据域发送消息
  581. * @param message {} 要发送的消息,message 中及嵌套对象中 key 的 value 只能是 primitive value。即 number、string、boolean、null、undefined。
  582. */
  583. postMessage(message) {
  584. this.openDataContext.postMessage(message);
  585. }
  586. /** 传递UserInfo */
  587. sendUserInfo(userInfo) {
  588. core.Manager.isLogined = true;
  589. let msg = { "action": core.DomainAction.UserInfo, "data": userInfo };
  590. this.postMessage(msg);
  591. }
  592. /** clearCanvas */
  593. clearCanvas() {
  594. let msg = { "action": core.DomainAction.ClearCanvas };
  595. this.postMessage(msg);
  596. }
  597. // private _rank:DYRankView;
  598. /** 显示排行榜 */
  599. showRank(rankType) {
  600. let msg;
  601. if (rankType == 0)
  602. msg = { "action": core.DomainAction.RankFriend };
  603. else
  604. msg = { "action": core.DomainAction.RankWorld };
  605. this.postMessage(msg);
  606. }
  607. /** 关闭排行榜 */
  608. hideRank() {
  609. }
  610. /** 排行榜切页(-1上一页 0下一页 >0跳转到具体页数) */
  611. rankChangePage(page) {
  612. this.postMessage({
  613. "action": core.DomainAction.Paging,
  614. "page": page
  615. });
  616. }
  617. /** 保存最高分 */
  618. saveScore(score) {
  619. this.postMessage({
  620. "action": core.DomainAction.SaveScore,
  621. "score": score
  622. });
  623. core.Manager.wtsdk.saveScore({ score: score });
  624. }
  625. //↓↓ 广告类 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
  626. /** 显示或隐藏Banner广告 */
  627. adBanner(isShow) {
  628. try {
  629. if (isShow)
  630. this.ad.showBanner();
  631. else
  632. this.ad.hideBanner();
  633. }
  634. catch (e) {
  635. log("adBanner error:", e);
  636. }
  637. }
  638. /** 显示激励式广告 */
  639. adRewardedVideo() {
  640. try {
  641. this.ad.showRewardedVideoAd();
  642. }
  643. catch (e) {
  644. log("adBanner error:", e);
  645. }
  646. }
  647. /** 显示插屏广告 */
  648. adInterstitial() {
  649. try {
  650. this.ad.showInterstitialAd();
  651. }
  652. catch (e) {
  653. log("adBanner error:", e);
  654. }
  655. }
  656. /** 显示或隐藏格子广告 */
  657. adGrid(isShow) {
  658. // if(isShow) this.ad.showGridAd();
  659. // else this.ad.hideGridAd();
  660. }
  661. //↑↑ 广告类 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
  662. //↓↓ 录屏 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
  663. initRecorder() {
  664. this.recorder = tt.getGameRecorderManager();
  665. this.recorder.onStart(res => {
  666. log("录屏开始", res);
  667. core.Manager.eDispatcher.event(core.Event.RECORD_START, res);
  668. });
  669. this.recorder.onStop(res => {
  670. log("录屏停止", res);
  671. core.Manager.eDispatcher.event(core.Event.RECORD_STOP, res.videoPath);
  672. });
  673. this.recorder.onError(err => {
  674. log("录屏错误", err);
  675. core.Manager.eDispatcher.event(core.Event.RECORD_ERROR, err);
  676. });
  677. // GameRecorderManager.onResume(function callback)// 监听录屏继续事件
  678. // GameRecorderManager.onPause(function callback)// 监听录屏暂停事件
  679. // GameRecorderManager.onInterruptionBegin(function callback)// 监听录屏中断开始
  680. // GameRecorderManager.onInterruptionEnd(function callback)// 监听录屏中断结束
  681. }
  682. /** 开始录屏 */
  683. startRecord() {
  684. try {
  685. let maskInfo = this.recorder.getMark();
  686. let x = (Laya.stage.width - maskInfo.markWidth) / 2;
  687. let y = (Laya.stage.height - maskInfo.markHeight) / 2;
  688. //添加水印并且居中处理
  689. this.recorder.start({
  690. duration: 30,
  691. isMarkOpen: true,
  692. locLeft: x,
  693. locTop: y,
  694. });
  695. }
  696. catch (e) {
  697. log("startRecord error:", e);
  698. }
  699. }
  700. /** 停止录屏 */
  701. stopRecord() {
  702. try {
  703. this.recorder.stop();
  704. }
  705. catch (e) {
  706. log("stopRecord error:", e);
  707. }
  708. }
  709. /**
  710. * 更多游戏图标
  711. * @parame isShow 是否显示
  712. */
  713. moreGameBtn(isShow) {
  714. core.Manager.eDispatcher.offAllCaller(this);
  715. // const systemInfo = tt.getSystemInfoSync();
  716. // iOS 不支持,建议先检测再使用
  717. if (this.systemInfo.device !== "ios") {
  718. if (isShow) {
  719. if (!core.Manager.gameList || core.Manager.gameList.length == 0) {
  720. core.Manager.eDispatcher.on(core.Event.GET_MOREGAME_COMPLETE, this, () => { this.moreGameBtn(true); });
  721. return null;
  722. }
  723. if (!this._moreGameBtn)
  724. this._moreGameBtn = new ttGame.MoreGameIcon(this.moreGameParams);
  725. }
  726. else if (this._moreGameBtn) {
  727. this._moreGameBtn.destroy();
  728. this._moreGameBtn = null;
  729. }
  730. return this._moreGameBtn;
  731. }
  732. }
  733. /** 更多游戏按钮参数 */
  734. get moreGameParams() {
  735. return { x: 0, y: 0, w: 60, h: 60, appLaunchOptions: [] };
  736. }
  737. /** 显示更多游戏 */
  738. showMoreGame() {
  739. // const systemInfo = tt.getSystemInfoSync();
  740. // iOS 不支持,建议先检测再使用
  741. if (this.systemInfo.device !== "ios") {
  742. // 打开小游戏盒子
  743. tt.showMoreGamesModal({
  744. appLaunchOptions: []
  745. });
  746. }
  747. }
  748. }
  749. ttGame.BasePlatform = BasePlatform;
  750. })(ttGame || (ttGame = {}));
  751. /// <reference path="./ADManager.ts" />
  752. /// <reference path="./base/BasePlatform.ts" />
  753. var ttGame;
  754. /// <reference path="./ADManager.ts" />
  755. /// <reference path="./base/BasePlatform.ts" />
  756. (function (ttGame) {
  757. class Platform extends ttGame.BasePlatform {
  758. constructor() {
  759. super();
  760. }
  761. /** 普通分享的数据 */
  762. getShareData(title) {
  763. if (!title)
  764. title = "跟我一起去感受风的速度吧!";
  765. let data = {
  766. title: title,
  767. };
  768. core.ObjectUtil.addKeys(data, super.getShareData(title));
  769. return data;
  770. }
  771. /** 视频分享的数据 */
  772. getVideoShareData(videoPath) {
  773. let data = {
  774. query: "",
  775. // templateId: "81d7bjb2go51hggi19", // 替换成通过审核的分享ID
  776. title: "酷跑男女",
  777. desc: "跟我一起去感受风的速度吧!",
  778. extra: {
  779. videoPath: videoPath,
  780. videoTopics: ["#酷跑男女 #跟我一起去感受风的速度吧!"],
  781. withVideoId: true,
  782. videoTag: "rankTag1",
  783. }
  784. };
  785. core.ObjectUtil.addKeys(data, super.getVideoShareData(videoPath));
  786. return data;
  787. }
  788. /** 透传资源到子域 */
  789. sendRes() {
  790. //加载完成后,使用接口将图集透传到子域
  791. ttGame.SendRes.sendAtlasList(["res/atlas/res/imgs/rank.atlas", "res/atlas/res/imgs/revive.atlas",]);
  792. // SendRes.sendSinglePicList(["unpack/rankBg.png"]);
  793. // SendRes.sendJsonList(["test/1.json"]);
  794. }
  795. /** 更多游戏按钮参数 */
  796. get moreGameParams() {
  797. return { x: Laya.stage.width / 2, y: 258, w: 80, h: 80, appLaunchOptions: [] };
  798. }
  799. /** 显示即将超越好友 */
  800. showSurpass(score) {
  801. this.postMessage({ "action": core.DomainActionEx.Surpass,
  802. "score": score,
  803. });
  804. }
  805. /**显示称号 */
  806. showTitle(score) {
  807. this.postMessage({
  808. "action": core.DomainActionEx.Title,
  809. "score": score
  810. });
  811. }
  812. // /**显示荣誉 */
  813. // public showRongYu()
  814. // {
  815. // this.postMessage({
  816. // "action":core.DomainActionEx.showRY
  817. // })
  818. // }
  819. /** 发送登录检查保存分数 */
  820. sendLoginCheck() {
  821. log('发送登录检查保存分数');
  822. this.postMessage({ 'action': core.DomainActionEx.LoginCheck });
  823. }
  824. }
  825. ttGame.Platform = Platform;
  826. })(ttGame || (ttGame = {}));
  827. window.ttGame = ttGame;
  828. //首次加载时,初始化
  829. window.platform = new ttGame.Platform();
  830. var ttGame;
  831. (function (ttGame) {
  832. class SendRes {
  833. //将加载完成的图集,使用接口透传到子域
  834. static sendAtlasList(urls) {
  835. if (!urls)
  836. return;
  837. while (urls.length > 0) {
  838. let url = urls.pop();
  839. if (Laya.loader.getRes(url))
  840. Laya.TTMiniAdapter.sendAtlasToOpenDataContext(url);
  841. else
  842. Laya.loader.load(url, Laya.Handler.create(Laya.TTMiniAdapter, Laya.TTMiniAdapter.sendAtlasToOpenDataContext, [url]));
  843. }
  844. }
  845. //将加载完成的单个图片文件,使用接口透传到子域
  846. static sendSinglePicList(urls) {
  847. if (!urls)
  848. return;
  849. while (urls.length > 0) {
  850. let url = urls.pop();
  851. if (Laya.loader.getRes(url))
  852. Laya.TTMiniAdapter.sendSinglePicToOpenDataContext(url);
  853. else
  854. Laya.loader.load(url, Laya.Handler.create(Laya.TTMiniAdapter, Laya.TTMiniAdapter.sendSinglePicToOpenDataContext, [url]));
  855. }
  856. }
  857. //将加载完成的json文件,使用接口透传到子域
  858. static sendJsonList(urls) {
  859. if (!urls)
  860. return;
  861. while (urls.length > 0) {
  862. let url = urls.pop();
  863. if (Laya.loader.getRes(url))
  864. Laya.TTMiniAdapter.sendJsonDataToDataContext(url);
  865. else
  866. Laya.loader.load(url, Laya.Handler.create(Laya.TTMiniAdapter, Laya.TTMiniAdapter.sendJsonDataToDataContext, [url]));
  867. }
  868. }
  869. }
  870. ttGame.SendRes = SendRes;
  871. })(ttGame || (ttGame = {}));
  872. var ttGame;
  873. (function (ttGame) {
  874. class SystemInfo {
  875. constructor() {
  876. this.info = tt.getSystemInfoSync();
  877. }
  878. /** 设备:android,ios */
  879. get device() {
  880. return this.info.platform;
  881. }
  882. /** app名字 */
  883. get appName() {
  884. return this.info.appName;
  885. }
  886. }
  887. ttGame.SystemInfo = SystemInfo;
  888. })(ttGame || (ttGame = {}));
  889. var ttGame;
  890. (function (ttGame) {
  891. class MoreGameIcon extends Laya.Box {
  892. constructor(params) {
  893. super();
  894. this.ICON_SIZE = 120; //游戏图标的始尺寸
  895. this.zOrder = core.ZOrder.POPUP;
  896. this._appLaunchOptions = params.appLaunchOptions;
  897. this.pos(params.x, params.y);
  898. // this.size(params.w, params.h);
  899. this.scale(params.w / this.ICON_SIZE, params.h / this.ICON_SIZE);
  900. Laya.stage.addChild(this);
  901. }
  902. onAwake() {
  903. this._index = 0;
  904. this._icon = new Laya.Image();
  905. this._icon.anchorX = 0.5;
  906. this._icon.anchorY = 0.5;
  907. this.addChild(this._icon);
  908. this._icon.mouseEnabled = true;
  909. this._label = new Laya.Image(this.getRes("gdhw")); //
  910. this._label.anchorX = 0.5;
  911. this._label.anchorY = 0.5;
  912. this._label.y = 86;
  913. this.addChild(this._label);
  914. this._label.mouseEnabled = true;
  915. this._iconUrls = [];
  916. let gameList = core.Manager.gameList;
  917. for (let i = gameList.length - 1; i >= 0; i--) {
  918. if (gameList[i].rec_degree == 1)
  919. this._iconUrls.push(gameList[i].icon); //取热门游戏
  920. }
  921. if (this._iconUrls.length == 0)
  922. this._iconUrls.push(gameList[0].icon); //无热门游戏,推第一个进去,保证起码有一个游戏
  923. this._tweentimeLite = new core.TweenTimeLine(0);
  924. this._tweentimeLite.addToTween(this, { scaleX: this.scaleX * 0.8, scaleY: this.scaleX * 0.8 }, 600, Laya.Ease.backOut);
  925. this._tweentimeLite.addToTween(this, { scaleX: this.scaleX, scaleY: this.scaleX }, 600, Laya.Ease.backIn);
  926. }
  927. getRes(name) {
  928. return core.Manager.commonResPath + "gameIcon/" + name + ".png"; //指定取资源图片
  929. }
  930. onEnable() {
  931. super.onEnable();
  932. this.timerLoop(5000, this, this.changeSkin);
  933. this.changeSkin();
  934. this._tweentimeLite.start();
  935. this.on(Laya.Event.CLICK, this, this.onClick);
  936. this._icon.on(Laya.Event.LOADED, this, this.iconLoaded);
  937. }
  938. changeSkin() {
  939. if (this._index >= this._iconUrls.length)
  940. this._index = 0;
  941. this._icon.skin = this._iconUrls[this._index];
  942. this._index++;
  943. }
  944. iconLoaded() {
  945. this._icon.size(this.ICON_SIZE, this.ICON_SIZE); //每个图片大小可能不一样,加载完成重新设置一下
  946. }
  947. onClick(e) {
  948. // // 监听弹窗关闭
  949. // tt.onMoreGamesModalClose(function (res) {
  950. // log("modal closed", res);
  951. // });
  952. // // 监听小游戏盒子跳转
  953. // tt.onNavigateToMiniGameBox(function (res) {
  954. // log(res.errCode);
  955. // log(res.errMsg);
  956. // });
  957. // const systemInfo = tt.getSystemInfoSync();
  958. // // iOS 不支持,建议先检测再使用
  959. // if (systemInfo.platform !== "ios") {
  960. // // 打开小游戏盒子
  961. // tt.showMoreGamesModal({
  962. // appLaunchOptions: this._appLaunchOptions,
  963. // // success(res) { log("success", res.errMsg); },
  964. // // fail(res) { log("fail", res.errMsg); },
  965. // });
  966. // }
  967. if (window.platform) {
  968. platform.showMoreGame();
  969. }
  970. }
  971. onDisable() {
  972. super.onDisable();
  973. this.clearTimer(this, this.changeSkin);
  974. this._tweentimeLite.pause();
  975. this.off(Laya.Event.CLICK, this, this.onClick);
  976. this._icon.off(Laya.Event.LOADED, this, this.iconLoaded);
  977. }
  978. destroy() {
  979. super.destroy();
  980. this._tweentimeLite.destroy();
  981. this._tweentimeLite = null;
  982. this._appLaunchOptions = null;
  983. this._iconUrls = null;
  984. }
  985. }
  986. ttGame.MoreGameIcon = MoreGameIcon;
  987. })(ttGame || (ttGame = {}));
  988. //# sourceMappingURL=ttGame.js.map