完善登录模块

This commit is contained in:
zhl 2019-02-27 18:39:51 +08:00
parent 20886ecf78
commit 8486c6f5f7
7 changed files with 172 additions and 34 deletions

View File

@ -1955,12 +1955,12 @@
"_N$string": "",
"_fontSize": 30,
"_lineHeight": 40,
"_enableWrapText": false,
"_enableWrapText": true,
"_N$file": null,
"_isSystemFontUsed": true,
"_spacingX": 0,
"_N$horizontalAlign": 0,
"_N$verticalAlign": 1,
"_N$verticalAlign": 0,
"_N$fontFamily": "Arial",
"_N$overflow": 1,
"_id": ""
@ -2054,12 +2054,12 @@
"_N$string": "",
"_fontSize": 20,
"_lineHeight": 50,
"_enableWrapText": false,
"_enableWrapText": true,
"_N$file": null,
"_isSystemFontUsed": true,
"_spacingX": 0,
"_N$horizontalAlign": 0,
"_N$verticalAlign": 1,
"_N$verticalAlign": 0,
"_N$fontFamily": "Arial",
"_N$overflow": 1,
"_id": ""
@ -2095,7 +2095,7 @@
},
"_N$returnType": 0,
"_N$inputFlag": 5,
"_N$inputMode": 2,
"_N$inputMode": 6,
"_N$fontSize": 30,
"_N$lineHeight": 40,
"_N$fontColor": {
@ -3192,7 +3192,6 @@
"codeImg": {
"__id__": 62
},
"needCode": false,
"_id": ""
},
{

View File

@ -52,7 +52,7 @@ cc.Class({
self.node.removeFromParent(true);
});
this.submitBtn.on('click', function () {
let mobile = self.mobileInput.string;
self.sendJoinInfo();
});
this.captchaBtn.on('click', function () {
console.log(self.mobileInput.string);
@ -61,7 +61,15 @@ cc.Class({
alert('请输入有效的手机号码。');
return false;
}
webapi.sendSms(mobile)
let code = '';
if(self.top.needCode) {
code = self.codeInput.string;
if (!stringUtil.checkCaptchaCode(code)) {
alert('请输入正确的图形验证码。');
return false;
}
}
webapi.sendSms(mobile, code)
.then(res => {
console.log(res);
})
@ -77,13 +85,12 @@ cc.Class({
showResultView() {
let result = cc.instantiate(this.resultPrefab);
this.top.node.addChild(result, 11);
this.node.removeFromParent(true);
},
showCaptchaInput() {
let self = this;
cc.loader.load({
url: webapi.captchaUrl()+'?'+new Date(),
url: webapi.captchaUrl()+'?token='+cc.sys.localStorage.getItem('activity_token')+'&data='+new Date(),
type: 'jpg'
}, function (err, texture) {
if (err) {
@ -92,5 +99,23 @@ cc.Class({
}
self.codeImg.spriteFrame = new cc.SpriteFrame(texture);
});
},
hide() {
this.node.removeFromParent(true);
},
sendJoinInfo() {
let self = this;
let mobile = self.mobileInput.string;
if (!stringUtil.checkMobile(mobile)) {
alert('请输入有效的手机号码。');
return false;
}
let moileCaptcha = self.captchaInput.string;
if (!stringUtil.checkAuthCode(moileCaptcha)) {
alert('请输入正确的验证码。');
return false;
}
this.top.userLogin(mobile, moileCaptcha);
}
});

View File

@ -1,5 +1,6 @@
var http = require('./utils/http');
var onfire = require('./utils/onfire');
let webapi = require('./utils/webapi');
let stringUtil = require('./utils/string.util');
cc.Class({
extends: cc.Component,
@ -56,24 +57,53 @@ cc.Class({
default: null,
type: cc.Node
},
infoView: {
default: null,
type: cc.Node
},
partsY: [],
needCode: true
needCode: true,
logined: false,
mobile: '',
images: []
},
// LIFE-CYCLE CALLBACKS:
onLoad () {
let self = this;
http.get('http://192.168.100.228/activity/api/index', {} )
.then(res => {
console.log(res);
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;
})
.catch(err => {
console.error(err);
.then(userRep => {
if (userRep) {
console.log(userRep);
if (userRep.errcode === 0) {
self.mobile = userRep.user.username;
self.images = userRep.user.images;
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();
});
@ -129,14 +159,18 @@ cc.Class({
this.topMenu.active = false;
},
showInfoMenu() {
let infoMenu;
if (this.needCode) {
infoMenu = cc.instantiate(this.infoMenuPrefab2);
this.infoView = cc.instantiate(this.infoMenuPrefab2);
} else {
infoMenu = cc.instantiate(this.infoMenuPrefab);
this.infoView = cc.instantiate(this.infoMenuPrefab);
}
infoMenu.getComponent('infoMenu').top = this;
this.node.addChild(infoMenu, 11);
this.infoView.getComponent('infoMenu').top = this;
this.node.addChild(this.infoView, 11);
},
showResultView() {
this.infoView.getComponent('infoMenu').hide();
let resultView = cc.instantiate(this.resultPrefab);
this.node.addChild(resultView, 11);
},
showInviteMenu() {
let inviteMenu = cc.instantiate(this.inviteMenuPrefab);
@ -156,5 +190,44 @@ cc.Class({
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)
.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.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();
}
}
})
.catch (err => {
console.log(err);
});
},
userLogout() {
let self = this;
if (confirm('确定退出?')) {
webapi.logout()
.then(rep => {
if (rep.errcode === 0) {
self.mainScrollContent.getComponent('scrollContent').toggleLoginStatus(false);
}
})
.catch(err => {
console.log(err);
})
}
}
});

View File

@ -77,13 +77,14 @@ cc.Class({
// LIFE-CYCLE CALLBACKS:
onLoad() {
let self = this;
this.loginStatusLabel.node.active = false;
var processBar = cc.instantiate(this.processBarPrefab);
// processBar.getComponent('progressBar').currentVal = 50000;
this.part1.addChild(processBar);
processBar.getComponent('progressBar').updateShow(50000);
var puzzle = cc.instantiate(this.puzzlePrefab);
this.part2.addChild(puzzle);
this.puzzle = cc.instantiate(this.puzzlePrefab);
this.part2.addChild(this.puzzle);
var tip = cc.instantiate(this.tipPrefab);
this.part2.addChild(tip);
tip.getComponent('inviteTip').setLabelTxt('xx接受了您的邀请');
@ -91,11 +92,14 @@ cc.Class({
this.part4.addChild(imageSwiper, 0);
var bottomSwiper = cc.instantiate(this.bottomSwiperPrefab);
this.part5.addChild(bottomSwiper);
let self = this;
this.scheduleOnce(function () {
processBar.getComponent('progressBar').updateShow(35000);
puzzle.getComponent('puzzle').updateValues([0, 1, 0, 1, 0, 1, 1, 0, 1])
}, 5);
this.logoutBtn.on('click', function () {
self.top.userLogout();
})
// this.scheduleOnce(function () {
// processBar.getComponent('progressBar').updateShow(35000);
// self.puzzle.getComponent('puzzle').updateValues([])
// }, 5);
},
start() {
@ -124,4 +128,14 @@ cc.Class({
this.time = 0;
this.startup = false;
},
updatePuzzle(valArr) {
this.puzzle.getComponent('puzzle').updateValues(valArr)
},
updateUserName(username) {
this.loginStatusLabel.string = username;
},
toggleLoginStatus(status) {
this.loginStatusLabel.node.active = status;
this.joinBtn.active = !status;
}
});

View File

@ -19,12 +19,16 @@ const post = (url, data, header) => {
const Ajax = (url, method, data, header) => {
header = header || {};
let token = cc.sys.localStorage.getItem('activity_token');
if (token) {
header['token'] = token;
}
return new Promise((resolve, reject) => {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 ){
if(xhr.status >= 200 && xhr.status < 400) {
resolve(xhr.responseText);
resolve(JSON.parse(xhr.responseText));
}else{
reject(xhr.statusText);
}

View File

@ -5,7 +5,10 @@ export default {
checkAuthCode(code) {
return /^\d{6}$/.test(code);
},
checkCaptcheCode(code) {
checkCaptchaCode(code) {
return /^[a-z,0-9]{4}$/.test(code);
},
parseMobile(mobile) {
return mobile.slice(0,3) + '****' + mobile.slice(7);
}
}

View File

@ -1,6 +1,6 @@
var http = require('./http');
const baseUrl = 'http://192.168.100.228';
const baseUrl = 'http://192.168.100.228:3000';
export default {
sendSms: (mobile, captcha) => {
let data = {mobile: mobile};
@ -12,6 +12,26 @@ export default {
return http.post(url, data);
},
captchaUrl: () => {
return baseUrl + '/captcha/ch/login'
return baseUrl + '/activity/ch/login'
},
preLogin: (data) => {
let url = baseUrl + '/activity/api/pre_login';
return http.post(url, data);
},
userInfo: () => {
let url = baseUrl + '/activity/api/user_info';
return http.get(url);
},
login: (mobile, code) => {
let data = {
mobile: mobile,
captcha: code
};
let url = baseUrl + '/activity/api/join';
return http.post(url, data);
},
logout: () => {
let url = baseUrl + '/activity/api/logout';
return http.post(url, {});
}
}