123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- import Log from "./util/Log";
- import GameUtil from "./util/GameUtil";
- export default class Plat {
- public static screenWidth = 750;
- public static screenHeight = 1334;
- public static userinfo: any = null;
- public static plat_name = "cocos";
- public static pt: any = {};
- //记录一些微信数据
- public static code: string = "";
- public static sessionKey: string = "";
- public static isWX(): boolean {
- return this.plat_name == "wx";
- }
- public static isCocos() {
- return this.plat_name == "cocos";
- }
- public static getGameName() {
- return this.plat_name + "_hwdfk"; //好玩的方块
- }
- public static init() {
- // Log.warn("平台初始化123");
- let info: any = {};
- info.nickName = "临时玩家" + GameUtil.randomRange(1, 1000);
- info.avatarUrl = "res/1.png";
- this.userinfo = info;
- if (cc.sys.platform == cc.sys.WECHAT_GAME) {
- // Log.warn("微信平台初始化");
- this.plat_name = "wx";
- this.pt = wx;
- wx.showShareMenu({
- menus: ['shareAppMessage', "shareTimeline"],
- // withShareTicket:true
- })
- wx.onShareAppMessage(function () {
- return {
- title: '你能救救小猫吗?',
- imageUrl: ""
- }
- })
- if (wx.onShareTimeline != null) {
- wx.onShareTimeline(function () {
- return {
- title: '你能救救小猫吗?',
- imageUrl: ""
- }
- })
- }
- this.initAds()
- }
- //注册显示回调
- if (this.pt.onShow != null) {
- this.pt.onShow(this.onshow);
- }
- //屏幕尺寸设置
- if (this.pt.getSystemInfoSync) {
- let system = this.pt.getSystemInfoSync();
- this.screenWidth = system.screenWidth;
- this.screenHeight = system.screenHeight;
- // console.warn("屏幕宽:"+system.screenWidth);
- // console.warn("屏幕高:"+system.screenHeight);
- }
- }
- private static fun_obj: any;
- private static share_obj: any;
- public static setShareCallback(obj: any, fun: any) {
- Plat.share_obj = obj;
- Plat.fun_obj = fun;
- }
- public static state = 0;
- //显示回调
- public static onshow(res) {
- // Log.warn("onshow-----")
- // Log.error(res)
- if (Plat.fun_obj != null) {
- Plat.fun_obj.call(Plat.share_obj);
- Plat.fun_obj = null;
- Plat.share_obj = null;
- }
- if (this.state == 0) {
- // AdInterstitial.show();
- }
- this.state = 0;
- }
- //显示回调
- public static getLaunchOptionsSync(res) {
- // Log.warn("getLaunchOptionsSync-----")
- // Log.error(res)
- }
- public static showToast(str, jg = 2000) {
- if (this.pt.showToast != null) {
- this.pt.showToast({
- title: str,
- content: str,
- message: str,
- icon: 'none',
- duration: jg
- })
- }
- }
- public static hideToast() {
- if (this.pt.hideToast != null) {
- this.pt.hideToast({
- })
- }
- }
- public static shareAppMessage() {
- Plat.state = 1;
- if (this.pt.shareAppMessage == null) {
- if (Plat.fun_obj != null) {
- Plat.fun_obj.call(Plat.share_obj);
- Plat.fun_obj = null;
- Plat.share_obj = null;
- }
- return;
- };
- this.pt.shareAppMessage({
- title: '你能救救小猫吗?',
- imageUrl: "",
- desc: "-_-来玩玩吧"
- })
- }
- public static setRank(level) {
- let data = {
- event: 'score',
- level: (level) + ''
- }
- console.log('设置分数:', data.level)
- this.postMessage(data)
- }
- public static updateRank() {
- this.postMessage({
- event: 'update'
- })
- }
- public static postMessage(object: any) {
- if (cc.sys.platform == cc.sys.WECHAT_GAME) {
- object.type = 'engine';
- this.pt.getOpenDataContext().postMessage(object)
- }
- }
- public static isBigSize() {
- if (Plat.screenHeight > Plat.screenWidth * 2) {
- return true;
- }
- return false;
- }
- private static rewardVideo2
- public static showRewardVideo(callback: Function) {
- if (cc.sys.platform != cc.sys.WECHAT_GAME) {
- callback(true);
- return;
- }
- let id = ""
- if (this.rewardVideo2 != null) {
- this.rewardVideo2.offClose(fun);
- }
- let rewardedVideoAd = wx.createRewardedVideoAd({
- adUnitId: id,
- });
- this.rewardVideo2 = rewardedVideoAd;
- rewardedVideoAd.load().then(() => {
- wx.showToast({
- title: "加载中,请稍后",
- icon: 'success',//图标,支持"success"、"loading"
- duration: 1500,//提示的延迟时间,单位毫秒,默认:1500
- mask: false,//是否显示透明蒙层,防止触摸穿透,默认:false
- success: function () { },
- fail: function () { },
- complete: function () { }
- })
- console.log('激励视频 广告加载成功');
- rewardedVideoAd.show();
- });
- rewardedVideoAd.onError(err => {
- console.log('激励视频 广告显示失败', err);
- wx.showToast({
- title: "请稍后再试",
- icon: 'fail',//图标,支持"success"、"loading"
- duration: 1500,//提示的延迟时间,单位毫秒,默认:1500
- mask: false,//是否显示透明蒙层,防止触摸穿透,默认:false
- success: function () { },
- fail: function () { },
- complete: function () { }
- })
- callback(false);
- })
- var fun = function (res) {
- if (res && res.isEnded) {
- console.log('res: ', res);
- callback(true);
- rewardedVideoAd.offClose(fun);
- } else {
- console.log('播放中途退出');
- callback(false);
- }
- }
- rewardedVideoAd.onClose(fun);
- }
- private static bannerAd = null
- private static interstitialAd = null
- public static initAds() {
- if (!wx) {
- return;
- }
- var size = cc.view.getFrameSize()
- this.bannerAd = wx.createBannerAd({
- adUnitId: '',
- style: {
- left: 0,
- top: size.height - 140,
- width: size.width
- }
- })
- this.bannerAd.onResize(res => {
- this.bannerAd.style.top = size.height - this.bannerAd.style.realHeight;
- });
- this.bannerAd.onError(err => {
- console.error(err.errMsg)
- });
- if (wx.createInterstitialAd) {
- this.interstitialAd = wx.createInterstitialAd({
- adUnitId: ''
- })
- }
- }
- public static showBanner() {
- if (this.bannerAd) {
- this.bannerAd.show()
- }
- }
- public static hideBanner() {
- if (this.bannerAd) {
- this.bannerAd.hide()
- }
- }
- public static showInterstitialAd() {
- if (this.interstitialAd) {
- this.interstitialAd.show().catch((err) => {
- console.error('插屏广告显示失败', err)
- })
- }
- }
- }
|