platformTT.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. import { Vec2, _decorator } from 'cc';
  2. import { constants } from '../data/constants';
  3. import { eventEmitter } from '../manager/eventEmitter';
  4. const TT_CONFIG = {
  5. AD_ID: {
  6. /** banner广告 */
  7. banner: '30a3ll993k15aakgla',
  8. /** 视频广告 */
  9. video: 'o1h5n7r8fnp31df3g1',
  10. /** 插屏广告 */
  11. interstitial: '4687fak66i8aklc5hd'
  12. }
  13. }
  14. var globalTT:any;
  15. const delayTime: number = 1900;
  16. class PlatformTT {
  17. private _onHideTime: number = Infinity;
  18. private _shareTimes: number = 0;
  19. private _rewardVideo: wx.RewardedVideoAd;
  20. private _bannerAd: wx.BannerAd;
  21. private _interstitialAd: any;
  22. private _shareSuccessHandler: Function = null;
  23. private _videoSuccessHandler: Function = null;
  24. private _showInterstitialAdTime: number = null;
  25. private _shareModalIsActive: boolean = false;
  26. private _shareStr: string = "东北F4来到美国西部拓荒时代,保卫城镇,完成自我救赎。";
  27. public initialize(): void {
  28. //@ts-ignore
  29. globalTT = tt;
  30. console.log("初始化抖音框架");
  31. //监听游戏生命周期回调
  32. globalTT.onShow(function (res) {
  33. //判断是否是从侧边栏复访
  34. if (res.scene == 1104) {
  35. console.log('从侧边栏复访');
  36. }
  37. });
  38. globalTT.showShareMenu({
  39. withShareTicket: true,
  40. menus: ['shareAppMessage', 'shareTimeline'],
  41. success: () => { },
  42. fail: () => { },
  43. complete: () => { }
  44. });
  45. //发送给朋友
  46. globalTT.onShareAppMessage(function(){
  47. globalTT.shareAppMessage({
  48. title: this._shareStr,
  49. query: 'shareMsg='+'a=1',
  50. });
  51. }.bind(this));
  52. globalTT.onShow((res: {
  53. scene: string;
  54. query: any;
  55. shareTicket: string;
  56. referrerInfo: wx.ReferrerInfo;
  57. }) => {
  58. if (this._shareSuccessHandler == null) return;
  59. if (this._shareModalIsActive) return;
  60. if (Date.now() - this._onHideTime >= delayTime) {
  61. if (this._shareSuccessHandler) this._shareSuccessHandler(true);
  62. } else {
  63. globalTT.showModal({
  64. title: '分享提示',
  65. content: '成功分享到群即可获得奖励',
  66. success: (res) => {
  67. if (res.confirm) {
  68. this.shareShowReward(this._shareSuccessHandler(false));
  69. } else if (res.cancel) {
  70. this._shareSuccessHandler = null;
  71. }
  72. this._shareModalIsActive = false;
  73. }
  74. });
  75. this._shareModalIsActive = true;
  76. }
  77. this._onHideTime = Infinity;
  78. });
  79. //this.__createBannerAd();
  80. this.__createVideoAd();
  81. //this.__createInterstitialAd();
  82. }
  83. //检查当前宿主是否支持跳转到侧边栏
  84. public isCheckScene(handler: Function = null){
  85. globalTT.checkScene({
  86. scene: 'sidebar',
  87. success(res) {
  88. if(handler){
  89. if(res.isExist) {//支持
  90. if(true){
  91. handler(false);
  92. }else{
  93. handler(true);
  94. }
  95. }else{//不支持
  96. handler(false)
  97. }
  98. }
  99. },
  100. fail(err) {
  101. console.error('检查侧边栏支持失败', err);
  102. // 可以选择默认操作,例如不展示奖励入口
  103. if(handler){handler(false)}
  104. }
  105. });
  106. }
  107. //添加测边栏功能
  108. public openSideBar(handler: Function = null): void {
  109. globalTT.navigateToScene({
  110. scene: 'sidebar',
  111. success() {
  112. handler?.('成功打开侧边栏',true);
  113. },
  114. fail(err) {
  115. handler?.('打开侧边栏失败',false);
  116. }
  117. });
  118. }
  119. /**
  120. * 是否支持添加桌面
  121. * @returns
  122. */
  123. public isSupportDesk(){
  124. return true;
  125. }
  126. //添加到桌面
  127. public addDesk(handler: Function = null): void {
  128. // 调用创建桌面快捷方式 API
  129. globalTT.addShortcut({
  130. success() {
  131. globalTT.showToast({title: '创建桌面快捷方式成功'});
  132. },
  133. fail(err) {
  134. globalTT.showToast({title: '创建桌面快捷方式失败', err});
  135. }
  136. });
  137. }
  138. public showToast(param: { title: string, icon?: 'success' | 'loading' | 'none', image?: string, duration?: number, mask?: boolean, success?: (res?: any) => void, fail?: (res?: any) => void, complete?: (res?: any) => void }): void {
  139. let finalParam = {
  140. title: '',
  141. icon: 'none',
  142. duration: 2000,
  143. } as any;
  144. Object.assign(finalParam, param);
  145. globalTT.showToast(finalParam);
  146. }
  147. public shareAppMessage(param: { title?: string, imageUrl?: string, query?: string, imageUrlId?: string } = {}): void {
  148. globalTT.shareAppMessage(param);
  149. }
  150. public vibrateShort(param: any = {}): void {
  151. globalTT.vibrateShort(param);
  152. }
  153. public vibrateLong(param: any = {}): void {
  154. globalTT.vibrateLong(param);
  155. }
  156. public showCustomAd(close: Function): void {
  157. }
  158. public hideCustomAd(): void {
  159. }
  160. public showSigleCustomAd(): void {
  161. }
  162. public hideSigleCustomAd(): void {
  163. }
  164. //分享
  165. public shareShowReward(handler: Function = null): void {
  166. if(handler){
  167. this._shareSuccessHandler = handler;
  168. }
  169. this._onHideTime = Date.now();
  170. this.shareAppMessage({
  171. title: this._shareStr,
  172. query: 'shareMsg='+'a=1',
  173. });
  174. }
  175. public showRewardVideo(handler: Function = null): void {
  176. //console.log('当日视频次数: ', ArchiveSystem.localData.videoRewardTimesToday);
  177. /*if (ArchiveSystem.localData.videoRewardTimesToday % 2 === 1) {
  178. cc.log(ArchiveSystem.localData.videoRewardTimesToday, '转为分享');
  179. this.__turnVideoToShare(handler);
  180. return;
  181. }*/
  182. if (!this._rewardVideo) {
  183. this.__turnVideoToShare(handler);
  184. return;
  185. }
  186. this._videoSuccessHandler = handler;
  187. this._rewardVideo
  188. .show()
  189. .then(() => {
  190. console.log("视频播放成功");
  191. })
  192. .catch(err => {
  193. this._rewardVideo.load()
  194. .then(() => {
  195. this._rewardVideo
  196. .show()
  197. .then(() => {
  198. console.log("视频重新加载,播放成功");
  199. });
  200. })
  201. .catch(err2 => {
  202. this._videoSuccessHandler?.(false);
  203. this.shareShowReward(handler);
  204. this._videoSuccessHandler = null;
  205. });
  206. });
  207. }
  208. public showBannerAd(): void {
  209. if (!this._bannerAd) return;
  210. const { screenWidth, screenHeight } = globalTT.getSystemInfoSync();
  211. this._bannerAd.show()
  212. .then(() => {
  213. this._bannerAd.style.left = (screenWidth - this._bannerAd.style.realWidth) / 2;
  214. this._bannerAd.style.top = screenHeight - this._bannerAd.style.realHeight;
  215. }).catch((e: any) => {
  216. console.log('banner广告显示出错', e);
  217. });
  218. }
  219. public hideBannerAd(recreate: boolean = false): void {
  220. if (!this._bannerAd) return;
  221. this._bannerAd.hide();
  222. if (recreate) {
  223. this.refreshBannerAd();
  224. }
  225. }
  226. public refreshBannerAd(): void {
  227. if (!this._bannerAd) return;
  228. this._bannerAd.destroy();
  229. this.__createBannerAd();
  230. }
  231. public showInterstitialAd(force: boolean = false): void {
  232. if(force){
  233. if (!this._interstitialAd) return;
  234. this._interstitialAd.show();
  235. }else{
  236. if (!this._interstitialAd) return;
  237. let timeNow = Date.now();
  238. if (this._showInterstitialAdTime == null) {
  239. this._showInterstitialAdTime = timeNow;
  240. return;
  241. }
  242. if (timeNow - this._showInterstitialAdTime > 120 * 1000) {
  243. this._interstitialAd.show();
  244. this._showInterstitialAdTime = timeNow;
  245. }
  246. }
  247. }
  248. public exitGame(): void {
  249. wx.exitMiniProgram({});
  250. }
  251. private __createVideoAd(): void {
  252. if (!TT_CONFIG.AD_ID.video) return;
  253. this._rewardVideo = globalTT.createRewardedVideoAd({ adUnitId: TT_CONFIG.AD_ID.video });
  254. // 注册视频的onClose事件
  255. this._rewardVideo.onClose(res => {
  256. if (res && res.isEnded || res === undefined) {
  257. this._videoSuccessHandler?.(true);
  258. } else {
  259. this.showToast({
  260. title: "完整观看视频才有奖励哦"
  261. });
  262. this._videoSuccessHandler?.(false);
  263. }
  264. this._videoSuccessHandler = null;
  265. });
  266. // 注册视频的onError事件
  267. this._rewardVideo.onError(res => {
  268. this._videoSuccessHandler?.(false);
  269. this.showToast({
  270. title: "暂时没有可观看的视频"
  271. });
  272. this.shareShowReward(this._videoSuccessHandler);
  273. this._videoSuccessHandler = null;
  274. });
  275. }
  276. private __createBannerAd(): void {
  277. if (!TT_CONFIG.AD_ID.banner) return;
  278. const { screenWidth, screenHeight } = globalTT.getSystemInfoSync();
  279. let w = screenWidth * 0.85;
  280. let left = (screenWidth-w)/2.0;
  281. this._bannerAd = globalTT.createBannerAd({
  282. adUnitId: TT_CONFIG.AD_ID.banner,
  283. adIntervals: 48,
  284. style: {
  285. left: left,
  286. top: screenHeight - 70,
  287. width: w,
  288. height: 70
  289. }
  290. });
  291. this._bannerAd.onError(err => {
  292. switch (err.errCode) {
  293. case 1001: {
  294. break;
  295. }
  296. case 1004: {
  297. break;
  298. }
  299. }
  300. });
  301. this._bannerAd.onResize(() => {});
  302. }
  303. private __createInterstitialAd(): void {
  304. if (!TT_CONFIG.AD_ID.interstitial) return;
  305. this._interstitialAd = globalTT.createInterstitialAd({ adUnitId: TT_CONFIG.AD_ID.interstitial });
  306. }
  307. private __turnVideoToShare(handler: Function): void {
  308. this.shareShowReward(handler);
  309. this._videoSuccessHandler = null;
  310. }
  311. }
  312. export default new PlatformTT();