platformTT.ts 11 KB

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