我智商爆棚
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.
 
 
 
 
 
wozhishangbaopeng/assets/Platform/qq/QQMiniGameAPI.ts

541 lines
23 KiB

import AppPlatform from "../../FrameWork/Util/AppPlatform";
import AppConfig from "../../FrameWork/Config/AppConfig";
import AppSwitchConfig from "../../FrameWork/Config/AppSwitchConfig";
import wudianMgr from "../../FrameWork/Mgr/WudianMgr";
/**
* QQ 小游戏
*/
export default class QQMiniGameAPI {
public static readonly AppBlockStyle_custom = { left: 120, top: 200 } //样式,积木广告左上角横纵坐标, mmp 最小值32
public static readonly AppBlockSize_custom = 5 //范围是1~5,积木广告的个数(展示以实际拉取广告数量为准)
public static readonly AppBlockOrientation_custom = "landscape" //landscape 或者 vertical,积木广告横向展示或者竖向展示
public static Login_custom(onSuccess: Function, onFail: Function) {
if (AppPlatform.is_QQ_PLAY_custom()) {
window["qq"].login(
{
success: (res) => {
if (res.code) {
let code = res.code;
onSuccess(code);
console.log("登陆成功,获取到code : " + code)
}
}
})
}
}
//-------------------------激励视频---------------------------------
protected static _isRegRewardedVideoAdEvent_custom = false;
protected static _onRewardedVideoAdFailed_custom: Function = null;
protected static _onRewardedVideoAdClose_custom: Function = null;
protected static onRewardedVideoAdLoad_custom() {
console.log('激励视频 广告加载完成')
}
protected static onRewardedVideoAdError_custom(err) {
console.log('激励视频 广告加载失败' + err)
if (QQMiniGameAPI._onRewardedVideoAdFailed_custom) {
QQMiniGameAPI._onRewardedVideoAdFailed_custom();
}
}
protected static onRewardedVideoAdClose_custom(res) {
if ((res && res.isEnded) || res == null) {
console.log('激励视频 已完整观看')
if (QQMiniGameAPI._onRewardedVideoAdClose_custom) {
QQMiniGameAPI._onRewardedVideoAdClose_custom(true)
}
}
else {
console.log('激励视频 未完整观看')
if (QQMiniGameAPI._onRewardedVideoAdClose_custom) {
QQMiniGameAPI._onRewardedVideoAdClose_custom(false)
}
}
}
protected static regRewardedVideoAdEvent_custom(rewardedVideoAd) {
rewardedVideoAd.onLoad(QQMiniGameAPI.onRewardedVideoAdLoad_custom)
rewardedVideoAd.onError(QQMiniGameAPI.onRewardedVideoAdError_custom)
rewardedVideoAd.onClose(QQMiniGameAPI.onRewardedVideoAdClose_custom)
QQMiniGameAPI._isRegRewardedVideoAdEvent_custom = true;
}
public static showRewardedVideoAd_custom(onAdClose: Function, onFailed: Function) {
if (AppPlatform.is_QQ_PLAY_custom()) {
QQMiniGameAPI._onRewardedVideoAdClose_custom = onAdClose;
QQMiniGameAPI._onRewardedVideoAdFailed_custom = onFailed;
var rewardedVideoAd = window["qq"].createRewardedVideoAd(
{
adUnitId: AppConfig.qq_adUnitId_custom,
}
);
if (!QQMiniGameAPI._isRegRewardedVideoAdEvent_custom) {
QQMiniGameAPI.regRewardedVideoAdEvent_custom(rewardedVideoAd);
}
rewardedVideoAd.load().then(() => {
var promise = rewardedVideoAd.show();
promise.then(() => console.log('激励视频 广告显示成功'));
promise.catch((err) => {
rewardedVideoAd.load()
.then(() => rewardedVideoAd.show())
.catch(err => {
console.log('激励视频 广告显示失败')
if (onFailed) {
onFailed();
}
})
});
}).catch(err => {
console.log('激励视频 广告加载失败')
if (onFailed) {
onFailed();
}
})
}
else {
onAdClose(true);
}
}
//----------------------------------------------------------------
//-------------------------小游戏跳转---------------------------
public static navigateToMiniProgram_custom(appId: string, path: string, onSuccess: Function, onFail: Function, onComplate: Function) {
if (AppPlatform.is_QQ_PLAY_custom()) {
console.log("跳转游戏: " + appId);
window["qq"].navigateToMiniProgram(
{
appId: appId,
path: path,
extraData: {
foo: 'bar'
},
envVersion: 'release',
success(res) {
if (onSuccess) {
onSuccess(res)
}
},
fail(res) {
if (onFail) {
onFail(res)
}
},
complete(res) {
if (onComplate) {
onComplate(res)
}
}
})
}
}
//----------------------------------------------------------------------
//---------------------分享----------------------------------------
protected static _onShow_custom: Function = null;
protected static _lastShareTime_custom: number = 0;
public static share_custom(complate: Function, titel: string, imageUrl: string) {
if (AppPlatform.is_QQ_PLAY_custom()) {
QQMiniGameAPI._onShow_custom = () => {
window["qq"].offShow(QQMiniGameAPI._onShow_custom)
QQMiniGameAPI._onShow_custom = null;
var c = Date.now() - this._lastShareTime_custom;
if (complate) {
if (Date.now() - this._lastShareTime_custom > 2000) {
complate(true)
}
else {
complate(false)
}
}
}
window["qq"].onShow(QQMiniGameAPI._onShow_custom)
QQMiniGameAPI._lastShareTime_custom = Date.now();
window["qq"].shareAppMessage(
{
title: titel,
imageUrl: imageUrl
}
);
}
}
//----------------------------------------------------------------------
//--------------------插屏幕广告---------------------------------------
public static showInterstitialAd_custom(onAdClose: Function, onFailed: Function) {
if (AppPlatform.is_QQ_PLAY_custom()) {
var interstitialAd = window["qq"].createInterstitialAd({
adUnitId: AppConfig.qq_InsAdUnitId_custom,
})
let _onLoad = () => {
console.log('插屏广告 加载完成');
interstitialAd.show().catch((err) => {
console.log('插屏广告 显示失败 :' + err)
interstitialAd.offLoad(_onLoad);
interstitialAd.offError(_onError);
interstitialAd.offClose(_onClose);
interstitialAd.destroy();
if (onFailed) {
onFailed();
}
})
}
interstitialAd.onLoad(_onLoad)
let _onError = (err) => {
console.log('插屏广告 加载失败' + err);
interstitialAd.offLoad(_onLoad);
interstitialAd.offError(_onError);
interstitialAd.offClose(_onClose);
interstitialAd.destroy();
if (onFailed) {
onFailed();
}
}
interstitialAd.onError(_onError)
let _onClose = () => {
console.log('插屏广告 关闭');
interstitialAd.offLoad(_onLoad);
interstitialAd.offError(_onError);
interstitialAd.offClose(_onClose);
interstitialAd.destroy();
if (onAdClose) {
onAdClose();
}
}
interstitialAd.onClose(_onClose)
}
else {
onAdClose();
}
}
//--------------------盒子广告---------------------------------------
public static mAppboxAd_custom = null;
protected static onBoxAdClose_custom: Function = null;
public static LoadAppBoxAd_custom(onAdClose: Function, onFailed: Function) {
if (AppPlatform.is_QQ_PLAY_custom()) {
this.mAppboxAd_custom = window["qq"].createAppBox({
adUnitId: AppConfig.qq_AppBoxId_custom,
})
this.mAppboxAd_custom.load().then(() => {
console.log('盒子广告 加载完成');
})
this.mAppboxAd_custom.onError((err) => {
console.log('盒子广告 加载失败' + err);
if (onFailed) {
onFailed();
onFailed = null;
}
})
this.onBoxAdClose_custom = () => {
console.log('盒子广告 关闭');
if (onAdClose) {
onAdClose();
onAdClose = null;
}
}
this.mAppboxAd_custom.onClose(this.onBoxAdClose_custom);
}
else {
if (onAdClose) {
onAdClose();
onAdClose = null;
}
}
}
public static showAppBoxAd_custom(onFailed: Function, onAdClose?: Function) {
if (AppPlatform.is_QQ_PLAY_custom()) {
if (QQMiniGameAPI.mAppboxAd_custom) {
console.log("显示盒子广告");
QQMiniGameAPI.mAppboxAd_custom.offClose(QQMiniGameAPI.onBoxAdClose_custom);
QQMiniGameAPI.onBoxAdClose_custom = () => {
console.log('盒子广告 关闭');
if (onAdClose) {
onAdClose();
onAdClose = null;
}
}
QQMiniGameAPI.mAppboxAd_custom.onClose(QQMiniGameAPI.onBoxAdClose_custom);
QQMiniGameAPI.mAppboxAd_custom.show().catch((err) => {
console.log('盒子广告 显示失败 :' + err);
if (onFailed) {
onFailed();
onFailed = null;
}
})
} else {
QQMiniGameAPI.LoadAppBoxAd_custom(onAdClose, onFailed);
}
}
}
/**
* 得到小程序启动参数的同步方法,可得到一个Object返回值,返回值具体的数据结构在下面的列表中
* scene number 启动小游戏的场景值
* query Object 启动小游戏的 query 参数
* shareTicket string shareTicket,详见获取更多转发信息
* referrerInfo object 来源信息。从另一个小程序、公众号或 App 进入小程序时返回。否则返回 {}
* https://developers.weixin.qq.com/minigame/dev/api/base/app/life-cycle/qq.getLaunchOptionsSync.html
* @static
* @returns {LaunchOptions}
* @memberof QQMiniGameAPI
*/
public static getLaunchOptionsSync_custom() {
// let result = { scene: 0, query: null, shareTicket: "", referrerInfo: null };
if (AppPlatform.is_QQ_PLAY_custom()) {
let obj = window["qq"].getLaunchOptionsSync()
console.log("场景值 " + obj.scene);
let str = JSON.stringify(obj.query);
console.log("Query参数 " + str);
let key = obj.query["key"];
console.log("Query参数:key " + key);
console.log("ShareTicket " + obj.shareTicket);
console.log("ReferrerInfo.appId " + obj.referrerInfo.appId);
console.log("ReferrerInfo.extraData " + obj.referrerInfo.extraData);
return obj;
}
let obj = { scene: 1001, query: "", shareTicket: "", appId: "", extraData: "" }
return obj;
}
//----------------------------------------------------------------------
/**
* 打开微信左上角分享转发点击事件,在游戏逻辑中调用一次即可
* 注意此方法只会在真机上执行,在微信模拟器环境下点击转发按钮什么都不会发生
*
* @static
* @param {string} titel 分享标题
* @param {string} imageUrl 分享图片地址
* @param {Function} [success] 成功回调函数(可不填)
* @param {Function} [fail] 失败回调函数(可不填)
* @param {Function} [complate] 完成回调函数,成功失败都会执行(可不填)
* @memberof QQMiniGameAPI
*/
public static SetShareMenu_custom(titel: string, imageUrl: string, success?: Function, fail?: Function, complate?: Function) {
if (AppPlatform.is_QQ_PLAY_custom()) {
console.log("小游戏设置转发按钮");
window["qq"].showShareMenu({
withShareTicket: false,
success: success,
fail: fail,
complete: complate
});
window["qq"].onShareAppMessage(function () {
return {
title: titel,
imageUrl: imageUrl
}
});
}
}
//显示QQ狂点界面1,此接口添加了对场景值,IP地区的判断
//data 传入界面的参数 格式 :{ Complete : Function , PrizeCount : String | Number}
//onSuccess 界面成功打开回调
//onFail 界面打开失败回调
public static tryShowWXCrazyClick_custom(): boolean {
let launchScene = QQMiniGameAPI.getLaunchOptionsSync_custom().scene;
let noEnterBySearch: boolean = true;
let wudianSceneList = AppSwitchConfig.getInstance_custom().getAppSwitchData_custom().wudianSceneList_custom;
for (let i = 0; i < wudianSceneList.length; ++i) {
let wudianSceneValue = wudianSceneList[i];
if (launchScene == wudianSceneValue) {
noEnterBySearch = false;
}
}
let ipBlocked = wudianMgr.ryw_GetIpBlocked_custom();
let wudian = AppSwitchConfig.getInstance_custom().getAppSwitchData_custom().wudian_custom;
let kuangdianBanner = AppSwitchConfig.getInstance_custom().getAppSwitchData_custom().qqcfg_custom.kuangdianBanner_custom;
if (AppConfig.Versions_custom == AppSwitchConfig.getInstance_custom().getAppSwitchData_custom().qqcfg_custom.qqversions_custom
&& ipBlocked && noEnterBySearch && 1 == wudian && 1 == kuangdianBanner) {
return true;
}
else {
return false;
}
}
//显示QQ狂点界面2,此接口添加了对场景值,IP地区的判断
//data 传入界面的参数 格式 :{ Complete : Function , PrizeCount : String | Number}
//onSuccess 界面成功打开回调
//onFail 界面打开失败回调
public static tryShowWXCrazyClick2_custom(): boolean {
let launchScene = QQMiniGameAPI.getLaunchOptionsSync_custom().scene;
let noEnterBySearch: boolean = true;
let wudianSceneList = AppSwitchConfig.getInstance_custom().getAppSwitchData_custom().wudianSceneList_custom;
for (let i = 0; i < wudianSceneList.length; ++i) {
let wudianSceneValue = wudianSceneList[i];
if (launchScene == wudianSceneValue) {
noEnterBySearch = false;
}
}
let ipBlocked = wudianMgr.ryw_GetIpBlocked_custom();
let wudian = AppSwitchConfig.getInstance_custom().getAppSwitchData_custom().wudian_custom;
let kuangdianBox = AppSwitchConfig.getInstance_custom().getAppSwitchData_custom().qqcfg_custom.kuangdianBox_custom;
if (AppConfig.Versions_custom == AppSwitchConfig.getInstance_custom().getAppSwitchData_custom().qqcfg_custom.qqversions_custom
&& ipBlocked && noEnterBySearch && 1 == wudian && 1 == kuangdianBox) {
return true;
}
else {
return false;
}
}
//---------------------- 积木广告 ------------------------------------- /
/**
* 1000 后端错误调用失败 该项错误不是开发者的异常情况 一般情况下忽略一段时间即可恢复。
* 1001 参数错误 使用方法错误 可以前往 q.qq.com 确认具体教程(小程序和小游戏分别有各自的教程,可以在顶部选项中,“设计”一栏的右侧进行切换。
* 1002 广告单元无效 可能是拼写错误、或者误用了其他 APP 的广告 ID 请重新前往 q.qq.com 确认广告位 ID。
* 1003 内部错误 该项错误不是开发者的异常情况 一般情况下忽略一段时间即可恢复。
* 1004 无适合的广告 广告不是每一次都会出现,这次没有出现可能是由于该用户不适合浏览广告 属于正常情况,且开发者需要针对这种情况做形态上的兼容。
* 1005 广告组件审核中 你的广告正在被审核,无法展现广告 请前往 q.qq.com 确认审核状态,且开发者需要针对这种情况做形态上的兼容。
* 1006 广告组件被驳回 你的广告审核失败,无法展现广告 请前往 q.qq.com 确认审核状态,且开发者需要针对这种情况做形态上的兼容
* 1007 广告组件被驳回 你的广告能力已经被封禁,封禁期间无法展现广告 请前往 q.qq.com 确认小程序广告封禁状态。
* 1008 广告单元已关闭 该广告位的广告能力已经被关闭 请前往 q.qq.com 重新打开对应广告位的展现。
* 1009 广告位置设置错误 该广告的位置设置导致展示不全 请重新调整广告的位置
*/
private static mAppBlockAd_custom = null;
private static onFail: Function;
private static screenWidth_custom: number = NaN;
private static screenHeight_custom: number;
private static pixelRatio_custom: number;
private static isIos_custom: boolean;
private static isAppBlockAdLoading_custom: boolean = false;
private static skdVersion_custom: string;
private static supportSDKVersion_custom = "1.15.0";
/**
*
* @param onFail 错误回调
* @param centerX centerX 单位为px
* @param top top px值
* @param orientation landscape | vertical
*/
public static showAppBlockAd_custom(onFail?: Function, top: number = 150, orientation: string = "landscape") {
if (!AppPlatform.is_QQ_PLAY_custom()) return;
if (!window["qq"].createBlockAd) return;
if (QQMiniGameAPI.isAppBlockAdLoading_custom) return;
QQMiniGameAPI.isAppBlockAdLoading_custom = true;
if (isNaN(QQMiniGameAPI.screenWidth_custom)) {
try {
let res = window["qq"].getSystemInfoSync();
QQMiniGameAPI.screenWidth_custom = res.windowWidth;
QQMiniGameAPI.screenHeight_custom = res.windowHeight;
QQMiniGameAPI.pixelRatio_custom = res.pixelRatio;
QQMiniGameAPI.isIos_custom = res.platform == "ios";
QQMiniGameAPI.skdVersion_custom = res.SDKVersion;
//IOS下使用DP单位,left,top均为DP,android使用PX
QQMiniGameAPI.screenWidth_custom *= (QQMiniGameAPI.isIos_custom ? 1 : QQMiniGameAPI.pixelRatio_custom);
QQMiniGameAPI.screenHeight_custom *= (QQMiniGameAPI.isIos_custom ? 1 : QQMiniGameAPI.pixelRatio_custom);
console.log("getSystemInfoSync ==> ", res.SDKVersion);
} catch (e) {
if (onFail) onFail();
return;
}
}
let arr1: Array<number> = QQMiniGameAPI.skdVersion_custom.split(".").map(v => parseInt(v));
let arr2: Array<number> = QQMiniGameAPI.supportSDKVersion_custom.split(".").map(v => parseInt(v));
let isSupport: boolean = true;
for (let i: number = 0; i < arr1.length; i++) {
if (arr1[i] < arr2[i]) {
isSupport = false;
break;
}
}
if (!isSupport) return;
console.log("QQMiniGameAPI.showAppBlockAd ", top);
QQMiniGameAPI.destroyAppBlockAd_custom();
QQMiniGameAPI.onFail = onFail;
let min: number = QQMiniGameAPI.isIos_custom ? 32 / QQMiniGameAPI.pixelRatio_custom : 32;
let mTop: number = Math.max(min, top / (QQMiniGameAPI.isIos_custom ? QQMiniGameAPI.pixelRatio_custom : 1));
let mLeft: number = QQMiniGameAPI.screenWidth_custom / 2;
mLeft = min;
QQMiniGameAPI.mAppBlockAd_custom = window["qq"].createBlockAd({
adUnitId: AppConfig.qq_blockAdArray_custom[Math.floor(Math.random() * AppConfig.qq_blockAdArray_custom.length)],
style: { left: mLeft, top: mTop },
size: QQMiniGameAPI.AppBlockSize_custom,
orientation: orientation,
});
// QQMiniGameAPI.mAppBlockAd.onResize(QQMiniGameAPI.appBlockADResize);
QQMiniGameAPI.mAppBlockAd_custom.onError(QQMiniGameAPI.appBlockADError_custom);
QQMiniGameAPI.mAppBlockAd_custom.show().catch((err) => {
console.log('积木广告 显示失败 :' + JSON.stringify(err));
if (onFail) {
onFail();
}
});
QQMiniGameAPI.isAppBlockAdLoading_custom = false;
}
private static appBlockADResize_custom(obj: any): void {
if (!QQMiniGameAPI.mAppBlockAd_custom["style"]) return;
let realWidth = obj.width;
let realHeight = obj.height;
// console.log("onResize ==> qq ", screenWidth, "=", realWidth, "=", screenHeight, "=", realHeight);
// if (!isNaN(centerX)) {
// mLeft = ((screenWidth - realWidth) / 2) + (centerX / (isIos ? pixelRatio : 1));
// } else if (!isNaN(left)) {
// left /= (isIos ? pixelRatio : 1);
// mLeft = Math.max(33, left);
// } else if (!isNaN(right)) {
// right /= (isIos ? pixelRatio : 1);
// mLeft = Math.max(screenWidth - realWidth, right);
// }
// console.log("onResize ==> 3 ", mLeft, "=", mTop);
// QQMiniGameAPI.mAppBlockAd.style.left = mLeft;
let mLeft = (QQMiniGameAPI.screenWidth_custom - realWidth) / 2;
QQMiniGameAPI.mAppBlockAd_custom.style.left = mLeft;
}
private static appBlockADError_custom(err: any): void {
console.log("积木广告 加载失败 ", JSON.stringify(err));
if (QQMiniGameAPI.onFail) QQMiniGameAPI.onFail();
}
public static destroyAppBlockAd_custom() {
if (!AppPlatform.is_QQ_PLAY_custom()) return;
if (!QQMiniGameAPI.mAppBlockAd_custom) return;
console.log("QQMiniGameAPI.destroyAppBlockAd");
QQMiniGameAPI.mAppBlockAd_custom.offResize(QQMiniGameAPI.appBlockADResize_custom);
QQMiniGameAPI.mAppBlockAd_custom.offError(QQMiniGameAPI.appBlockADError_custom);
QQMiniGameAPI.mAppBlockAd_custom.hide();
QQMiniGameAPI.mAppBlockAd_custom.destroy();
QQMiniGameAPI.mAppBlockAd_custom = null;
}
//---------------------- 积木广告 ------------------------------------- /
}