WeChatPlatform.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. //分享图片
  32. private _shareImage: string[] = [
  33. 'W1rJy8OeTMy3A4w+fStBZw==',
  34. '5ZVIdG5hQGGaCs0j2LplTg==',
  35. 'QqKo2705RAWkAdz0FaX0yQ=='
  36. ]
  37. private _shareInfo:any[] = [
  38. {
  39. imageUrlId: 'W1rJy8OeTMy3A4w+fStBZw==',
  40. imageUrl: 'https://mmocgame.qpic.cn/wechatgame/oibRozFPq5kVrHgYD9aBicGTxzPOOPvHfia8TZdkQ7BnzsEuBfFKcianic9mCdq2OiaKod/0'
  41. },
  42. {
  43. imageUrlId: '5ZVIdG5hQGGaCs0j2LplTg==',
  44. imageUrl: 'https://mmocgame.qpic.cn/wechatgame/oibRozFPq5kVm5USa9okMA4qqvWe3IrMKWu1PZp2NbGWH3gSmaiazcJ5lwzYlcEg8Z/0'
  45. },
  46. {
  47. imageUrlId: 'QqKo2705RAWkAdz0FaX0yQ==',
  48. imageUrl: 'https://mmocgame.qpic.cn/wechatgame/oibRozFPq5kVdXISc8e9SQnmPCHRI0r72q7lUyfiaMkfDuLaw5E8vVQTPMod4nc93J/0'
  49. },
  50. ]
  51. constructor() {
  52. super()
  53. this.__systemInfo = this.wx.getSystemInfoSync();
  54. this.__windowWidth = this.__systemInfo.screenWidth;
  55. this.__windowHeight = this.__systemInfo.screenHeight;
  56. }
  57. login(success: Function, failure: Function): void {
  58. this.__bannerLoop = new BannerLoop();
  59. this.__bannerLoop.init(2, this.__bannerIDs, this.__bannerCreate.bind(this), this.__bannerDestory.bind(this));
  60. //激励广告
  61. this.__initRewardedVideoAd();
  62. }
  63. private __initRewardedVideoAd(): void {
  64. try {
  65. //提前创建广告
  66. this.__rewardedVideoAd = this.wx.createRewardedVideoAd({ adUnitId: 'adunit-06ba7bdaa32c6cc5' })
  67. //拉取成功
  68. this.__rewardedVideoAd.onLoad(() => {
  69. console.log("激励广告拉取成功");
  70. });
  71. //拉取失败
  72. this.__rewardedVideoAd.onError(err => {
  73. console.log("激励广告拉取失败");
  74. });
  75. //激励广告回调
  76. this.__rewardedVideoAd.onClose(res => {
  77. // 用户点击了【关闭广告】按钮
  78. // 小于 2.1.0 的基础库版本,res 是一个 undefined
  79. if (res && res.isEnded || res === undefined) {
  80. console.log("广告看完了");
  81. // 正常播放结束,可以下发游戏奖励
  82. if (this.__rewardedSuccess) {
  83. this.__rewardedSuccess();
  84. this.__rewardedSuccess = null;
  85. }
  86. this.__rewardedfailure = null;
  87. }
  88. else {
  89. console.log("看广告中途退出!");
  90. // 播放中途退出,不下发游戏奖励
  91. if (this.__rewardedfailure) {
  92. this.__rewardedfailure();
  93. this.__rewardedfailure = null;
  94. }
  95. this.__rewardedSuccess = null;
  96. }
  97. });
  98. } catch (error) {
  99. console.error("初始化激励广告失败");
  100. }
  101. }
  102. private __bannerCreate(id: string): any {
  103. let targetBannerAdWidth: number = this.__windowWidth;
  104. let resizeBool: boolean = false;
  105. let banner: any = this.wx.createBannerAd(
  106. {
  107. adUnitId: id,
  108. style: {
  109. width: targetBannerAdWidth,
  110. left: 0,
  111. top: this.__windowHeight - (targetBannerAdWidth / 16 * 9)
  112. }
  113. }
  114. );
  115. // 尺寸调整时会触发回调
  116. // 注意:如果在回调里再次调整尺寸,要确保不要触发死循环!!!
  117. banner.onResize(size => {
  118. if (resizeBool == false) {
  119. resizeBool = true;
  120. banner.style.top = this.__windowHeight - size.height;
  121. banner.style.left = (this.__windowWidth - size.width) / 2;
  122. }
  123. });
  124. banner.onError(err => {
  125. console.log("err", err)
  126. })
  127. return banner;
  128. }
  129. private __bannerDestory(banner: any): void {
  130. banner.destroy();
  131. }
  132. navigate2Mini(data: any, success?: Function, failure?: Function): void {
  133. throw new Error('Method not implemented.');
  134. }
  135. shareMessage(data: any, success?: Function, failure?: Function): void {
  136. throw new Error('Method not implemented.');
  137. }
  138. onShareAppMessage(): void {
  139. try {
  140. this.wx.showShareMenu({
  141. withShareTicket: true
  142. });
  143. let shareTitle = this._shareTitles[Math.floor(Math.random() * this._shareTitles.length)]
  144. let shareinfo = this._shareInfo[Math.floor(Math.random() * this._shareInfo.length)]
  145. let imageUrlId = shareinfo.imageUrlId
  146. let imageUrl = shareinfo.imageUrl
  147. this.wx.onShareAppMessage(() => {
  148. return {
  149. title: shareTitle,
  150. imageUrlId: imageUrlId,
  151. imageUrl: imageUrl
  152. }
  153. })
  154. } catch (error) {
  155. }
  156. }
  157. showBanner(data: any): void {
  158. console.log("显示Banner");
  159. this.__bannerLoop.showBanner();
  160. }
  161. hideBanner(): void {
  162. console.log("隐藏Banner");
  163. this.__bannerLoop.hideBanner();
  164. }
  165. showRewardedVideo(success?: Function, failure?: Function): void {
  166. console.log("显示激励广告");
  167. this.__rewardedSuccess = success;
  168. this.__rewardedfailure = failure;
  169. if (this.__rewardedVideoAd) {
  170. this.__rewardedVideoAd.show().catch(err => {
  171. this.__rewardedVideoAd.load().then(() => this.__rewardedVideoAd.show())
  172. });
  173. }
  174. }
  175. showInsertAd(success?: Function, failure?: Function): void {
  176. throw new Error('Method not implemented.');
  177. }
  178. showNativeAd(success?: Function, failure?: Function): void {
  179. throw new Error('Method not implemented.');
  180. }
  181. sendEvent(eventName: string, data?: any): void {
  182. throw new Error('Method not implemented.');
  183. }
  184. setLoadingProgress(progress: number): void {
  185. throw new Error('Method not implemented.');
  186. }
  187. loadingComplete(completeHandler?: Function): void {
  188. throw new Error('Method not implemented.');
  189. }
  190. getRandomPageAd(num: number): any[] {
  191. throw new Error('Method not implemented.');
  192. }
  193. /**
  194. * 通用打点
  195. * @param str 打点数据
  196. * @param num 参数
  197. */
  198. branchAnalytics(str: string, num?: string): void {
  199. try {
  200. if (this.wx) {
  201. let Analytics = {}
  202. if (num) {
  203. Analytics = {
  204. branchId: str,
  205. branchDim: num,
  206. eventType: 1
  207. }
  208. } else {
  209. Analytics = {
  210. branchId: str,
  211. eventType: 1
  212. }
  213. }
  214. this.wx.reportUserBehaviorBranchAnalytics(Analytics);
  215. }
  216. } catch (error) {
  217. console.log("打点失败:", str)
  218. }
  219. }
  220. ///////////下面的版本,如果上面成功,即下个版本删除2020-10-19, 1.0.7//////////////////////
  221. /**
  222. * 开始游戏
  223. * @param num
  224. */
  225. startBranchAnalytics(num: string): void {
  226. try {
  227. if (this.wx) {
  228. this.wx.reportUserBehaviorBranchAnalytics({
  229. branchId: 'BCBgAAoXHx5d0WLb_vUvr7',
  230. branchDim: num,
  231. eventType: 1
  232. });
  233. }
  234. } catch (error) {
  235. }
  236. }
  237. /**
  238. * 成功
  239. */
  240. successBranchAnalytics(num: string): void {
  241. try {
  242. if (this.wx) {
  243. this.wx.reportUserBehaviorBranchAnalytics({
  244. branchId: 'BCBgAAoXHx5d0WLb_vUvr-',
  245. branchDim: num,
  246. eventType: 1
  247. });
  248. }
  249. } catch (error) {
  250. }
  251. }
  252. /**
  253. * 失败
  254. */
  255. failBranchAnalytics(num: string): void {
  256. try {
  257. if (this.wx) {
  258. this.wx.reportUserBehaviorBranchAnalytics({
  259. branchId: 'BCBgAAoXHx5d0WLb_vUvr9',
  260. branchDim: num,
  261. eventType: 1
  262. });
  263. }
  264. } catch (error) {
  265. }
  266. }
  267. }