LoginCtrl.js 4.6 KB

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