jcfw-lite/jcfw/platform/plat_baidu.js
2020-03-20 20:20:24 +08:00

576 lines
14 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var PLAT_BAIDU = {
SHARE_FAIL: 40,
SHARE_TIME: 3000,
shareCount: {},
isThisPT() {
return typeof (swan) != 'undefined';
},
getLaunchInfo() {
if (this.isThisPT()) {
return swan.getLaunchOptionsSync();
}
return null;
},
getSystemInfo() {
if (this.isThisPT()) {
return swan.getSystemInfoSync();
}
return null;
},
/** example:
*
* @param {object} btninfo :
* {
* width: 100,
* height: 100,
* name: logo.png
* }
* @param {function} cb
*/
getUserInfo_Old(btninfo, cb) {
if (!this.isThisPT()) {
cb && cb(null, -1000);
return;
}
this._usercb = cb;
let self = this;
let getInfo = function () {
console.log('[swan.getUserInfo]');
this._usercb && this._usercb(null, -1);
// wx.getUserInfo({
// withCredentials: true,
// lang: 'zh_CN',
// success: function(res) {
// self._usercb && self._usercb(res, 1);
// },
// fail:function(res){
// console.log(res);
// self._usercb && self._usercb(res, -1);
// }
// });
}
if (swan.createUserInfoButton) {
swan.getSetting({
success: (res) => {
if (!res.authSetting['scope.userInfo']) {
var sysInfo = swan.getSystemInfoSync();
var sz = {
width: sysInfo.screenWidth,
height: sysInfo.screenHeight
}
var realw = btninfo.width / 512 * sz.width;
var realh = btninfo.height / 1200 * sz.height;
//var realx = sz.width / 2 + btnPos.x / 480 * sz.width - realw / 2;
//var realy = sz.height / 2 - btnPos.y / 960 * sz.height - realh / 2;
var realx = btninfo.x / 512 * sz.width - realw / 2;
var realy = sz.height - btninfo.y / 1200 * sz.height - realh / 2;
var windowW = sysInfo.windowWidth;
var windowH = sysInfo.windowHeight;
var btnwidth = btninfo.width;//102*1.4;
var btnheight = btninfo.height;//36*1.4;
console.log('[swan.createUserInfoButton]');
console.log(sysInfo);
console.log(btninfo);
let stype = btninfo.name ? 'image' : 'text';
this.btnAuthorize = swan.createUserInfoButton({
type: stype,
text: '',
image: btninfo.name,
style: {
left: realx,
top: realy,
width: realw,
height: realh
},
withCredentials: true,
lang: 'zh_CN'
}
);
this.btnAuthorize.onTap((uinfo) => {
if (uinfo.userInfo) {
this._usercb && this._usercb(uinfo, 1);
} else {
// swan.showToast({title:"授权失败"});
this._usercb && this._usercb(null, -10);
}
if (this.btnAuthorize) {
this.btnAuthorize.destroy && this.btnAuthorize.destroy();
this.btnAuthorize = null;
}
});
this._usercb && this._usercb(null, 0, this.btnAuthorize);
} else {
console.log('authSetting success');
getInfo();
}
},
fail: (res) => {
console.error('getSetting fail:');
console.error(res);
this._usercb && this._usercb(null, -100);
}
});
} else {
console.log('swan.createUserInfoButton is null');
getInfo();
}
},
getUserInfo(btninfo, cb) {
if (!this.isThisPT()) {
cb && cb(null, -1000);
return;
}
this._usercb = cb;
let self = this;
let getInfo = function () {
console.log('[swan.getUserInfo]');
swan.getUserInfo({
withCredentials: true,
lang: 'zh_CN',
success: function (res) {
self._usercb && self._usercb(res, 1);
},
fail: function (res) {
console.log(res);
self._usercb && self._usercb(res, -1);
}
});
}
getInfo();
},
setUserInfo(dstinfo, ptinfo) {
dstinfo.nickname = ptinfo.nickName;
dstinfo.country = ptinfo.country;
dstinfo.province = ptinfo.province;
dstinfo.city = ptinfo.city;
dstinfo.avatar_url = ptinfo.avatarUrl;
dstinfo.sex = ptinfo.gender;
},
setTokenInfo(dstinfo, tokeninfo) {
if (tokeninfo && tokeninfo.data) {
dstinfo.rawData = tokeninfo.data;
dstinfo.iv = tokeninfo.iv;
}
},
init(owner, channelid) {
this._owner = owner;
this._ptid = channelid;
// this.loadInfo();
if (this.isThisPT()) {
swan.showShareMenu({
withShareTicket: true
});
let self = this;
let _onShow = function (res) {
console.log('[swan.onShow]');
self._owner.gameShow();
self._man && self._man.onPlatShow && self._man.onPlatShow(res);
};
swan.offShow(_onShow);
swan.onShow(_onShow);
let _onHide = function () {
console.log('[swan.onHide]');
self._owner.gameHide();
self._man && self._man.onPlatHide && self._man.onPlatHide();
};
swan.offHide(_onHide);
swan.onHide(_onHide);
}
},
bindController(ctrl) {
this._man = ctrl;
},
commonShare(img, title, stype, param, successcb, failcb, exinfo) {
var st = (new Date()).getTime();
let sp = this._owner.makeShareParam(stype, param, exinfo);
let uuid = this._owner.currUUID();
this.PTShare(title, img, sp);
this._checkShareRes(st, stype, sp, uuid, successcb, failcb);
},
captureShare(rc, title, stype, param, successcb, failcb, exinfo) {
var st = (new Date()).getTime();
let sp = this._owner.makeShareParam(stype, param, exinfo);
let uuid = this._owner.currUUID();
this.PTShareCapture(title, rc, sp);
this._checkShareRes(st, stype, sp, uuid, successcb, failcb);
},
_checkShareRes(starttime, sharetype, param, tokenid, successcb, failcb) {
if (!this._man) {
try {
failcb && failcb(-100, param, tokenid);
} catch (err) {
}
return;
}
this._man.scheduleOnce(() => {
let dt = (new Date()).getTime() - starttime;
console.log(dt);
if (this.shareCount[sharetype] == undefined) {
this.shareCount[sharetype] = false;
}
if (this.shareCount[sharetype] == false && Math.random() * 100 > this.SHARE_FAIL) {
this.shareCount[sharetype] = true;
try {
failcb && failcb(-1, param, tokenid, dt);
} catch (err) {
}
return;
}
if (dt > this.SHARE_TIME) {
try {
successcb && successcb(param, tokenid, dt);
} catch (err) {
}
} else {
try {
failcb && failcb(-2, param, tokenid, dt);
} catch (err) {
}
}
}, 0.5);
},
PTInitShare(normalinfo) {
if (!normalinfo) {
return;
}
swan.showShareMenu({
withShareTicket: true,
success: function (data) {
console.log(data);
},
fail: function (data) {
console.log(data);
}
});
swan.onShareAppMessage(() => ({
title: normalinfo.title,
imageUrl: normalinfo.imgurl // 图片 URL
}));
},
PTShare(content, imgurl, param, successcb, failcb, tokenid) {
if (this.isThisPT()) {
swan.shareAppMessage({
title: content,
imageUrl: imgurl,
query: param
});
successcb && successcb(tokenid);
} else {
failcb && failcb(-1000, null, tokenid);
}
},
PTShareCapture(content, rc, param, successcb, failcb, tokenid) {
if (this.isThisPT()) {
let surl = canvas.toTempFilePathSync({
x: rc.x,
y: rc.y,
width: rc.width,
height: rc.height,
destWidth: 500,
destHeight: 400
});
swan.shareAppMessage({
title: content,
imageUrl: surl,
query: param
});
successcb && successcb(tokenid);
} else {
failcb && failcb(-1000, null, tokenid);
}
},
PTOpenKF(cb) {
if (this.isThisPT()) {
if (swan && swan.openCustomerServiceConversation) {
let self = this;
let obj = {
// sessionFrom: '烧味蹦蹦蹦',
showMessageCard: false,
sendMessageTitle: '',
sendMessagePath: '',
sendMessageImg: '',
success: function (res) {
console.log('[swan.openCustomerServiceConversation]success');
console.log(res);
cb && cb(true);
if (self._man && self._man.onKFOpened) {
self._man.onKFOpened();
}
},
fail: function (res) {
console.log('[swan.openCustomerServiceConversation]fail');
console.log(res);
cb && cb(false);
}
}
swan.openCustomerServiceConversation(obj);
}
} else {
cb && cb(false);
}
},
PTShowToast(str) {
if (this.isThisPT()) {
swan.showToast({title: str});
}
},
PTGotoApp(appid, apppath, exobj, cb, qdid) {
if (this.isThisPT()) {
let self = this;
let obj = {
appId: appid,
path: apppath,
extraData: exobj,
success: function (res) {
self._owner.gamelog.logJumpApp(appid, apppath, 1, qdid);
cb && cb(1, res);
},
fail: function (res) {
self._owner.gamelog.logJumpApp(appid, apppath, -1, qdid);
cb && cb(0, res);
}
};
swan.navigateToMiniProgram(obj);
} else {
this._owner.gamelog.logJumpApp(appid, apppath, -1000, qdid);
cb && cb(-1000, null);
}
},
PTLogin(info, successcb, failcb) {
if (this.isThisPT()) {
var self = this;
swan.login({
success: function (res) {
console.log('[swan]login success!' + res.code);
self._owner.login.login({token: res.code, successcb, failcb});
},
fail: function (res) {
console.log('[swan]login fail!');
failcb && failcb(0, res ? res.errcode : -1, res ? res.errmsg : '');
}
});
} else {
failcb && failcb(0, -1000, 'not weixin platform!');
}
},
PTSetClipboardData: function (content, successcb, failcb) {
if (this.isThisPT()) {
failcb && failcb(-1);
} else {
failcb && failcb(-1000);
}
},
PTGetClipboardData: function (successcb, failcb) {
if (this.isThisPT()) {
failcb && failcb(-1);
} else {
failcb && failcb(-1000);
}
},
PTPreviewImage(imgurl, successcb, failcb) {
if (this.isThisPT()) {
swan.previewImage({
urls: [imgurl],
successcb,
failcb
})
} else {
failcb && failcb(-1000);
}
},
//广告相关
PTInitADService(param) {
this._adsid = param;
},
PTCreateBannerAD(adid, st) {
if (this.isThisPT()) {
if (swan.createBannerAd) {
let data = {
adUnitId: adid,
appSid: this._adsid,
style: st
};
return swan.createBannerAd(data);
}
}
return null;
},
PTCreateVideoAD(adid) {
if (this.isThisPT()) {
if (swan.createRewardedVideoAd) {
let data = {
adUnitId: adid,
appSid: this._adsid
};
return swan.createRewardedVideoAd(data);
}
}
return null;
},
PTCreateInsertAD(adid) {
return null;
},
PTSetVideoADCallback(adobj, adowner, cb) {
if (this.isThisPT()) {
let closeCb = function (res) {
console.log('[ad]onClose:' + JSON.stringify(res));
// 用户点击了【关闭广告】按钮
// 小于 2.1.0 的基础库版本res 是一个 undefined
if (res && res.isEnded || res === undefined) {
// 正常播放结束,可以下发游戏奖励
cb && cb(res.isEnded, obj, res);
} else {
// 播放中途退出,不下发游戏奖励
cb && cb(false, adowner, res);
}
// ad.offClose && ad.offClose(closeCb);
};
adobj.onClose && adobj.onClose(closeCb);
}
},
PTVibrateShort(successcb, failcb) {
if (this.isThisPT()) {
swan.vibrateShort({
success: () => {
successcb && successcb();
},
fail: () => {
failcb && failcb();
}
})
}
},
PTVibrateLong(successcb, failcb) {
if (this.isThisPT()) {
swan.vibrateLong({
success: () => {
successcb && successcb();
},
fail: () => {
failcb && failcb();
}
})
}
},
PTOpenDataContext() {
if (this.isThisPT()) {
return swan.getOpenDataContext();
}
return null;
},
PTPostOpenMsg(msg) {
if (this.isThisPT()) {
const openDataContext = this.PTOpenDataContext();
openDataContext.postMessage(msg);
}
},
// 排行榜相关
saveRankData(nscore, nmoney, stitle) {
if (this.isThisPT()) {
const openDataContext = swan.getOpenDataContext();
openDataContext.postMessage({
type: 'save_data',
score: nscore,
money: nmoney,
title: stitle
});
}
},
showRankData() {
if (this.isThisPT()) {
const openDataContext = swan.getOpenDataContext();
openDataContext.postMessage({
type: 'friends_all'
});
}
},
hideRankData() {
if (this.isThisPT()) {
const openDataContext = swan.getOpenDataContext();
openDataContext.postMessage({
type: 'friends_all_hide'
});
}
},
prevRankPage() {
if (this.isThisPT()) {
const openDataContext = swan.getOpenDataContext();
openDataContext.postMessage({
type: 'pagechange',
prev: 1
});
}
},
nextRankPage() {
if (this.isThisPT()) {
const openDataContext = swan.getOpenDataContext();
openDataContext.postMessage({
type: 'pagechange',
next: 1
});
}
}
};
module.exports = PLAT_BAIDU;