WxPlatform.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { _decorator, Component, Node } from 'cc';
  2. import { WECHAT } from 'cc/env';
  3. import { Global } from '../Global';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('WxPlatform')
  6. export class WxPlatform {
  7. static _ins: WxPlatform;
  8. public wx = window["wx"];
  9. /**
  10. *
  11. */
  12. static get ins() {
  13. if (!this._ins) {
  14. this._ins = new WxPlatform();
  15. }
  16. return this._ins;
  17. }
  18. wechat_setting() {
  19. console.log("1 执行 wechat_setting",WECHAT);
  20. if (!WECHAT) {
  21. return;
  22. }
  23. console.log("2 执行 wechat_setting");
  24. this.wx.showShareMenu({
  25. withShareTicket: true,
  26. menus: ['shareAppMessage', 'shareTimeline']
  27. });
  28. //upgrade
  29. const updateManager = this.wx.getUpdateManager()
  30. updateManager.onCheckForUpdate(function (res) {
  31. console.log("请求完新版本信息的回调", res.hasUpdate);
  32. })
  33. updateManager.onUpdateReady(function () {
  34. this.wx.showModal({
  35. title: '更新提示',
  36. content: '新版本已经准备好,是否重启应用?',
  37. success(res) {
  38. if (res.confirm) {
  39. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  40. updateManager.applyUpdate()
  41. }
  42. }
  43. })
  44. })
  45. updateManager.onUpdateFailed(function () {
  46. // 新版本下载失败
  47. console.log("新版本下载失败")
  48. })
  49. }
  50. /**
  51. *
  52. * @param delay
  53. * @returns
  54. */
  55. show_interstitial(delay = 0) {
  56. console.log("显示差评广告》〉》〉》〉》〉》〉》〉》");
  57. if (!WECHAT) {
  58. return;
  59. }
  60. // let id = Main.ins.Platforms[0].intersitialId[0];
  61. let ad_video_id = Global.ad_intersitial_id;
  62. let interstitialAd = this.wx.createInterstitialAd({
  63. adUnitId: ad_video_id.trim(),
  64. });
  65. if (interstitialAd) {
  66. interstitialAd.load().then(() => {
  67. console.log('插屏 广告加载成功');
  68. });
  69. interstitialAd.onClose(res => {
  70. console.log('关闭插屏广告', res);
  71. });
  72. interstitialAd.onError(err => {
  73. console.log('err:插屏广告加载失败', err);
  74. });
  75. setTimeout(() => {
  76. interstitialAd.show().catch((err) => {
  77. console.log('插屏广告展示失败', err);
  78. });
  79. }, delay * 1000);
  80. }
  81. }
  82. rewardVideo2: any;
  83. /**
  84. *
  85. * @param callback
  86. */
  87. show_reward_video(callback: Function) {
  88. let my = this;
  89. let ad_video_id = Global.ad_video_id;
  90. // let id = Main.ins.Platforms[0].videoId[0];
  91. if (my.rewardVideo2 != null) {
  92. my.rewardVideo2.offClose(fun);
  93. }
  94. let rewardedVideoAd = this.wx.createRewardedVideoAd({
  95. adUnitId: ad_video_id.trim(),
  96. });
  97. my.rewardVideo2 = rewardedVideoAd;
  98. rewardedVideoAd.load().then(() => {
  99. this.wx.showToast({
  100. title: "加载中,请稍后",
  101. icon: 'success',//图标,支持"success"、"loading" 
  102. duration: 1500,//提示的延迟时间,单位毫秒,默认:1500 
  103. mask: false,//是否显示透明蒙层,防止触摸穿透,默认:false 
  104. success: function () { },
  105. fail: function () { },
  106. complete: function () { }
  107. })
  108. console.log('激励视频 广告加载成功');
  109. rewardedVideoAd.show();
  110. });
  111. rewardedVideoAd.onError(err => {
  112. console.log('激励视频 广告显示失败', err);
  113. this.wx.showToast({
  114. title: "请稍后再试",
  115. icon: 'fail',//图标,支持"success"、"loading" 
  116. duration: 1500,//提示的延迟时间,单位毫秒,默认:1500 
  117. mask: false,//是否显示透明蒙层,防止触摸穿透,默认:false 
  118. success: function () { },
  119. fail: function () { },
  120. complete: function () { }
  121. })
  122. callback(2);
  123. })
  124. var fun = function (res) {
  125. if (res && res.isEnded) {
  126. console.log('res: ', res);
  127. callback(1);
  128. rewardedVideoAd.offClose(fun);
  129. } else {
  130. console.log('播放中途退出');
  131. callback(0);
  132. }
  133. }
  134. rewardedVideoAd.onClose(fun);
  135. }
  136. }