(function() {
Vue.component('login-form', {
template: `
下载APP
`,
props: [],
data: function() {
return {
type: 1, //记录选择的登录方式,1-手机号登录,2-验证码登录
account: '17500000001',
mobile: '',
password: '123456',
imgcaptcha: '',
captcha: '',
infoText: '获取验证码',
countdown: false, //是否在倒计时
codeType: 1,
idCardNo: "350526199501142015",
rPassword: 'a123123',
confirmPassword: "a123123",
toggle: true,
registerFlag: false,
maxLength: 18,
}
},
methods: {
changeType: function(val) {
this.type = val;
},
getCaptcha: function() {
//先校验手机号码是否正确
var isMobile = isMobilePhone(this.mobile);
if(!isMobile) {
alert('请输入正确的手机号码!');
return false;
}
setTimer(this);
//发送请求
// sendToGetCaptcha(this);
},
login: function() {
var vm = this
if(this.type == 1) {
if(!vm.account) {
console.log("账号不能为空!");
return false;
}
if(!vm.password) {
console.log("登录密码不能为空!");
return false;
}
console.log(httpRequest)
var data = {
grant_type: httpRequest.grant_type,
client_id: httpRequest.client_id,
username: vm.account,
password: vm.password
}
console.log(data)
loginAPI.accessToken(data).then(function(oauthInfo) {
storage.setItem("oauthInfo", JSON.stringify(oauthInfo));
var data1 = {
userName: oauthInfo.user,
accessToken: oauthInfo.accessToken
}
loginAPI.getUserInfo(data1).then(function(res) {
console.log("登录成功");
loginedList = [res.telephone]
storage.setItem("loginedList", JSON.stringify(loginedList));
storage.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;
}
if(!vm.imgcaptcha) {
console.log("图形验证码不能为空!");
return false;
}
if(!vm.captcha) {
console.log("手机验证码不能为空!");
return false;
}
}
},
getJueSeData: function(userAgent) {
var vm = this;
//获取居民权限接口
loginAPI.findByUserId({
userId: userAgent.id
}).then(function(data) {
storage.setItem("roleList", JSON.stringify(data.detailModelList)); //缓存角色列表
// app.openWebviewExtras("../../home/html/home.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 setTimer(vm) {
var seconds = 59;
vm.countdown = true;
timer = setInterval(function() {
if(seconds == 0) {
clearInterval(timer);
timer = null;
seconds = 59;
vm.infoText = "获取验证码";
vm.countdown = false;
return;
}
seconds--;
vm.infoText = seconds + "s后重新获取";
}, 1000)
}
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)) {
alert("身份证号输入不合法");
} else {
isidcard = true
}
} else {
var reg = /0?(13|14|15|18|17)[0-9]{9}/;
if(!reg.test(card)) {
alert("手机号码输入不合法");
} else {
isidcard = true
}
}
return isidcard
}
function sendToGetCaptcha(vm) {
var url = "common/captcha",
params = {
mobile: vm.mobile,
type: 5,
captchaToken: 5
};
httpRequest.post(url, {
data: params
}).then(function(res) {
if(res.status == 200) {
tip('发送成功', 'add');
} else {
tip('获取验证码失败');
}
});
}
function getPublicKey(vm) {
var url = "login/public_key";
httpRequest.post(url).then(function(res) {
if(res.status) {
var mod = res.data.modulus;
var exp = res.data.exponent;
key = RSAUtils.getKeyPair(exp, "", mod);
if(key) {
encryedPwd = RSAUtils.encryStr(key, vm.password);
login({
mobile: vm.mobile,
password: encryedPwd,
platform: 4
})
} else {
tip("获取数据失败")
$submit.removeAttr('disabled')
}
} else {
tip(res.msg);
}
})
}
function login(data) {
httpRequest.post('login/doctor', {
data: data
}).then(function(res) {
if(res.status == 200) {
var docInfo = res.data;
localoginlStorage.setItem(httpRequest.agentName, JSON.stringify({
id: docInfo.id,
uid: docInfo.uid,
token: docInfo.token,
imei: localStorage.getItem('WLYY_IMEI'),
platform: 4
}));
//将用户的角色信息单独存储在localstorage中
localStorage.setItem("userRole", JSON.stringify(docInfo.userRole));
window.location.href = "../home/html/index.html";
} else {
tip(res.msg);
}
});
}
})()