823 lines
20 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_TT = {
SHARE_FAIL: 40,
SHARE_TIME: 3000,
shareCount: {},
isThisPT() {
return this._ptid === jc.channel.TT;
},
getLaunchInfo() {
if (this.isThisPT()) {
return tt.getLaunchOptionsSync();
}
return null;
},
getSystemInfo() {
if (this.isThisPT()) {
return tt.getSystemInfoSync();
}
return null;
},
/** example:
*
* @param {object} btninfo :
* {
* width: 100,
* height: 100,
* name: logo.png
* }
* @param {function} cb
*/
getUserInfo(btninfo, cb) {
if (!this.isThisPT()) {
cb && cb(null, -1000);
return;
}
this._usercb = cb;
let self = this;
let getInfo = function () {
console.log('[tt.getUserInfo]');
tt.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 (tt.createUserInfoButton) {
tt.getSetting({
success: (res) => {
if (!res.authSetting['scope.userInfo']) {
var sysInfo = tt.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('[tt.createUserInfoButton]');
console.log(sysInfo);
console.log(btninfo);
let stype = btninfo.name ? 'image' : 'text';
this.btnAuthorize = tt.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) => {
console.log('[tt.getUserInfo]:', uinfo);
if (uinfo.userInfo) {
this._usercb && this._usercb(uinfo, 1);
} else {
// tt.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, 1);
} else {
console.log('authSetting success');
getInfo();
}
},
fail: () => {
console.error('getSetting fail');
this._usercb && this._usercb(null, -100);
}
});
} else {
console.log('tt.createUserInfoButton is null');
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.rawData) {
dstinfo.rawData = tokeninfo.rawData;
dstinfo.signature = tokeninfo.signature;
dstinfo.encryptedData = tokeninfo.encryptedData;
dstinfo.iv = tokeninfo.iv;
}
},
init(owner, channelid) {
this._owner = owner;
this._ptid = channelid;
// this.loadInfo();
if (this.isThisPT()) {
window.wx = window.tt;
tt.showShareMenu({
withShareTicket: true
});
tt.updateShareMenu({
withShareTicket: true
});
let self = this;
let _onShow = function (res) {
console.log('[tt.onShow]');
self._owner.gameShow();
self._man && self._man.onPlatShow && self._man.onPlatShow(res);
};
tt.offShow(_onShow);
tt.onShow(_onShow);
let _onHide = function () {
console.log('[tt.onHide]');
self._owner.gameHide();
self._man && self._man.onPlatHide && self._man.onPlatHide();
};
tt.offHide(_onHide);
tt.onHide(_onHide);
tt.onAudioInterruptionBegin((res) => {
console.log('tt.onAudioInterruptionBegin');
self._owner.event.emit(self._owner.event.Events.AUDIO_INTERRUPT_BEGIN, res);
});
tt.onAudioInterruptionEnd((res) => {
console.log('tt.onAudioInterruptionEnd');
self._owner.event.emit(self._owner.event.Events.AUDIO_INTERRUPT_END, res);
});
}
},
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 self = this;
if (exinfo && exinfo.videoPath) {
this.videoShare(title, img, sp, function () {
successcb && successcb();
}, function () {
failcb && failcb(-3, param);
}, exinfo);
} else {
this.PTShare(title, img, sp, function () {
successcb && successcb();
}, function () {
failcb && failcb(-3, param);
}, exinfo);
}
},
captureShare(rc, title, stype, param, successcb, failcb, exinfo) {
var st = (new Date()).getTime();
let sp = this._owner.makeShareParam(stype, param, exinfo);
this.PTShareCapture(title, rc, sp, function () {
successcb && successcb();
}, function () {
failcb && failcb(-3, param);
}, exinfo);
},
_checkShareRes(starttime, sharetype, param, 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);
},
PTStartRecord(dur, cb) {
if (!this.isThisPT()) {
return -100;
}
let maxdur = dur ? dur : 300;
if (!this.recorder) {
let self = this;
this.recorder = tt.getGameRecorderManager();
this.recorder.onStop(res => {
console.log('[video] on record stop', res.videoPath);
self._stoprecording = false;
self.recording = false;
self._videoPath = res.videoPath;
self._endTime = Date.now();
let obj = {
videoPath: self._videoPath,
endTime: self._endTime,
totalSec: self._endTime - self._beginTime
};
self._owner.event.emit(self._owner.event.Events.RECORD_STOP, obj);
if (self._stoprecordcb) {
self._stoprecordcb(obj);
self._stoprecordcb = null;
}
});
this.recorder.onStart(res => {
console.log('[video] on record start');
self._startrecording = false;
self.recording = true;
self._beginTime = Date.now();
let obj = {
beginTime: self._beginTime
};
self._owner.event.emit(self._owner.event.Events.RECORD_START, obj);
if (self._startrecordcb) {
self._startrecordcb(obj);
self._startrecordcb = null;
}
});
this.recorder.onError(res => {
console.log('[video] on record error');
console.log(res);
self._startrecording = false;
self._stoprecording = false;
// self.recording = false;
self._owner.event.emit(self._owner.event.Events.RECORD_ERROR, res);
});
}
if (this.recording) {
let obj = {
beginTime: this._beginTime
};
cb && cb(obj);
return 1;
} else {
this._startrecordcb = cb;
if (this._startrecording) {
return 100;
}
this._startrecording = true;
this.recorder.start({
duration: maxdur
});
this._man && this._man.scheduleOnce(this.PTStopRecord, maxdur > 10 ? maxdur - 10 : maxdur);
return 0;
}
;
},
PTStopRecord(cb) {
if (!this.isThisPT()) {
return -100;
}
if (!this.recording) {
let obj = {
videoPath: self._videoPath,
endTime: self._endTime,
totalSec: self._endTime - self._beginTime
};
cb && cb(obj);
return 1;
}
this._stoprecordcb = cb;
if (this._stoprecording) {
return 100;
}
this._stoprecording = true;
this.recorder && this.recorder.stop();
this._man && this._man.unschedule(this.PTStopRecord);
return 0;
},
PTIsRecording() {
return this.recording;
},
PTInitShare(normalinfo) {
console.log('[tt] init share config', normalinfo);
let self = this;
if (!normalinfo) {
return;
}
if (this.isThisPT()) {
tt.showShareMenu({
withShareTicket: true,
success: function (data) {
console.log(data);
},
fail: function (data) {
console.log(data);
}
});
tt.updateShareMenu({
withShareTicket: true,
success: function (data) {
console.log(data);
},
fail: function (data) {
console.log(data);
}
});
tt.onShareAppMessage(function (res) {
console.log(res.channel);
return {
title: normalinfo.title,
imageUrl: normalinfo.imgurl,
success() {
console.log('分享成功')
tt.showToast('分享成功', 'success');
},
fail(e) {
console.log('分享失败', e)
tt.showToast({title: '分享失败', icon: 'none'});
}
}
});
}
},
/**
* 头条的视频分享
* */
videoShare(content, imgurl, param, successcb, failcb, extra) {
let channel = 'article';
if (extra && extra.channel) {
channel = extra.channel;
delete extra['channel'];
}
let videoPath = extra.videoPath;
delete extra['videoPath'];
if (this.isThisPT()) {
tt.shareVideo({
videoPath: videoPath,
title: content,
query: param,
extra: extra,
success() {
console.log(`[TT]视频分享成功!`);
successcb && successcb();
},
fail(e) {
console.log(`[TT]视频分享失败!`, e);
failcb && failcb(-1000);
}
});
}
},
PTShare(content, imgurl, param, successcb, failcb, extra) {
let channel = 'article';
if (extra && extra.channel) {
channel = extra.channel;
delete extra['channel'];
}
if (this.isThisPT()) {
tt.shareAppMessage({
title: content,
imageUrl: imgurl,
query: param,
extra: extra,
success() {
console.log('[TT]分享成功');
successcb && successcb();
},
fail(e) {
console.log('[TT]分享失败');
failcb && failcb(-1000);
}
});
} else {
failcb && failcb(-1000);
}
},
PTShareCapture(content, rc, param, successcb, failcb) {
if (this.isThisPT()) {
let surl = canvas.toTempFilePathSync({
x: rc.x,
y: rc.y,
width: rc.width,
height: rc.height,
destWidth: 500,
destHeight: 400
});
tt.shareAppMessage({
title: content,
imageUrl: surl,
query: param,
success() {
console.log('分享成功')
tt.showToast({title: '分享成功'});
},
fail(e) {
console.log('分享失败', e)
tt.showToast({title: '分享失败', icon: 'none'});
}
});
} else {
failcb && failcb(-1000);
}
},
PTOpenKF(cb) {
if (this.isThisPT()) {
if (tt && tt.openCustomerServiceConversation) {
let self = this;
let obj = {
// sessionFrom: '烧味蹦蹦蹦',
showMessageCard: false,
sendMessageTitle: '',
sendMessagePath: '',
sendMessageImg: '',
success: function (res) {
console.log('[tt.openCustomerServiceConversation]success');
console.log(res);
cb && cb(true);
if (self._man && self._man.onKFOpened) {
self._man.onKFOpened();
}
},
fail: function (res) {
console.log('[tt.openCustomerServiceConversation]fail');
console.log(res);
cb && cb(false);
}
}
tt.openCustomerServiceConversation(obj);
}
} else {
cb && cb(false);
}
},
PTCreateGameClubButton(nleft, ntop, nwidth) {
if (this.isThisPT() && tt.createGameClubButton) {
let btn = tt.createGameClubButton({
icon: 'light',
style: {
left: nleft,
top: ntop,
width: nwidth,
height: nwidth
}
});
if (btn) {
btn.onTap((res) => {
console.log(res);
});
}
return btn;
}
},
/**
* @param {string} str 显示的文字
* @param {icon} icon 图标类型 默认'none';
* success: 显示成功图标
* loading: 显示加载图标
* none: 不显示图标,此时 title 文本最多可显示两行
* */
PTShowToast(str, icon) {
wx.showToast({title: str, icon: icon || 'none'});
},
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);
}
};
tt.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;
tt.login({
success: function (res) {
console.log('[tt]login success!' + res.code);
self._owner.login.login(res.code, successcb, failcb, null, false, res.anonymousCode || '');
},
fail: function (res) {
console.log('[tt]login fail!' + JSON.stringify(res));
// self._owner.login.loginAnonymous(successcb, failcb);
tt.login({
force: false,
success: function (res) {
console.log('[tt] second login success!' + res.code);
self._owner.login.login(res.code || '', successcb, failcb, null, false, res.anonymousCode || '');
},
fail: function (res) {
console.log('[tt]second login fail!' + JSON.stringify(res));
failcb && failcb(0, res ? res.errcode : -1, res ? res.errmsg : '');
}
})
// 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()) {
tt.setClipboardData({
data: content,
successcb,
failcb
});
} else {
failcb && failcb(-1000);
}
},
PTGetClipboardData: function (successcb, failcb) {
if (this.isThisPT()) {
tt.getClipboardData({
success: function (res) {
successcb && successcb(res.data);
},
failcb
});
} else {
failcb && failcb(-1000);
}
},
PTPreviewImage(imgurl, successcb, failcb) {
if (this.isThisPT()) {
tt.previewImage({
urls: [imgurl],
successcb,
failcb
})
} else {
failcb && failcb(-1000);
}
},
//广告相关
PTInitADService() {
},
PTBannerADMaxWidth() {
return 208;
},
PTBannerNeedLoad() {
return true;
},
PTCreateBannerAD(adid, st) {
if (this.isThisPT()) {
console.log('[tt] begin create banner ad,', adid);
if (tt.createBannerAd) {
let rst = {
width: st.width,
top: st.top,
left: st.left
}
let data = {
adUnitId: adid,
style: rst
};
return tt.createBannerAd(data);
}
}
return null;
},
PTCreateVideoAD(adid) {
if (this.isThisPT()) {
if (tt.createRewardedVideoAd) {
let data = {
adUnitId: adid
};
return tt.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, adowner, res);
} else {
// 播放中途退出,不下发游戏奖励
cb && cb(false, adowner, res);
}
// ad.offClose && ad.offClose(closeCb);
};
adobj.onClose && adobj.onClose(closeCb);
}
},
PTVibrateShort(successcb, failcb) {
if (this.isThisPT()) {
tt.vibrateShort({
success: () => {
successcb && successcb();
},
fail: () => {
failcb && failcb();
}
})
}
},
PTVibrateLong(successcb, failcb) {
if (this.isThisPT()) {
tt.vibrateLong({
success: () => {
successcb && successcb();
},
fail: () => {
failcb && failcb();
}
})
}
},
PTOpenDataContext() {
if (this.isThisPT()) {
return tt.getOpenDataContext();
}
return null;
},
PTPostOpenMsg(msg) {
if (this.isThisPT()) {
const openDataContext = tt.getOpenDataContext();
openDataContext.postMessage(msg);
}
},
// 排行榜相关
saveRankData(nscore, nmoney, stitle) {
if (this.isThisPT()) {
const openDataContext = tt.getOpenDataContext();
openDataContext.postMessage({
type: 'save_data',
score: nscore,
money: nmoney,
title: stitle
});
}
},
showRankData() {
if (this.isThisPT()) {
const openDataContext = tt.getOpenDataContext();
openDataContext.postMessage({
type: 'friends_all'
});
}
},
hideRankData() {
if (this.isThisPT()) {
const openDataContext = tt.getOpenDataContext();
openDataContext.postMessage({
type: 'friends_all_hide'
});
}
},
prevRankPage() {
if (this.isThisPT()) {
const openDataContext = tt.getOpenDataContext();
openDataContext.postMessage({
type: 'pagechange',
prev: 1
});
}
},
nextRankPage() {
if (this.isThisPT()) {
const openDataContext = tt.getOpenDataContext();
openDataContext.postMessage({
type: 'pagechange',
next: 1
});
}
},
setLocalStorage(key, value) {
tt.setStorageSync(key, value);
},
getLocalStorage(key) {
return tt.getStorageSync(key);
},
removeStorage(key) {
tt.removeStorageSync(key);
}
};
module.exports = PLAT_TT;