//在节点按钮button上创建一个透明的按钮,等待用户点击授权,授权后隐藏或销毁按钮
createAuthorizeBtn:function(btnNode){
let btnSize = cc.size(btnNode.width+10,btnNode.height+10);
let frameSize = cc.view.getFrameSize();
let winSize = cc.director.getWinSize();
// console.log("winSize: ",winSize);
// console.log("frameSize: ",frameSize);
//适配不同机型来创建微信授权按钮
let left = (winSize.width*0.5+btnNode.x-btnSize.width*0.5)/winSize.width*frameSize.width;
let top = (winSize.height*0.5-btnNode.y-btnSize.height*0.5)/winSize.height*frameSize.height;
let width = btnSize.width/winSize.width*frameSize.width;
let height = btnSize.height/winSize.height*frameSize.height;
// console.log("button pos: ",cc.v2(left,top));
// console.log("button size: ",cc.size(width,height));
let self = this;
self.btnAuthorize = wx.createUserInfoButton({
type:"text",
text:"",
style:{
left:left,
top:top,
width:width,
height:height,
lineHeight:0,
backgroundColor:"",//透明
color:"#ffffff",
textAlign:'center',
fontSize:16,
borderRadius:4,
},
});
self.btnAuthorize.onTap((res)=>{
console.log("onTap res: " + res);
if(res.userInfo){
// console.log("wxLogin Auth success!");
wx.showToast({title:"授权成功"});
//授权成功 隐藏或销毁按钮
cc.find("Canvas").getComponent("StartMainScene").bAuthorizeSuccess = true;
cc.find("Canvas").getComponent("StartMainScene").btnAuthorize.hide();//隐藏按钮,或btnAuthorize.destroy();
wx.getUserInfo({
success:function(res){
cc.log(res.userInfo)
cc.find("Canvas").getComponent("StartMainScene").userInfo = res.userInfo;
cc.loader.load({url:res.userInfo.avatarUrl,type:"jpg"}, function(err, texture){
cc.find("Canvas/playerphoto").getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture);
});
}
});
}
else{
// console.log("wxLogin Auth fail");
wx.showToast({title:"授权失败"});
}
});
},