WeChatPlatform.ts 8.5 KB

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