Login.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. loaderUI: cc.Node,
  12. _updating: false, //是否正在更新
  13. _canRetry: false, //能否重新获取更新
  14. _preloaded: false, //是否已经预加载完资源
  15. },
  16. onLoad() {
  17. // if (CC_WECHATGAME) {
  18. // wx.postMessage({ //初始化的时候关闭子域刷新
  19. // messageType: 8,
  20. // });
  21. // }
  22. this.loadAllGameRes();
  23. },
  24. loadAllGameRes() {
  25. this.loaderUI.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._endPreload()
  33. this._hideLoaderUI()
  34. cc.director.loadScene("game");
  35. });
  36. },
  37. _loadScene() {
  38. let dis = 0.012
  39. this.loginLabel.string = '正在拼命加载游戏资源...'
  40. this.timeCtrl = () => {
  41. if (parseInt(this.preloadProgress.progress) == 1) {
  42. this._preloaded = true;
  43. this.unschedule(this.timeCtrl);
  44. } else {
  45. dis = dis < 0.002 ? 0.002 : dis - 0.0001;
  46. this.preloadProgress.progress += dis;
  47. }
  48. if(!this.preloadRes) {
  49. this.preloadLabel.string = (this.preloadProgress.progress * 100).toFixed(0) + '%'
  50. }
  51. }
  52. this.schedule(this.timeCtrl, 0.03);
  53. },
  54. /**
  55. * 预加载结束
  56. */
  57. _endPreload() {
  58. this.preloadProgress.progress = 1;
  59. },
  60. _hideLoaderUI () {
  61. this.loginLabel.node.active = false
  62. this.loaderUI.active = false;
  63. },
  64. _startWxLogin() {
  65. this._hideLoaderUI()
  66. if (Global.needLogin) {
  67. //服务端通知登录态失效,重新调起微信登录获取新的token
  68. weChat.login(() => {
  69. this._getUserInfoAndStartGame();
  70. });
  71. } else {
  72. //检查登录态
  73. weChat.checkLogin(flag => {
  74. if (flag) {
  75. //登录态未失效,静默登录
  76. weChat.loginStatic(() => {
  77. this._getUserInfoAndStartGame();
  78. })
  79. } else {
  80. //登录态失效,重新调起微信登录获取新的token
  81. weChat.login(() => {
  82. this._getUserInfoAndStartGame();
  83. });
  84. }
  85. })
  86. }
  87. },
  88. _getUserInfoAndStartGame() {
  89. LoginApi.getUserInfoPromise().then(({ data, msg }) => {
  90. Global.needLogin = false;
  91. Global.devCityId = data.cityId;
  92. // 获取存在服务端的用户guideState
  93. Api.httpGet({
  94. url: "/direct/me.do",
  95. data: {},
  96. success: res => {
  97. if(res) {
  98. window.guideState = JSON.parse(res)
  99. }
  100. cc.director.loadScene("game");
  101. }
  102. })
  103. }).catch(({ code, msg }) => {
  104. if (msg) {
  105. console.log(msg);
  106. }
  107. });
  108. },
  109. });