new Vue({ el: "#main", data: { type: 1, //记录选择的登录方式,1-手机号登录,2-验证码登录 mobile: '', password: '', captcha: '', eyeIsOpen: false, infoText: '获取验证码', countdown: false, //是否在倒计时 imgCode: "", imgCaptcha: "" }, mounted: function() { this.getImgCode() }, methods: { changeType: function(val){ this.type = val; }, getCaptcha: function(){ //先校验手机号码是否正确 var isMobile = isMobilePhone(this.mobile); if(!isMobile){ toastr.error('请输入正确的手机号码!'); return false; } this.sendSmsCode() }, login: function(){ var vm = this if(this.type == 1){ var validation = $("#form1").valid(); if(validation){ getPublicKey(this); } }else{ var validation = $("#form2").valid(); if(validation){ login({ mobile: this.mobile, captcha: this.captcha, platform: 4, imgCaptcha: this.imgCaptcha }).catch(function() { vm.getImgCode() }) } } }, getImgCode: getImgCode, sendSmsCode: sendSmsCode } }); function guid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); } function getImgCode() { var vm = this httpRequest.get('/weixin/imgCaptcha', {key: guid()}).then(function(res) { if(res.status == 200){ vm.imgCode = res.data.image } else { toastr.error('获取图形验证码失败'); } }) } function getSmsImgCode() { var vm = this httpRequest.get('/weixin/imgCaptcha', {key: guid()}).then(function(res) { if(res.status == 200){ $("#imgSmsCode img").attr('src', res.data.image) } else { toastr.error('获取图形验证码失败'); } }) } //验证码定时 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 sendToGetCaptcha(vm){ var url = "common/captcha", params = { mobile: vm.mobile, type: 5, captchaToken: 5, imgCaptcha: vm.imgCaptcha }; setTimer(this); httpRequest.post(url, {data: params}).then(function(res){ if(res.status == 200){ toastr.success('发送成功'); }else{ toastr.error('获取验证码失败'); } }); } 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, imgCaptcha: vm.imgCaptcha }) }else { toastr.error("获取数据失败") $submit.removeAttr('disabled') } }else{ tip(res.msg); } }) } function login(data) { return httpRequest.post('login/doctor', {data: data}).then(function(res){ if(res.status == 200){ var docInfo = res.data; localStorage.setItem(httpRequest.agentName,JSON.stringify({ id: docInfo.id, uid: docInfo.uid, token: docInfo.token, imei: localStorage.getItem('WLYY_IMEI'), platform: 4 })); localStorage.setItem("app_storage", JSON.stringify({ 'api_login_doctor': docInfo, 'IMEI': localStorage.getItem('WLYY_IMEI') })) //将用户的角色信息单独存储在localstorage中 localStorage.setItem("userRole", JSON.stringify(docInfo.userRole)); sessionStorage.setItem("userRole", JSON.stringify(docInfo.userRole)); window.location.href = "../home/html/index.html"; }else{ toastr.error(res.msg); return Promise.reject() } }); } function sendSmsCode() { var vm = this dialog({ title:'验证码', content: '
\ \ \
', okValue:'发送', cancelValue: "取消", ok: function (){ var imgCaptcha = $('#imgSmsCaptchaCode').val() if(!imgCaptcha) { toastr.error('请输入图形验证码') return false } vm.imgCaptcha = imgCaptcha sendToGetCaptcha(vm) }, cancel: function() { } }).showModal(); setTimeout(function() { getSmsImgCode() }, 100) }