WxPlatform.ts 4.5 KB

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