const $ = require('jquery');
require("../css/reset.less")
require("../css/login.less")
require("./modal.js")
const { api } = require('./api.js')
const { post, setCookie, delCookie, getUrlArgObject, toast } = require('../js/utils.js');
const hideLogo = getUrlArgObject("hideLg");
const otherLogo = getUrlArgObject("adLg");
let type;
let is3His=false; //医院标记:湘雅三院1/通用0
$(function () {
//清除需要跳转到登录页的标记
localStorage.removeItem('toLogin');
if (hideLogo || otherLogo) {
$(".copy-right").text("");
}
$(document).keyup(function (event) {
if (event.keyCode == 13) {
sureLogin()
}
});
getHospitalMark()
});
//获取验证码
function getSecurityCode() {
var xhr = new XMLHttpRequest();
var url = api.getCaptcha;//验证码请求地址
xhr.open("GET", url, true);
xhr.responseType = "blob";
xhr.onload = function () {
if (this.status == 200) {
var blob = this.response;
const img = document.createElement("img");
const qrUrl = window.URL.createObjectURL(blob);
img.src = qrUrl;
//释放对象
img.onload = function () {
console.log('释放')
window.URL.revokeObjectURL(qrUrl)
}
const imgDiv = document.getElementById("codeImg");
$(imgDiv).html(img);
}
}
xhr.send();
}
function getHospitalMark() {
post(api.getHospitalMark).then((res) => {
const data = res.data;//{ code: 0, data: 0 }
if (data.code == 0) {
type = parseInt(data.data)
is3His=type===1;
if (is3His) { //湘雅三院登录样式
str = '
'
str += '
'
str += '
'
str += '
用户名:
'
str += '
'
str += ''
str += '
'
str += '
'
str += '
密码:
'
str += '
'
str += ''
str += '
'
str += '
登录
'
$('.login').html(str)
} else { //通用版样式
str = '
'
str += '
欢迎使用!
'
str += '
'
str += '
用户名:
'
str += '
'
str += ''
str += '
'
str += '
'
str += '
密码:
'
str += '
'
str += ''
str += '
'
str += '
'
str += '
验证码:
'
str += '
'
str += '
'
str += '
'
str += '
换一张'
str += '
'
str += '
登录
由杭州朗通信息技术有限公司提供技术支持
',
$('.login').html(str)
getSecurityCode();
}
$(".sureLogin").click(() => {
sureLogin()
})
//点击换一张事件
$("#codeImg,#changeCode").click(function () {
getSecurityCode();
$(".security-code input").val('')
})
$("#password").focus(function () {
$(this).attr("type", "password")
})
$(".iptWrap:not(.security-code) input").focus(function () {
$(this).parent().addClass('inputFocus')
})
$(".security-code input").focus(function () {
$(this).addClass('inputFocus')
})
$(".iptWrap input").blur(function () {
$(this).parent().removeClass('inputFocus')
$(this).removeClass('inputFocus')
})
$("body").on('input', '#username,#password', function () {
if ($(this).val().trim() != '') {
$(".login .waring").html('')
}
})
}
}).catch(() => {
})
}
function sureLogin() {
localStorage.removeItem('accessToken')
$(".login .waring").html('')
let username = $("#username").val().trim();
let password = $("#password").val().trim();
let captcha = !is3His?$("#code").val().trim():undefined;
if (!username) {
if (is3His) {
toast("请输入用户名!");
} else {
$(".login .waring").css('visibility', 'visible').html('请输入用户名')
}
return
} else if (!password) {
if (is3His) {
toast("请输入密码!");
} else {
$(".login .waring").css('visibility', 'visible').html('请输入密码')
}
return
}
if (!is3His&&!captcha) { //三院不用验证码
$(".login .waring").css('visibility', 'visible').html('请输入验证码')
return
}
$.alerModal({ type: "loading" });
post(api.getJwt, {
username: username,
password: password,
captcha: captcha
}).then((res) => {
const data = res.data;
if (data.code == 0) {
setCookie('isPlacefile', 1)
setCookie('passwordComplexity', data.data.passwordComplexity)
//setCookie('mmps',password);
localStorage.setItem('accessToken', data.data.accessToken)
$(".divModal").remove()
$.alerModal({ "message": "登录成功", type: "tip", time: '1000', win: true });
setTimeout(() => {
cacheCheckDatas(data.data.selRoles);
const otherLogoPm = otherLogo ? '?adLg=1' : '';
const hideLgPm = hideLogo ? '?hideLg=1' : '';
window.location.href = 'index.html' + hideLgPm + otherLogoPm + "?is3His="+is3His;
}, 600);
} else {
if(!is3His){
getSecurityCode();
$(".security-code input").val('')
}//登录失败切换验证码
$(".divModal").remove()
if (type == 1) {
toast(data.msg);
} else {
$(".login .waring").css('visibility', 'visible').html(data.msg)
}
}
}).catch(() => {
$(".divModal").remove()
})
}
//核查权限数据保存
function cacheCheckDatas(data) {
delCookie("checkAuth");
const arr = data && data.map((it) => {
return it.id;
}) || [];
setCookie("checkAuth", arr.join(","));
}