Login.js 6.0 KB

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