You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
546 lines
20 KiB
546 lines
20 KiB
1 week ago
|
import AppPlatform from "../../FrameWork/Util/AppPlatform";
|
||
|
import AppSwitchConfig from "../../FrameWork/Config/AppSwitchConfig";
|
||
|
import AppConfig from "../../FrameWork/Config/AppConfig";
|
||
|
|
||
|
export default class VIVOAPI {
|
||
|
public static readonly adUnitId_custom = ""; //视频广告
|
||
|
public static readonly bannerAdUnitId_custom = ""; //banner广告
|
||
|
public static readonly nativeAdId_custom = ""; //原生广告
|
||
|
public static readonly InsAdUnitId_custom = ""; //插屏广告
|
||
|
|
||
|
public static rewardedAd_custom = null;
|
||
|
public static rewardedAdNum_custom = 0;
|
||
|
public static get BannerInstance_custom() {
|
||
|
return this._banner_custom;
|
||
|
}
|
||
|
protected static _banner_custom: any = null;
|
||
|
|
||
|
public static Login_custom(onSuccess: Function, onFail: Function) {
|
||
|
if (window["qg"].getSystemInfoSync().platformVersionCode >= 1053) {
|
||
|
console.log("vivo 开始登陆 >= 1053");
|
||
|
window["qg"].login().then((res) => {
|
||
|
if (res.data.token) {
|
||
|
let token = res.data.token;
|
||
|
onSuccess(token, true);
|
||
|
console.log("vivo 登陆成功,获取到 token : " + token);
|
||
|
}
|
||
|
else {
|
||
|
console.log('登录失败 res.data.token 为 null');
|
||
|
onFail();
|
||
|
}
|
||
|
}, (err) => {
|
||
|
console.log('登录失败' + JSON.stringify(err));
|
||
|
onFail();
|
||
|
});
|
||
|
} else {
|
||
|
console.log("vivo 开始登陆 < 1053");
|
||
|
window["qg"].authorize({
|
||
|
type: "token",
|
||
|
success: function (data) {
|
||
|
// 使用token进行服务端对接
|
||
|
window["qg"].getProfile({
|
||
|
token: data.accessToken,
|
||
|
success: function (data) {
|
||
|
console.log('openid获取成功', data.openid)
|
||
|
onSuccess(data.openid, false);
|
||
|
},
|
||
|
fail: function (data, code) {
|
||
|
console.log("获取openid失败 : " + code);
|
||
|
onFail();
|
||
|
}
|
||
|
})
|
||
|
},
|
||
|
fail: function (data, code) {
|
||
|
console.log('登录失败' + code);
|
||
|
onFail();
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//提示弹窗
|
||
|
public static showDialog_custom(titel: string, message: string, buttons: Array<any>, success: Function, cancel: Function, fail: Function) {
|
||
|
window["qg"].showDialog({
|
||
|
title: titel,
|
||
|
message: message,
|
||
|
buttons: buttons,
|
||
|
success: function (data) {
|
||
|
console.log('handling callback')
|
||
|
success();
|
||
|
},
|
||
|
cancel: function () {
|
||
|
console.log('handling cancel')
|
||
|
cancel();
|
||
|
},
|
||
|
fail: function (data, code) {
|
||
|
console.log(`handling fail, code = ${code}`)
|
||
|
fail();
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
//创建视频广告
|
||
|
public static createRewardedVideoAd_custom() {
|
||
|
if (AppPlatform.is_VIVO_GAME_custom()) {
|
||
|
VIVOAPI.rewardedAd_custom = window["qg"].createRewardedVideoAd({
|
||
|
posId: VIVOAPI.adUnitId_custom,
|
||
|
style: {}
|
||
|
});
|
||
|
|
||
|
VIVOAPI.rewardedAd_custom.onError(err => {
|
||
|
switch (err.errCode) {
|
||
|
case -3:
|
||
|
console.log("激励广告加载失败---调用太频繁", JSON.stringify(err));
|
||
|
break;
|
||
|
case -4:
|
||
|
console.log("激励广告加载失败--- 一分钟内不能重复加载", JSON.stringify(err));
|
||
|
break;
|
||
|
case 30008:
|
||
|
// 当前启动来源不支持激励视频广告,请选择其他激励策略
|
||
|
break;
|
||
|
default:
|
||
|
// 参考 https://minigame.vivo.com.cn/documents/#/lesson/open-ability/ad?id=广告错误码信息 对错误码做分类处理
|
||
|
console.log("激励广告展示失败")
|
||
|
console.log(JSON.stringify(err))
|
||
|
break;
|
||
|
}
|
||
|
})
|
||
|
VIVOAPI.rewardedAd_custom.onLoad(() => {
|
||
|
let adshow = VIVOAPI.rewardedAd_custom.show();
|
||
|
// 捕捉show失败的错误
|
||
|
adshow && adshow.then(() => {
|
||
|
console.log("激励广告展示成功");
|
||
|
}).catch(err => {
|
||
|
console.log("激励广告展示失败" + JSON.stringify(err))
|
||
|
VIVOAPI.onFailed();
|
||
|
})
|
||
|
})
|
||
|
|
||
|
VIVOAPI.rewardedAd_custom.onClose(res => {
|
||
|
if (res && res.isEnded) {
|
||
|
console.log("正常播放结束,可以下发游戏奖励");
|
||
|
VIVOAPI.onAdClose(true);
|
||
|
} else {
|
||
|
console.log("播放中途退出,不下发游戏奖励");
|
||
|
VIVOAPI.onAdClose(false);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//显示视频广告
|
||
|
//显示视频广告
|
||
|
private static onAdClose;
|
||
|
private static onFailed
|
||
|
public static showRewardedVideoAd_custom(onAdClose: Function, onFailed: Function) {
|
||
|
if (AppPlatform.is_VIVO_GAME_custom()) {
|
||
|
VIVOAPI.onAdClose = onAdClose;
|
||
|
VIVOAPI.onFailed = onFailed
|
||
|
console.log("---------------------------------- VIVOAPI.rewardedAd:", VIVOAPI.rewardedAd_custom + ",VIVOAPI.rewardedAdNum:", VIVOAPI.rewardedAdNum_custom)
|
||
|
// if (VIVOAPI.rewardedAd == null) {
|
||
|
// onFailed();
|
||
|
// return;
|
||
|
// }
|
||
|
|
||
|
if (VIVOAPI.rewardedAdNum_custom == 0) {
|
||
|
VIVOAPI.createRewardedVideoAd_custom();
|
||
|
} else {
|
||
|
// 第一次creat后广告可以在onload里面直接show
|
||
|
// 后续的加载必须要load才能触发onload接着才能show出广告
|
||
|
let adLoad = VIVOAPI.rewardedAd_custom.load();//第一次调用 可能会报-3 广告能正常展示就可以忽略
|
||
|
// 捕捉load失败的错误
|
||
|
adLoad && adLoad.catch(err => {
|
||
|
console.log("激励广告load失败" + JSON.stringify(err))
|
||
|
onFailed();
|
||
|
})
|
||
|
}
|
||
|
|
||
|
VIVOAPI.rewardedAdNum_custom = 1;
|
||
|
console.log("近来showRewardedVideoAd");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static mBannerAd_custom = null;
|
||
|
public static showBannerAd_custom() {
|
||
|
var self = this;
|
||
|
if (AppPlatform.is_VIVO_GAME_custom()) {
|
||
|
console.log('===========bannerAd showBanerAd');
|
||
|
var systemInfo = window["qg"].getSystemInfoSync();
|
||
|
var sw = systemInfo.screenWidth;
|
||
|
var sh = systemInfo.screenHeight;
|
||
|
this.mBannerAd_custom = window["qg"].createBannerAd({
|
||
|
posId: VIVOAPI.bannerAdUnitId_custom,
|
||
|
style: {}
|
||
|
});
|
||
|
let adshow = this.mBannerAd_custom.show();
|
||
|
// 调用then和catch之前需要对show的结果做下判空处理,防止出错(如果没有判空,在平台版本为1052以及以下的手机上将会出现错误)
|
||
|
adshow && adshow.then(() => {
|
||
|
console.log("banner广告展示成功");
|
||
|
}).catch((err) => {
|
||
|
switch (err.code) {
|
||
|
case 30003:
|
||
|
console.log("新用户7天内不能曝光Banner,请将手机时间调整为7天后,退出游戏重新进入")
|
||
|
break;
|
||
|
case 30009:
|
||
|
console.log("10秒内调用广告次数超过1次,10秒后再调用")
|
||
|
// setTimeout(() => {
|
||
|
// show()
|
||
|
// }, 10000);
|
||
|
break;
|
||
|
case 30002:
|
||
|
console.log("加载广告失败,重新加载广告")
|
||
|
// setTimeout(() => {
|
||
|
// retryShow()
|
||
|
// }, 10000);
|
||
|
break;
|
||
|
default:
|
||
|
// 参考 https://minigame.vivo.com.cn/documents/#/lesson/open-ability/ad?id=广告错误码信息 对错误码做分类处理
|
||
|
console.log("banner广告展示失败")
|
||
|
console.log(JSON.stringify(err))
|
||
|
break;
|
||
|
}
|
||
|
});
|
||
|
|
||
|
this.mBannerAd_custom.onError(function (err) {
|
||
|
console.log('Banner广告加载失败111:' + JSON.stringify(err));
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static hideBannerAd_custom() {
|
||
|
if (this.mBannerAd_custom) {
|
||
|
console.log('===========bannerAd 隐藏');
|
||
|
this.mBannerAd_custom.hide();
|
||
|
this.mBannerAd_custom.destroy();
|
||
|
this.mBannerAd_custom = null;
|
||
|
} else {
|
||
|
console.log('===========bannerAd 为空');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static navigateToMiniProgram_custom(pkgName: string, path: string, onSuccess: Function, onFail: Function, onComplate: Function) {
|
||
|
if (AppPlatform.is_VIVO_GAME_custom()) {
|
||
|
console.log("vivo 跳转游戏: " + pkgName);
|
||
|
window["qg"].navigateToMiniGame(
|
||
|
{
|
||
|
pkgName: pkgName,
|
||
|
path: path,
|
||
|
extraData: {
|
||
|
from: AppConfig.AppID_custom
|
||
|
},
|
||
|
envVersion: 'release',
|
||
|
success(res) {
|
||
|
if (onSuccess) {
|
||
|
onSuccess(res)
|
||
|
}
|
||
|
},
|
||
|
fail(res) {
|
||
|
if (onFail) {
|
||
|
onFail(res)
|
||
|
}
|
||
|
},
|
||
|
complete(res) {
|
||
|
if (onComplate) {
|
||
|
onComplate(res)
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static showInterstitialAd_custom(onAdClose: Function, onFailed: Function) {
|
||
|
if (AppPlatform.is_VIVO_GAME_custom()) {
|
||
|
var insertAd = window["qg"].createInterstitialAd({
|
||
|
posId: VIVOAPI.InsAdUnitId_custom
|
||
|
})
|
||
|
insertAd.onLoad(() => {
|
||
|
console.log("插屏广告加载完成");
|
||
|
})
|
||
|
insertAd.onClose(() => {
|
||
|
if (onAdClose) onAdClose();
|
||
|
})
|
||
|
insertAd.onError((err) => {
|
||
|
console.log("插屏广告拉取失败", JSON.stringify(err));
|
||
|
if (onFailed) {
|
||
|
onFailed();
|
||
|
}
|
||
|
});
|
||
|
insertAd.show().then(() => {
|
||
|
console.log("插屏广告显示成功");
|
||
|
}).catch(err => {
|
||
|
if (onFailed) onFailed();
|
||
|
});
|
||
|
}
|
||
|
else {
|
||
|
if (onAdClose) onAdClose();
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
public static getLaunchOptionsSync_custom() {
|
||
|
return {};
|
||
|
}
|
||
|
|
||
|
public static share_custom(complate: Function) {
|
||
|
if (AppPlatform.is_VIVO_GAME_custom()) {
|
||
|
window["qg"].share({
|
||
|
success() {
|
||
|
if (complate != null) {
|
||
|
complate(true);
|
||
|
}
|
||
|
|
||
|
window["qg"].showToast({
|
||
|
message: "分享成功"
|
||
|
})
|
||
|
},
|
||
|
|
||
|
fail(erromsg, errocode) {
|
||
|
// window["qg"].showToast({
|
||
|
// message: "分享失败:" + errocode + ': ' + erromsg
|
||
|
// })
|
||
|
|
||
|
window["qg"].showToast({
|
||
|
message: "分享失败"
|
||
|
})
|
||
|
},
|
||
|
|
||
|
cancel() {
|
||
|
window["qg"].showToast({
|
||
|
message: "分享失败"
|
||
|
})
|
||
|
},
|
||
|
|
||
|
complete() {
|
||
|
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static createDesktopIcon_custom(onSuccess: Function, onFail: Function) {
|
||
|
if (AppPlatform.is_VIVO_GAME_custom()) {
|
||
|
window["qg"].hasShortcutInstalled({
|
||
|
success: function (res) {
|
||
|
if (res == false) {
|
||
|
window["qg"].installShortcut(
|
||
|
{
|
||
|
success: function () {
|
||
|
if (onSuccess) {
|
||
|
onSuccess();
|
||
|
}
|
||
|
},
|
||
|
fail: function (err) {
|
||
|
if (onFail) {
|
||
|
onFail();
|
||
|
}
|
||
|
console.log("创建桌面图标失败!!!!", err);
|
||
|
for (var key in err) {
|
||
|
console.log(key, err);
|
||
|
}
|
||
|
},
|
||
|
complete: function () {
|
||
|
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
else {
|
||
|
console.log("桌面图标已存在!!!!");
|
||
|
if (onFail) {
|
||
|
onFail();
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
fail: function (err) {
|
||
|
if (onFail) {
|
||
|
onFail();
|
||
|
}
|
||
|
console.log("判断桌面图标是否存在失败!!!", err);
|
||
|
for (var key in err) {
|
||
|
console.log(key, err);
|
||
|
}
|
||
|
},
|
||
|
complete: function () {
|
||
|
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
else {
|
||
|
if (onFail) {
|
||
|
onFail();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
public static tryShowNativeAd_custom() {
|
||
|
let yuanshengSwitch = AppSwitchConfig.getInstance_custom().getAppSwitchData_custom().vivocfg_custom.yuanshengSwitch_custom;
|
||
|
let vivoVersions = AppSwitchConfig.getInstance_custom().getAppSwitchData_custom().vivocfg_custom.vivoversions_custom;
|
||
|
|
||
|
if (1 == yuanshengSwitch && vivoVersions == AppConfig.Versions_custom) {
|
||
|
return true
|
||
|
}
|
||
|
else {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
//尝试根据配置自动弹出创建图标确认框
|
||
|
public static tryPopCreateDestopIcon_custom(onSuccess: Function, onFail: Function) {
|
||
|
if (!AppPlatform.is_VIVO_GAME_custom()) {
|
||
|
if (null != onFail) {
|
||
|
onFail();
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
if (1 == AppSwitchConfig.getInstance_custom().getAppSwitchData_custom().vivocfg_custom.addToDesktop_custom) {
|
||
|
VIVOAPI.createDesktopIcon_custom(onSuccess, onFail);
|
||
|
}
|
||
|
else {
|
||
|
if (null != onFail) {
|
||
|
onFail();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//尝试根据配置显示插屏广告
|
||
|
public static tryShowInsAd_custom(onSuccess: Function, onFail: Function) {
|
||
|
let chapingSwitch = AppSwitchConfig.getInstance_custom().getAppSwitchData_custom().vivocfg_custom.chapingSwitch_custom;
|
||
|
if (1 == chapingSwitch) {
|
||
|
let rate = Math.random() * 100;
|
||
|
if (rate <= AppSwitchConfig.getInstance_custom().getAppSwitchData_custom().vivocfg_custom.chaping_custom) {
|
||
|
VIVOAPI.showInterstitialAd_custom(() => {
|
||
|
if (onSuccess) {
|
||
|
onSuccess();
|
||
|
}
|
||
|
},
|
||
|
() => {
|
||
|
if (onFail) {
|
||
|
onFail();
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
else {
|
||
|
if (onFail) {
|
||
|
onFail();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
if (onFail) {
|
||
|
onFail();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static _cachedNativeAd_custom: any = null;
|
||
|
public static _cachedAdItem_custom: any = null;
|
||
|
public static _cachedimgUrl_custom: string = null;
|
||
|
public static _tryLoadCount_custom: number = 5;
|
||
|
|
||
|
|
||
|
|
||
|
public static LoadCahcedNativeAd_custom(): void {
|
||
|
if (VIVOAPI._cachedNativeAd_custom) {
|
||
|
VIVOAPI._cachedNativeAd_custom.destroy();
|
||
|
VIVOAPI._cachedNativeAd_custom = null;
|
||
|
}
|
||
|
VIVOAPI._cachedAdItem_custom = null;
|
||
|
VIVOAPI._cachedNativeAd_custom = window["qg"].createNativeAd({
|
||
|
posId: VIVOAPI.nativeAdId_custom
|
||
|
})
|
||
|
VIVOAPI._cachedNativeAd_custom.load();
|
||
|
++VIVOAPI._tryLoadCount_custom;
|
||
|
console.log("缓存 原生广告 开始加载");
|
||
|
|
||
|
let self = this;
|
||
|
|
||
|
VIVOAPI._cachedNativeAd_custom.onLoad((res) => {
|
||
|
console.log("缓存 原生广告 加载成功:", res);
|
||
|
var adlist = res.adList;
|
||
|
for (var i = 0; i < adlist.length; ++i) {
|
||
|
var ad = adlist[i];
|
||
|
console.log("缓存 原生广告 数据:", i);
|
||
|
for (var key in ad) {
|
||
|
console.log(key, ad[key]);
|
||
|
}
|
||
|
}
|
||
|
VIVOAPI._cachedAdItem_custom = adlist[Math.floor(Math.random() * adlist.length)];
|
||
|
if (null != VIVOAPI._cachedAdItem_custom) {
|
||
|
for (var i = 0; i < VIVOAPI._cachedAdItem_custom.imgUrlList.length; ++i) {
|
||
|
console.log("缓存 原生广告 imgUrlList : ", i + " ", VIVOAPI._cachedAdItem_custom.imgUrlList[i])
|
||
|
}
|
||
|
let imgUrlList = VIVOAPI._cachedAdItem_custom.imgUrlList;
|
||
|
for (let i = 0; i < imgUrlList.length; ++i) {
|
||
|
let imgUrl = imgUrlList[i];
|
||
|
VIVOAPI._cachedimgUrl_custom = imgUrl;
|
||
|
if (null != VIVOAPI._cachedimgUrl_custom && "" != VIVOAPI._cachedimgUrl_custom) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
if (null != VIVOAPI._cachedimgUrl_custom && "" != VIVOAPI._cachedimgUrl_custom) {
|
||
|
console.log("缓存 原生广告 加载图片", VIVOAPI._cachedimgUrl_custom);
|
||
|
}
|
||
|
else {
|
||
|
console.log("缓存 原生广告 加载失败 imgulr is : ", VIVOAPI._cachedimgUrl_custom);
|
||
|
VIVOAPI._cachedNativeAd_custom.destroy();
|
||
|
VIVOAPI._cachedNativeAd_custom = null;
|
||
|
setTimeout(() => {
|
||
|
VIVOAPI.LoadCahcedNativeAd_custom();
|
||
|
}, 2500);
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
VIVOAPI._cachedNativeAd_custom.onError((res) => {
|
||
|
console.log("缓存 原生广告 加载失败:", res);
|
||
|
for (var key in res) {
|
||
|
console.log(key, res[key]);
|
||
|
}
|
||
|
VIVOAPI._cachedNativeAd_custom.destroy();
|
||
|
VIVOAPI._cachedNativeAd_custom = null;
|
||
|
setTimeout(() => {
|
||
|
VIVOAPI.LoadCahcedNativeAd_custom();
|
||
|
}, 5000);
|
||
|
})
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 判断是否创建桌面图标
|
||
|
* @param onSuccess
|
||
|
* @param onFail
|
||
|
*/
|
||
|
public static hasShortcutInstalled_custom(onSuccess: Function, onFail: Function) {
|
||
|
window["qg"].hasShortcutInstalled({
|
||
|
success: function (res) {
|
||
|
if (res == false) {
|
||
|
console.log("桌面图标不存在!!!!");
|
||
|
if (onSuccess) {
|
||
|
onSuccess(false);
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
console.log("桌面图标已存在!!!!");
|
||
|
if (onSuccess) {
|
||
|
onSuccess(true);
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
fail: function (err) {
|
||
|
if (onFail) {
|
||
|
onFail();
|
||
|
}
|
||
|
console.log("判断桌面图标是否存在失败!!!", err);
|
||
|
for (var key in err) {
|
||
|
console.log(key, err);
|
||
|
}
|
||
|
},
|
||
|
complete: function () {
|
||
|
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
}
|