316 lines
11 KiB
JavaScript
316 lines
11 KiB
JavaScript
var onfire = require('./utils/onfire');
|
|
let webapi = require('./utils/webapi');
|
|
let stringUtil = require('./utils/string.util');
|
|
|
|
cc.Class({
|
|
extends: cc.Component,
|
|
|
|
properties: {
|
|
scrollContent: {
|
|
default: null,
|
|
type: cc.Node
|
|
},
|
|
scrollView: {
|
|
default: null,
|
|
type: cc.ScrollView
|
|
},
|
|
mainScrollPrefab: {
|
|
default: null,
|
|
type: cc.Prefab
|
|
},
|
|
topMenuBtn: {
|
|
default: null,
|
|
type: cc.Button
|
|
},
|
|
mainScrollContent: {
|
|
default: null,
|
|
type: cc.Node
|
|
},
|
|
topMenuPrefab: {
|
|
default: null,
|
|
type: cc.Prefab
|
|
},
|
|
infoMenuPrefab: {
|
|
default: null,
|
|
type: cc.Prefab
|
|
},
|
|
inviteMenuPrefab: {
|
|
default: null,
|
|
type: cc.Prefab
|
|
},
|
|
infoMenuPrefab2: {
|
|
default: null,
|
|
type: cc.Prefab
|
|
},
|
|
rulePrefab: {
|
|
default: null,
|
|
type: cc.Prefab
|
|
},
|
|
resultPrefab: {
|
|
default: null,
|
|
type: cc.Prefab
|
|
},
|
|
menuContentPrefab: {
|
|
default: null,
|
|
type: cc.Prefab
|
|
},
|
|
topMenu: {
|
|
default: null,
|
|
type: cc.Node
|
|
},
|
|
infoView: {
|
|
default: null,
|
|
type: cc.Node
|
|
},
|
|
partsY: [],
|
|
needCode: true,
|
|
logined: false,
|
|
mobile: '',
|
|
images: [],
|
|
uid: '',
|
|
allshow: false,
|
|
infoSchedule: null,
|
|
shareUser: ''
|
|
},
|
|
|
|
// LIFE-CYCLE CALLBACKS:
|
|
|
|
onLoad () {
|
|
this.shareUser = stringUtil.getRequest('uid');
|
|
let self = this;
|
|
webapi.preLogin({})
|
|
.then(rep => {
|
|
if (rep.errcode === 0) {
|
|
self.needCode = rep.smsCheck;
|
|
self.logined = rep.isLogin;
|
|
cc.sys.localStorage.setItem('activity_token', rep.token);
|
|
if (self.logined) {
|
|
return webapi.userInfo()
|
|
}
|
|
}
|
|
return null;
|
|
})
|
|
.then(userRep => {
|
|
if (userRep) {
|
|
console.log(userRep);
|
|
if (userRep.errcode === 0) {
|
|
self.mobile = userRep.user.username;
|
|
self.images = userRep.user.images;
|
|
self.checkAllShow();
|
|
self.uid = userRep.user.uid;
|
|
self.mainScrollContent.getComponent('scrollContent').updateUserName(stringUtil.parseMobile(self.mobile));
|
|
self.mainScrollContent.getComponent('scrollContent').updatePuzzle(self.images);
|
|
self.mainScrollContent.getComponent('scrollContent').toggleLoginStatus(true);
|
|
}
|
|
|
|
}
|
|
})
|
|
.catch (err => {
|
|
console.log(err);
|
|
});
|
|
this.partsY = [0, 1560, 2920, 3666, 4686];
|
|
this.mainScrollContent = cc.instantiate(this.mainScrollPrefab);
|
|
this.scrollContent.addChild(this.mainScrollContent);
|
|
this.mainScrollContent.getComponent('scrollContent').top = this;
|
|
this.mainScrollContent.getComponent('scrollContent').joinBtn.on('click', function () {
|
|
self.showInfoMenu();
|
|
});
|
|
this.mainScrollContent.getComponent('scrollContent').inviteBtn.on('click', function () {
|
|
self.showInviteMenu();
|
|
});
|
|
this.mainScrollContent.getComponent('scrollContent').ruleBtn.on('click', function () {
|
|
self.showRuleMenu();
|
|
});
|
|
let topHeader = cc.instantiate(this.topMenuPrefab);
|
|
this.node.addChild(topHeader, 10);
|
|
topHeader.getComponent('topMenu').menuBtn.on('click', function () {
|
|
self.showTopMenu();
|
|
});
|
|
this.scrollView.node.on('scroll-ended', function (e) {
|
|
// let offsetY = self.scrollView.getScrollOffset().y;
|
|
// console.log('getScrollOffset: ' + offsetY);
|
|
});
|
|
this.longPressListener = onfire.on('longPressed', this.longPressed.bind(this));
|
|
if (this.logined && !this.allshow) {
|
|
this.scheduleUpdateInfo();
|
|
}
|
|
|
|
},
|
|
|
|
start () {
|
|
},
|
|
onDestroy: function () {
|
|
onfire.un(this.topMenuListener);
|
|
onfire.un(this.longPressListener);
|
|
},
|
|
// update (dt) {},
|
|
showTopMenu() {
|
|
let offsetY = this.scrollView.getScrollOffset().y;
|
|
let menuNo = 0;
|
|
for(let i = 0; i < this.partsY.length; i++) {
|
|
if (offsetY >= this.partsY[i]-20) {
|
|
menuNo= i;
|
|
}
|
|
}
|
|
|
|
if (!this.topMenu) {
|
|
this.topMenu = cc.instantiate(this.menuContentPrefab);
|
|
this.node.addChild(this.topMenu, 9);
|
|
this.topMenuListener = onfire.on("topMenuClick", this.onTopMenuClick.bind(this));
|
|
} else {
|
|
this.topMenu.active = !this.topMenu.active ;
|
|
}
|
|
this.topMenu.getComponent('topMenuContent').updateMenuShow(menuNo);
|
|
},
|
|
|
|
onTopMenuClick: function (idx) {
|
|
console.log(idx);
|
|
let y = this.partsY[idx];
|
|
this.topMenu.getComponent('topMenuContent').updateMenuShow(idx);
|
|
this.scrollView.scrollToOffset(cc.v2(0, y), 0.1);
|
|
this.topMenu.active = false;
|
|
},
|
|
showInfoMenu() {
|
|
if (this.needCode) {
|
|
this.infoView = cc.instantiate(this.infoMenuPrefab2);
|
|
} else {
|
|
this.infoView = cc.instantiate(this.infoMenuPrefab);
|
|
}
|
|
this.infoView.getComponent('infoMenu').top = this;
|
|
this.node.addChild(this.infoView, 11);
|
|
},
|
|
changeInfoMenu(mobile) {
|
|
this.needCode = true;
|
|
this.infoView.removeFromParent(true);
|
|
this.showInfoMenu();
|
|
this.infoView.getComponent('infoMenu').setMobileVal(mobile);
|
|
},
|
|
showResultView() {
|
|
this.infoView.getComponent('infoMenu').hide();
|
|
let resultView = cc.instantiate(this.resultPrefab);
|
|
this.node.addChild(resultView, 11);
|
|
},
|
|
showInviteMenu() {
|
|
if (this.logined) {
|
|
let inviteMenu = cc.instantiate(this.inviteMenuPrefab);
|
|
inviteMenu.getComponent('inviteMenu').top = this;
|
|
this.node.addChild(inviteMenu, 11);
|
|
} else {
|
|
alert('请先预约');
|
|
let y = this.partsY[0];
|
|
this.scrollView.scrollToOffset(cc.v2(0, y), 0.1);
|
|
if (this.topMenu) {
|
|
this.topMenu.getComponent('topMenuContent').updateMenuShow(0);
|
|
}
|
|
}
|
|
|
|
},
|
|
showRuleMenu() {
|
|
let ruleMenu = cc.instantiate(this.rulePrefab);
|
|
this.node.addChild(ruleMenu, 11);
|
|
},
|
|
longPressed(data) {
|
|
console.log(data);
|
|
switch (data.target) {
|
|
case 'qr_wechat':
|
|
window.location.href = 'https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzU0MTk4MzkyNQ==&scene=124#wechat_redirect';
|
|
break;
|
|
case 'qr_qq':
|
|
window.location.href = 'http://qm.qq.com/cgi-bin/qm/qr?k=9OEVDuOyP1BZ_cXOApidE9P2ea8daCVF';
|
|
break;
|
|
}
|
|
},
|
|
userLogin(mobile, code) {
|
|
let self = this;
|
|
webapi.login(mobile, code, self.shareUser)
|
|
.then(rep => {
|
|
if (rep.errcode === 0 || rep.errcode === 102 || rep.errcode === 103) {
|
|
self.logined = true;
|
|
self.mobile = rep.user.username;
|
|
self.images = rep.user.images;
|
|
self.checkAllShow();
|
|
self.uid = rep.user.uid;
|
|
self.mainScrollContent.getComponent('scrollContent').updateUserName(stringUtil.parseMobile(self.mobile));
|
|
self.mainScrollContent.getComponent('scrollContent').updatePuzzle(self.images);
|
|
self.mainScrollContent.getComponent('scrollContent').toggleLoginStatus(true);
|
|
self.infoView.getComponent('infoMenu').hide();
|
|
if (rep.errcode === 103 || rep.errcode === 102) {
|
|
alert('您已预约');
|
|
}
|
|
if (rep.errcode === 0) {
|
|
self.showResultView();
|
|
}
|
|
if (this.logined && !this.allshow) {
|
|
this.scheduleUpdateInfo();
|
|
}
|
|
} else if (rep.errcode === 104) {
|
|
alert('验证码错误, 请重新输入或重新获取');
|
|
if (!self.needCode) {
|
|
self.changeInfoMenu(mobile);
|
|
}
|
|
}
|
|
})
|
|
.catch (err => {
|
|
console.log(err);
|
|
});
|
|
},
|
|
userLogout() {
|
|
let self = this;
|
|
if (confirm('确定退出?')) {
|
|
webapi.logout()
|
|
.then(rep => {
|
|
if (rep.errcode === 0) {
|
|
self.logined = false;
|
|
self.mainScrollContent.getComponent('scrollContent').toggleLoginStatus(false);
|
|
self.mainScrollContent.getComponent('scrollContent').updatePuzzle([0,0,0,0,0,0,0,0,0]);
|
|
}
|
|
})
|
|
.catch(err => {
|
|
console.log(err);
|
|
})
|
|
}
|
|
},
|
|
checkAllShow() {
|
|
let result = 0;
|
|
for(let i of this.images) {
|
|
result += i;
|
|
}
|
|
this.allshow = result === 9;
|
|
if (this.allshow) {
|
|
this.mainScrollContent.getComponent('scrollContent').updateTipTxt('隐藏角色已开启');
|
|
}
|
|
},
|
|
scheduleUpdateInfo() {
|
|
let self = this;
|
|
if (this.infoSchedule) {
|
|
this.unschedule(this.infoSchedule);
|
|
}
|
|
webapi.updateInviteInfo()
|
|
.then(rep => {
|
|
if (rep.errcode === 0) {
|
|
let text = `${stringUtil.parseMobile(rep.mobile)}接受了你的邀请`;
|
|
self.mainScrollContent.getComponent('scrollContent').updateTipTxt(text);
|
|
} else if (rep.errcode === 101) {
|
|
self.mainScrollContent.getComponent('scrollContent').hideTipTxt();
|
|
}
|
|
})
|
|
.catch(err => {
|
|
|
|
})
|
|
this.infoSchedule = this.schedule(function () {
|
|
webapi.updateInviteInfo()
|
|
.then(rep => {
|
|
if (rep.errcode === 0) {
|
|
let text = `${stringUtil.parseMobile(rep.mobile)}接受了你的邀请`;
|
|
self.mainScrollContent.getComponent('scrollContent').updateTipTxt(text);
|
|
} else if (rep.errcode === 101) {
|
|
self.mainScrollContent.getComponent('scrollContent').hideTipTxt();
|
|
}
|
|
})
|
|
.catch(err => {
|
|
|
|
})
|
|
}, 60); //每5分钟更新一次
|
|
}
|
|
});
|