var PLAT_QQMINI = { SHARE_FAIL: 40, SHARE_TIME: 3000, shareCount: {}, isThisPT(){ return this._ptid === cc.jc.channel.QQ_MINI; }, getLaunchInfo(){ if(this.isThisPT()){ return wx.getLaunchOptionsSync(); } return null; }, getSystemInfo(){ if(this.isThisPT()){ return wx.getSystemInfoSync(); } return null; }, /** example: * * @param {object} btninfo : * { * width: 100, * height: 100, * name: logo.png * } * @param {function} cb */ getUserInfo(btninfo, cb){ console.log('cc.sys.platform:' + cc.sys.platform); if(!this.isThisPT()){ cb && cb(null, -1000); return; } this._usercb = cb; let self = this; let getInfo = function() { console.log('[qq.getUserInfo]'); 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(wx.createUserInfoButton){ wx.getSetting({ success: (res) => { if (!res.authSetting["scope.userInfo"]) { var sysInfo = wx.getSystemInfoSync(); var sz = { width: cc.winSize.width, height: cc.winSize.height }; sz.width = sysInfo.screenWidth;//sysInfo.screenHeight / 2; sz.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('[qq.createUserInfoButton]'); console.log(sysInfo); console.log(btninfo); let stype = btninfo.name? 'image': 'text'; this.btnAuthorize = wx.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 { // wx.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('wx.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.qq; wx.showShareMenu({ withShareTicket: true }); wx.updateShareMenu({ withShareTicket: true }); let self = this; let _onShow = function(res){ console.log('[qq.onShow]'); self._owner.gameShow(); self._man && self._man.onPlatShow && self._man.onPlatShow(res); }; wx.offShow(_onShow); wx.onShow(_onShow); let _onHide = function(){ console.log('[qq.onHide]'); self._owner.gameHide(); self._man && self._man.onPlatHide && self._man.onPlatHide(); }; wx.offHide(_onHide); wx.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; } if(this.isThisPT()){ 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); } }); wx.onShareAppMessage(() => ({ title: normalinfo.title, imageUrl: normalinfo.imgurl // 图片 URL })); } }, PTShare(content, imgurl, param, successcb, failcb, tokenid){ if(this.isThisPT()){ wx.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, }); wx.shareAppMessage({ title: content, imageUrl: surl, query: param }); successcb && successcb(tokenid); }else{ failcb && failcb(-1000, null, tokenid); } }, PTOpenKF(cb){ if (this.isThisPT()) { if(wx && wx.openCustomerServiceConversation){ let self = this; let obj = { // sessionFrom: '烧味蹦蹦蹦', showMessageCard: false, sendMessageTitle: '', sendMessagePath: '', sendMessageImg:'', success:function(res){ console.log('[qq.openCustomerServiceConversation]success'); console.log(res); cb && cb(true); if(self._man && self._man.onKFOpened){ self._man.onKFOpened(); } }, fail:function(res){ console.log('[qq.openCustomerServiceConversation]fail'); console.log(res); cb && cb(false); } } wx.openCustomerServiceConversation(obj); } }else{ cb && cb(false); } }, PTCreateGameClubButton(nleft, ntop, nwidth){ if (this.isThisPT() && 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){ if (cc.sys.platform === cc.sys.WECHAT_GAME) { 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); } }; wx.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; wx.login({ success: function(res){ console.log("[qq]login success!" + res.code); self._owner.login.login(res.code, successcb, failcb); }, fail: function(res){ console.log("[qq]login fail!" + JSON.stringify(res)); failcb && failcb(0, res? res.errcode: -1, res? res.errmsg:""); }, }); }else{ failcb && failcb(0, -1000, "not weixin platform!"); } }, // isFromMyApp(){ // if(cc.sys.platform == cc.sys.WECHAT_GAME){ // let launchinfo = wx.getLaunchOptionsSync(); // if(launchinfo && launchinfo.scene){ // return launchinfo.scene == 1089; // } // } // return false; // }, PTSetClipboardData: function (content, successcb, failcb) { if (this.isThisPT()) { wx.setClipboardData({ data: content, successcb, failcb }); }else{ failcb && failcb(-1000); } }, PTGetClipboardData: function (successcb, failcb) { if (this.isThisPT()) { wx.getClipboardData({ success: function(res){ successcb && successcb(res.data); }, failcb }); }else{ failcb && failcb(-1000); } }, PTPreviewImage(imgurl, successcb, failcb){ if(this.isThisPT()){ wx.previewImage({ urls: [imgurl], successcb, failcb }) }else{ failcb && failcb(-1000); } }, //广告相关 PTInitADService(){ }, PTCreateBannerAD(adid, st){ if(this.isThisPT()){ if(wx.createBannerAd){ let rst = { width: st.width, top: st.top } let data = { adUnitId: adid, style: rst }; return wx.createBannerAd(data); } } return null; }, PTCreateVideoAD(adid){ if(this.isThisPT()){ if(wx.createRewardedVideoAd){ let data = { adUnitId: adid }; return wx.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()){ wx.vibrateShort({ success: ()=>{ successcb && successcb(); }, fail: () =>{ failcb && failcb(); } }) } }, PTVibrateLong(successcb, failcb){ if(this.isThisPT()){ wx.vibrateLong({ success: ()=>{ successcb && successcb(); }, fail: () =>{ failcb && failcb(); } }) } }, PTOpenDataContext(){ if(this.isThisPT()){ return wx.getOpenDataContext(); } return null; }, PTPostOpenMsg(msg){ if(this.isThisPT()){ const openDataContext = wx.getOpenDataContext(); openDataContext.postMessage(msg); } }, // 排行榜相关 saveRankData(nscore, nmoney, stitle){ if(this.isThisPT()){ const openDataContext = wx.getOpenDataContext(); openDataContext.postMessage({ type: "save_data", score: nscore, money: nmoney, title: stitle }); } }, showRankData(){ if(this.isThisPT()){ const openDataContext = wx.getOpenDataContext(); openDataContext.postMessage({ type: "friends_all", }); } }, hideRankData(){ if(this.isThisPT()){ const openDataContext = wx.getOpenDataContext(); openDataContext.postMessage({ type: "friends_all_hide", }); } }, prevRankPage(){ if(this.isThisPT()){ const openDataContext = wx.getOpenDataContext(); openDataContext.postMessage({ type: 'pagechange', prev: 1 }); } }, nextRankPage(){ if(this.isThisPT()){ const openDataContext = wx.getOpenDataContext(); openDataContext.postMessage({ type: 'pagechange', next: 1 }); } } }; module.exports = PLAT_QQMINI;