LoginCtrl.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. const weChat = require('./net/WeChat');
  2. const LoginApi = require('./net/LoginApi');
  3. const DWTool = require('./utils/DWTool');
  4. // var tutorial = require('./utils/Bundle').tutorial;
  5. // console.log(tutorial);
  6. cc.Class({
  7. extends: cc.Component,
  8. properties: {
  9. preloadProgress: cc.ProgressBar,
  10. preloadLabel: cc.Label,
  11. updateUI: cc.Node,
  12. _updating: false, //是否正在更新
  13. _canRetry: false, //能否重新获取更新
  14. _preloaded: false, //是否已经预加载完资源
  15. },
  16. onLoad() {
  17. // this._testProto();
  18. // this.hotUpdate = this.node.getComponent('HotUpdate');
  19. // this.hotUpdate.init();
  20. // cc.director.preloadScene("game", () => {
  21. // this.hotUpdate.endPreload();
  22. // this._startGame();
  23. // cc.log("Next scene preloaded");
  24. // });
  25. this.loadAllGameRes();
  26. },
  27. loadAllGameRes() {
  28. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  29. this.updateUI.active = true;
  30. // 加载需要的图片资源
  31. cc.loader.loadResDir("./building", cc.Texture2D, (completeCount, totalCount, res) => {
  32. console.log("资源总数量: " + totalCount);
  33. console.log("当前下载中: " + completeCount);
  34. // 正在加载图片资源中...
  35. this.preloadProgress.progress = completeCount / totalCount;
  36. this.preloadLabel.string = '加载游戏资源中,请稍候...' + (this.preloadProgress.progress * 100).toFixed(0) + '%';
  37. }, (err, res) => {
  38. Global.buildRes = res;
  39. this.preloadProgress.progress = 0;
  40. this.loadSense();
  41. // 加载图片资源完成, 开始加载首页场景
  42. cc.director.preloadScene("game", () => {
  43. // 加载完成, 开始进入游戏
  44. this.endPreload();
  45. this._startGame();
  46. cc.log("Next scene preloaded");
  47. });
  48. });
  49. } else {
  50. this.updateUI.active = false;
  51. Global.showLoginPage = false;
  52. Global.channel = 'LuciferChannel';
  53. Global.os = 1;
  54. Global.ver = 1;
  55. let uid = DWTool.getUrlParam('uid');
  56. let token = DWTool.getUrlParam('token');
  57. if (!uid && !token) {
  58. Global.user = {
  59. uid: 1,
  60. token: 'lucifer_test_token',
  61. nick: 'lucifer',
  62. avatar: "",
  63. gender: 1
  64. }
  65. } else {
  66. Global.user = {
  67. uid: uid,
  68. token: token,
  69. nick: "游客",
  70. avatar: "",
  71. gender: 1
  72. }
  73. }
  74. this._getUserInfoAndStartGame();
  75. }
  76. },
  77. loadSense() {
  78. let dis = 0.012
  79. this.timeCtrl = () => {
  80. if (parseInt(this.preloadProgress.progress) == 1) {
  81. this._preloaded = true;
  82. this.unschedule(this.timeCtrl);
  83. } else {
  84. dis = dis < 0.002 ? 0.002 : dis - 0.0001;
  85. this.preloadProgress.progress += dis;
  86. }
  87. this.preloadLabel.string = '正在进入游戏,请稍候...' + (this.preloadProgress.progress * 100).toFixed(0) + '%'
  88. }
  89. this.schedule(this.timeCtrl, 0.03);
  90. },
  91. /**
  92. * 预加载结束
  93. */
  94. endPreload() {
  95. this.preloadProgress.progress = 1;
  96. this.scheduleOnce(() => {
  97. this.updateUI.active = false;
  98. }, 0.5)
  99. },
  100. _startGame() {
  101. if (Global.needLogin) {
  102. //服务端通知登录态失效,重新调起微信登录获取新的token
  103. weChat.login(() => {
  104. this._getUserInfoAndStartGame();
  105. });
  106. } else {
  107. //检查登录态
  108. weChat.checkLogin(flag => {
  109. if (flag) {
  110. //登录态未失效,静默登录
  111. weChat.loginStatic(() => {
  112. this._getUserInfoAndStartGame();
  113. })
  114. } else {
  115. //登录态失效,重新调起微信登录获取新的token
  116. weChat.login(() => {
  117. this._getUserInfoAndStartGame();
  118. });
  119. }
  120. })
  121. }
  122. },
  123. _getUserInfoAndStartGame() {
  124. LoginApi.getUserInfoPromise().then(({ data, msg }) => {
  125. Global.needLogin = false;
  126. Global.cityId = data.cityId;
  127. cc.director.loadScene("game");
  128. }).catch(({ code, msg }) => {
  129. console.error(msg);
  130. });
  131. },
  132. _testProto() {
  133. var Message = tutorial.Message;
  134. var payload = {
  135. commonMessage: {
  136. msg: "我现在来测试一下啦"
  137. }
  138. }
  139. // var errMsg = Message.verify(payload)
  140. // console.log(errMsg);
  141. var msg = Message.create(payload);
  142. console.log('[msg]: ', msg);
  143. var buffer = Message.encode(msg).finish()
  144. console.log('[buffer]: ', buffer);
  145. // var decoded2 = Message.decode(buffer);
  146. // console.log('[decoded]: ', decoded2);
  147. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  148. var socketTask = wx.connectSocket({
  149. url: "ws://172.16.15.133:8108/connect?uid=1&token=lucifer_test_token&channel=LuciferChannel&ver=1&os=1",
  150. header: {},
  151. success: function (ret) {
  152. }
  153. })
  154. socketTask.onOpen(function (res) {
  155. socketTask.send({
  156. data: buffer
  157. })
  158. })
  159. socketTask.onMessage(function (res) {
  160. var tt = new Uint8Array(res.data)
  161. var decoded = Message.decode(tt);
  162. console.log("[Response text msg]: ", decoded);
  163. })
  164. } else {
  165. var ws = new WebSocket("ws://172.16.15.133:8108/connect?uid=1&token=lucifer_test_token&channel=LuciferChannel&ver=1&os=1");
  166. ws.binaryType = "arraybuffer";
  167. ws.onopen = function (event) {
  168. console.log("[Send Text WS was opened]");
  169. };
  170. ws.onmessage = function (event) {
  171. // var buf = new ArrayBuffer(event.data);
  172. var tt = new Uint8Array(event.data)
  173. var decoded = Message.decode(tt);
  174. console.log("[Response text msg]: ", decoded);
  175. // console.log("response text msg: " + event.data);
  176. // console.log("response text type: " + typeof(event.data));
  177. };
  178. setTimeout(function () {
  179. ws.send(buffer);
  180. }, 3000)
  181. }
  182. }
  183. });