123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- const weChat = require('../net/WeChat');
- const QQPlayer = require('../net/QQPlayer');
- const LoginApi = require('../net/LoginApi');
- const HomeApi = require("../net/HomeApi");
- const Api = require('../net/Api');
- const DWTool = require('../utils/DWTool');
- const GameModule = require('../utils/GameModule');
- const TapTapTool = require("../utils/TapTapTool");
- var Promise = require('../lib/es6-promise').Promise;
- cc.Class({
- extends: cc.Component,
- properties: {
- preloadProgress: cc.ProgressBar,
- preloadLabel: cc.Label,
- loginLabel: cc.Label,
- updateUINode: cc.Node,
- restartButton: cc.Button,
- _updating: false, //是否正在更新
- _canRetry: false //能否重新获取更新
- // _preloaded: false, //是否已经预加载完资源
- },
- onLoad() {
- if (window.tt != undefined) {
- } else if (CC_WECHATGAME) {
- window.wx.postMessage({ //初始化的时候关闭子域刷新
- messageType: 8,
- });
- }
- this._updateChannel();
- GameGlobal._socketCount = 0;
- this.loadAllGameRes();
- },
- loadAllGameRes() {
- this.updateUINode.active = true;
- this.preloadProgress.progress = 0;
- this.loadScene();
- // 加载图片资源完成, 开始加载首页场景
- cc.director.preloadScene("game", () => {
- this.preloadProgress.progress = 1;
- this.preloadLabel.string = '100%';
- setTimeout(() => {
- this._downloadGameResources();
- }, 200);
- });
- },
- //下载游戏所需资源
- _downloadGameResources() {
- this._downloadBuildingRes().then((isOK) => {
- this._downloadPrefabRes().then((isOK) => {
- this._downloadStarAvatarRes().then((isOK) => {
- this._downloadStarSpine().then((isOK) => {
- this._downloadFinished();
- });
- });
- });
- });
- },
- _downloadBuildingRes() {
- this.preloadProgress.progress = 0;
- this.loginLabel.string = '正在拼命加载游戏资源...';
- return new Promise((resolve, reject) => {
- // 加载楼层图片资源
- cc.loader.loadResDir("./textures/building", cc.Texture2D, (completeCount, totalCount, res) => {
- // 正在加载图片资源中...
- let currentProgress = completeCount / totalCount * 0.25;
- if(currentProgress && !isNaN(currentProgress)) {
- this.preloadProgress.progress = currentProgress;
- this.preloadLabel.string = (this.preloadProgress.progress * 100).toFixed(0) + '%';
- } else {
- this.preloadLabel.string = 0 + '%';
- }
- }, (err, res) => {
- if (err) {
- // reject(err);
- } else {
- if (GameGlobal) {
- GameGlobal.buildRes = res;
- }
- }
- resolve(true);
- });
- });
- },
- _downloadPrefabRes() {
- this.preloadProgress.progress = 0.25;
- this.loginLabel.string = '正在拼命加载游戏资源...';
- return new Promise((resolve, reject) => {
- // 加载prefab资源
- cc.loader.loadResDir("./prefabs/precedence", cc.Prefab, (completeCount, totalCount, res) => {
- // 正在加载图片资源中...
- let currentProgress = completeCount / totalCount * 0.25;
- if(currentProgress && !isNaN(currentProgress)) {
- this.preloadProgress.progress = currentProgress + 0.25;
- this.preloadLabel.string = (this.preloadProgress.progress * 100).toFixed(0) + '%';
- } else {
- this.preloadLabel.string = 25 + '%';
- }
- }, (err, res) => {
- if (err) {
- // reject(err);
- } else {
- if (GameGlobal) {
- GameGlobal.prefabsRes = res;
- }
- // resolve(true);
- }
- resolve(true);
- });
- });
- },
- _downloadStarAvatarRes() {
- this.preloadProgress.progress = 0.5;
- this.loginLabel.string = '正在拼命加载游戏资源...';
- return new Promise((resolve, reject) => {
- // 加载明星头像图片资源
- cc.loader.loadResDir("./textures/star_handbook", cc.Texture2D, (completeCount, totalCount, res) => {
- // 正在加载图片资源中...
- let currentProgress = completeCount / totalCount * 0.25;
- if(currentProgress && !isNaN(currentProgress)) {
- this.preloadProgress.progress = currentProgress + 0.5;
- this.preloadLabel.string = (this.preloadProgress.progress * 100).toFixed(0) + '%';
- } else {
- this.preloadLabel.string = 50 + '%';
- }
- }, (err, res) => {
- if (err) {
- // reject(err);
- } else {
- if (GameGlobal) {
- GameGlobal.starAvatarRes = res;
- }
- // resolve(true);
- }
- resolve(true);
- });
- });
- },
- _downloadStarSpine() {
- this.preloadProgress.progress = 0.75;
- this.loginLabel.string = '正在拼命加载游戏资源...';
- return new Promise((resolve, reject) => {
- // 加载prefab资源
- cc.loader.loadResDir("./spines/precedence", sp.SkeletonData, (completeCount, totalCount, res) => {
- // 正在加载图片资源中...
- let currentProgress = completeCount / totalCount * 0.25;
- if(currentProgress && !isNaN(currentProgress)) {
- this.preloadProgress.progress = currentProgress + 0.75;
- this.preloadLabel.string = (this.preloadProgress.progress * 100).toFixed(0) + '%';
- } else {
- this.preloadLabel.string = 75 + '%';
- }
- }, (err, res) => {
- if (err) {
- // reject(err);
- } else {
- if (GameGlobal) {
- GameGlobal.starSpineRes = res;
- }
- // resolve(true);
- }
- resolve(true);
- });
- });
- },
- // 加载完成, 开始进入游戏
- _downloadFinished() {
- this.endPreload();
- this.startLogin();
- },
- //// 开始登录
- startLogin() {
- if (window.tt != undefined) {
- this._startWxLogin();
- }else if(CC_WECHATGAME) {
- this._startWxLogin();
- } else if (CC_QQPLAY) {
- QQPlayer.qqLogin((isError) => {
- if (isError) {
- this._getDataFail();
- } else {
- this._getUserInfoAndStartGame();
- }
- });
- } else {
- GameGlobal.user = {
- uid: DWTool.getUrlParam('uid'),
- token: DWTool.getUrlParam('token'),
- nick: '游客',
- avatar: "",
- gender: 1
- }
- this._getUserInfoAndStartGame();
- }
- },
- //假的等待加载
- loadScene() {
- let dis = 0.012;
- this.loginLabel.string = '正在进入点点大明星...';
- this.timeCtrl = () => {
- if (parseInt(this.preloadProgress.progress) == 1) {
- this.unschedule(this.timeCtrl);
- } else {
- dis = dis < 0.002 ? 0.002 : dis - 0.0001;
- this.preloadProgress.progress += dis;
- if(this.preloadProgress.progress && !isNaN(this.preloadProgress.progress)) {
- this.preloadLabel.string = (this.preloadProgress.progress * 100).toFixed(0) + '%';
- } else {
- this.preloadLabel.string = 0 + '%';
- }
- }
- }
- this.schedule(this.timeCtrl, 0.02);
- },
- /**
- * 预加载结束
- */
- endPreload() {
- this.preloadProgress.progress = 1;
- },
-
- _startWxLogin() {
- this.loginLabel.node.active = false;
- this.updateUINode.active = false;
- if (GameGlobal.needLogin) {
- //服务端通知登录态失效,重新调起微信登录获取新的token
- weChat.login((isError) => {
- if (isError) {
- this._getDataFail();
- } else {
- this._getUserInfoAndStartGame();
- }
- });
- } else {
- //检查登录态
- weChat.checkLogin(flag => {
- if (flag) {
- //登录态未失效,静默登录
- weChat.loginStatic(() => {
- this._getUserInfoAndStartGame();
- });
- } else {
- //登录态失效,重新调起微信登录获取新的token
- weChat.login((isError) => {
- if (isError) {
- this._getDataFail();
- } else {
- this._getUserInfoAndStartGame();
- }
- });
- }
- })
- }
- },
- _getUserInfoAndStartGame() {
- if (GameGlobal.user == undefined || GameGlobal.user.uid == undefined) {
- if (CC_QQPLAY) {
- this.startLogin();
- } else if (CC_WECHATGAME || window.tt != undefined) {
- weChat.login((isError) => {
- if (isError) {
- this._getDataFail();
- } else {
- this._getUserInfoAndStartGame();
- }
- });
- }
- return;
- }
- LoginApi.getUserInfoPromise().then(({ data, msg }) => {
- GameGlobal.needLogin = false;
- GameGlobal.userData = data.userExtra;
- GameGlobal.diamond = data.userExtra.diamond;
- GameGlobal.skills = data.userSkills; //拥有技能
- GameGlobal.userLoginReward = data.userLoginReward; //7天登录奖励数据
- /// 通知信息
- GameGlobal.messageNotice = data.messageNotice;
- //是否已经领取过我的小程序奖励
- GameGlobal.appletAward = data.appletAward == 0 ? false : true;
- //当天是否已经签到
- GameGlobal.isSignAward = data.isSignAward == 0 ? false : true;
- GameGlobal.localData = data.localData == 0 ? false : true;
- //好友助力剩余使用时间
- GameGlobal.friendRewardCdTime = data.friendRewardCdTime;
- // Global.friendRewardCdTime = 10000;
- //签到的次数
- GameGlobal.signCount = data.signCount;
- GameGlobal.rsDiamond = data.rsDiamond;
- /// 通知获得的钻石数量
- GameGlobal.noticeDiamond = data.noticeDiamond;
- //// 用户拥有的商品
- GameGlobal.shops = data.shops;
-
- GameGlobal.offlineGold = data.offlineGold; //离线收益
- GameGlobal._shopZk = data.shopZk; /// 商品折扣
- // Global.offlineGold = {n: 461, e:2}; //测试使用假的离线收益
- /// 收到socket消息的数量
- //判断当前版本是否正在审核中
- if (data.ver === GameGlobal.ver) {
- GameGlobal.isCheck = true;
- }
- // 获取存在服务端的用户guideState
- Api.httpGet({
- url: "/direct/me.do",
- success: res => {
- if (res) {
- window.guideState = JSON.parse(res);
- }
- this.getUserBuildings()
- .then((responseData) => {
- this.restartButton.node.active = false;
- GameGlobal.BuildingManager.networkRooms = responseData.userRooms;
- cc.director.loadScene("game");
- }).catch((err) => {
- this._getDataFail();
- });
- },
- fail: (res) => {
- this._getDataFail();
- }
- })
- }).catch(({ code, msg }) => {
- if (msg) {
- this._getDataFail();
- }
- });
- },
- _getDataFail() {
- this.loginLabel.node.active = false;
- this.updateUINode.active = false;
- GameGlobal.commonAlert.showCommonErrorAlert('登录失败请重试');
- if (window.tt != undefined) {
- if (this.restartButton) {
- this.restartButton.node.active = true;
- this.restartButton.interactable = true;
- }
- } else if (CC_WECHATGAME) {
- //检查登录态
- weChat.checkLogin(flag => {
- if (flag) {
- //登录态未失效,静默登录
- if (this.restartButton) {
- this.restartButton.node.active = true;
- this.restartButton.interactable = true;
- }
- } else {
- //登录态失效,重新调起微信登录获取新的token
- weChat.login((isError) => {
- if (isError) {
- this._getDataFail();
- } else {
- this._getUserInfoAndStartGame();
- }
- });
- }
- })
- } else {
- console.log('get data fail');
- if (this.restartButton) {
- this.restartButton.node.active = true;
- this.restartButton.interactable = true;
- }
- }
- },
- restartGame() {
- this.restartButton.interactable = false;
- this._getUserInfoAndStartGame();
- },
- getUserBuildings() {
- return new Promise((resolve, reject) => {
- // 获取目标用户的建筑
- HomeApi.getUserBuildings((responseData) => {
- resolve(responseData);
- }, (error) => {
- reject(error);
- });
- })
- },
- /// 更新全局channel
- _updateChannel() {
- if (window.tt != undefined) {
- GameGlobal.channel = 'toutiao';
- }else if (CC_WECHATGAME) {
- GameGlobal.channel = 'weixin';
- } else if (CC_QQPLAY) {
- GameGlobal.channel = 'qq';
- } else {
- GameGlobal.channel = 'LuciferChannel';
- }
- },
- _getUserInfo() {
- if (CC_WECHATGAME && window.tt == undefined) {
- this._getWXCode().then( (code) => {
- wx.getUserInfo({
- withCredentials: true,
- success: (res) => {
- LoginApi.updateUnionId(code, res.encryptedData, res.iv, (responseData) => {
- }, (error) => {
- });
- }
- })
- }).catch((err) => {
- });
- }
- },
- _getWXCode() {
- return new Promise((resolve, reject) => {
- wx.login({
- success(res) {
- if (res.code) {
- if (res.code) {
- resolve(res.code);
- } else {
- reject('error');
- }
- } else {
- reject('error');
- }
- }
- })
- });
- }
- });
|