Login.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. },
  5. onLoad() {
  6. let self = this;
  7. //如果不是微信环境
  8. if (typeof wx === 'undefined') {
  9. return;
  10. }
  11. cc.audioEngine.stopAll();
  12. this.progressTag = false;
  13. this.oldprogressTag = -99;
  14. cc.loader.onProgress = (completeCount, totalCount) => {
  15. if (this.progressTag) {
  16. this.progress = (1 * completeCount / totalCount).toFixed(1);
  17. // console.log("进度条", this.progress + '%', this.progressNode);
  18. if (this.progressNode != null) {
  19. this.progressNode.getComponent(cc.ProgressBar).progress = this.progress;
  20. }
  21. }
  22. };
  23. UtilsPrefabs.init(self.node).addPrefabs(UtilsPrefabs.PrefabsName[2], null, (node) => {
  24. // if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  25. // }
  26. //获取登录按钮ui,用wx 接口获取临时链接
  27. WeChat.onGetPlayButtonPicture((data) => {
  28. // console.log('按钮图片:', data);
  29. self.loginbuttonImageUrl = data.tempFileURL;
  30. self.wxLogin();
  31. }, (err) => {
  32. console.error('获取按钮图片失败,检查网络!');
  33. })
  34. });
  35. },
  36. wxLogin() {
  37. let self = this;
  38. let isOnTap = false;
  39. wx.login({
  40. success: function (res) {
  41. if (res.code) {
  42. console.log("登陆成功,获取到code", res.code);
  43. }
  44. //self.loginbuttonImageUrl
  45. // type: 'text',
  46. // text: '开始游戏',
  47. //image: 'https://7465-test-533989-1259102327.tcb.qcloud.la/pictures/PlayButtonnew.png?sign=545e70218ce3106a7e518fdfb8240c5e&t=1556089089',
  48. let button = wx.createUserInfoButton({
  49. type: 'image',
  50. image: self.loginbuttonImageUrl,
  51. style: {
  52. left: wx.getSystemInfoSync().screenWidth / 2 - 80,
  53. top: wx.getSystemInfoSync().screenHeight - 230,
  54. width: 160,
  55. height: 60,
  56. lineHeight: 40,
  57. backgroundColor: '#fb94a9',
  58. color: '#ffffff',
  59. textAlign: 'center',
  60. fontSize: 16,
  61. borderRadius: 4
  62. }
  63. })
  64. button.show();
  65. button.onTap((res) => {
  66. // console.log(res)
  67. if (res.errMsg == "getUserInfo:ok") {
  68. if (isOnTap) return;
  69. isOnTap = true;
  70. //点击允许,授权
  71. // console.log('已经授权。');
  72. //全局函数
  73. //注册或者登录用户信息
  74. WeChat.onRegisterUser(res.userInfo,
  75. (res) => {
  76. //记录用户信息在全局变量
  77. GlobalD.UserInfo = res.result.data.userinfo;
  78. userData.openId = res.result.data.openid;
  79. // console.log('用户信息:',);
  80. //清除微信授权按钮
  81. button.destroy();
  82. self._showLoading();//显示载入loading
  83. //全局函数
  84. //获取游戏数据
  85. WeChat.onGetGameData((res) => {
  86. console.log('获取的游戏数据:', res);
  87. //记录读取的数据到内存里面
  88. userData.readData = res.data;
  89. self._gotoScene();//跳转场景
  90. })
  91. }, (fail) => {//登录失败
  92. isOnTap = false;
  93. });
  94. } else {
  95. //点击拒绝,没有授权
  96. console.log('不允许登录。');
  97. }
  98. })
  99. }
  100. });
  101. },
  102. _showLoading() {
  103. console.log('显示载入预制');
  104. //显示载入预制
  105. UtilsPrefabs.init(this.node)
  106. .addPrefabs(UtilsPrefabs.PrefabsName[3], null, function (node) {
  107. node.y = -500;
  108. var bgbar = UtilsPrefabs.getNode("bgbar", node);
  109. this.progressNode = UtilsPrefabs.getNode("progress", bgbar);
  110. this.progressTag = true;
  111. }.bind(this));
  112. },
  113. _gotoScene() {
  114. cc.director.loadScene('MyCityScene - 004', function () {
  115. this.progressTag = false;
  116. }.bind(this));
  117. }
  118. });