1084 lines
46 KiB
JavaScript
1084 lines
46 KiB
JavaScript
"use strict";
|
||
cc._RF.push(module, 'c1af9nd7gNA95YJ04h9DdNX', 'MiniGameSdk');
|
||
// Script/Sdk/MiniGameSdk.ts
|
||
|
||
"use strict";
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
exports.MiniGameSdk = void 0;
|
||
/**
|
||
* 小游戏平台SDK工具封装,目前只支持微信和抖音平台
|
||
*/
|
||
var MiniGameSdk;
|
||
(function (MiniGameSdk) {
|
||
function isWechat() {
|
||
//@ts-ignore
|
||
return window.wx !== null && window.wx !== undefined;
|
||
}
|
||
MiniGameSdk.isWechat = isWechat;
|
||
function isBytedance() {
|
||
//@ts-ignore
|
||
return window.tt !== null && window.tt !== undefined;
|
||
}
|
||
MiniGameSdk.isBytedance = isBytedance;
|
||
function getSysWinSize() {
|
||
var sys;
|
||
if (isWechat()) {
|
||
// @ts-ignore
|
||
sys = wx.getSystemInfoSync();
|
||
}
|
||
else if (isBytedance()) {
|
||
// @ts-ignore
|
||
sys = tt.getSystemInfoSync();
|
||
}
|
||
var size = { width: 0, height: 0 };
|
||
if (sys) {
|
||
size.width = sys.windowWidth;
|
||
size.height = sys.windowHeight;
|
||
}
|
||
return size;
|
||
}
|
||
/**
|
||
* 插屏广告。微信抖音都支持!
|
||
*/
|
||
var ADInterstitial = /** @class */ (function () {
|
||
function ADInterstitial(adUid) {
|
||
this._adUid = adUid;
|
||
}
|
||
Object.defineProperty(ADInterstitial.prototype, "aduid", {
|
||
get: function () {
|
||
return this._adUid;
|
||
},
|
||
enumerable: false,
|
||
configurable: true
|
||
});
|
||
ADInterstitial.prototype.show = function () {
|
||
var _this = this;
|
||
var _a, _b;
|
||
// @ts-ignore
|
||
if (isWechat() && !wx.createInterstitialAd) {
|
||
console.warn('wechat unsupport interstitial AD!');
|
||
this._interstitial = null;
|
||
return;
|
||
}
|
||
// @ts-ignore
|
||
if (isBytedance() && !tt.createInterstitialAd) {
|
||
console.warn('bytedance unsupport interstitial AD!');
|
||
this._interstitial = null;
|
||
return;
|
||
}
|
||
if (this._interstitial) {
|
||
this._interstitial.load();
|
||
}
|
||
else {
|
||
if (isWechat()) {
|
||
// @ts-ignore
|
||
this._interstitial = wx.createInterstitialAd({ adUnitId: this._adUid });
|
||
}
|
||
else if (isBytedance()) {
|
||
// @ts-ignore
|
||
this._interstitial = tt.createInterstitialAd({ adUnitId: this._adUid });
|
||
}
|
||
else {
|
||
this._interstitial = null;
|
||
}
|
||
(_a = this._interstitial) === null || _a === void 0 ? void 0 : _a.onLoad(function () {
|
||
console.log('load interstitial ad success');
|
||
_this._interstitial.show().catch(function (err) {
|
||
console.log('catch interstitial ad error:', err);
|
||
});
|
||
});
|
||
(_b = this._interstitial) === null || _b === void 0 ? void 0 : _b.onError(function (err) {
|
||
console.log('interstitial ad on error:', err);
|
||
});
|
||
}
|
||
};
|
||
ADInterstitial.prototype.destory = function () {
|
||
var _a;
|
||
(_a = this._interstitial) === null || _a === void 0 ? void 0 : _a.destroy();
|
||
};
|
||
return ADInterstitial;
|
||
}());
|
||
var ADBanner = /** @class */ (function () {
|
||
/**
|
||
* 抖音和微信都支持
|
||
* 横幅广告。预估宽度默认为300,预估高度为140。如果你不确定就按默认值来。
|
||
* @param adUid 广告UID,后端配置
|
||
* @param isTop 是否在屏幕顶部展示。内部会自动居中计算位置。
|
||
* @param bannerWidth 横幅广告的预估宽度。默认300
|
||
* @param autoShow 广告加载完成后是否立刻显示,默认为不显示
|
||
*/
|
||
function ADBanner(adUid, param, bannerWidth, autoShow) {
|
||
if (bannerWidth === void 0) { bannerWidth = 300; }
|
||
if (autoShow === void 0) { autoShow = false; }
|
||
this._adUid = adUid;
|
||
this.create(autoShow, bannerWidth, param); // 默认300比较合适
|
||
}
|
||
Object.defineProperty(ADBanner.prototype, "aduid", {
|
||
get: function () {
|
||
return this._adUid;
|
||
},
|
||
enumerable: false,
|
||
configurable: true
|
||
});
|
||
ADBanner.prototype.create = function (autoShow, bannerWidth, param) {
|
||
var _this = this;
|
||
var _a, _b;
|
||
if (!isWechat() && !isBytedance()) {
|
||
this._banner = null;
|
||
return;
|
||
}
|
||
this.destroy();
|
||
var winSize = getSysWinSize();
|
||
var height = bannerWidth * 0.4;
|
||
var top = 0, left = 0;
|
||
if (typeof param === "boolean") {
|
||
left = (winSize.width - bannerWidth) / 2;
|
||
top = param ? 5 : (winSize.height - height);
|
||
}
|
||
else {
|
||
left = param.left;
|
||
top = param.top;
|
||
}
|
||
var params = {
|
||
adUnitId: this._adUid,
|
||
adIntervals: 30,
|
||
style: { left: left, top: top, width: bannerWidth }
|
||
};
|
||
if (isWechat()) {
|
||
// @ts-ignore
|
||
this._banner = wx.createBannerAd(params);
|
||
}
|
||
else if (isBytedance()) {
|
||
// @ts-ignore
|
||
this._banner = tt.createBannerAd(params);
|
||
}
|
||
else {
|
||
this._banner = null;
|
||
}
|
||
(_a = this._banner) === null || _a === void 0 ? void 0 : _a.onError(function (err) {
|
||
console.log('ad banner error:', err);
|
||
});
|
||
(_b = this._banner) === null || _b === void 0 ? void 0 : _b.onLoad(function () {
|
||
autoShow && _this._banner.show();
|
||
});
|
||
};
|
||
ADBanner.prototype.show = function () {
|
||
var _a;
|
||
(_a = this._banner) === null || _a === void 0 ? void 0 : _a.show();
|
||
};
|
||
ADBanner.prototype.hide = function () {
|
||
var _a;
|
||
(_a = this._banner) === null || _a === void 0 ? void 0 : _a.hide();
|
||
};
|
||
ADBanner.prototype.destroy = function () {
|
||
var _a;
|
||
(_a = this._banner) === null || _a === void 0 ? void 0 : _a.destroy();
|
||
};
|
||
return ADBanner;
|
||
}());
|
||
var ADCustom = /** @class */ (function () {
|
||
/**
|
||
* 由于原生模板广告在微信服务后端可以定制宽度大小,个数,缩放比例等,所以位置调整要根据设置的宽度来定。抖音不支持!
|
||
* @param adUid 广告UID,后端配置
|
||
* @param top 从左上角开始,距离屏幕顶部的距离。注意:这个数据为设备屏幕宽度width。如果需要获取屏幕的像素,需要乘以设备像素比Pixel-Ratio,例如iPhone 13 Pro的Pixel-Ratio为3,像素为Width*3。
|
||
* @param left 从左上角开始,距离屏幕最左边的距离。注意:这个数据为设备屏幕宽度width。如果需要获取屏幕的像素,需要乘以设备像素比Pixel-Ratio,例如iPhone 13 Pro的Pixel-Ratio为3,像素为Width*3。
|
||
* @param scale 原生模板广告的尺寸,默认为1,即100%。此值在微信服务后端广告中获得,默认为100%,目前有100%,90%,80%三种,一般情况不用修改。若有修改,记得传入值,例如90%就传入0.9。
|
||
*/
|
||
function ADCustom(adUid, top, left, scale) {
|
||
if (top === void 0) { top = 0; }
|
||
if (left === void 0) { left = 0; }
|
||
if (scale === void 0) { scale = 1.0; }
|
||
this._adUid = adUid;
|
||
this.createCustomAd(top, left, scale);
|
||
}
|
||
Object.defineProperty(ADCustom.prototype, "aduid", {
|
||
get: function () {
|
||
return this._adUid;
|
||
},
|
||
enumerable: false,
|
||
configurable: true
|
||
});
|
||
ADCustom.prototype.createCustomAd = function (top, left, scale) {
|
||
var _a;
|
||
if (!isWechat()) { // only wechat support custom ad
|
||
this._adCustom = null;
|
||
console.log('Only wechat support Custom Ad');
|
||
return;
|
||
}
|
||
this.destroy();
|
||
// 原生模板5个应用宽度为375,若设置了缩放比例,则宽度也需要设置
|
||
// let width = 375 * this._scale;
|
||
// let newLeft = (sys.windowWidth - width) / 2;
|
||
// let newTop = sys.windowHeight / 2; // 120是预估高度
|
||
// @ts-ignore
|
||
this._adCustom = wx.createCustomAd({
|
||
adUnitId: this._adUid,
|
||
style: { left: left, top: top, fixed: true }
|
||
});
|
||
(_a = this._adCustom) === null || _a === void 0 ? void 0 : _a.onError(function (err) {
|
||
console.log('ad custom error:', err);
|
||
});
|
||
};
|
||
ADCustom.prototype.show = function () {
|
||
var _a;
|
||
(_a = this._adCustom) === null || _a === void 0 ? void 0 : _a.show();
|
||
};
|
||
ADCustom.prototype.hide = function () {
|
||
var _a;
|
||
(_a = this._adCustom) === null || _a === void 0 ? void 0 : _a.hide();
|
||
};
|
||
ADCustom.prototype.destroy = function () {
|
||
var _a;
|
||
(_a = this._adCustom) === null || _a === void 0 ? void 0 : _a.destroy();
|
||
};
|
||
return ADCustom;
|
||
}());
|
||
/**
|
||
* 视频广告用户点击行为结果
|
||
*/
|
||
var EAdVideoResult;
|
||
(function (EAdVideoResult) {
|
||
/**
|
||
* 用户看完了广告,游戏可发放奖励。
|
||
*/
|
||
EAdVideoResult[EAdVideoResult["ACCEPT"] = 0] = "ACCEPT";
|
||
/**
|
||
* 用户中途关闭了广告,即未看完状态。不可发放奖励。
|
||
*/
|
||
EAdVideoResult[EAdVideoResult["REJECT"] = 1] = "REJECT";
|
||
/**
|
||
* 广告组件内部发生了错误。不可发放奖励。
|
||
*/
|
||
EAdVideoResult[EAdVideoResult["ERROR"] = 2] = "ERROR";
|
||
})(EAdVideoResult = MiniGameSdk.EAdVideoResult || (MiniGameSdk.EAdVideoResult = {}));
|
||
var ADVideo = /** @class */ (function () {
|
||
function ADVideo(adUid) {
|
||
this._adVideo = null;
|
||
this._adUid = adUid;
|
||
}
|
||
Object.defineProperty(ADVideo.prototype, "aduid", {
|
||
get: function () {
|
||
return this._adUid;
|
||
},
|
||
enumerable: false,
|
||
configurable: true
|
||
});
|
||
/**
|
||
* 由于微信和抖音视频广告机制不同,微信可以看的视频广告个数只有0和1个,抖音平台则可以看0~maxVideoCount
|
||
* @param onResult 两个参数:第一个res是EAdVideoResult定义,第二count是用户看了多少个视频广告。
|
||
* @param target onResult的拥有者
|
||
* @param maxVideoCount 可以连续看最大视频个数,可最大化商业效率。默认为3个。
|
||
* @returns
|
||
*/
|
||
ADVideo.prototype.show = function (onResult, target, maxVideoCount) {
|
||
var _this = this;
|
||
var _a, _b, _c, _d, _e;
|
||
if (maxVideoCount === void 0) { maxVideoCount = 3; }
|
||
var callback = function (state, count) {
|
||
onResult === null || onResult === void 0 ? void 0 : onResult.call(target, state, count);
|
||
};
|
||
if (!isWechat() && !isBytedance()) {
|
||
callback(EAdVideoResult.ACCEPT, 1);
|
||
this._adVideo = null;
|
||
return;
|
||
}
|
||
var onAdVideoClosed = function (res) {
|
||
var _a;
|
||
(_a = _this._adVideo) === null || _a === void 0 ? void 0 : _a.offClose(onAdVideoClosed);
|
||
if (isWechat()) {
|
||
if (res && res.isEnded || res === undefined) {
|
||
callback(EAdVideoResult.ACCEPT, 1);
|
||
}
|
||
else {
|
||
callback(EAdVideoResult.REJECT, 0);
|
||
}
|
||
}
|
||
else if (isBytedance()) {
|
||
var resConverted = res;
|
||
if (resConverted && resConverted.count > 0) {
|
||
callback(EAdVideoResult.ACCEPT, resConverted.count);
|
||
}
|
||
else {
|
||
callback(EAdVideoResult.REJECT, 0);
|
||
}
|
||
}
|
||
};
|
||
(_a = this._adVideo) === null || _a === void 0 ? void 0 : _a.offClose(onAdVideoClosed);
|
||
if (isWechat()) {
|
||
// @ts-ignore
|
||
this._adVideo = wx.createRewardedVideoAd({
|
||
adUnitId: this._adUid
|
||
});
|
||
}
|
||
else if (isBytedance()) {
|
||
// @ts-ignore
|
||
this._adVideo = tt.createRewardedVideoAd({
|
||
adUnitId: this._adUid,
|
||
multiton: true,
|
||
multitonRewardMsg: ['多1次奖励', '再多一次奖励', '再多一次奖励'],
|
||
multitonRewardTimes: maxVideoCount,
|
||
});
|
||
}
|
||
else {
|
||
this._adVideo = null;
|
||
}
|
||
(_b = this._adVideo) === null || _b === void 0 ? void 0 : _b.onLoad(function () {
|
||
console.log('Ad load success');
|
||
});
|
||
(_c = this._adVideo) === null || _c === void 0 ? void 0 : _c.onError(function (err) {
|
||
console.log('Ad video error:', err);
|
||
callback(EAdVideoResult.ERROR, 0);
|
||
});
|
||
(_d = this._adVideo) === null || _d === void 0 ? void 0 : _d.onClose(onAdVideoClosed);
|
||
(_e = this._adVideo) === null || _e === void 0 ? void 0 : _e.show().catch(function () {
|
||
var _a;
|
||
(_a = _this._adVideo) === null || _a === void 0 ? void 0 : _a.load().then(function () { var _a; return (_a = _this._adVideo) === null || _a === void 0 ? void 0 : _a.show(); }).catch(function (err) {
|
||
console.log('Catch video ad error:', err);
|
||
callback(EAdVideoResult.ERROR, 0);
|
||
});
|
||
});
|
||
};
|
||
ADVideo.prototype.destory = function () {
|
||
var _a;
|
||
(_a = this._adVideo) === null || _a === void 0 ? void 0 : _a.destory();
|
||
};
|
||
return ADVideo;
|
||
}());
|
||
var EAdBannerLocation;
|
||
(function (EAdBannerLocation) {
|
||
/**
|
||
* 屏幕顶部
|
||
*/
|
||
EAdBannerLocation[EAdBannerLocation["TOP"] = 0] = "TOP";
|
||
/**
|
||
* 屏幕底部
|
||
*/
|
||
EAdBannerLocation[EAdBannerLocation["BOTTOM"] = 1] = "BOTTOM";
|
||
})(EAdBannerLocation = MiniGameSdk.EAdBannerLocation || (MiniGameSdk.EAdBannerLocation = {}));
|
||
var AdvertManager = /** @class */ (function () {
|
||
function AdvertManager() {
|
||
this._customs = {};
|
||
}
|
||
Object.defineProperty(AdvertManager, "instance", {
|
||
get: function () {
|
||
if (!AdvertManager._instance) {
|
||
AdvertManager._instance = new AdvertManager();
|
||
}
|
||
return AdvertManager._instance;
|
||
},
|
||
enumerable: false,
|
||
configurable: true
|
||
});
|
||
/**
|
||
* 预加载横幅广告,不会显示。只有你在调用showBanner时才会显示。
|
||
* 可重复调用,但是会销毁上一次的实例。一般情况,全局有一个就行了,太多占用内存,而且没必要。
|
||
* @param adUid 广告UID
|
||
* @param location 位置有两种情况:1、可以传入枚举值,默认上方; 2、可以自定义位置传入IPosition,注意IPosition中的top和left跟平台的top,left是一致(没有乘以设备像素比ratio),需要开发者自己调试位置
|
||
* @param scale 默认为跟屏幕一样的宽度,可以通过设置缩放比例来调整大小。当然,平台有规定最大或最小宽度,函数内部会自动计算。
|
||
*/
|
||
AdvertManager.prototype.loadBanner = function (adUid, location, scale) {
|
||
var _a;
|
||
if (location === void 0) { location = EAdBannerLocation.TOP; }
|
||
if (scale === void 0) { scale = 1.0; }
|
||
(_a = this._banner) === null || _a === void 0 ? void 0 : _a.destroy();
|
||
var size = getSysWinSize();
|
||
// 当 style.width 小于 300 时,会取作 300。 当 style.width 大于屏幕宽度时,会取作屏幕宽度。
|
||
var width = size.width * scale;
|
||
width = width < 300 ? 300 : width; // 最小值矫正
|
||
width = width > size.width ? size.width : width; //最大值矫正
|
||
this._banner = typeof location === 'number' ? new ADBanner(adUid, location === EAdBannerLocation.TOP, width, false) : new ADBanner(adUid, location, width, false);
|
||
};
|
||
/**
|
||
* 显示横幅广告
|
||
*/
|
||
AdvertManager.prototype.showBanner = function () {
|
||
if (this._banner) {
|
||
this._banner.show();
|
||
}
|
||
else {
|
||
console.warn('MiniGameSDK: banner is null, you must call loadBanner(...) first!');
|
||
}
|
||
};
|
||
/**
|
||
* 隐藏横幅广告
|
||
*/
|
||
AdvertManager.prototype.hideBanner = function () {
|
||
var _a;
|
||
(_a = this._banner) === null || _a === void 0 ? void 0 : _a.hide();
|
||
};
|
||
/**
|
||
* 弹出插屏广告
|
||
* @param adUid 广告单元id
|
||
*/
|
||
AdvertManager.prototype.showInterstitial = function (adUid) {
|
||
var _a;
|
||
if (this._interstitial && this._interstitial.aduid === adUid) {
|
||
this._interstitial.show();
|
||
}
|
||
else {
|
||
(_a = this._interstitial) === null || _a === void 0 ? void 0 : _a.destory();
|
||
this._interstitial = new ADInterstitial(adUid);
|
||
this._interstitial.show();
|
||
}
|
||
};
|
||
/**
|
||
* 加载原生模板广告,不会显示。只有你在调用showCustom时才会显示。
|
||
* 由于原生模板广告在微信服务后端可以定制宽度大小,个数,缩放比例等,所以位置调整要根据设置的宽度来定。抖音不支持本函数,会调用无效!
|
||
* @param adUid 广告ID
|
||
* @param location 位置有两种情况:1、可以传入枚举值,默认上方; 2、可以自定义位置传入IPosition,注意IPosition中的top和left跟平台的top,left是一致(没有乘以设备像素比ratio),需要开发者自己调试位置
|
||
* @param scale 缩放比例,默认是1,即不缩放。这个缩放并不是自己填,而是根据微信MP后台你配置的原生模板广告的缩放比例填,目前有100%,90%,80%三种,一般情况不用修改。若有后台修改,记得传入值,例如90%就传入0.9。
|
||
*/
|
||
AdvertManager.prototype.loadCustom = function (adUid, location, scale) {
|
||
if (location === void 0) { location = { top: 0, left: 0 }; }
|
||
if (scale === void 0) { scale = 1; }
|
||
// this._custom?.destroy();
|
||
// this._custom = new ADCustom(adUid, location.top, location.left, scale);
|
||
if (this._customs[adUid]) {
|
||
console.log(adUid + " has been loaded.");
|
||
return;
|
||
}
|
||
this._customs[adUid] = new ADCustom(adUid, location.top, location.left, scale);
|
||
};
|
||
/**
|
||
* 显示自定义广告。
|
||
* @param adUid 广告的唯一标识符。使用此标识符来查找和显示特定的自定义广告。
|
||
*
|
||
* 此方法尝试根据提供的adUid显示一个自定义广告。如果给定的adUid对应的自定义广告已加载,
|
||
* 则调用该广告的显示方法。如果广告未加载,则在控制台输出警告信息。
|
||
*/
|
||
AdvertManager.prototype.showCustom = function (adUid) {
|
||
if (this._customs[adUid]) {
|
||
this._customs[adUid].show();
|
||
}
|
||
else {
|
||
console.warn("You have not load " + adUid + " of Custom AD, can not show!");
|
||
}
|
||
};
|
||
/**
|
||
* 隐藏指定的自定义广告单元
|
||
*
|
||
* 此方法用于隐藏通过广告单元标识符(adUid)指定的自定义广告。如果指定的广告单元已加载并显示,
|
||
* 则将其隐藏;如果广告单元未加载,则在控制台输出警告信息。
|
||
*
|
||
* @param adUid 广告单元标识符,用于唯一标识一个自定义广告单元。
|
||
*/
|
||
AdvertManager.prototype.hideCustom = function (adUid) {
|
||
if (this._customs[adUid]) {
|
||
this._customs[adUid].hide();
|
||
}
|
||
else {
|
||
console.warn("You have not load " + adUid + " of Custom AD, can not hide!");
|
||
}
|
||
};
|
||
/**
|
||
* 由于微信和抖音视频广告机制不同,微信可以看的视频广告个数只有0和1个,抖音平台则可以看0~maxVideoCount
|
||
* @param adUid 广告ID。如果与上一次UID不同,则内部会重新创建实例。开发者完全不用关心这个细节。
|
||
* @param onVideoResult 两个参数:第一个res是EAdVideoResult定义,第二count是用户看了多少个视频广告。
|
||
* @param target onVideoResult的拥有者
|
||
* @param maxVideoCount 最大视频个数。默认是3,仅对抖音平台生效。微信平台看完视频count的结果永远是1或0
|
||
*/
|
||
AdvertManager.prototype.showVideo = function (adUid, onVideoResult, target, maxVideoCount) {
|
||
var _a;
|
||
if (maxVideoCount === void 0) { maxVideoCount = 3; }
|
||
if (this._video && this._video.aduid === adUid) {
|
||
this._video.show(onVideoResult, target, maxVideoCount);
|
||
}
|
||
else {
|
||
(_a = this._video) === null || _a === void 0 ? void 0 : _a.destory();
|
||
this._video = new ADVideo(adUid);
|
||
this._video.show(onVideoResult, target, maxVideoCount);
|
||
}
|
||
};
|
||
/**
|
||
* 销毁内部所有实例,清空内存
|
||
*/
|
||
AdvertManager.prototype.destroyAll = function () {
|
||
var _a, _b, _c, _d;
|
||
(_a = this._banner) === null || _a === void 0 ? void 0 : _a.destroy();
|
||
this._banner = null;
|
||
(_b = this._interstitial) === null || _b === void 0 ? void 0 : _b.destory();
|
||
this._interstitial = null;
|
||
(_c = this._video) === null || _c === void 0 ? void 0 : _c.destory();
|
||
this._video = null;
|
||
if (this._customs) {
|
||
for (var val in this._customs) {
|
||
(_d = this._customs[val]) === null || _d === void 0 ? void 0 : _d.destroy();
|
||
}
|
||
this._customs = {};
|
||
}
|
||
};
|
||
return AdvertManager;
|
||
}());
|
||
MiniGameSdk.AdvertManager = AdvertManager;
|
||
var EGameClubIcon;
|
||
(function (EGameClubIcon) {
|
||
/** 绿色图标 */
|
||
EGameClubIcon["GREEN"] = "green";
|
||
/** 红色图标 */
|
||
EGameClubIcon["WHITE"] = "white";
|
||
/** 有黑色圆角背景的白色图标 */
|
||
EGameClubIcon["DARK"] = "dark";
|
||
/** 有白色圆角背景的绿色图标 */
|
||
EGameClubIcon["LIGHT"] = "light";
|
||
})(EGameClubIcon = MiniGameSdk.EGameClubIcon || (MiniGameSdk.EGameClubIcon = {}));
|
||
var GameClub = /** @class */ (function () {
|
||
function GameClub() {
|
||
}
|
||
Object.defineProperty(GameClub, "instance", {
|
||
get: function () {
|
||
if (!this._instance) {
|
||
this._instance = new GameClub();
|
||
}
|
||
return this._instance;
|
||
},
|
||
enumerable: false,
|
||
configurable: true
|
||
});
|
||
/**
|
||
* 创建游戏圈按钮
|
||
* @param icon
|
||
* @param position
|
||
* @param size
|
||
* @param openLink
|
||
*/
|
||
GameClub.prototype.create = function (icon, position, size, openLink) {
|
||
if (icon === void 0) { icon = EGameClubIcon.GREEN; }
|
||
if (position === void 0) { position = { top: 0, left: 0 }; }
|
||
if (size === void 0) { size = { width: 40, height: 40 }; }
|
||
if (isWechat()) {
|
||
// @ts-ignore
|
||
this._club = wx.createGameClubButton({
|
||
icon: icon,
|
||
style: {
|
||
left: position.left,
|
||
top: position.top,
|
||
width: size.width,
|
||
height: size.height
|
||
},
|
||
openlink: openLink
|
||
});
|
||
}
|
||
};
|
||
GameClub.prototype.show = function () {
|
||
var _a;
|
||
(_a = this._club) === null || _a === void 0 ? void 0 : _a.show();
|
||
};
|
||
GameClub.prototype.hide = function () {
|
||
var _a;
|
||
(_a = this._club) === null || _a === void 0 ? void 0 : _a.hide();
|
||
};
|
||
GameClub.prototype.destory = function () {
|
||
var _a;
|
||
(_a = this._club) === null || _a === void 0 ? void 0 : _a.destroy();
|
||
};
|
||
return GameClub;
|
||
}());
|
||
MiniGameSdk.GameClub = GameClub;
|
||
/**
|
||
* 振动类型
|
||
*/
|
||
var EVirbrateType;
|
||
(function (EVirbrateType) {
|
||
/**
|
||
* 短振动
|
||
*/
|
||
EVirbrateType[EVirbrateType["SHORT"] = 0] = "SHORT";
|
||
/**
|
||
* 长振动
|
||
*/
|
||
EVirbrateType[EVirbrateType["LONG"] = 1] = "LONG";
|
||
})(EVirbrateType = MiniGameSdk.EVirbrateType || (MiniGameSdk.EVirbrateType = {}));
|
||
/**
|
||
* 平台常用API合集
|
||
*/
|
||
var API = /** @class */ (function () {
|
||
function API() {
|
||
}
|
||
/**
|
||
* 分享app给朋友,微信小游戏分享是没有onSuccess回调的。
|
||
* @param title 标题
|
||
* @param description 细节描述信息
|
||
* @param imageUrl 图片地址
|
||
* @param query 查询信息
|
||
* @param onSuccess 抖音会回调,微信不会回调
|
||
*/
|
||
API.shareAppToFriends = function (title, description, imageUrl, query, onSuccess) {
|
||
if (description === void 0) { description = ''; }
|
||
if (isWechat()) {
|
||
try {
|
||
//@ts-ignore
|
||
wx.shareAppMessage({
|
||
title: title,
|
||
imageUrl: imageUrl,
|
||
query: query,
|
||
});
|
||
}
|
||
catch (err) {
|
||
console.log("share faild: " + err);
|
||
}
|
||
}
|
||
if (isBytedance()) {
|
||
//@ts-ignore
|
||
tt.shareAppMessage({
|
||
title: title,
|
||
desc: description,
|
||
imageUrl: imageUrl !== null && imageUrl !== void 0 ? imageUrl : '',
|
||
query: query !== null && query !== void 0 ? query : '',
|
||
success: function (res) {
|
||
console.log('share success:', res);
|
||
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess();
|
||
},
|
||
fail: function (res) {
|
||
console.log('share fail:', res);
|
||
}
|
||
});
|
||
}
|
||
};
|
||
/**
|
||
* 显示提示信息
|
||
* @param title 标题
|
||
* @param duration 时长(单位:秒)
|
||
* @returns
|
||
*/
|
||
API.showToast = function (title, duration) {
|
||
if (duration === void 0) { duration = 2; }
|
||
if (isWechat()) {
|
||
// @ts-ignore
|
||
wx.showToast({
|
||
title: title,
|
||
icon: 'success',
|
||
duration: duration * 1000
|
||
});
|
||
}
|
||
if (isBytedance()) {
|
||
//@ts-ignore
|
||
tt.showToast({
|
||
title: title,
|
||
duration: duration * 1000,
|
||
success: function (res) {
|
||
console.log("" + res);
|
||
},
|
||
fail: function (res) {
|
||
console.log("showToast\u8C03\u7528\u5931\u8D25");
|
||
},
|
||
});
|
||
}
|
||
};
|
||
/**
|
||
* 设备震动效果,默认为短震动。注意:可能一些机型不会生效,具体看平台方的说明
|
||
* @param type MiniGameSdk.API.EVirbrateType
|
||
*/
|
||
API.vibrate = function (type) {
|
||
if (type === void 0) { type = EVirbrateType.SHORT; }
|
||
if (isWechat()) {
|
||
switch (type) {
|
||
case EVirbrateType.SHORT:
|
||
//@ts-ignore
|
||
wx.vibrateShort({
|
||
success: function (res) {
|
||
console.log('vibrate success:', res);
|
||
},
|
||
fail: function (res) {
|
||
console.log('vibrateShort failed', res);
|
||
},
|
||
});
|
||
break;
|
||
case EVirbrateType.LONG:
|
||
//@ts-ignore
|
||
wx.vibrateLong({
|
||
success: function (res) {
|
||
console.log('vibrate success', res);
|
||
},
|
||
fail: function (res) {
|
||
console.log("vibrateLong failed", res);
|
||
},
|
||
});
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
if (isBytedance()) {
|
||
switch (type) {
|
||
case EVirbrateType.SHORT:
|
||
//@ts-ignore
|
||
tt.vibrateShort({
|
||
success: function (res) {
|
||
console.log('vibrate success:', res);
|
||
},
|
||
fail: function (res) {
|
||
console.log('vibrateShort failed', res);
|
||
},
|
||
});
|
||
break;
|
||
case EVirbrateType.LONG:
|
||
//@ts-ignore
|
||
tt.vibrateLong({
|
||
success: function (res) {
|
||
console.log('vibrate success', res);
|
||
},
|
||
fail: function (res) {
|
||
console.log("vibrateLong failed", res);
|
||
},
|
||
});
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
};
|
||
/**
|
||
* 重启小游戏
|
||
*/
|
||
API.reboot = function () {
|
||
if (isWechat()) {
|
||
//@ts-ignore
|
||
wx.restartMiniProgram({
|
||
success: function () {
|
||
console.log('restart success');
|
||
},
|
||
fail: function () {
|
||
console.log('restart failed');
|
||
}
|
||
});
|
||
}
|
||
if (isBytedance()) {
|
||
try {
|
||
// @ts-ignore
|
||
tt.restartMiniProgramSync();
|
||
}
|
||
catch (error) {
|
||
console.log("restartMiniProgramSync", error);
|
||
}
|
||
}
|
||
};
|
||
/**
|
||
* 退出小游戏
|
||
*/
|
||
API.exit = function () {
|
||
if (isWechat()) {
|
||
//@ts-ignore
|
||
wx.exitMiniProgram({
|
||
success: function () {
|
||
console.log('exit success');
|
||
},
|
||
fail: function () {
|
||
console.log('exit failed');
|
||
}
|
||
});
|
||
}
|
||
if (isBytedance()) {
|
||
// @ts-ignore
|
||
tt.exitMiniProgram({
|
||
success: function (res) {
|
||
console.log("exit success:", res === null || res === void 0 ? void 0 : res.data);
|
||
},
|
||
fail: function (res) {
|
||
console.log("exit fail:", res === null || res === void 0 ? void 0 : res.errMsg);
|
||
},
|
||
});
|
||
}
|
||
};
|
||
/**
|
||
* 显示转发按钮。通常在刚进入游戏的时候调用。
|
||
* 主要是打开平台“...”这个按钮里面的分享菜单,一般默认是关闭的,需要调用这个函数打开。可以让用户分享你的游戏入口。
|
||
*/
|
||
API.showShareMenu = function () {
|
||
if (isWechat()) {
|
||
//@ts-ignore
|
||
wx.showShareMenu({
|
||
withShareTicket: true,
|
||
menus: ['shareAppMessage', 'shareTimeline'],
|
||
success: function () { },
|
||
fail: function () { },
|
||
complete: function () { }
|
||
});
|
||
}
|
||
if (isBytedance()) {
|
||
//@ts-ignore
|
||
tt.showShareMenu({
|
||
success: function (res) {
|
||
console.log("show menu is showing");
|
||
},
|
||
fail: function (err) {
|
||
console.log("showShareMenu:", err.errMsg);
|
||
},
|
||
complete: function (res) {
|
||
console.log("showShareMenu complete");
|
||
},
|
||
});
|
||
}
|
||
};
|
||
/**
|
||
* 微信小游戏:跳转到另外一款小游戏
|
||
* 抖音小游戏:跳转到指定的视频界面
|
||
* @param targetId 微信小游戏appid或者视频界面
|
||
*/
|
||
API.navigateTo = function (targetId, onSuccess) {
|
||
if (isWechat()) {
|
||
// @ts-ignore
|
||
wx.navigateToMiniProgram({
|
||
appId: targetId,
|
||
extraData: {
|
||
foo: 'bar'
|
||
},
|
||
envVersion: 'develop',
|
||
success: function (res) {
|
||
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess();
|
||
}
|
||
});
|
||
}
|
||
if (isBytedance()) {
|
||
// @ts-ignore
|
||
tt.navigateToVideoView({
|
||
videoId: targetId,
|
||
success: function (res) {
|
||
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess();
|
||
},
|
||
fail: function (err) {
|
||
console.log("bytedance navigateToVideoView fail", err);
|
||
},
|
||
});
|
||
}
|
||
};
|
||
/**
|
||
* 小游戏平台登录功能。微信返回code,抖音返回code和anonymousCode。用于登录的凭证,需要把这个code传回你的服务器程序中去调用code2Session
|
||
* @param callback (code, anonymousCode) 第一个参数为code,微信和抖音都支持;第二个参数为匿名设备ID,仅抖音支持,失败都返回null
|
||
*/
|
||
API.login = function (callback) {
|
||
var loginPlatform = function () {
|
||
if (isWechat()) {
|
||
//@ts-ignore
|
||
wx.login({
|
||
success: function (res) {
|
||
if (res.code) {
|
||
API._loginCode = res.code;
|
||
API._loginAnonymousCode = null;
|
||
callback === null || callback === void 0 ? void 0 : callback(API._loginCode, API._loginAnonymousCode);
|
||
}
|
||
else {
|
||
console.log('login error:', res.errMsg);
|
||
}
|
||
},
|
||
fail: function () {
|
||
API._loginCode = null;
|
||
API._loginAnonymousCode = null;
|
||
callback === null || callback === void 0 ? void 0 : callback(API._loginCode, API._loginAnonymousCode);
|
||
console.log('login fail');
|
||
}
|
||
});
|
||
}
|
||
else if (isBytedance()) {
|
||
//@ts-ignore
|
||
tt.login({
|
||
force: true,
|
||
success: function (res) {
|
||
var _a, _b;
|
||
console.log("login " + res.code + " " + res.anonymousCode);
|
||
if (res.code) {
|
||
API._loginCode = (_a = res.code) === null || _a === void 0 ? void 0 : _a.toString();
|
||
API._loginAnonymousCode = (_b = res.anonymousCode) === null || _b === void 0 ? void 0 : _b.toString();
|
||
callback === null || callback === void 0 ? void 0 : callback(API._loginCode, API._loginAnonymousCode);
|
||
}
|
||
else {
|
||
console.log('login error:', res.errMsg);
|
||
}
|
||
},
|
||
fail: function (res) {
|
||
API._loginCode = null;
|
||
API._loginAnonymousCode = null;
|
||
callback === null || callback === void 0 ? void 0 : callback(API._loginCode, API._loginAnonymousCode);
|
||
console.log("login fail", res);
|
||
},
|
||
});
|
||
}
|
||
else {
|
||
API._loginCode = null;
|
||
API._loginAnonymousCode = null;
|
||
callback === null || callback === void 0 ? void 0 : callback(API._loginCode, API._loginAnonymousCode);
|
||
console.log('not mini game platform, login codes are all null');
|
||
}
|
||
};
|
||
if (!API._loginCode) {
|
||
loginPlatform();
|
||
}
|
||
else {
|
||
if (isWechat()) {
|
||
//@ts-ignore
|
||
wx.checkSession({
|
||
success: function () {
|
||
console.log("session is valid, use current code:", API._loginCode);
|
||
callback === null || callback === void 0 ? void 0 : callback(API._loginCode, API._loginAnonymousCode);
|
||
},
|
||
fail: function () {
|
||
console.log("session expired");
|
||
loginPlatform();
|
||
}
|
||
});
|
||
}
|
||
else if (isBytedance()) {
|
||
//@ts-ignore
|
||
tt.checkSession({
|
||
success: function () {
|
||
console.log("session is valid, user current code: " + API._loginCode + ", " + API._loginAnonymousCode);
|
||
callback === null || callback === void 0 ? void 0 : callback(API._loginCode, API._loginAnonymousCode);
|
||
},
|
||
fail: function () {
|
||
console.log("session expired");
|
||
loginPlatform();
|
||
},
|
||
});
|
||
}
|
||
else {
|
||
console.log('not mini game platform, login null');
|
||
callback === null || callback === void 0 ? void 0 : callback(null, null);
|
||
}
|
||
}
|
||
};
|
||
/**
|
||
* 调用微信云函数。由于参数需要自定义,所以为any,需要自行解释。函数只完成通道和处理一场的作用
|
||
* @param callback 返回云函数调用结果。需要检查返回参数是否为空,失败的时候为空
|
||
* @param name 云函数的名字
|
||
* @param data 云函数的内容
|
||
*/
|
||
API.callWechatCloudFunction = function (callback, name, data) {
|
||
if (!isWechat()) {
|
||
console.log('Not wechat platform, not support callWechatCloudFunction');
|
||
return;
|
||
}
|
||
this.login(function (code, anonymousCode) {
|
||
if (!API._hasInitWechatCloudFunction) {
|
||
//@ts-ignore
|
||
wx.cloud.init();
|
||
API._hasInitWechatCloudFunction = true;
|
||
}
|
||
//@ts-ignore
|
||
wx.cloud.callFunction({
|
||
name: name,
|
||
data: data,
|
||
success: function (res) { return callback === null || callback === void 0 ? void 0 : callback(res); },
|
||
fail: function (err) {
|
||
console.log('wechat cloud function error:', err);
|
||
callback === null || callback === void 0 ? void 0 : callback(null);
|
||
}
|
||
});
|
||
});
|
||
};
|
||
/**
|
||
* 存储用户信息,数据量不能大。可以考虑用于分数排行榜。用户之间可共享排行数据。
|
||
* @param key
|
||
* @param value
|
||
*/
|
||
API.setUserCloudStorage = function (key, value) {
|
||
if (isWechat()) {
|
||
// @ts-ignore
|
||
wx.setUserCloudStorage({
|
||
KVDataList: [{ key: key, value: value }],
|
||
success: function () { return console.log("set cloud storage success:" + key + ", value:" + value); },
|
||
fail: function (err) { return console.log('set cloud storage error:', err); }
|
||
});
|
||
}
|
||
if (isBytedance()) {
|
||
// @ts-ignore
|
||
tt.setUserCloudStorage({
|
||
KVDataList: [{ key: key, value: value, }],
|
||
success: function () { return console.log("set cloud storage success:" + key + ", value:" + value); },
|
||
fail: function (err) { return console.log('set cloud storage error:', err); }
|
||
});
|
||
}
|
||
};
|
||
API._loginCode = null;
|
||
API._loginAnonymousCode = null;
|
||
API._hasInitWechatCloudFunction = false;
|
||
return API;
|
||
}());
|
||
MiniGameSdk.API = API;
|
||
/**
|
||
* 抖音侧边栏专属接口
|
||
*/
|
||
var BytedanceSidebar = /** @class */ (function () {
|
||
function BytedanceSidebar() {
|
||
}
|
||
/**
|
||
* 本游戏在抖音环境下启动监控,需要放在全局环境中,保证能第一时间启动。因为可能监听抖音失败(抖音小游戏官方的说明)!
|
||
* @param onResult 包含一个boolean参数的函数
|
||
* @param target 上述函数的拥有者,如果是类的成员函数,需要传入this。普通或匿名函数忽略即可。
|
||
*/
|
||
BytedanceSidebar.listenFromSidebar = function (onResult, target) {
|
||
if (!isBytedance()) {
|
||
onResult === null || onResult === void 0 ? void 0 : onResult.call(target, false);
|
||
return;
|
||
}
|
||
// @ts-ignore
|
||
tt.onShow(function (res) {
|
||
console.log('onShow launch res:', res);
|
||
if (res.scene === '021036') {
|
||
onResult === null || onResult === void 0 ? void 0 : onResult.call(target, true);
|
||
console.log('launch from sidebar');
|
||
}
|
||
else {
|
||
onResult === null || onResult === void 0 ? void 0 : onResult.call(target, false);
|
||
console.log('NOT launch from douyin sidebar!');
|
||
}
|
||
});
|
||
// @ts-ignore
|
||
var options = tt.getLaunchOptionsSync();
|
||
if (options && options.scene === '021036') {
|
||
onResult === null || onResult === void 0 ? void 0 : onResult.call(target, true);
|
||
}
|
||
};
|
||
/**
|
||
* 检测抖音侧边栏是否存在
|
||
* @param onResult 包含一个boolean参数的函数
|
||
* @param target 上述函数的拥有者,如果是类的成员函数,需要传入this。普通或匿名函数忽略即可。
|
||
* @returns
|
||
*/
|
||
BytedanceSidebar.checkSideBar = function (onResult, target) {
|
||
if (!isBytedance()) {
|
||
onResult === null || onResult === void 0 ? void 0 : onResult.call(target, false);
|
||
return;
|
||
}
|
||
//@ts-ignore
|
||
tt.checkScene({
|
||
scene: "sidebar",
|
||
success: function (res) {
|
||
console.log("check scene success: ", res.isExist);
|
||
onResult === null || onResult === void 0 ? void 0 : onResult.call(target, res.isExist);
|
||
},
|
||
fail: function (res) {
|
||
console.log("check scene fail:", res);
|
||
onResult === null || onResult === void 0 ? void 0 : onResult.call(target, false);
|
||
}
|
||
});
|
||
};
|
||
/**
|
||
* 跳转到抖音侧边栏
|
||
* @param onResult 包含一个boolean参数的函数
|
||
* @param target 上述函数的拥有者,如果是类的成员函数,需要传入this。普通或匿名函数忽略即可。
|
||
* @returns
|
||
*/
|
||
BytedanceSidebar.navigateToSidebar = function (onResult, target) {
|
||
if (!isBytedance()) {
|
||
console.log("not douyin platform!");
|
||
onResult === null || onResult === void 0 ? void 0 : onResult.call(target, false);
|
||
return;
|
||
}
|
||
// @ts-ignore
|
||
tt.navigateToScene({
|
||
scene: "sidebar",
|
||
success: function () {
|
||
console.log("navigate success");
|
||
onResult === null || onResult === void 0 ? void 0 : onResult.call(target, true);
|
||
},
|
||
fail: function (res) {
|
||
console.log("navigate failed reason:", res);
|
||
onResult === null || onResult === void 0 ? void 0 : onResult.call(target, false);
|
||
},
|
||
});
|
||
};
|
||
return BytedanceSidebar;
|
||
}());
|
||
MiniGameSdk.BytedanceSidebar = BytedanceSidebar;
|
||
})(MiniGameSdk = exports.MiniGameSdk || (exports.MiniGameSdk = {}));
|
||
|
||
cc._RF.pop(); |