WeChatPlatforms.ts 9.3 KB

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