meizu.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //2021年3月15日版
  2. class meizu {
  3. ////////////////////////////
  4. // 类成员
  5. ///////////////////////////
  6. public static readonly _instance: meizu = new meizu();
  7. /** 系统信息 */
  8. private systemInfo: any = null;
  9. /** banner广告id */
  10. private _bannerAdUnitId = 'WhRwDMYh';
  11. /** 奖励视频广告id */
  12. private _rewardedVideoAdUnitId = 'VtOtGAoC';
  13. /**插屏广告id */
  14. private _interstitialAdUnitId = '94URyDKw';
  15. /**插屏广告AD实例 */
  16. private interstitialAd: any = null;
  17. /** banner广告对象 */
  18. private bannerAd: any = null;
  19. /** 奖励视频广告对象 */
  20. private rewardedVideoAd: any;
  21. /**网络状态 */
  22. netWorkInfo: any;
  23. ////////////////////////////
  24. // get、set访问器
  25. ///////////////////////////
  26. public set bannerAdUnitId(adUnitId: string) {
  27. this._bannerAdUnitId = adUnitId;
  28. }
  29. public set rewardedVideoAdUnitId(adUnitId: string) {
  30. this._rewardedVideoAdUnitId = adUnitId;
  31. }
  32. /**获取系统信息。 */
  33. getSystemInfo(){
  34. console.log("系统信息",mz.getSystemInfo());
  35. return mz.getSystemInfo()
  36. }
  37. /////////////////////
  38. //banner广告
  39. ////////////////////
  40. /**
  41. * @description 显示banner广告
  42. */
  43. public showBannerAd() {
  44. if (this.bannerAd) return
  45. //创建 Banner 广告组件
  46. var screenHeight = qg.getSystemInfoSync().screenHeight;
  47. var screenWidth = qg.getSystemInfoSync().screenWidth;
  48. console.log("系统高度", screenHeight, "screenWidth", screenWidth);
  49. this.bannerAd = mz.createBannerAd({
  50. adUnitId: this._bannerAdUnitId, //广告id,使用申请的id
  51. style: {
  52. left: 0,
  53. top: screenHeight - screenWidth / 6.7,
  54. width: screenWidth, // 设置banner需要的宽度,横屏游戏宽度建议使用参考值1440,必须设置             
  55. height: screenWidth / 6.7 // 广告期望高度,在onResize里面可以根据广告的高度重新设置高度           
  56. }
  57. });
  58. //设置成功加载回调
  59. this.bannerAd.onLoad(() => {
  60. console.log("banner 广告加载成功");
  61. this.bannerAd.show();
  62. })
  63. // this.bannerAd.onResize((res)=>{
  64. // console.log("Banner 尺寸改变");
  65. // let screenHeight = qg.getSystemInfoSync().screenHeight;
  66. // //重新修改位置
  67. // this.bannerAd.style.top = screenHeight - res.height; //确定左上角位置,为底部位置
  68. // this.bannerAd.style.left = 0;
  69. // this.bannerAd.style.width = res.width;
  70. // this.bannerAd.style.height = res.height;
  71. // });
  72. }
  73. /**隐藏banner广告 */
  74. hideBannerAd() {
  75. console.log("----> hideBannerAd", this.bannerAd);
  76. if (this.bannerAd) {
  77. this.bannerAd.hide();
  78. this.bannerAd.destroy();
  79. this.bannerAd = null;
  80. }
  81. }
  82. /////////////////////
  83. //激励广告
  84. ////////////////////
  85. showRewardedVideoAd() {
  86. return new Promise<void>((resolve, reject) => {
  87. this.rewardedVideoAd = mz.createRewardedVideoAd({
  88. adUnitId: this._rewardedVideoAdUnitId
  89. });
  90. this.rewardedVideoAd.load()
  91. this.rewardedVideoAd.onLoad(() => {
  92. this.rewardedVideoAd.show()
  93. })
  94. this.rewardedVideoAd.onClose(()=>{
  95. resolve()
  96. // 发放奖励逻辑涉及动画建议延时500ms操作
  97. });
  98. })
  99. }
  100. /////////////////////
  101. //插屏广告
  102. ////////////////////
  103. /**
  104. * 展示插屏广告
  105. */
  106. public showInterstitialAd() {
  107. console.log("插屏广告");
  108. this.interstitialAd = mz.createInsertAd({
  109. adUnitId: this._interstitialAdUnitId
  110. })
  111. this.interstitialAd.onLoad(()=>{
  112. console.log("insert 广告加载成功");
  113. this.interstitialAd.show(()=>{
  114. console.log("insert 广告展示成功");
  115. })
  116. })
  117. this.interstitialAd.load(); //调用load成功后会回调onLoad。在onLoad里面调用show即可展示
  118. }
  119. /**网络状态 */
  120. getNetworkType() {
  121. mz.getNetworkType({ success: (res) => {
  122. console.log("getNetworkType success = " + res.networkType);
  123. this.netWorkInfo.string = res.networkType;
  124. }, fail: (res) => {
  125. console.log("getNetworkType success = " + res.errMsg);
  126. this.netWorkInfo.string = res.errMsg; },
  127. complete: (res) => {
  128. console.log("getNetworkType complete = " + res);
  129. } })
  130. }
  131. /**获取当前渠道名字 */
  132. public returnAppName(): string {
  133. return mz.getProvider()
  134. }
  135. }
  136. export const Meizu = meizu._instance