WeChatPlatform.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. this.wx.navigateToMiniProgram({
  112. appId: data.app_id,
  113. path: data.app_path,
  114. success: (res) =>
  115. {
  116. LiangLiangSDK.CpaReport(data);
  117. if(success!=null){
  118. success();
  119. }
  120. },
  121. fail: (res) => {
  122. if(failure!=null){
  123. failure();
  124. }
  125. },
  126. complete: (res) => {},
  127. });
  128. }
  129. shareMessage(data: any, success?: Function, failure?: Function): void {
  130. throw new Error('Method not implemented.');
  131. }
  132. onShareAppMessage():void{
  133. try {
  134. this.wx.showShareMenu({
  135. withShareTicket: true
  136. });
  137. let shareTitle = this._shareTitles[Math.floor(Math.random()*this._shareTitles.length)]
  138. this.wx.onShareAppMessage(()=>{
  139. return {
  140. title: shareTitle,
  141. imageUrl: "https://www.maoxingame.com/QiangShenZhanJi/share/share1.jpg"
  142. }
  143. })
  144. } catch (error) {
  145. }
  146. }
  147. showBanner(data: any): void {
  148. console.log("显示Banner");
  149. if(this.__bannerLoop){
  150. this.__bannerLoop.showBanner();
  151. }
  152. }
  153. hideBanner(): void {
  154. console.log("隐藏Banner");
  155. if(this.__bannerLoop){
  156. this.__bannerLoop.hideBanner();
  157. }
  158. }
  159. showRewardedVideo(success?: Function, failure?: Function): void {
  160. console.log("显示激励广告");
  161. this.__rewardedSuccess = success;
  162. this.__rewardedfailure = failure;
  163. if (this.__rewardedVideoAd) {
  164. this.__rewardedVideoAd.show().catch(err => {
  165. this.__rewardedVideoAd.load().then(() => this.__rewardedVideoAd.show())
  166. });
  167. }
  168. }
  169. showInsertAd(success?: Function, failure?: Function): void {
  170. throw new Error('Method not implemented.');
  171. }
  172. showNativeAd(success?: Function, failure?: Function): void {
  173. throw new Error('Method not implemented.');
  174. }
  175. sendEvent(eventName: string, data?: any): void {
  176. throw new Error('Method not implemented.');
  177. }
  178. setLoadingProgress(progress: number): void {
  179. throw new Error('Method not implemented.');
  180. }
  181. loadingComplete(completeHandler?: Function): void {
  182. throw new Error('Method not implemented.');
  183. }
  184. getRandomPageAd(num: number): any[] {
  185. return LiangLiangSDK.GetList();
  186. }
  187. /**
  188. * 通用打点
  189. * @param str 打点数据
  190. * @param num 参数
  191. */
  192. branchAnalytics(str: string, num?: string): void {
  193. try {
  194. if (this.wx) {
  195. let Analytics = {}
  196. if (num) {
  197. Analytics = {
  198. branchId: str,
  199. branchDim: num,
  200. eventType: 1
  201. }
  202. } else {
  203. Analytics = {
  204. branchId: str,
  205. eventType: 1
  206. }
  207. }
  208. this.wx.reportUserBehaviorBranchAnalytics(Analytics);
  209. }
  210. } catch (error) {
  211. console.log("打点失败:", str)
  212. }
  213. }
  214. ///////////下面的版本,如果上面成功,即下个版本删除2020-10-19, 1.0.7//////////////////////
  215. /**
  216. * 开始游戏
  217. * @param num
  218. */
  219. startBranchAnalytics(num: string): void {
  220. try {
  221. if (this.wx) {
  222. this.wx.reportUserBehaviorBranchAnalytics({
  223. branchId: 'BCBgAAoXHx5d0WLb_vUvr7',
  224. branchDim: num,
  225. eventType: 1
  226. });
  227. }
  228. } catch (error) {
  229. }
  230. }
  231. /**
  232. * 成功
  233. */
  234. successBranchAnalytics(num: string): void {
  235. try {
  236. if (this.wx) {
  237. this.wx.reportUserBehaviorBranchAnalytics({
  238. branchId: 'BCBgAAoXHx5d0WLb_vUvr-',
  239. branchDim: num,
  240. eventType: 1
  241. });
  242. }
  243. } catch (error) {
  244. }
  245. }
  246. /**
  247. * 失败
  248. */
  249. failBranchAnalytics(num: string): void {
  250. try {
  251. if (this.wx) {
  252. this.wx.reportUserBehaviorBranchAnalytics({
  253. branchId: 'BCBgAAoXHx5d0WLb_vUvr9',
  254. branchDim: num,
  255. eventType: 1
  256. });
  257. }
  258. } catch (error) {
  259. }
  260. }
  261. }