LoginMessage.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. 头像: cc.Sprite,
  5. },
  6. onLoad() {
  7. this.获取用户信息();
  8. },
  9. start() {
  10. },
  11. 获取用户信息() {
  12. var self = this;
  13. if (CC_WECHATGAME) {
  14. let button = wx.createUserInfoButton({
  15. type: 'text',
  16. text: '',//获取用户信息
  17. style: {
  18. left: 0,
  19. top: 0,
  20. width: window.innerWidth,
  21. height: window.innerHeight,
  22. lineHeight: 40,
  23. backgroundColor: '#968C8C00',
  24. color: '#ffffff00',
  25. textAlign: 'center',
  26. fontSize: 16,
  27. borderRadius: 4
  28. },
  29. withCredentials: false,
  30. lang: 'zh_CN'
  31. })
  32. button.onTap((res) => {
  33. button.hide();
  34. // console.log(typeof (res.userInfo.nickName) + " " + res.userInfo.nickName);
  35. if (res.userInfo) {
  36. var userInfo = res.userInfo;
  37. //var nickName = userInfo.nickName;
  38. // var gender = userInfo.gender;
  39. var avatarUrl = userInfo.avatarUrl;
  40. button.destroy();
  41. self.头像设置(avatarUrl, self.头像);
  42. wx.showToast({ title: "授权成功" });
  43. } else {
  44. console.log("wxLogin auth fail");
  45. wx.showToast({ title: "授权失败", icon: 'none' });
  46. }
  47. })
  48. }
  49. },
  50. 弹窗(str) {
  51. if (CC_WECHATGAME) {
  52. wx.showToast({
  53. title: str,
  54. icon: 'none'
  55. });
  56. }
  57. },
  58. 头像设置: function (avatar, sp) {
  59. if (avatar == null || avatar == "")
  60. return;
  61. cc.loader.load(
  62. {
  63. url: avatar,
  64. type: 'jpg'
  65. },
  66. function (err, texture) {
  67. console.log("avatar ==========", err, texture)
  68. if (err) {
  69. console.log('加载玩家头像err === ', err);
  70. return;
  71. }
  72. texture.width = 74;
  73. texture.height = 74;
  74. sp.spriteFrame = new cc.SpriteFrame(texture);
  75. }
  76. )
  77. },
  78. // update (dt) {},
  79. });