LoginCtrl.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. const weChat = require('../net/WeChat');
  2. const QQPlayer = require('../net/QQPlayer');
  3. const LoginApi = require('../net/LoginApi');
  4. const HomeApi = require("../net/HomeApi");
  5. const Api = require('../net/Api');
  6. const DWTool = require('../utils/DWTool');
  7. const GameModule = require('../utils/GameModule');
  8. const TapTapTool = require("../utils/TapTapTool");
  9. var Promise = require('../lib/es6-promise').Promise;
  10. cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. preloadProgress: cc.ProgressBar,
  14. preloadLabel: cc.Label,
  15. loginLabel: cc.Label,
  16. updateUINode: cc.Node,
  17. restartButton: cc.Button,
  18. _updating: false, //是否正在更新
  19. _canRetry: false //能否重新获取更新
  20. // _preloaded: false, //是否已经预加载完资源
  21. },
  22. onLoad() {
  23. if (window.tt != undefined) {
  24. } else if (CC_WECHATGAME) {
  25. window.wx.postMessage({ //初始化的时候关闭子域刷新
  26. messageType: 8,
  27. });
  28. }
  29. this._updateChannel();
  30. GameGlobal._socketCount = 0;
  31. this.loadAllGameRes();
  32. },
  33. loadAllGameRes() {
  34. this.updateUINode.active = true;
  35. this.preloadProgress.progress = 0;
  36. this.loadScene();
  37. // 加载图片资源完成, 开始加载首页场景
  38. cc.director.preloadScene("game", () => {
  39. this.preloadProgress.progress = 1;
  40. this.preloadLabel.string = '100%';
  41. setTimeout(() => {
  42. this._downloadGameResources();
  43. }, 200);
  44. });
  45. },
  46. //下载游戏所需资源
  47. _downloadGameResources() {
  48. this._downloadBuildingRes().then((isOK) => {
  49. this._downloadPrefabRes().then((isOK) => {
  50. this._downloadStarAvatarRes().then((isOK) => {
  51. this._downloadStarSpine().then((isOK) => {
  52. this._downloadFinished();
  53. });
  54. });
  55. });
  56. });
  57. },
  58. _downloadBuildingRes() {
  59. this.preloadProgress.progress = 0;
  60. this.loginLabel.string = '正在拼命加载游戏资源...';
  61. return new Promise((resolve, reject) => {
  62. // 加载楼层图片资源
  63. cc.loader.loadResDir("./textures/building", cc.Texture2D, (completeCount, totalCount, res) => {
  64. // 正在加载图片资源中...
  65. let currentProgress = completeCount / totalCount * 0.25;
  66. if(currentProgress && !isNaN(currentProgress)) {
  67. this.preloadProgress.progress = currentProgress;
  68. this.preloadLabel.string = (this.preloadProgress.progress * 100).toFixed(0) + '%';
  69. } else {
  70. this.preloadLabel.string = 0 + '%';
  71. }
  72. }, (err, res) => {
  73. if (err) {
  74. // reject(err);
  75. } else {
  76. if (GameGlobal) {
  77. GameGlobal.buildRes = res;
  78. }
  79. }
  80. resolve(true);
  81. });
  82. });
  83. },
  84. _downloadPrefabRes() {
  85. this.preloadProgress.progress = 0.25;
  86. this.loginLabel.string = '正在拼命加载游戏资源...';
  87. return new Promise((resolve, reject) => {
  88. // 加载prefab资源
  89. cc.loader.loadResDir("./prefabs/precedence", cc.Prefab, (completeCount, totalCount, res) => {
  90. // 正在加载图片资源中...
  91. let currentProgress = completeCount / totalCount * 0.25;
  92. if(currentProgress && !isNaN(currentProgress)) {
  93. this.preloadProgress.progress = currentProgress + 0.25;
  94. this.preloadLabel.string = (this.preloadProgress.progress * 100).toFixed(0) + '%';
  95. } else {
  96. this.preloadLabel.string = 25 + '%';
  97. }
  98. }, (err, res) => {
  99. if (err) {
  100. // reject(err);
  101. } else {
  102. if (GameGlobal) {
  103. GameGlobal.prefabsRes = res;
  104. }
  105. // resolve(true);
  106. }
  107. resolve(true);
  108. });
  109. });
  110. },
  111. _downloadStarAvatarRes() {
  112. this.preloadProgress.progress = 0.5;
  113. this.loginLabel.string = '正在拼命加载游戏资源...';
  114. return new Promise((resolve, reject) => {
  115. // 加载明星头像图片资源
  116. cc.loader.loadResDir("./textures/star_handbook", cc.Texture2D, (completeCount, totalCount, res) => {
  117. // 正在加载图片资源中...
  118. let currentProgress = completeCount / totalCount * 0.25;
  119. if(currentProgress && !isNaN(currentProgress)) {
  120. this.preloadProgress.progress = currentProgress + 0.5;
  121. this.preloadLabel.string = (this.preloadProgress.progress * 100).toFixed(0) + '%';
  122. } else {
  123. this.preloadLabel.string = 50 + '%';
  124. }
  125. }, (err, res) => {
  126. if (err) {
  127. // reject(err);
  128. } else {
  129. if (GameGlobal) {
  130. GameGlobal.starAvatarRes = res;
  131. }
  132. // resolve(true);
  133. }
  134. resolve(true);
  135. });
  136. });
  137. },
  138. _downloadStarSpine() {
  139. this.preloadProgress.progress = 0.75;
  140. this.loginLabel.string = '正在拼命加载游戏资源...';
  141. return new Promise((resolve, reject) => {
  142. // 加载prefab资源
  143. cc.loader.loadResDir("./spines/precedence", sp.SkeletonData, (completeCount, totalCount, res) => {
  144. // 正在加载图片资源中...
  145. let currentProgress = completeCount / totalCount * 0.25;
  146. if(currentProgress && !isNaN(currentProgress)) {
  147. this.preloadProgress.progress = currentProgress + 0.75;
  148. this.preloadLabel.string = (this.preloadProgress.progress * 100).toFixed(0) + '%';
  149. } else {
  150. this.preloadLabel.string = 75 + '%';
  151. }
  152. }, (err, res) => {
  153. if (err) {
  154. // reject(err);
  155. } else {
  156. if (GameGlobal) {
  157. GameGlobal.starSpineRes = res;
  158. }
  159. // resolve(true);
  160. }
  161. resolve(true);
  162. });
  163. });
  164. },
  165. // 加载完成, 开始进入游戏
  166. _downloadFinished() {
  167. this.endPreload();
  168. this.startLogin();
  169. },
  170. //// 开始登录
  171. startLogin() {
  172. if (window.tt != undefined) {
  173. this._startWxLogin();
  174. }else if(CC_WECHATGAME) {
  175. this._startWxLogin();
  176. } else if (CC_QQPLAY) {
  177. QQPlayer.qqLogin((isError) => {
  178. if (isError) {
  179. this._getDataFail();
  180. } else {
  181. this._getUserInfoAndStartGame();
  182. }
  183. });
  184. } else {
  185. GameGlobal.user = {
  186. uid: DWTool.getUrlParam('uid'),
  187. token: DWTool.getUrlParam('token'),
  188. nick: '游客',
  189. avatar: "",
  190. gender: 1
  191. }
  192. this._getUserInfoAndStartGame();
  193. }
  194. },
  195. //假的等待加载
  196. loadScene() {
  197. let dis = 0.012;
  198. this.loginLabel.string = '正在进入点点大明星...';
  199. this.timeCtrl = () => {
  200. if (parseInt(this.preloadProgress.progress) == 1) {
  201. this.unschedule(this.timeCtrl);
  202. } else {
  203. dis = dis < 0.002 ? 0.002 : dis - 0.0001;
  204. this.preloadProgress.progress += dis;
  205. if(this.preloadProgress.progress && !isNaN(this.preloadProgress.progress)) {
  206. this.preloadLabel.string = (this.preloadProgress.progress * 100).toFixed(0) + '%';
  207. } else {
  208. this.preloadLabel.string = 0 + '%';
  209. }
  210. }
  211. }
  212. this.schedule(this.timeCtrl, 0.02);
  213. },
  214. /**
  215. * 预加载结束
  216. */
  217. endPreload() {
  218. this.preloadProgress.progress = 1;
  219. },
  220. _startWxLogin() {
  221. this.loginLabel.node.active = false;
  222. this.updateUINode.active = false;
  223. if (GameGlobal.needLogin) {
  224. //服务端通知登录态失效,重新调起微信登录获取新的token
  225. weChat.login((isError) => {
  226. if (isError) {
  227. this._getDataFail();
  228. } else {
  229. this._getUserInfoAndStartGame();
  230. }
  231. });
  232. } else {
  233. //检查登录态
  234. weChat.checkLogin(flag => {
  235. if (flag) {
  236. //登录态未失效,静默登录
  237. weChat.loginStatic(() => {
  238. this._getUserInfoAndStartGame();
  239. });
  240. } else {
  241. //登录态失效,重新调起微信登录获取新的token
  242. weChat.login((isError) => {
  243. if (isError) {
  244. this._getDataFail();
  245. } else {
  246. this._getUserInfoAndStartGame();
  247. }
  248. });
  249. }
  250. })
  251. }
  252. },
  253. _getUserInfoAndStartGame() {
  254. if (GameGlobal.user == undefined || GameGlobal.user.uid == undefined) {
  255. if (CC_QQPLAY) {
  256. this.startLogin();
  257. } else if (CC_WECHATGAME || window.tt != undefined) {
  258. weChat.login((isError) => {
  259. if (isError) {
  260. this._getDataFail();
  261. } else {
  262. this._getUserInfoAndStartGame();
  263. }
  264. });
  265. }
  266. return;
  267. }
  268. LoginApi.getUserInfoPromise().then(({ data, msg }) => {
  269. GameGlobal.needLogin = false;
  270. GameGlobal.userData = data.userExtra;
  271. GameGlobal.diamond = data.userExtra.diamond;
  272. GameGlobal.skills = data.userSkills; //拥有技能
  273. GameGlobal.userLoginReward = data.userLoginReward; //7天登录奖励数据
  274. /// 通知信息
  275. GameGlobal.messageNotice = data.messageNotice;
  276. //是否已经领取过我的小程序奖励
  277. GameGlobal.appletAward = data.appletAward == 0 ? false : true;
  278. //当天是否已经签到
  279. GameGlobal.isSignAward = data.isSignAward == 0 ? false : true;
  280. GameGlobal.localData = data.localData == 0 ? false : true;
  281. //好友助力剩余使用时间
  282. GameGlobal.friendRewardCdTime = data.friendRewardCdTime;
  283. // Global.friendRewardCdTime = 10000;
  284. //签到的次数
  285. GameGlobal.signCount = data.signCount;
  286. GameGlobal.rsDiamond = data.rsDiamond;
  287. /// 通知获得的钻石数量
  288. GameGlobal.noticeDiamond = data.noticeDiamond;
  289. //// 用户拥有的商品
  290. GameGlobal.shops = data.shops;
  291. GameGlobal.offlineGold = data.offlineGold; //离线收益
  292. GameGlobal._shopZk = data.shopZk; /// 商品折扣
  293. // Global.offlineGold = {n: 461, e:2}; //测试使用假的离线收益
  294. /// 收到socket消息的数量
  295. //判断当前版本是否正在审核中
  296. if (data.ver === GameGlobal.ver) {
  297. GameGlobal.isCheck = true;
  298. }
  299. // 获取存在服务端的用户guideState
  300. Api.httpGet({
  301. url: "/direct/me.do",
  302. success: res => {
  303. if (res) {
  304. window.guideState = JSON.parse(res);
  305. }
  306. this.getUserBuildings()
  307. .then((responseData) => {
  308. this.restartButton.node.active = false;
  309. GameGlobal.BuildingManager.networkRooms = responseData.userRooms;
  310. cc.director.loadScene("game");
  311. }).catch((err) => {
  312. this._getDataFail();
  313. });
  314. },
  315. fail: (res) => {
  316. this._getDataFail();
  317. }
  318. })
  319. }).catch(({ code, msg }) => {
  320. if (msg) {
  321. this._getDataFail();
  322. }
  323. });
  324. },
  325. _getDataFail() {
  326. this.loginLabel.node.active = false;
  327. this.updateUINode.active = false;
  328. GameGlobal.commonAlert.showCommonErrorAlert('登录失败请重试');
  329. if (window.tt != undefined) {
  330. if (this.restartButton) {
  331. this.restartButton.node.active = true;
  332. this.restartButton.interactable = true;
  333. }
  334. } else if (CC_WECHATGAME) {
  335. //检查登录态
  336. weChat.checkLogin(flag => {
  337. if (flag) {
  338. //登录态未失效,静默登录
  339. if (this.restartButton) {
  340. this.restartButton.node.active = true;
  341. this.restartButton.interactable = true;
  342. }
  343. } else {
  344. //登录态失效,重新调起微信登录获取新的token
  345. weChat.login((isError) => {
  346. if (isError) {
  347. this._getDataFail();
  348. } else {
  349. this._getUserInfoAndStartGame();
  350. }
  351. });
  352. }
  353. })
  354. } else {
  355. console.log('get data fail');
  356. if (this.restartButton) {
  357. this.restartButton.node.active = true;
  358. this.restartButton.interactable = true;
  359. }
  360. }
  361. },
  362. restartGame() {
  363. this.restartButton.interactable = false;
  364. this._getUserInfoAndStartGame();
  365. },
  366. getUserBuildings() {
  367. return new Promise((resolve, reject) => {
  368. // 获取目标用户的建筑
  369. HomeApi.getUserBuildings((responseData) => {
  370. resolve(responseData);
  371. }, (error) => {
  372. reject(error);
  373. });
  374. })
  375. },
  376. /// 更新全局channel
  377. _updateChannel() {
  378. if (window.tt != undefined) {
  379. GameGlobal.channel = 'toutiao';
  380. }else if (CC_WECHATGAME) {
  381. GameGlobal.channel = 'weixin';
  382. } else if (CC_QQPLAY) {
  383. GameGlobal.channel = 'qq';
  384. } else {
  385. GameGlobal.channel = 'LuciferChannel';
  386. }
  387. },
  388. _getUserInfo() {
  389. if (CC_WECHATGAME && window.tt == undefined) {
  390. this._getWXCode().then( (code) => {
  391. wx.getUserInfo({
  392. withCredentials: true,
  393. success: (res) => {
  394. LoginApi.updateUnionId(code, res.encryptedData, res.iv, (responseData) => {
  395. }, (error) => {
  396. });
  397. }
  398. })
  399. }).catch((err) => {
  400. });
  401. }
  402. },
  403. _getWXCode() {
  404. return new Promise((resolve, reject) => {
  405. wx.login({
  406. success(res) {
  407. if (res.code) {
  408. if (res.code) {
  409. resolve(res.code);
  410. } else {
  411. reject('error');
  412. }
  413. } else {
  414. reject('error');
  415. }
  416. }
  417. })
  418. });
  419. }
  420. });