LoginCtrl.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. wx.postMessage({ //初始化的时候关闭子域刷新
  19. messageType: 8,
  20. });
  21. // 先请求微信版本更新管理器
  22. if (typeof wx.getUpdateManager === 'function') {
  23. const updateManager = wx.getUpdateManager();
  24. updateManager.onCheckForUpdate((res) => {
  25. // 请求完新版本信息的回调
  26. if(res.hasUpdate) {
  27. wx.showLoading({
  28. title: '发现新版本内容,正在更新,请稍后...',
  29. success: function () {}
  30. })
  31. } else {
  32. // 无新版本内容,直接开始加载游戏资源
  33. this.loadAllGameRes();
  34. }
  35. })
  36. // 有新版本内容,则等待新版本下载完,再提示
  37. updateManager.onUpdateReady(() => {
  38. wx.hideLoading();
  39. wx.showModal({
  40. title: '更新提示',
  41. content: '新版本已经准备好,是否重启应用?',
  42. success: function (ret) {
  43. if (ret.confirm) {
  44. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  45. updateManager.applyUpdate()
  46. }
  47. }
  48. })
  49. })
  50. updateManager.onUpdateFailed(() => {
  51. // 新版本下载失败
  52. wx.hideLoading();
  53. this.loadAllGameRes();
  54. })
  55. } else {
  56. // 不支持版本更新管理器的情况,直接开始加载游戏资源
  57. this.loadAllGameRes();
  58. }
  59. } else {
  60. // 非微信环境,直接开始加载游戏资源
  61. this.loadAllGameRes();
  62. }
  63. },
  64. loadAllGameRes() {
  65. this.updateUI.active = true;
  66. this.preloadRes = false;
  67. this.preloadProgress.progress = 0;
  68. this.loadScene();
  69. // 加载图片资源完成, 开始加载首页场景
  70. cc.director.preloadScene("game", () => {
  71. cc.log("Next scene preloaded");
  72. this.preloadProgress.progress = 0;
  73. this.loginLabel.string = '正在拼命加载游戏资源...'
  74. // 加载需要的图片资源
  75. cc.loader.loadResDir("./building", cc.Texture2D, (completeCount, totalCount, res) => {
  76. this.preloadRes = true
  77. // 正在加载图片资源中...
  78. this.preloadProgress.progress = completeCount / totalCount;
  79. if (this.preloadProgress.progress) {
  80. this.preloadLabel.string = (this.preloadProgress.progress * 100).toFixed(0) + '%';
  81. }
  82. }, (err, res) => {
  83. Global.buildRes = res;
  84. // 加载完成, 开始进入游戏
  85. this.endPreload();
  86. if (CC_WECHATGAME) {
  87. this._startWxLogin();
  88. } else {
  89. Global.user = {
  90. uid: DWTool.getUrlParam('uid'),
  91. token: DWTool.getUrlParam('token'),
  92. nick: '游客',
  93. avatar: "",
  94. gender: 1
  95. }
  96. this._getUserInfoAndStartGame();
  97. }
  98. });
  99. });
  100. },
  101. loadScene() {
  102. let dis = 0.012
  103. this.loginLabel.string = '正在进入好友全明星...'
  104. this.timeCtrl = () => {
  105. if (parseInt(this.preloadProgress.progress) == 1) {
  106. this._preloaded = true;
  107. this.unschedule(this.timeCtrl);
  108. } else {
  109. dis = dis < 0.002 ? 0.002 : dis - 0.0001;
  110. this.preloadProgress.progress += dis;
  111. }
  112. if (!this.preloadRes) {
  113. this.preloadLabel.string = (this.preloadProgress.progress * 100).toFixed(0) + '%'
  114. }
  115. }
  116. this.schedule(this.timeCtrl, 0.02);
  117. },
  118. /**
  119. * 预加载结束
  120. */
  121. endPreload() {
  122. this.preloadProgress.progress = 1;
  123. },
  124. _startWxLogin() {
  125. this.loginLabel.node.active = false
  126. this.updateUI.active = false;
  127. if (Global.needLogin) {
  128. //服务端通知登录态失效,重新调起微信登录获取新的token
  129. weChat.login(() => {
  130. this._getUserInfoAndStartGame();
  131. });
  132. } else {
  133. //检查登录态
  134. weChat.checkLogin(flag => {
  135. if (flag) {
  136. //登录态未失效,静默登录
  137. weChat.loginStatic(() => {
  138. this._getUserInfoAndStartGame();
  139. })
  140. } else {
  141. //登录态失效,重新调起微信登录获取新的token
  142. weChat.login(() => {
  143. this._getUserInfoAndStartGame();
  144. });
  145. }
  146. })
  147. }
  148. },
  149. _getUserInfoAndStartGame() {
  150. LoginApi.getUserInfoPromise().then(({ code, data }) => {
  151. if (code == 0) {
  152. Global.needLogin = false;
  153. Global.userData = data
  154. Global.devCityId = data.cityId;
  155. // 获取存在服务端的用户guideState
  156. Api.httpGet({
  157. url: "/direct/me.do",
  158. data: {},
  159. success: res => {
  160. if (res) {
  161. window.guideState = JSON.parse(res)
  162. }
  163. cc.director.loadScene("game");
  164. }
  165. })
  166. } else {
  167. console.log(data.msg);
  168. }
  169. }).catch(({ code, msg }) => {
  170. if (msg) {
  171. console.log(msg);
  172. }
  173. });
  174. },
  175. });