当前位置:   article > 正文

wx.createUserInfoButton

wx.createuserinfobutton
  1. /**
  2. * wx api: https://developers.weixin.qq.com/minigame/dev/document/open-api/user-info/wx.createUserInfoButton.html
  3. */
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. wxSubContextView: cc.Node,
  8. tips: cc.Label,
  9. avatar: cc.Sprite,
  10. nickName: cc.Label,
  11. readme: cc.Label,
  12. background: cc.Node
  13. },
  14. start () {
  15. this.loadReadme();
  16. this.initAction();
  17. this.initUserInfoButton();
  18. },
  19. initAction () {
  20. this._isShow = false;
  21. this.wxSubContextView.y = 1000;
  22. this._showAction = cc.moveTo(0.5, this.wxSubContextView.x, 110);
  23. this._hideAction = cc.moveTo(0.5, this.wxSubContextView.x, 1000);
  24. this.background.on('touchstart', this.onClick, this);
  25. },
  26. initUserInfoButton () {
  27. if (typeof wx === 'undefined') {
  28. return;
  29. }
  30. let systemInfo = wx.getSystemInfoSync();
  31. let width = systemInfo.windowWidth;
  32. let height = systemInfo.windowHeight;
  33. let button = wx.createUserInfoButton({
  34. type: 'text',
  35. text: '',
  36. style: {
  37. left: 0,
  38. top: 0,
  39. width: width,
  40. height: height,
  41. lineHeight: 40,
  42. backgroundColor: '#00000000',
  43. color: '#00000000',
  44. textAlign: 'center',
  45. fontSize: 10,
  46. borderRadius: 4
  47. }
  48. });
  49. button.onTap((res) => {
  50. let userInfo = res.userInfo;
  51. if (!userInfo) {
  52. this.tips.string = res.errMsg;
  53. return;
  54. }
  55. this.nickName.string = userInfo.nickName;
  56. cc.loader.load({url: userInfo.avatarUrl, type: 'png'}, (err, texture) => {
  57. if (err) {
  58. console.error(err);
  59. return;
  60. }
  61. this.avatar.spriteFrame = new cc.SpriteFrame(texture);
  62. });
  63. wx.getOpenDataContext().postMessage({
  64. message: "User info get success."
  65. });
  66. this.wxSubContextView.runAction(this._showAction);
  67. this._isShow = true;
  68. button.hide();
  69. button.destroy();
  70. });
  71. },
  72. onClick () {
  73. this._isShow = !this._isShow;
  74. if (this._isShow) {
  75. this.wxSubContextView.runAction(this._showAction);
  76. }
  77. else {
  78. this.wxSubContextView.runAction(this._hideAction);
  79. }
  80. },
  81. onShowReadme () {
  82. this.readme.node.parent.active = true;
  83. },
  84. loadReadme () {
  85. cc.loader.loadRes('readme', cc.TextAsset, (err, res) => {
  86. this.readme.string = '\n' + res.text;
  87. });
  88. this.readme.node.on('touchstart' , () => {
  89. this.readme.node.parent.active = false;
  90. }, this);
  91. },
  92. });

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/99843
推荐阅读
相关标签
  

闽ICP备14008679号