Palt.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. import Log from "./util/Log";
  2. import GameUtil from "./util/GameUtil";
  3. export default class Plat {
  4. public static screenWidth = 750;
  5. public static screenHeight = 1334;
  6. public static userinfo: any = null;
  7. public static plat_name = "cocos";
  8. public static pt: any = {};
  9. //记录一些微信数据
  10. public static code: string = "";
  11. public static sessionKey: string = "";
  12. public static isWX(): boolean {
  13. return this.plat_name == "wx";
  14. }
  15. public static isCocos() {
  16. return this.plat_name == "cocos";
  17. }
  18. public static getGameName() {
  19. return this.plat_name + "_hwdfk"; //好玩的方块
  20. }
  21. public static init() {
  22. // Log.warn("平台初始化123");
  23. let info: any = {};
  24. info.nickName = "临时玩家" + GameUtil.randomRange(1, 1000);
  25. info.avatarUrl = "res/1.png";
  26. this.userinfo = info;
  27. if (cc.sys.platform == cc.sys.WECHAT_GAME) {
  28. // Log.warn("微信平台初始化");
  29. this.plat_name = "wx";
  30. this.pt = wx;
  31. wx.showShareMenu({
  32. menus: ['shareAppMessage', "shareTimeline"],
  33. // withShareTicket:true
  34. })
  35. wx.onShareAppMessage(function () {
  36. return {
  37. title: '你能救救小猫吗?',
  38. imageUrl: ""
  39. }
  40. })
  41. if (wx.onShareTimeline != null) {
  42. wx.onShareTimeline(function () {
  43. return {
  44. title: '你能救救小猫吗?',
  45. imageUrl: ""
  46. }
  47. })
  48. }
  49. this.initAds()
  50. }
  51. //注册显示回调
  52. if (this.pt.onShow != null) {
  53. this.pt.onShow(this.onshow);
  54. }
  55. //屏幕尺寸设置
  56. if (this.pt.getSystemInfoSync) {
  57. let system = this.pt.getSystemInfoSync();
  58. this.screenWidth = system.screenWidth;
  59. this.screenHeight = system.screenHeight;
  60. // console.warn("屏幕宽:"+system.screenWidth);
  61. // console.warn("屏幕高:"+system.screenHeight);
  62. }
  63. }
  64. private static fun_obj: any;
  65. private static share_obj: any;
  66. public static setShareCallback(obj: any, fun: any) {
  67. Plat.share_obj = obj;
  68. Plat.fun_obj = fun;
  69. }
  70. public static state = 0;
  71. //显示回调
  72. public static onshow(res) {
  73. // Log.warn("onshow-----")
  74. // Log.error(res)
  75. if (Plat.fun_obj != null) {
  76. Plat.fun_obj.call(Plat.share_obj);
  77. Plat.fun_obj = null;
  78. Plat.share_obj = null;
  79. }
  80. if (this.state == 0) {
  81. // AdInterstitial.show();
  82. }
  83. this.state = 0;
  84. }
  85. //显示回调
  86. public static getLaunchOptionsSync(res) {
  87. // Log.warn("getLaunchOptionsSync-----")
  88. // Log.error(res)
  89. }
  90. public static showToast(str, jg = 2000) {
  91. if (this.pt.showToast != null) {
  92. this.pt.showToast({
  93. title: str,
  94. content: str,
  95. message: str,
  96. icon: 'none',
  97. duration: jg
  98. })
  99. }
  100. }
  101. public static hideToast() {
  102. if (this.pt.hideToast != null) {
  103. this.pt.hideToast({
  104. })
  105. }
  106. }
  107. public static shareAppMessage() {
  108. Plat.state = 1;
  109. if (this.pt.shareAppMessage == null) {
  110. if (Plat.fun_obj != null) {
  111. Plat.fun_obj.call(Plat.share_obj);
  112. Plat.fun_obj = null;
  113. Plat.share_obj = null;
  114. }
  115. return;
  116. };
  117. this.pt.shareAppMessage({
  118. title: '你能救救小猫吗?',
  119. imageUrl: "",
  120. desc: "-_-来玩玩吧"
  121. })
  122. }
  123. public static setRank(level) {
  124. let data = {
  125. event: 'score',
  126. level: (level) + ''
  127. }
  128. console.log('设置分数:', data.level)
  129. this.postMessage(data)
  130. }
  131. public static updateRank() {
  132. this.postMessage({
  133. event: 'update'
  134. })
  135. }
  136. public static postMessage(object: any) {
  137. if (cc.sys.platform == cc.sys.WECHAT_GAME) {
  138. object.type = 'engine';
  139. this.pt.getOpenDataContext().postMessage(object)
  140. }
  141. }
  142. public static isBigSize() {
  143. if (Plat.screenHeight > Plat.screenWidth * 2) {
  144. return true;
  145. }
  146. return false;
  147. }
  148. private static rewardVideo2
  149. public static showRewardVideo(callback: Function) {
  150. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  151. callback(true);
  152. return;
  153. }
  154. let id = ""
  155. if (this.rewardVideo2 != null) {
  156. this.rewardVideo2.offClose(fun);
  157. }
  158. let rewardedVideoAd = wx.createRewardedVideoAd({
  159. adUnitId: id,
  160. });
  161. this.rewardVideo2 = rewardedVideoAd;
  162. rewardedVideoAd.load().then(() => {
  163. wx.showToast({
  164. title: "加载中,请稍后",
  165. icon: 'success',//图标,支持"success"、"loading" 
  166. duration: 1500,//提示的延迟时间,单位毫秒,默认:1500 
  167. mask: false,//是否显示透明蒙层,防止触摸穿透,默认:false 
  168. success: function () { },
  169. fail: function () { },
  170. complete: function () { }
  171. })
  172. console.log('激励视频 广告加载成功');
  173. rewardedVideoAd.show();
  174. });
  175. rewardedVideoAd.onError(err => {
  176. console.log('激励视频 广告显示失败', err);
  177. wx.showToast({
  178. title: "请稍后再试",
  179. icon: 'fail',//图标,支持"success"、"loading" 
  180. duration: 1500,//提示的延迟时间,单位毫秒,默认:1500 
  181. mask: false,//是否显示透明蒙层,防止触摸穿透,默认:false 
  182. success: function () { },
  183. fail: function () { },
  184. complete: function () { }
  185. })
  186. callback(false);
  187. })
  188. var fun = function (res) {
  189. if (res && res.isEnded) {
  190. console.log('res: ', res);
  191. callback(true);
  192. rewardedVideoAd.offClose(fun);
  193. } else {
  194. console.log('播放中途退出');
  195. callback(false);
  196. }
  197. }
  198. rewardedVideoAd.onClose(fun);
  199. }
  200. private static bannerAd = null
  201. private static interstitialAd = null
  202. public static initAds() {
  203. if (!wx) {
  204. return;
  205. }
  206. var size = cc.view.getFrameSize()
  207. this.bannerAd = wx.createBannerAd({
  208. adUnitId: '',
  209. style: {
  210. left: 0,
  211. top: size.height - 140,
  212. width: size.width
  213. }
  214. })
  215. this.bannerAd.onResize(res => {
  216. this.bannerAd.style.top = size.height - this.bannerAd.style.realHeight;
  217. });
  218. this.bannerAd.onError(err => {
  219. console.error(err.errMsg)
  220. });
  221. if (wx.createInterstitialAd) {
  222. this.interstitialAd = wx.createInterstitialAd({
  223. adUnitId: ''
  224. })
  225. }
  226. }
  227. public static showBanner() {
  228. if (this.bannerAd) {
  229. this.bannerAd.show()
  230. }
  231. }
  232. public static hideBanner() {
  233. if (this.bannerAd) {
  234. this.bannerAd.hide()
  235. }
  236. }
  237. public static showInterstitialAd() {
  238. if (this.interstitialAd) {
  239. this.interstitialAd.show().catch((err) => {
  240. console.error('插屏广告显示失败', err)
  241. })
  242. }
  243. }
  244. }