WeChatPlatform.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import { _decorator, Component, Node } from 'cc';
  2. import { NoticeManager } from '../../engines/notices/NoticeManager';
  3. import { BannerLoop } from '../BannerLoop';
  4. import { BasePlatform } from './BasePlatform';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('WeChatPlatform')
  7. export class WeChatPlatform extends BasePlatform {
  8. private wx = window['wx'];
  9. private __bannerIDs: string[] = ["adunit-91be4efcdebcba8d", "adunit-ff8ca74a9b61c63b"];
  10. private __bannerLoop: BannerLoop;
  11. private __windowWidth: number;
  12. private __windowHeight: number;
  13. //激励广告
  14. private __rewardedVideoAd: any;
  15. private __rewardedSuccess: Function;
  16. private __rewardedfailure: Function;
  17. private __systemInfo: any;
  18. constructor() {
  19. super()
  20. this.__systemInfo = this.wx.getSystemInfoSync();
  21. this.__windowWidth = this.__systemInfo.screenWidth;
  22. this.__windowHeight = this.__systemInfo.screenHeight;
  23. }
  24. login(success: Function, failure: Function): void {
  25. this.__bannerLoop = new BannerLoop();
  26. this.__bannerLoop.init(2, this.__bannerIDs, this.__bannerCreate.bind(this), this.__bannerDestory.bind(this));
  27. //激励广告
  28. this.__initRewardedVideoAd();
  29. }
  30. private __initRewardedVideoAd(): void {
  31. try {
  32. //提前创建广告
  33. this.__rewardedVideoAd = this.wx.createRewardedVideoAd({ adUnitId: 'adunit-06ba7bdaa32c6cc5' })
  34. //拉取成功
  35. this.__rewardedVideoAd.onLoad(() => {
  36. console.log("激励广告拉取成功");
  37. });
  38. //拉取失败
  39. this.__rewardedVideoAd.onError(err => {
  40. console.log("激励广告拉取失败");
  41. });
  42. //激励广告回调
  43. this.__rewardedVideoAd.onClose(res => {
  44. // 用户点击了【关闭广告】按钮
  45. // 小于 2.1.0 的基础库版本,res 是一个 undefined
  46. if (res && res.isEnded || res === undefined) {
  47. console.log("广告看完了");
  48. // 正常播放结束,可以下发游戏奖励
  49. if (this.__rewardedSuccess) {
  50. this.__rewardedSuccess();
  51. this.__rewardedSuccess = null;
  52. }
  53. this.__rewardedfailure = null;
  54. }
  55. else {
  56. console.log("看广告中途退出!");
  57. // 播放中途退出,不下发游戏奖励
  58. if (this.__rewardedfailure) {
  59. this.__rewardedfailure();
  60. this.__rewardedfailure = null;
  61. }
  62. this.__rewardedSuccess = null;
  63. }
  64. });
  65. } catch (error) {
  66. console.error("初始化激励广告失败");
  67. }
  68. }
  69. private __bannerCreate(id: string): any {
  70. let targetBannerAdWidth: number = this.__windowWidth;
  71. let resizeBool: boolean = false;
  72. let banner: any = this.wx.createBannerAd(
  73. {
  74. adUnitId: id,
  75. style: {
  76. width: targetBannerAdWidth,
  77. left: 0,
  78. top: this.__windowHeight - (targetBannerAdWidth / 16 * 9)
  79. }
  80. }
  81. );
  82. // 尺寸调整时会触发回调
  83. // 注意:如果在回调里再次调整尺寸,要确保不要触发死循环!!!
  84. banner.onResize(size => {
  85. if (resizeBool == false) {
  86. resizeBool = true;
  87. banner.style.top = this.__windowHeight - size.height;
  88. banner.style.left = (this.__windowWidth - size.width) / 2;
  89. }
  90. });
  91. banner.onError(err => {
  92. console.log("err", err)
  93. })
  94. return banner;
  95. }
  96. private __bannerDestory(banner: any): void {
  97. banner.destroy();
  98. }
  99. navigate2Mini(data: any, success?: Function, failure?: Function): void {
  100. throw new Error('Method not implemented.');
  101. }
  102. shareMessage(data: any, success?: Function, failure?: Function): void {
  103. throw new Error('Method not implemented.');
  104. }
  105. onShareAppMessage():void{
  106. try {
  107. this.wx.showShareMenu({
  108. withShareTicket: true
  109. });
  110. this.wx.onShareAppMessage(()=>{
  111. return {
  112. title: '打僵尸,我是最强的,不服来战',
  113. imageUrl: "https://www.maoxingame.com/QiangShenZhanJi/icons/10377_1.png"
  114. }
  115. })
  116. } catch (error) {
  117. }
  118. }
  119. showBanner(data: any): void {
  120. console.log("显示Banner");
  121. this.__bannerLoop.showBanner();
  122. }
  123. hideBanner(): void {
  124. console.log("隐藏Banner");
  125. this.__bannerLoop.hideBanner();
  126. }
  127. showRewardedVideo(success?: Function, failure?: Function): void {
  128. console.log("显示激励广告");
  129. this.__rewardedSuccess = success;
  130. this.__rewardedfailure = failure;
  131. if (this.__rewardedVideoAd) {
  132. this.__rewardedVideoAd.show().catch(err => {
  133. this.__rewardedVideoAd.load().then(() => this.__rewardedVideoAd.show())
  134. });
  135. }
  136. }
  137. showInsertAd(success?: Function, failure?: Function): void {
  138. throw new Error('Method not implemented.');
  139. }
  140. showNativeAd(success?: Function, failure?: Function): void {
  141. throw new Error('Method not implemented.');
  142. }
  143. sendEvent(eventName: string, data?: any): void {
  144. throw new Error('Method not implemented.');
  145. }
  146. setLoadingProgress(progress: number): void {
  147. throw new Error('Method not implemented.');
  148. }
  149. loadingComplete(completeHandler?: Function): void {
  150. throw new Error('Method not implemented.');
  151. }
  152. getRandomPageAd(num: number): any[] {
  153. throw new Error('Method not implemented.');
  154. }
  155. /**
  156. * 通用打点
  157. * @param str 打点数据
  158. * @param num 参数
  159. */
  160. branchAnalytics(str: string, num?: string): void {
  161. try {
  162. if (this.wx) {
  163. let Analytics = {}
  164. if (num) {
  165. Analytics = {
  166. branchId: str,
  167. branchDim: num,
  168. eventType: 1
  169. }
  170. } else {
  171. Analytics = {
  172. branchId: str,
  173. eventType: 1
  174. }
  175. }
  176. this.wx.reportUserBehaviorBranchAnalytics(Analytics);
  177. }
  178. } catch (error) {
  179. console.log("打点失败:", str)
  180. }
  181. }
  182. ///////////下面的版本,如果上面成功,即下个版本删除2020-10-19, 1.0.7//////////////////////
  183. /**
  184. * 开始游戏
  185. * @param num
  186. */
  187. startBranchAnalytics(num: string): void {
  188. try {
  189. if (this.wx) {
  190. this.wx.reportUserBehaviorBranchAnalytics({
  191. branchId: 'BCBgAAoXHx5d0WLb_vUvr7',
  192. branchDim: num,
  193. eventType: 1
  194. });
  195. }
  196. } catch (error) {
  197. }
  198. }
  199. /**
  200. * 成功
  201. */
  202. successBranchAnalytics(num: string): void {
  203. try {
  204. if (this.wx) {
  205. this.wx.reportUserBehaviorBranchAnalytics({
  206. branchId: 'BCBgAAoXHx5d0WLb_vUvr-',
  207. branchDim: num,
  208. eventType: 1
  209. });
  210. }
  211. } catch (error) {
  212. }
  213. }
  214. /**
  215. * 失败
  216. */
  217. failBranchAnalytics(num: string): void {
  218. try {
  219. if (this.wx) {
  220. this.wx.reportUserBehaviorBranchAnalytics({
  221. branchId: 'BCBgAAoXHx5d0WLb_vUvr9',
  222. branchDim: num,
  223. eventType: 1
  224. });
  225. }
  226. } catch (error) {
  227. }
  228. }
  229. }