QQPlatform.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import { TERRAIN_HEIGHT_BASE, _decorator } from 'cc';
  2. import { BannerLoop } from '../BannerLoop';
  3. import { IPlatform } from '../IPlatform';
  4. const { ccclass } = _decorator;
  5. @ccclass('WeChatPlatforms')
  6. export class QQPlatform implements IPlatform {
  7. private qq = window['qq'];
  8. private __bannerIDs: string[] = ["de3b6c8f68574edc7a8c05dbc71abd5c", "f565e7f53d0b4765bb6553148e3dea7d"];
  9. private __bannerLoop: BannerLoop;
  10. private __windowWidth: number;
  11. private __windowHeight: number;
  12. //激励广告
  13. private __rewardedVideoAd: any;
  14. private __rewardedSuccess: Function;
  15. private __rewardedfailure: Function;
  16. private __systemInfo: any;
  17. //广告盒子
  18. private __appBox: any;
  19. //插屏广告
  20. private __interstitialAd: any;
  21. //分享语录
  22. private _shareTitles: string[] = [
  23. '打僵尸,我是最强的,不服来战',
  24. '你的空投到了!点击领取!',
  25. '什么,这游戏还能这么玩?!',
  26. '这一波我有点扛不住啊!',
  27. '枪神?我预定了,不服来战!',
  28. '我终于得到了“哒哒哒”冒蓝光的加特林',
  29. '老同学,还记得当年一起打的僵尸吗',
  30. '人类!就你这样的手速,最后将会沦为我的食物!',
  31. '啊呜,啊呜,啊呜,虽然二哈叫声也这样,但我可是会吃人的!',
  32. '当我扣下扳机的那一刻,请你从哪里来回哪去!'
  33. ]
  34. constructor() {
  35. this.__systemInfo = this.qq.getSystemInfoSync();
  36. this.__windowWidth = this.__systemInfo.screenWidth;
  37. this.__windowHeight = this.__systemInfo.screenHeight;
  38. }
  39. login(success: Function, failure: Function): void {
  40. this.__bannerLoop = new BannerLoop();
  41. this.__bannerLoop.init(2, this.__bannerIDs, this.__bannerCreate.bind(this), this.__bannerDestory.bind(this));
  42. //激励广告
  43. this.__initRewardedVideoAd();
  44. //广告盒子
  45. this.__initAppBox();
  46. //插屏广告
  47. this.__initInterstitialAd();
  48. }
  49. private __initRewardedVideoAd(): void {
  50. try {
  51. //提前创建广告
  52. this.__rewardedVideoAd = this.qq.createRewardedVideoAd({ adUnitId: '5f59e35585f559483db5e5cd92657ea1' })
  53. //拉取成功
  54. this.__rewardedVideoAd.onLoad(() => {
  55. console.log("激励广告拉取成功");
  56. });
  57. //拉取失败
  58. this.__rewardedVideoAd.onError(() => {
  59. console.log("激励广告拉取失败");
  60. });
  61. //激励广告回调
  62. this.__rewardedVideoAd.onClose(res => {
  63. // 用户点击了【关闭广告】按钮
  64. // 小于 2.1.0 的基础库版本,res 是一个 undefined
  65. if (res && res.isEnded || res === undefined) {
  66. console.log("广告看完了");
  67. // 正常播放结束,可以下发游戏奖励
  68. if (this.__rewardedSuccess) {
  69. this.__rewardedSuccess();
  70. this.__rewardedSuccess = null;
  71. }
  72. this.__rewardedfailure = null;
  73. }
  74. else {
  75. console.log("看广告中途退出!");
  76. // 播放中途退出,不下发游戏奖励
  77. if (this.__rewardedfailure) {
  78. this.__rewardedfailure();
  79. this.__rewardedfailure = null;
  80. }
  81. this.__rewardedSuccess = null;
  82. }
  83. });
  84. } catch (error) {
  85. console.error("初始化激励广告失败");
  86. }
  87. }
  88. private __initAppBox(): any {
  89. try {
  90. this.__appBox = this.qq.createAppBox({
  91. adUnitId: 'b77f028c393b5860ad523df1a5d64907'
  92. })
  93. this.__appBox.load();
  94. this.__appBox.onClose();
  95. this.__appBox.offClose();
  96. } catch (error) {
  97. console.error("初始化广告盒子失败")
  98. }
  99. }
  100. private __initInterstitialAd(): void {
  101. try {
  102. this.__interstitialAd = this.qq.createInterstitialAd({
  103. adUnitId: '47e2cbf95183f93ec6514513e9be09b3'
  104. })
  105. //拉取成功
  106. this.__interstitialAd.onLoad(() => {
  107. console.log("插屏广告拉取成功");
  108. });
  109. //拉取失败
  110. this.__interstitialAd.onError(() => {
  111. console.log("插屏广告拉取失败");
  112. });
  113. this.__interstitialAd.load();
  114. this.__interstitialAd.onClose();
  115. this.__interstitialAd.offClose();
  116. } catch (error) {
  117. console.error("初始化插屏广告失败")
  118. }
  119. }
  120. private __bannerCreate(id: string): any {
  121. let targetBannerAdWidth: number = this.__windowWidth;
  122. let resizeBool: boolean = false;
  123. let banner: any = this.qq.createBannerAd(
  124. {
  125. adUnitId: id,
  126. style: {
  127. width: targetBannerAdWidth,
  128. left: 0,
  129. top: this.__windowHeight - (targetBannerAdWidth / 16 * 9)
  130. }
  131. }
  132. );
  133. // 尺寸调整时会触发回调
  134. // 注意:如果在回调里再次调整尺寸,要确保不要触发死循环!!!
  135. banner.onResize(size => {
  136. if (resizeBool == false) {
  137. resizeBool = true;
  138. banner.style.top = this.__windowHeight - size.height;
  139. banner.style.left = (this.__windowWidth - size.width) / 2;
  140. }
  141. });
  142. banner.onError(err => {
  143. console.log("err", err)
  144. })
  145. return banner;
  146. }
  147. private __bannerDestory(banner: any): void {
  148. banner.destroy();
  149. }
  150. navigate2Mini(data: any, success?: Function, failure?: Function): void {
  151. throw new Error('Method not implemented.');
  152. }
  153. shareMessage(data: any, success?: Function, failure?: Function): void {
  154. throw new Error('Method not implemented.');
  155. }
  156. onShareAppMessage(): void {
  157. try {
  158. this.qq.showShareMenu({
  159. showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
  160. });
  161. let shareTitle = this._shareTitles[Math.floor(Math.random() * this._shareTitles.length)]
  162. this.qq.onShareAppMessage(() => {
  163. return {
  164. title: shareTitle,
  165. imageUrl: ''
  166. }
  167. })
  168. } catch (error) {
  169. }
  170. }
  171. showBanner(data: any): void {
  172. console.log("显示Banner");
  173. this.__bannerLoop.showBanner();
  174. }
  175. hideBanner(): void {
  176. console.log("隐藏Banner");
  177. this.__bannerLoop.hideBanner();
  178. }
  179. showAppBox(): void {
  180. console.log("显示广告盒子");
  181. if (this.__appBox) {
  182. this.__appBox.show();
  183. }else{
  184. this.__initAppBox();
  185. }
  186. }
  187. showInterstitialAd():void{
  188. console.log("显示插屏广告");
  189. if(this.__interstitialAd){
  190. this.__interstitialAd.show();
  191. }else{
  192. this.__initInterstitialAd();
  193. }
  194. }
  195. showRewardedVideo(success?: Function, failure?: Function): void {
  196. console.log("显示激励广告");
  197. this.__rewardedSuccess = success;
  198. this.__rewardedfailure = failure;
  199. if (this.__rewardedVideoAd) {
  200. this.__rewardedVideoAd.show().catch(() => {
  201. this.__rewardedVideoAd.load().then(() => this.__rewardedVideoAd.show())
  202. });
  203. }
  204. }
  205. showInsertAd(success?: Function, failure?: Function): void {
  206. throw new Error('Method not implemented.');
  207. }
  208. showNativeAd(success?: Function, failure?: Function): void {
  209. throw new Error('Method not implemented.');
  210. }
  211. sendEvent(eventName: string, data?: any): void {
  212. throw new Error('Method not implemented.');
  213. }
  214. setLoadingProgress(progress: number): void {
  215. throw new Error('Method not implemented.');
  216. }
  217. loadingComplete(completeHandler?: Function): void {
  218. throw new Error('Method not implemented.');
  219. }
  220. getRandomPageAd(num: number): any[] {
  221. throw new Error('Method not implemented.');
  222. }
  223. }