134 lines
3.8 KiB
JavaScript

let webapi = require('./utils/webapi');
let stringUtil = require('./utils/string.util');
cc.Class({
extends: cc.Component,
properties: {
closeBtn: {
default: null,
type: cc.Node
},
submitBtn: {
default: null,
type: cc.Node
},
captchaBtn: {
default: null,
type: cc.Node
},
resultPrefab: {
default: null,
type: cc.Prefab
},
mobileInput: {
default: null,
type: cc.EditBox
},
captchaInput: {
default: null,
type: cc.EditBox
},
codeInput: {
default: null,
type: cc.EditBox
},
codeImg: {
default: null,
type: cc.Sprite
}
},
// LIFE-CYCLE CALLBACKS:
onLoad () {
let self = this;
if (this.top.needCode) {
this.showCaptchaImage();
this.codeImg.node.on('click', function () {
self.showCaptchaImage();
})
}
this.closeBtn.on('click', function () {
self.node.removeFromParent(true);
});
this.submitBtn.on('click', function () {
self.sendJoinInfo();
});
this.captchaBtn.on('click', function () {
console.log(self.mobileInput.string);
let mobile = self.mobileInput.string;
if (!stringUtil.checkMobile(mobile)) {
alert('请输入有效的手机号码。');
return false;
}
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);
if (res.errcode === 103) {
alert('当前手机号超过当日发送限额。');
} else if (res.errcode === 102) {
alert('发送太过频繁,请稍候再试。');
} else if (res.errcode === 101) {
alert('图形验证码错误。');
} else {
alert('短信已发送至您手机, 请输入收到的验证码。');
}
})
.catch(err => {
console.log(err);
})
})
},
start () {
},
showResultView() {
let result = cc.instantiate(this.resultPrefab);
this.top.node.addChild(result, 11);
},
showCaptchaImage() {
let self = this;
cc.loader.load({
url: webapi.captchaUrl()+'?token='+cc.sys.localStorage.getItem('activity_token')+'&data='+new Date(),
type: 'jpg'
}, function (err, texture) {
if (err) {
console.log('load avatar error === ', err);
return;
}
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);
},
setMobileVal(mobile) {
this.mobileInput.string = mobile;
}
});