一、注意事项:由于微信按钮永远处于游戏的顶层,所以当你不需要授权的按钮处于其他ui的下层,或者关闭后,需要对你绑定的按钮进行销毁,当处于上层后,你需要重新绑定。我的操作是通过事件和UI管理器来处理这个问题,显示ui发送一个事件,有授权按钮的界面监听打开ui事件,判定这界面来取消绑定,关闭ui的时候判定需要授权的按钮是否在顶层来开启绑定。思路就是这个思路。
按钮坐标转换:
var wxSysInfo = wx.getSystemInfoSync()
var leftPos = wxSysInfo.windowWidth * 0.5 - 100
var topPos = wxSysInfo * 0.5 - 20
var width = 200
var height = 40
if (btn) {
if (btn.wxButton) {
btn.wxButton.destroy()
}
var btnRect = btn.getBoundingBoxToWorld()
var ratio = cc.view._devicePixelRatio
var scale = cc.view.getScaleX()
var factor = scale / ratio
var point = cc.v2(btnRect.x, btnRect.y)
cc.view._convertPointWithScale(point)
leftPos = btnRect.x * factor
topPos = wxSysInfo.screenHeight - (btnRect.y + btnRect.height) * factor
width = btnRect.width * factor
height = btnRect.height * factor
}
二、绑定和取消绑定
bindWxLoginButtons: function() {
this.bindWxBtns = []
if (CC_WECHATGAME) {
console.log("绑定所有需要微信授权的按钮")
if (this.getNode("btn_friend_rank")) {
QQWechatSDK.getWechatUserInfo(function(userInfo) {
if (!userInfo) {
cc.single.NoticeMgr.push("获取微信数据失败,不能进入排行榜")
return
}
// 发消息给子域
console.log("打开微信好友排行")
window.wx.postMessage({
messageType: 1,
tag: GameConfig.WechatRankTag,
});
var msg = {}
msg.playerData = {}
msg.playerData.nickname = userInfo.nickName
msg.playerData.avatarUrl = userInfo.avatarUrl
cc.single.NetMgr.sendToServer(GameMsg.MSGID.CS_GETPLAYERPASSLEVELRANK, msg)
cc.single.UIMgr.show(UIConfig.UI_WECHAT_RANK)
}.bind(this), this.getNode("btn_friend_rank"))
this.bindWxBtns.push(this.getNode("btn_friend_rank"))
}
if (this.getNode("btn_invite_friend")) {
// 根据微信授权情况来获取微信用户数据
QQWechatSDK.getWechatUserInfo(function(userInfo) {
if (!userInfo) {
cc.single.NoticeMgr.push("获取微信数据失败,不能进入好友邀请")
return
}
var msg = {}
msg.playerData = {}
msg.playerData.nickname = userInfo.nickName
msg.playerData.avatarUrl = userInfo.avatarUrl
cc.single.NetMgr.sendToServer(GameMsg.MSGID.CS_UPDATEWECHATPLAYERINFO, msg)
cc.single.UIMgr.show(UIConfig.INVITE)
}, this.getNode("btn_invite_friend"))
this.bindWxBtns.push(this.getNode("btn_invite_friend"))
}
}
},
unbindWxLoginButtons: function() {
for (var i = 0; i < this.bindWxBtns.length; i++) {
var btn = this.bindWxBtns[i]
if (btn) {
if (btn.wxButton) {
btn.wxButton.destroy()
}
}
}
},
三、动态创建微信授权按钮,透明度为0,即可不可见这个按钮,但是功能还在哦
上代码:
QQWechatSDK.getWechatUserInfo = function(callback, btn) {
if (!CC_WECHATGAME)
return
var _callback = function(_data) {
if (callback) {
callback(_data)
}
}
//检查是否有userinfo授权
var that = this
wx.getSetting({
success: function(res) {
if (res.authSetting["scope.userInfo"] || !wx.createUserInfoButton) {
var __getInfo = function(__callback) {
wx.getUserInfo({
success: function(res) {
var userInfo = res.userInfo
console.log("------已经授权! 调用wx.getUserInfo:-------", userInfo)
if (userInfo) {
__callback(userInfo)
} else {
console.log("success 微信用户信息获取失败")
}
},
fail: function(res) {
console.log("fail 微信用户信息获取失败")
__callback()
},
});
}
if (btn) {
btn.targetOff("click")
__getInfo(function(_userInfo) {
btn.on("click", function() {
_callback(_userInfo)
})
})
}
} else {
console.log("------未授权! 调用wx.createUserInfoButton-------", wx.getSystemInfoSync().windowWidth)
var wxSysInfo = wx.getSystemInfoSync()
var leftPos = wxSysInfo.windowWidth * 0.5 - 100
var topPos = wxSysInfo * 0.5 - 20
var width = 200
var height = 40
if (btn) {
if (btn.wxButton) {
btn.wxButton.destroy()
}
var btnRect = btn.getBoundingBoxToWorld()
var ratio = cc.view._devicePixelRatio
var scale = cc.view.getScaleX()
var factor = scale / ratio
var point = cc.v2(btnRect.x, btnRect.y)
cc.view._convertPointWithScale(point)
leftPos = btnRect.x * factor
topPos = wxSysInfo.screenHeight - (btnRect.y + btnRect.height) * factor
width = btnRect.width * factor
height = btnRect.height * factor
}
let button = wx.createUserInfoButton({
type: 'text',
text: '',
style: {
left: leftPos,
top: topPos,
width: width,
height: height,
lineHeight: 40,
backgroundColor: '#ff000000',
color: '#D8A9DF',
textAlign: 'center',
fontSize: 16,
borderRadius: 4
}
})
button.onTap((res) => {
cc.single.UIMgr.close(UIConfig.UI_WECHAT)
if (res.userInfo != "") {
var userInfo = res.userInfo
if (userInfo) {
button.destroy()
console.log("获取微信用户信息成功", res)
_callback(userInfo)
} else {
console.log("获取微信用户信息失败")
}
} else {
console.log("获取微信用户信息失败")
callback()
}
})
if (btn) {
btn.wxButton = button
}
}
},
});