123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- const weChat = require('./net/WeChat');
- const LoginApi = require('./net/LoginApi');
- const Api = require('./net/Api');
- const DWTool = require('./utils/DWTool');
- cc.Class({
- extends: cc.Component,
- properties: {
- preloadProgress: cc.ProgressBar,
- preloadLabel: cc.Label,
- loginLabel: cc.Label,
- updateUI: cc.Node,
- _updating: false, //是否正在更新
- _canRetry: false, //能否重新获取更新
- _preloaded: false, //是否已经预加载完资源
- },
- onLoad() {
- if (CC_WECHATGAME) {
- window.wx.postMessage({ //初始化的时候关闭子域刷新
- messageType: 8,
- });
- }
- this.loadAllGameRes();
- },
- loadAllGameRes() {
- this.updateUI.active = true;
- this.preloadRes = false;
- this.preloadProgress.progress = 0;
- this.loadScene();
- // 加载图片资源完成, 开始加载首页场景
- cc.director.preloadScene("game", () => {
- cc.log("Next scene preloaded");
- this.preloadProgress.progress = 0;
- this.loginLabel.string = '正在拼命加载游戏资源...'
- // 加载需要的图片资源
- cc.loader.loadResDir("./building", cc.Texture2D, (completeCount, totalCount, res) => {
- this.preloadRes = true
- // 正在加载图片资源中...
- this.preloadProgress.progress = completeCount / totalCount;
- if(this.preloadProgress.progress) {
- this.preloadLabel.string = (this.preloadProgress.progress * 100).toFixed(0) + '%';
- }
- }, (err, res) => {
- Global.buildRes = res;
- // 加载完成, 开始进入游戏
- this.endPreload();
- if(CC_WECHATGAME) {
- this._startWxLogin();
- } else {
- Global.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._preloaded = true;
- this.unschedule(this.timeCtrl);
- } else {
- dis = dis < 0.002 ? 0.002 : dis - 0.0001;
- this.preloadProgress.progress += dis;
- }
- if(!this.preloadRes) {
- this.preloadLabel.string = (this.preloadProgress.progress * 100).toFixed(0) + '%'
- }
- }
- this.schedule(this.timeCtrl, 0.02);
- },
- /**
- * 预加载结束
- */
- endPreload() {
- this.preloadProgress.progress = 1;
- },
-
- _startWxLogin() {
- this.loginLabel.node.active = false
- this.updateUI.active = false;
- if (Global.needLogin) {
- //服务端通知登录态失效,重新调起微信登录获取新的token
- weChat.login(() => {
- this._getUserInfoAndStartGame();
- });
- } else {
- //检查登录态
- weChat.checkLogin(flag => {
- if (flag) {
- //登录态未失效,静默登录
- weChat.loginStatic(() => {
- this._getUserInfoAndStartGame();
- })
- } else {
- //登录态失效,重新调起微信登录获取新的token
- weChat.login(() => {
- this._getUserInfoAndStartGame();
- });
- }
- })
- }
- },
- _getUserInfoAndStartGame() {
- LoginApi.getUserInfoPromise().then(({ data, msg }) => {
- Global.needLogin = false;
- Global.userData = data
- Global.devCityId = data.cityId;
-
- // 获取存在服务端的用户guideState
- Api.httpGet({
- url: "/direct/me.do",
- data: {},
- success: res => {
- if(res) {
- window.guideState = JSON.parse(res)
- }
- cc.director.loadScene("game");
- }
- })
- }).catch(({ code, msg }) => {
- if (msg) {
- console.log(msg);
- }
- });
- },
- });
|