KuaiShouAdManager.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. export class KuaiShouAdManager {
  2. private bannerAd: any;
  3. private rewardVideoAd: any;
  4. private interstitialAd: any;
  5. constructor() {
  6. this.initBannerAd();
  7. this.initRewardVideoAd();
  8. this.initInterstitialAd();
  9. }
  10. private initBannerAd() {
  11. let ks = (window as any)["ks"];
  12. if (ks.createBannerAd) {
  13. this.bannerAd = ks.createBannerAd({
  14. adUnitId: 'YOUR_BANNER_AD_ID',
  15. style: {
  16. left: 0,
  17. top: 0,
  18. width: 300
  19. }
  20. });
  21. this.bannerAd.onLoad(() => {
  22. this.bannerAd.show();
  23. });
  24. this.bannerAd.onError((err) => {
  25. console.log('bannerAd onError', err);
  26. });
  27. this.bannerAd.onClose(() => {
  28. console.log('bannerAd onClose');
  29. });
  30. }
  31. }
  32. private initRewardVideoAd() {
  33. let ks = (window as any)["ks"];
  34. if (ks.createRewardedVideoAd) {
  35. this.rewardVideoAd = ks.createRewardedVideoAd({
  36. adUnitId: 'YOUR_REWARD_VIDEO_AD_ID'
  37. });
  38. this.rewardVideoAd.onLoad(() => {
  39. console.log('rewardVideoAd onLoad');
  40. });
  41. this.rewardVideoAd.onError((err) => {
  42. console.log('rewardVideoAd onError', err);
  43. });
  44. this.rewardVideoAd.onClose((res) => {
  45. if (res && res.isEnded) {
  46. console.log('rewardVideoAd onClose, user finished video');
  47. } else {
  48. console.log('rewardVideoAd onClose, user left video');
  49. }
  50. });
  51. }
  52. }
  53. private initInterstitialAd() {
  54. let ks = (window as any)["ks"];
  55. if (ks.createInterstitialAd) {
  56. this.interstitialAd = ks.createInterstitialAd({
  57. adUnitId: 'YOUR_INTERSTITIAL_AD_ID'
  58. });
  59. this.interstitialAd.onLoad(() => {
  60. console.log('interstitialAd onLoad');
  61. });
  62. this.interstitialAd.onError((err) => {
  63. console.log('interstitialAd onError', err);
  64. });
  65. this.interstitialAd.onClose(() => {
  66. console.log('interstitialAd onClose');
  67. });
  68. }
  69. }
  70. public showRewardVideoAd(callback: Function) {
  71. if (this.rewardVideoAd) {
  72. this.rewardVideoAd.show().then(() => {
  73. console.log('rewardVideoAd shown successfully');
  74. callback();
  75. })
  76. }
  77. }
  78. public showBannerAd() {
  79. if (this.bannerAd) {
  80. this.bannerAd.show().catch((err) => {
  81. console.log('showBannerAd error', err);
  82. });
  83. }
  84. }
  85. public showInterstitialAd() {
  86. if (this.interstitialAd) {
  87. this.interstitialAd.show().catch((err) => {
  88. console.log('showInterstitialAd error', err);
  89. });
  90. }
  91. }
  92. }