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

667 lines
16 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_WX = {
SHARE_FAIL: 25,
SHARE_TIME: 3000,
shareCount: {},
getLaunchInfo() {
return wx.getLaunchOptionsSync();
},
getSystemInfo() {
return wx.getSystemInfoSync();
},
/** example:
*
* @param {object} btninfo :
* {
* width: 100,
* height: 100,
* name: logo.png
* }
* @param {function} cb
*/
getUserInfo(btninfo, cb) {
this._usercb = cb;
let _flag = 0;
if (btninfo && btninfo.flag) {
_flag = btninfo.flag;
}
let self = this;
let getInfo = function () {
console.log('[wx.getUserInfo]');
wx.getUserInfo({
withCredentials: true,
lang: 'zh_CN',
success: function (res) {
res.flag = _flag;
self._usercb && self._usercb(res, 1);
},
fail: function (res) {
res.flag = _flag;
console.log(res);
self._usercb && self._usercb(res, -1, null, _flag);
}
})
}
if (wx.createUserInfoButton) {
wx.getSetting({
success: (res) => {
if (!res.authSetting['scope.userInfo']) {
var sysInfo = wx.getSystemInfoSync();
console.log('[wx.createUserInfoButton]');
console.log(sysInfo);
if (!this.btnAuthorize) {
this.btnAuthorize = wx.createUserInfoButton({
type: 'text',
text: '',
style: {
left: 0,
top: 0,
width: sysInfo.screenWidth,
height: sysInfo.screenHeight
},
withCredentials: true,
lang: 'zh_CN'
});
this.btnAuthorize.onTap((uinfo) => {
if (uinfo.userInfo) {
uinfo.flag = _flag;
this._usercb && this._usercb(uinfo, 1);
this.destoryAuth();
} else {
// wx.showToast({title:"授权失败"});
this._usercb && this._usercb(null, -10, null, _flag);
}
});
}
this._usercb && this._usercb(null, 0, this.btnAuthorize, _flag);
} else {
console.log('authSetting success');
getInfo();
}
},
fail: () => {
console.error('getSetting fail');
this._usercb && this._usercb(null, -100, null, _flag);
}
});
} else {
console.log('wx.createUserInfoButton is null');
getInfo();
}
},
destoryAuth() {
if (this.btnAuthorize) {
this.btnAuthorize.destroy && this.btnAuthorize.destroy();
this.btnAuthorize = null;
}
},
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();
wx.showShareMenu({
withShareTicket: true
});
wx.updateShareMenu({
withShareTicket: true
});
let self = this;
let _onShow = function (res) {
console.log('[wx.onShow]', res.query);
self._owner.gameShow(res);
self._man && self._man.onPlatShow && self._man.onPlatShow(res);
if (self._jumpnow) {
self._jumpnow = false;
self._jumpobj.result = 2;
self._jumpobj.finishTime = new Date().getTime();
self._owner.event.emit(self._owner.event.Events.GOTO_APP_RESULT, self._jumpobj);
self._jumpobj = null;
}
};
try {
wx.offShow(_onShow);
} catch (err) {
}
wx.onShow(_onShow);
let _onHide = function () {
console.log('[wx.onHide]');
self._owner.gameHide();
self._man && self._man.onPlatHide && self._man.onPlatHide();
};
try {
wx.offHide(_onHide);
} catch (err) {
}
wx.onHide(_onHide);
wx.onAudioInterruptionBegin((res) => {
console.log('wx.onAudioInterruptionBegin');
self._owner.event.emit(self._owner.event.Events.AUDIO_INTERRUPT_BEGIN, res);
});
wx.onAudioInterruptionEnd((res) => {
console.log('wx.onAudioInterruptionEnd');
self._owner.event.emit(self._owner.event.Events.AUDIO_INTERRUPT_END, res);
});
},
bindController(ctrl) {
this._man = ctrl;
},
commonShare(aimg, atitle, stype, param, successcb, failcb, exinfo) {
var st = (new Date()).getTime();
let sp = this._owner.makeShareParam(stype, param, exinfo);
let uuid = this._owner.currUUID();
let img = aimg;
if (!img && this._exshareinfo) {
img = this._exshareinfo.imgurl;
}
let title = atitle;
if (!title && this._exshareinfo) {
title = this._exshareinfo.title;
}
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;
}
if (!this._exshareinfo) {
this._exshareinfo = normalinfo;
} else {
if (normalinfo.title && this._exshareinfo.title != normalinfo.title) {
this._exshareinfo.title = normalinfo.title;
}
if (normalinfo.imgurl && this._exshareinfo.imgurl != normalinfo.imgurl) {
this._exshareinfo.imgurl = normalinfo.imgurl;
}
}
wx.showShareMenu({
withShareTicket: true,
success: function (data) {
console.log(data);
},
fail: function (data) {
console.log(data);
}
});
wx.updateShareMenu({
withShareTicket: true,
success: function (data) {
console.log(data);
},
fail: function (data) {
console.log(data);
}
});
if (wx.aldOnShareAppMessage) {
wx.aldOnShareAppMessage(() => {
return {
title: this._exshareinfo.title,
imageUrl: this._exshareinfo.imgurl,
query: 'fromid=' + this._owner.accountID + '&fromname=' + this._owner.nickName
}
});
} else {
wx.onShareAppMessage(() => {
return {
title: this._exshareinfo.title,
imageUrl: this._exshareinfo.imgurl,
query: 'fromid=' + this._owner.accountID + '&fromname=' + this._owner.nickName
}
});
}
},
PTShare(content, imgurl, param, successcb, failcb) {
if (wx.aldShareAppMessage) {
wx.aldShareAppMessage({
title: content,
imageUrl: imgurl,
query: param
});
} else {
wx.shareAppMessage({
title: content,
imageUrl: imgurl,
query: param
});
}
successcb && successcb();
},
PTShareCapture(content, rc, param, successcb, failcb) {
let surl = canvas.toTempFilePathSync({
x: rc.x,
y: rc.y,
width: rc.width,
height: rc.height,
destWidth: 500,
destHeight: 400
});
if (wx.aldShareAppMessage) {
wx.aldShareAppMessage({
title: content,
imageUrl: surl,
query: param
});
} else {
wx.shareAppMessage({
title: content,
imageUrl: surl,
query: param
});
}
successcb && successcb();
},
PTOpenKF(cb, info) {
if (wx && wx.openCustomerServiceConversation) {
let self = this;
let obj = {
// sessionFrom: '烧味蹦蹦蹦',
showMessageCard: info.showCard,
sendMessageTitle: info.title,
sendMessagePath: '',
sendMessageImg: info.img,
success: function (res) {
console.log('[wx.openCustomerServiceConversation]success');
console.log(res);
cb && cb(true);
if (self._man && self._man.onKFOpened) {
self._man.onKFOpened();
}
},
fail: function (res) {
console.log('[wx.openCustomerServiceConversation]fail');
console.log(res);
cb && cb(false);
}
}
wx.openCustomerServiceConversation(obj);
}
},
PTCreateGameClubButton(nleft, ntop, nwidth) {
if (wx.createGameClubButton) {
let btn = wx.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, env, acontent) {
let starttm = new Date().getTime();
let self = this;
let envstr = env ? env : 'release';
let obj = {
appId: appid,
path: apppath,
extraData: exobj,
envVersion: envstr,
success: function (res) {
let endtm = new Date().getTime();
self._owner.gamelog.logJumpApp(appid, apppath, 1, qdid);
cb && cb(1, res, starttm, endtm);
let resobj = {
result: 1,
startTime: starttm,
endTime: endtm
};
(typeof (acontent) == 'object') && Object.assign(resobj, acontent);
self._owner.event.emit(self._owner.event.Events.GOTO_APP_RESULT, resobj);
self._jumpnow = true;
self._jumpobj = resobj;
},
fail: function (res) {
let endtm = new Date().getTime();
self._owner.gamelog.logJumpApp(appid, apppath, -1, qdid);
cb && cb(0, res);
let resobj = {
result: 0,
startTime: starttm,
endTime: endtm
};
(typeof (acontent) == 'object') && Object.assign(resobj, acontent);
self._owner.event.emit(self._owner.event.Events.GOTO_APP_RESULT, resobj);
}
};
wx.navigateToMiniProgram(obj);
},
PTLogin(info, successcb, failcb) {
var self = this;
wx.login({
success: function (res) {
console.log('[wx]login success!' + res.code);
self._owner.login.login({token: res.code, successcb, failcb});
},
fail: function (res) {
console.log('[wx]login fail!');
failcb && failcb(0, res ? res.errcode : -1, res ? res.errmsg : '');
}
});
},
PTSetClipboardData: function (content, successcb, failcb) {
wx.setClipboardData({
data: content,
successcb,
failcb
});
},
PTGetClipboardData: function (successcb, failcb) {
wx.getClipboardData({
success: function (res) {
successcb && successcb(res.data);
},
failcb
});
},
PTPreviewImage(imgurl, successcb, failcb) {
wx.previewImage({
urls: [imgurl],
successcb,
failcb
})
},
//广告相关
PTInitADService() {
},
PTCreateBannerAD(adid, st) {
if (wx.createBannerAd) {
let data = {
adUnitId: adid,
style: st
};
return wx.createBannerAd(data);
}
return null;
},
PTCreateVideoAD(adid) {
if (wx.createRewardedVideoAd) {
let data = {
adUnitId: adid
};
return wx.createRewardedVideoAd(data);
}
return null;
},
PTCreateInsertAD(adid) {
if (wx.createInterstitialAd) {
let data = {
adUnitId: adid
};
return wx.createInterstitialAd(data);
}
return null;
},
PTSetVideoADCallback(adobj, adowner, cb) {
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) {
wx.vibrateShort({
success: () => {
successcb && successcb();
},
fail: () => {
failcb && failcb();
}
})
},
PTVibrateLong(successcb, failcb) {
wx.vibrateLong({
success: () => {
successcb && successcb();
},
fail: () => {
failcb && failcb();
}
})
},
/**
* @return {null}
*/
PTOpenDataContext() {
if (wx && wx.getOpenDataContext) {
return wx.getOpenDataContext();
} else {
return null;
}
},
PTPostOpenMsg(msg) {
const openDataContext = this.PTOpenDataContext();
openDataContext && openDataContext.postMessage(msg);
},
// 排行榜相关
saveRankData(nscore, nmoney, stitle) {
const openDataContext = this.PTOpenDataContext();
openDataContext && openDataContext.postMessage({
type: 'save_data',
score: nscore,
money: nmoney,
title: stitle
});
},
showRankData() {
const openDataContext = this.PTOpenDataContext();
openDataContext && openDataContext.postMessage({
type: 'friends_all'
});
},
hideRankData() {
const openDataContext = this.PTOpenDataContext();
openDataContext && openDataContext.postMessage({
type: 'friends_all_hide'
});
},
prevRankPage() {
const openDataContext = this.PTOpenDataContext();
openDataContext && openDataContext.postMessage({
type: 'pagechange',
prev: 1
});
},
nextRankPage() {
const openDataContext = this.PTOpenDataContext();
openDataContext && openDataContext.postMessage({
type: 'pagechange',
next: 1
});
},
PTADWatch(adid, successcb, failcb) {
let videoAd = wx.createRewardedVideoAd({
adUnitId: adid
});
videoAd.load()
.then(function () {
videoAd.show();
})
.catch(function (err) {
console.log(err.errMsg);
});
var errorCb = function (res) {
console.log('[videoad]onerror:' + JSON.stringify(res));
failcb && failcb(adid, res, true, adid);
videoAd.offError(this);
}
videoAd.onError(errorCb);
var getFun = function (res) {
if (res.isEnded) {
try {
successcb && successcb(adid, res, adid);
} catch (e) {
}
cb = null;
}
videoAd.offClose(this);
}
videoAd.onClose(getFun);
},
setLocalStorage(key, value) {
wx.setStorageSync(key, value);
},
getLocalStorage(key) {
return wx.getStorageSync(key);
},
removeStorage(key) {
wx.removeStorageSync(key);
}
};
module.exports = PLAT_WX;