WeChatPlatform.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. //分享语录
  19. private _shareTitles:string[]=[
  20. '打僵尸,我是最强的,不服来战',
  21. '你的空投到了!点击领取!',
  22. '什么,这游戏还能这么玩?!',
  23. '这一波我有点扛不住啊!',
  24. '枪神?我预定了,不服来战!',
  25. '我终于得到了“哒哒哒”冒蓝光的加特林',
  26. '老同学,还记得当年一起打的僵尸吗',
  27. '人类!就你这样的手速,最后将会沦为我的食物!',
  28. '啊呜,啊呜,啊呜,虽然二哈叫声也这样,但我可是会吃人的!',
  29. '当我扣下扳机的那一刻,请你从哪里来回哪去!'
  30. ]
  31. constructor() {
  32. super()
  33. this.__systemInfo = this.wx.getSystemInfoSync();
  34. this.__windowWidth = this.__systemInfo.screenWidth;
  35. this.__windowHeight = this.__systemInfo.screenHeight;
  36. }
  37. login(success: Function, failure: Function): void {
  38. this.__bannerLoop = new BannerLoop();
  39. this.__bannerLoop.init(2, this.__bannerIDs, this.__bannerCreate.bind(this), this.__bannerDestory.bind(this));
  40. //激励广告
  41. this.__initRewardedVideoAd();
  42. }
  43. private __initRewardedVideoAd(): void {
  44. try {
  45. //提前创建广告
  46. this.__rewardedVideoAd = this.wx.createRewardedVideoAd({ adUnitId: 'adunit-06ba7bdaa32c6cc5' })
  47. //拉取成功
  48. this.__rewardedVideoAd.onLoad(() => {
  49. console.log("激励广告拉取成功");
  50. });
  51. //拉取失败
  52. this.__rewardedVideoAd.onError(err => {
  53. console.log("激励广告拉取失败");
  54. });
  55. //激励广告回调
  56. this.__rewardedVideoAd.onClose(res => {
  57. // 用户点击了【关闭广告】按钮
  58. // 小于 2.1.0 的基础库版本,res 是一个 undefined
  59. if (res && res.isEnded || res === undefined) {
  60. console.log("广告看完了");
  61. // 正常播放结束,可以下发游戏奖励
  62. if (this.__rewardedSuccess) {
  63. this.__rewardedSuccess();
  64. this.__rewardedSuccess = null;
  65. }
  66. this.__rewardedfailure = null;
  67. }
  68. else {
  69. console.log("看广告中途退出!");
  70. // 播放中途退出,不下发游戏奖励
  71. if (this.__rewardedfailure) {
  72. this.__rewardedfailure();
  73. this.__rewardedfailure = null;
  74. }
  75. this.__rewardedSuccess = null;
  76. }
  77. });
  78. } catch (error) {
  79. console.error("初始化激励广告失败");
  80. }
  81. }
  82. private __bannerCreate(id: string): any {
  83. let targetBannerAdWidth: number = this.__windowWidth;
  84. let resizeBool: boolean = false;
  85. let banner: any = this.wx.createBannerAd(
  86. {
  87. adUnitId: id,
  88. style: {
  89. width: targetBannerAdWidth,
  90. left: 0,
  91. top: this.__windowHeight - (targetBannerAdWidth / 16 * 9)
  92. }
  93. }
  94. );
  95. // 尺寸调整时会触发回调
  96. // 注意:如果在回调里再次调整尺寸,要确保不要触发死循环!!!
  97. banner.onResize(size => {
  98. if (resizeBool == false) {
  99. resizeBool = true;
  100. banner.style.top = this.__windowHeight - size.height;
  101. banner.style.left = (this.__windowWidth - size.width) / 2;
  102. }
  103. });
  104. banner.onError(err => {
  105. console.log("err", err)
  106. })
  107. return banner;
  108. }
  109. private __bannerDestory(banner: any): void {
  110. banner.destroy();
  111. }
  112. navigate2Mini(data: any, success?: Function, failure?: Function): void {
  113. throw new Error('Method not implemented.');
  114. }
  115. shareMessage(data: any, success?: Function, failure?: Function): void {
  116. throw new Error('Method not implemented.');
  117. }
  118. onShareAppMessage():void{
  119. try {
  120. this.wx.showShareMenu({
  121. withShareTicket: true
  122. });
  123. let shareTitle = this._shareTitles[Math.floor(Math.random()*this._shareTitles.length)]
  124. this.wx.onShareAppMessage(()=>{
  125. return {
  126. title: shareTitle,
  127. imageUrl: "https://www.maoxingame.com/QiangShenZhanJi/share/share1.jpg"
  128. }
  129. })
  130. } catch (error) {
  131. }
  132. }
  133. showBanner(data: any): void {
  134. console.log("显示Banner");
  135. this.__bannerLoop.showBanner();
  136. }
  137. hideBanner(): void {
  138. console.log("隐藏Banner");
  139. this.__bannerLoop.hideBanner();
  140. }
  141. showRewardedVideo(success?: Function, failure?: Function): void {
  142. console.log("显示激励广告");
  143. this.__rewardedSuccess = success;
  144. this.__rewardedfailure = failure;
  145. if (this.__rewardedVideoAd) {
  146. this.__rewardedVideoAd.show().catch(err => {
  147. this.__rewardedVideoAd.load().then(() => this.__rewardedVideoAd.show())
  148. });
  149. }
  150. }
  151. showInsertAd(success?: Function, failure?: Function): void {
  152. throw new Error('Method not implemented.');
  153. }
  154. showNativeAd(success?: Function, failure?: Function): void {
  155. throw new Error('Method not implemented.');
  156. }
  157. sendEvent(eventName: string, data?: any): void {
  158. throw new Error('Method not implemented.');
  159. }
  160. setLoadingProgress(progress: number): void {
  161. throw new Error('Method not implemented.');
  162. }
  163. loadingComplete(completeHandler?: Function): void {
  164. throw new Error('Method not implemented.');
  165. }
  166. getRandomPageAd(num: number): any[] {
  167. throw new Error('Method not implemented.');
  168. }
  169. /**
  170. * 通用打点
  171. * @param str 打点数据
  172. * @param num 参数
  173. */
  174. branchAnalytics(str: string, num?: string): void {
  175. try {
  176. if (this.wx) {
  177. let Analytics = {}
  178. if (num) {
  179. Analytics = {
  180. branchId: str,
  181. branchDim: num,
  182. eventType: 1
  183. }
  184. } else {
  185. Analytics = {
  186. branchId: str,
  187. eventType: 1
  188. }
  189. }
  190. this.wx.reportUserBehaviorBranchAnalytics(Analytics);
  191. }
  192. } catch (error) {
  193. console.log("打点失败:", str)
  194. }
  195. }
  196. ///////////下面的版本,如果上面成功,即下个版本删除2020-10-19, 1.0.7//////////////////////
  197. /**
  198. * 开始游戏
  199. * @param num
  200. */
  201. startBranchAnalytics(num: string): void {
  202. try {
  203. if (this.wx) {
  204. this.wx.reportUserBehaviorBranchAnalytics({
  205. branchId: 'BCBgAAoXHx5d0WLb_vUvr7',
  206. branchDim: num,
  207. eventType: 1
  208. });
  209. }
  210. } catch (error) {
  211. }
  212. }
  213. /**
  214. * 成功
  215. */
  216. successBranchAnalytics(num: string): void {
  217. try {
  218. if (this.wx) {
  219. this.wx.reportUserBehaviorBranchAnalytics({
  220. branchId: 'BCBgAAoXHx5d0WLb_vUvr-',
  221. branchDim: num,
  222. eventType: 1
  223. });
  224. }
  225. } catch (error) {
  226. }
  227. }
  228. /**
  229. * 失败
  230. */
  231. failBranchAnalytics(num: string): void {
  232. try {
  233. if (this.wx) {
  234. this.wx.reportUserBehaviorBranchAnalytics({
  235. branchId: 'BCBgAAoXHx5d0WLb_vUvr9',
  236. branchDim: num,
  237. eventType: 1
  238. });
  239. }
  240. } catch (error) {
  241. }
  242. }
  243. }