LoginCtrl.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. const weChat = require('../net/WeChat');
  2. const LoginApi = require('../net/LoginApi');
  3. const HomeApi = require("../net/HomeApi");
  4. const Api = require('../net/Api');
  5. const DWTool = require('../utils/DWTool');
  6. const GameModule = require('../utils/GameModule');
  7. cc.Class({
  8. extends: cc.Component,
  9. properties: {
  10. preloadProgress: cc.ProgressBar,
  11. preloadLabel: cc.Label,
  12. loginLabel: cc.Label,
  13. updateUI: cc.Node,
  14. _updating: false, //是否正在更新
  15. _canRetry: false, //能否重新获取更新
  16. _preloaded: false, //是否已经预加载完资源
  17. },
  18. onLoad() {
  19. if (CC_WECHATGAME) {
  20. window.wx.postMessage({ //初始化的时候关闭子域刷新
  21. messageType: 8,
  22. });
  23. }
  24. this.loadAllGameRes();
  25. },
  26. loadAllGameRes() {
  27. this.updateUI.active = true;
  28. this.preloadRes = false;
  29. this.preloadProgress.progress = 0;
  30. this.loadScene();
  31. // 加载图片资源完成, 开始加载首页场景
  32. cc.director.preloadScene("game", () => {
  33. cc.log("Next scene preloaded");
  34. this.preloadProgress.progress = 0;
  35. this.loginLabel.string = '正在拼命加载游戏资源...'
  36. // 加载需要的图片资源
  37. cc.loader.loadResDir("./textures/building", cc.Texture2D, (completeCount, totalCount, res) => {
  38. this.preloadRes = true
  39. // 正在加载图片资源中...
  40. this.preloadProgress.progress = completeCount / totalCount;
  41. if(this.preloadProgress.progress) {
  42. this.preloadLabel.string = (this.preloadProgress.progress * 100).toFixed(0) + '%';
  43. }
  44. }, (err, res) => {
  45. Global.buildRes = res;
  46. // 加载完成, 开始进入游戏
  47. this.endPreload();
  48. if(CC_WECHATGAME) {
  49. this._startWxLogin();
  50. } else {
  51. Global.user = {
  52. uid: DWTool.getUrlParam('uid'),
  53. token: DWTool.getUrlParam('token'),
  54. nick: '游客',
  55. avatar: "",
  56. gender: 1
  57. }
  58. this._getUserInfoAndStartGame();
  59. }
  60. });
  61. });
  62. },
  63. loadScene() {
  64. let dis = 0.012
  65. this.loginLabel.string = '正在进入好友全明星...'
  66. this.timeCtrl = () => {
  67. if (parseInt(this.preloadProgress.progress) == 1) {
  68. this._preloaded = true;
  69. this.unschedule(this.timeCtrl);
  70. } else {
  71. dis = dis < 0.002 ? 0.002 : dis - 0.0001;
  72. this.preloadProgress.progress += dis;
  73. }
  74. if(!this.preloadRes) {
  75. this.preloadLabel.string = (this.preloadProgress.progress * 100).toFixed(0) + '%'
  76. }
  77. }
  78. this.schedule(this.timeCtrl, 0.02);
  79. },
  80. /**
  81. * 预加载结束
  82. */
  83. endPreload() {
  84. this.preloadProgress.progress = 1;
  85. },
  86. _startWxLogin() {
  87. this.loginLabel.node.active = false;
  88. this.updateUI.active = false;
  89. if (Global.needLogin) {
  90. //服务端通知登录态失效,重新调起微信登录获取新的token
  91. weChat.login(() => {
  92. this._getUserInfoAndStartGame();
  93. });
  94. } else {
  95. //检查登录态
  96. weChat.checkLogin(flag => {
  97. if (flag) {
  98. //登录态未失效,静默登录
  99. weChat.loginStatic(() => {
  100. this._getUserInfoAndStartGame();
  101. })
  102. } else {
  103. //登录态失效,重新调起微信登录获取新的token
  104. weChat.login(() => {
  105. this._getUserInfoAndStartGame();
  106. });
  107. }
  108. })
  109. }
  110. },
  111. _getUserInfoAndStartGame() {
  112. LoginApi.getUserInfoPromise().then(({ data, msg }) => {
  113. Global.needLogin = false;
  114. Global.userData = data.userExtra;
  115. Global.diamond = data.userExtra.diamond;
  116. Global.skills = data.userSkills; //拥有技能
  117. Global.offlineGold = data.offlineGold; //离线收益
  118. this.getUserBuildings()
  119. .then((responseData) => {
  120. Global.BuildingManager.networkRooms = responseData.userRooms;
  121. cc.director.loadScene("game");
  122. }).catch((err) => {
  123. this._getDataFail();
  124. });
  125. }).catch(({ code, msg }) => {
  126. if (msg) {
  127. this._getDataFail();
  128. }
  129. });
  130. },
  131. _getDataFail() {
  132. this.loginLabel.node.active = false;
  133. this.updateUI.active = false;
  134. if (CC_WECHATGAME) {
  135. weChat.login(() => {
  136. this._getUserInfoAndStartGame();
  137. });
  138. } else {
  139. console.log();
  140. }
  141. },
  142. getUserBuildings() {
  143. return new Promise((resolve, reject) => {
  144. // 获取目标用户的建筑
  145. HomeApi.getUserBuildings((responseData) => {
  146. resolve(responseData);
  147. }, (error) => {
  148. reject(error);
  149. });
  150. })
  151. },
  152. });