173 lines
5.1 KiB
JavaScript
173 lines
5.1 KiB
JavaScript
/**
|
|
* wx call unity
|
|
* @param cmd cmd
|
|
* @param content content
|
|
* @constructor
|
|
*/
|
|
function WxCallUnity(cmd, content) {
|
|
GameGlobal.Module.SendMessage("HCWxTools", "OnWxCallUnity", cmd + "#@#" + content);
|
|
}
|
|
|
|
function _WxCallUnity(cmd, adPostion, adUnit, code, msg) {
|
|
let result = {
|
|
"cmd": cmd,
|
|
"adPostion": adPostion,
|
|
"adUnit": adUnit,
|
|
"code": code,
|
|
"msg": msg,
|
|
};
|
|
WxCallUnity(cmd, JSON.stringify(result))
|
|
}
|
|
|
|
var tdId = null;
|
|
function UnityToWxMiniGame(reqStr) {
|
|
if(wx.HaichiMini && !tdId){
|
|
var temp = wx.HaichiMini.getDistinctId();
|
|
if(temp && temp!=tdId){
|
|
tdId = temp;
|
|
WxCallUnity("getDistinctId", tdId);
|
|
}
|
|
}
|
|
let res = JSON.parse(reqStr);
|
|
let cmd = res["cmd"];
|
|
switch (cmd) {
|
|
case "LoadNative":
|
|
LoadNative(res["adUnit"], res);
|
|
break
|
|
case "RemoveNative":
|
|
RemoveNative(res["adUnit"], res);
|
|
break
|
|
case "ShowNative":
|
|
ShowNative(res["adUnit"], res);
|
|
break
|
|
case "DestroyNative":
|
|
DestroyNative(res["adUnit"], res);
|
|
break
|
|
case "isShow":
|
|
return isShow(res["adUnit"], res);
|
|
break
|
|
case "umeng":
|
|
umeng(res["eventName"], res["data"], res);
|
|
break
|
|
case "HCTDAnalytics":
|
|
if(wx.HaichiMini){
|
|
return wx.HaichiMini.HCTDAnalytics(res);
|
|
}
|
|
break
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
function umeng(eventName, data, res) {
|
|
if(data){
|
|
data["wx_version"] = wx.getAccountInfoSync().miniProgram.version;
|
|
data["wx_model"] = wx.getSystemInfoSync().model;
|
|
data["wx_brand"] = wx.getSystemInfoSync().brand;
|
|
data["wx_language"] = wx.getSystemInfoSync().language;
|
|
data["wx_system"] = wx.getSystemInfoSync().system;
|
|
data["wx_platform"] = wx.getSystemInfoSync().platform;
|
|
data["wx_env_version"] = wx.getAccountInfoSync().miniProgram.envVersion;
|
|
}
|
|
wx.uma.trackEvent(eventName, data);
|
|
}
|
|
|
|
let dic = {}
|
|
|
|
function isShow(adUnit, res) {
|
|
let nativeAd = dic[adUnit];
|
|
return nativeAd && nativeAd.isShow();
|
|
}
|
|
|
|
function LoadNative(adUnit, res) {
|
|
if (!res.top || !res.left || !res.width) {
|
|
_WxCallUnity("onError", res.adPostion, res.adUnit, -99, "res is null")
|
|
return;
|
|
}
|
|
let style = {
|
|
top: res.top,
|
|
left: res.left,
|
|
width: res.width,
|
|
fixed: res._fixed,
|
|
};
|
|
|
|
let nativeAd = wx.createCustomAd({
|
|
adUnitId: adUnit,
|
|
style: style
|
|
});
|
|
nativeAd.onLoad(() => {
|
|
_WxCallUnity("onLoad", res.adPostion, res.adUnit, 0, "")
|
|
})
|
|
nativeAd.onClose(closeRes => {
|
|
console.log('关闭原生模板广告' + adUnit, closeRes, res);
|
|
_WxCallUnity("onClose", res.adPostion, res.adUnit, 0, "")
|
|
});
|
|
nativeAd.onError(err => {
|
|
// console.log('原生模板CustomAd 广告加载失败:' + adUnit, err);
|
|
_WxCallUnity("onError", res.adPostion, res.adUnit, err.errCode, err.errMsg)
|
|
});
|
|
nativeAd.onHide(() => {
|
|
// console.log('隐藏原生模板广告' + adUnit, res);
|
|
_WxCallUnity("onHide", res.adPostion, res.adUnit, 0, "")
|
|
});
|
|
|
|
dic[adUnit] = nativeAd
|
|
_WxCallUnity("newNativeAdObject", res.adPostion, res.adUnit, 0, "");
|
|
}
|
|
|
|
function RemoveNative(adUnit, res) {
|
|
let nativeAd = dic[adUnit];
|
|
if (nativeAd) {
|
|
nativeAd.hide()
|
|
.then(() => {
|
|
_WxCallUnity("onHideSuccess", res.adPostion, res.adUnit, 0, "")
|
|
})
|
|
.catch(err => {
|
|
_WxCallUnity("onHideFail", res.adPostion, res.adUnit, err.errCode, err.errMsg)
|
|
});
|
|
} else {
|
|
_WxCallUnity("onHideFail", res.adPostion, res.adUnit, -98, "native ad don't exit!")
|
|
}
|
|
}
|
|
|
|
function DestroyNative(adUnit, res) {
|
|
let nativeAd = dic[adUnit];
|
|
if (nativeAd) {
|
|
nativeAd.destroy()
|
|
.then(() => {
|
|
_WxCallUnity("onDestroySuccess", res.adPostion, res.adUnit, 0, "")
|
|
})
|
|
.catch(err => {
|
|
_WxCallUnity("onDestroyFail", res.adPostion, res.adUnit, err.errCode, err.errMsg)
|
|
});
|
|
} else {
|
|
_WxCallUnity("onDestroyFail", res.adPostion, res.adUnit, -97, "native ad don't exit!")
|
|
}
|
|
}
|
|
|
|
|
|
function ShowNative(adUnit, res) {
|
|
let nativeAd = dic[adUnit];
|
|
|
|
if (nativeAd && nativeAd.isDestroy) {
|
|
console.log("native ad 被销毁,或者不存在", nativeAd);
|
|
_WxCallUnity("onShowFail", res.adPostion, res.adUnit, -97, "native destroy");
|
|
_WxCallUnity("onDestroy", res.adPostion, res.adUnit, -97, "native destroy - 1");
|
|
return;
|
|
}
|
|
|
|
if (nativeAd.isDestroyed()) {
|
|
// 通常是点击不感兴趣被关闭的
|
|
_WxCallUnity("onShowFail", res.adPostion, res.adUnit, -97, "native destroy");
|
|
_WxCallUnity("onDestroy", res.adPostion, res.adUnit, -95, "native destroy - 2");
|
|
return;
|
|
}
|
|
nativeAd.show()
|
|
.then(() => {
|
|
_WxCallUnity("onShowSuccess", res.adPostion, res.adUnit, 0, "")
|
|
})
|
|
.catch(err => {
|
|
_WxCallUnity("onShowFail", res.adPostion, res.adUnit, err.errCode, err.errMsg)
|
|
});
|
|
|
|
} |