WeChatPlatform.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. if(this.__bannerLoop){
  134. this.__bannerLoop.showBanner();
  135. }
  136. }
  137. hideBanner(): void {
  138. console.log("隐藏Banner");
  139. if(this.__bannerLoop){
  140. this.__bannerLoop.hideBanner();
  141. }
  142. }
  143. showRewardedVideo(success?: Function, failure?: Function): void {
  144. console.log("显示激励广告");
  145. this.__rewardedSuccess = success;
  146. this.__rewardedfailure = failure;
  147. if (this.__rewardedVideoAd) {
  148. this.__rewardedVideoAd.show().catch(err => {
  149. this.__rewardedVideoAd.load().then(() => this.__rewardedVideoAd.show())
  150. });
  151. }
  152. }
  153. showInsertAd(success?: Function, failure?: Function): void {
  154. throw new Error('Method not implemented.');
  155. }
  156. showNativeAd(success?: Function, failure?: Function): void {
  157. throw new Error('Method not implemented.');
  158. }
  159. sendEvent(eventName: string, data?: any): void {
  160. throw new Error('Method not implemented.');
  161. }
  162. setLoadingProgress(progress: number): void {
  163. throw new Error('Method not implemented.');
  164. }
  165. loadingComplete(completeHandler?: Function): void {
  166. throw new Error('Method not implemented.');
  167. }
  168. getRandomPageAd(num: number): any[] {
  169. return LiangLiangSDK.GetList();
  170. }
  171. /**
  172. * 通用打点
  173. * @param str 打点数据
  174. * @param num 参数
  175. */
  176. branchAnalytics(str: string, num?: string): void {
  177. try {
  178. if (this.wx) {
  179. let Analytics = {}
  180. if (num) {
  181. Analytics = {
  182. branchId: str,
  183. branchDim: num,
  184. eventType: 1
  185. }
  186. } else {
  187. Analytics = {
  188. branchId: str,
  189. eventType: 1
  190. }
  191. }
  192. this.wx.reportUserBehaviorBranchAnalytics(Analytics);
  193. }
  194. } catch (error) {
  195. console.log("打点失败:", str)
  196. }
  197. }
  198. ///////////下面的版本,如果上面成功,即下个版本删除2020-10-19, 1.0.7//////////////////////
  199. /**
  200. * 开始游戏
  201. * @param num
  202. */
  203. startBranchAnalytics(num: string): void {
  204. try {
  205. if (this.wx) {
  206. this.wx.reportUserBehaviorBranchAnalytics({
  207. branchId: 'BCBgAAoXHx5d0WLb_vUvr7',
  208. branchDim: num,
  209. eventType: 1
  210. });
  211. }
  212. } catch (error) {
  213. }
  214. }
  215. /**
  216. * 成功
  217. */
  218. successBranchAnalytics(num: string): void {
  219. try {
  220. if (this.wx) {
  221. this.wx.reportUserBehaviorBranchAnalytics({
  222. branchId: 'BCBgAAoXHx5d0WLb_vUvr-',
  223. branchDim: num,
  224. eventType: 1
  225. });
  226. }
  227. } catch (error) {
  228. }
  229. }
  230. /**
  231. * 失败
  232. */
  233. failBranchAnalytics(num: string): void {
  234. try {
  235. if (this.wx) {
  236. this.wx.reportUserBehaviorBranchAnalytics({
  237. branchId: 'BCBgAAoXHx5d0WLb_vUvr9',
  238. branchDim: num,
  239. eventType: 1
  240. });
  241. }
  242. } catch (error) {
  243. }
  244. }
  245. }