(function() {
Vue.component('login-form', {
template: `
下载APP
`,
props: [],
data: function() {
return {
type: 1, //记录选择的登录方式,1-手机号登录,2-验证码登录
account: '362321199703137824',
mobile: '17805985317',
password: '11111111',
codeType: 1,
captcha:'',
idCardNo: "",
rPassword: '',
confirmPassword: "",
toggle: true,
registerFlag: false,
maxLength: 18,
codeMsg:"获取验证码",
spaceTime:60,//验证码间隔时间
}
},
methods: {
changeType: function(val) {
this.type = val;
},
getCode: function(num){
var vm=this;
if(num<0){
vm.codeMsg="获取验证码";
}else{
setTimeout(function(){
num--;
vm.codeMsg="("+num+"s)重新获取";
vm.getCode(num);
},1000)
}
},
login: function() {
var vm = this
if(vm.type == 1) {
if(!vm.account) {
console.log("账号不能为空!");
return false;
}
if(!vm.password) {
console.log("登录密码不能为空!");
return false;
}
var data = {
client_id: httpRequest.client_id,
username: vm.account,
password: vm.password
}
loginAPI.accessToken(data).then(function(oauthInfo) {
sessionStorage.setItem("oauthInfo", JSON.stringify(oauthInfo));
var data1 = {
userName: oauthInfo.user,
accessToken: "token=" + oauthInfo.accessToken
}
loginAPI.getUserInfo(data1).then(function(res) {
console.log("登录成功");
loginedList = [res.telephone]
sessionStorage.setItem("loginedList", JSON.stringify(loginedList));
sessionStorage.setItem("userAgent", JSON.stringify(res));
vm.getJueSeData(res);
}),
function error(xhr, type, errorThrown) {
if(type == "timeout" || type == "abort" || type == "error") {
console.log("账号或密码错误");
}
}
}).catch(function(e) {
console.log("账号或密码错误")
})
} else {
if(!vm.mobile) {
console.log("手机号不能为空!");
return false;
}
var isMobile = isCardNo(vm.mobile,2);
if(!isMobile) {
toastr.error('请输入正确的手机号码!');
return false;
}
if(!vm.captcha) {
console.log("手机验证码不能为空!");
return false;
}
vm.verifyCodeExpire(2);
}
},
sendMsg:function(){
var vm=this;
var isMobile = isCardNo(vm.mobile,2);
if(!isMobile) {
toastr.error('请输入正确的手机号码!');
return false;
}
if(vm.codeMsg==="获取验证码"){
vm.codeMsg="正在获取验证码";
}
vm.verifyCodeExpire(1);
},
verifyCodeExpire:function(num){//num:1表示验证短信发送间隔问题,2表示时效性
var vm=this;
loginAPI.verifyCodeExpire({
client_id: httpRequest.client_id,
username: vm.mobile
}).then(function(res){
console.log(res)
if(num===1){
if(res.nextRequestTime<=0) vm.verifyCode();//获取验证码
else{
console.log(vm.codeMsg || (vm.spaceTime+"秒内只能获取一次验证码"));
vm.codeMsg=vm.spaceTime+"秒内只能获取一次验证码";
}
}
if(num===2){
if(res.expiresIn<=0){
toastr.error("验证码已失效,请重新获取验证码!");
}else{
var data = {
// grant_type: httpRequest.grant_type,
client_id: httpRequest.client_id,
username: vm.mobile,
verify_code: vm.captcha
}
loginAPI.accessToken(data).then(function(oauthInfo) {
sessionStorage.setItem("oauthInfo", JSON.stringify(oauthInfo));
var data1 = {
userName: oauthInfo.user,
accessToken: "token=" + oauthInfo.accessToken
}
loginAPI.getUserInfo(data1).then(function(res) {
console.log("登录成功");
loginedList = [res.telephone]
sessionStorage.setItem("loginedList", JSON.stringify(loginedList));
sessionStorage.setItem("userAgent", JSON.stringify(res));
vm.getJueSeData(res);
}),
function error(xhr, type, errorThrown) {
if(type == "timeout" || type == "abort" || type == "error") {
console.log("账号或密码错误");
}
}
}).catch(function(e) {
console.error&&console.error(e);
console.log("服务器繁忙,请稍后再试")
})
}
}
}).catch(function(err){
console.log(err)
toastr.error("获取短信验证码失败")
vm.codeMsg="获取验证码";
verifyCodeFlag=false;
})
},
verifyCode :function(){
var vm=this;
if(vm.codeMsg!=="获取验证码"&&vm.codeMsg!=="正在获取验证码"){
console.log(vm.codeMsg || (vm.spaceTime+"秒内只能获取一次验证码"));
vm.codeMsg="正在获取验证码";
return ;
}
if(vm.codeMsg==="获取验证码"){
vm.codeMsg="正在获取验证码";
}
var data = {
username:vm.mobile,
client_id: httpRequest.client_id,
}
loginAPI.verifyCode(data).then(function(res) {
if(res.expiresIn==600) vm.getCode(vm.spaceTime);
else{
vm.codeMsg="获取验证码";
console.log(res.errorMsg);
}
}).catch(function(err) {
console.error(err);
vm.codeMsg="获取验证码";
})
},
getJueSeData: function(userAgent) {
var vm = this;
//获取居民权限接口
loginAPI.findByUserId({
userId: userAgent.id
}).then(function(data) {
sessionStorage.setItem("roleList", JSON.stringify(data.detailModelList)); //缓存角色列表
// location.href = "../../../home/html/home.html"//跳转到主页面
if(history.length>=2){
history.back();
}else{
location.href = "../../../page/mine/html/personal-info.html"
}
})
},
toggleRegister: function() {
location.href = "../../register/html/register.html"
}
},
watch: {
Password: function(newVal, oldVal) {
var val = newVal.toString();
var reg = /\s+/g; //去除空格
if(reg.test(val)) {
this.Password = oldVal;
}
},
confirmPassword: function(newVal, oldVal) {
var val = newVal.toString();
var reg = /\s+/g; //去除空格
if(reg.test(val)) {
this.confirmPassword = oldVal;
}
},
codeType: function(newVal) {
var vm = this;
var val = newVal.toString();
if(val == 1) {
$("#IDCard").attr("placeholder", "请输入身份证号")
vm.maxLength = 18
} else if(val == 2) {
$("#IDCard").attr("placeholder", "请输入手机号码")
vm.maxLength = 11
}
}
}
})
function isCardNo(card, codeType) {
var isidcard = false
if(codeType == 1) { // 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
if(!reg.test(card)) {
toastr.error("身份证号输入不合法");
} else {
isidcard = true
}
} else {
var reg = /0?(13|14|15|18|17)[0-9]{9}/;
if(!reg.test(card)) {
toastr.error("手机号码输入不合法");
} else {
isidcard = true
}
}
return isidcard
}
})()