登录的样板如下:
代码如下:
前端:
<div class="layui-container">
<div class="admin-login-background">
<div class="layui-form login-form">
<div class="layui-form-item logo-title">
<h1>贷款系统登录 </h1>
</div>
<div class="layui-form-item">
<label class="layui-icon layui-icon-username"></label>
<input type="text" id="userName2" placeholder="账号或手机号" maxlength="11" autocomplete="off" class="layui-input" onkeyup="value=value.replace(/[^\d]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">
</div>
<div class="layui-form-item">
<label class="layui-icon layui-icon-password"></label>
<input type="password" id="userPassword2" placeholder="密码" autocomplete="off" class="layui-input">
<div class="see">
<img src="~/Condent/img/openeye.png" id="openEye" style="display:none;" />
<img src="~/Condent/img/closeeye.png" alt="隐藏密码" id="closeEye" />
</div>
</div>
@* ==============获取验证码=============== *@
<div class="form-group row" style="margin-right:-65px;">
<div class="col-sm-9 col-xl-10 pr-0">
<div class="input-group">
<input type="text" class="form-control layui-input" style="" id="IdentifyingCode" name="validCode" placeholder="请您输入验证码">
<div class="input-group-prepend">
<img src="/BackgroundMain/ValideCode" class="input-group-text p-0 " id="ValidateCode" />
</div>
</div>
</div>
</div>
<div class="layui-form-item">
<button id="btn" class="layui-btn layui-btn-normal layui-btn-fluid" onclick="login()">登 入</button>
<p class="hang"><span>是否有账号?</span><a id="ZhuChe" class="zhuzhu">注 册 </a></p>
</div>
</div>
</div>
</div>
//------------------登录---------------------
在登录按钮绑定点击事件
function login() {
var UserName = $("#userName2").val();
var UserPassword = $("#userPassword2").val();
var IdentifyingCode = $("#IdentifyingCode").val();
if (UserName != "" && UserName != null && UserName != undefined) {
if (UserPassword != "" && UserPassword != null && UserPassword != undefined) {
if (IdentifyingCode != "" && IdentifyingCode != null && IdentifyingCode != undefined) {
//打开加载层
var index = layer.load();
//提交数据
$.post("/BackgroundMain/Logindl",
{
userName: UserName,
userPassword: UserPassword,
identifyingCode: IdentifyingCode,
}, function (rtjson) {
//关闭加载层
layer.close(index);
if (rtjson.Code == "1") {
//登录成功,跳转到后台页面
window.location.replace("/BackgroundMain/Main");
} else if (rtjson.Code == "2") {
//登录成功,跳转到前台页面
window.location.replace("/BackgroundMain/Websit");
}
layer.alert(rtjson.Text);
if (rtjson != "") {
$.post("/BackgroundMain/RefreshData", { strnum: 0 })
}
});
} else {
//刷新验证码,通过click()方法触发验证码的点击事件
$("#vcImg").click();
layer.alert("请填写验证码!", { anim: 6, closeBtn: 2, shade: [0.6, '#393D49'], icon: 0, title: '提示' });
}
} else {
layer.alert("请填写用户密码!", { anim: 6, closeBtn: 2, shade: [0.6, '#393D49'], icon: 0, title: '提示' });
}
} else {
layer.alert("请填写用户账号!", { anim: 6, closeBtn: 2, shade: [0.6, '#393D49'], icon: 0, title: '提示' });
}
}
同时也在登录按钮上绑定回车事件
//--------------------回车事件----------------------
//回车登录
window.onkeyup = function (event) {
var e = event || window.event;
//判断当前是否点击了回车键,键盘码为13
if (event.keyCode == 13) {
var UserName1 = $("#userName").val();
var UserPhonrNum1 = $("#userPhonrNum").val();
var UserPassword1 = $("#userPassword").val();
if (UserName1 != "" && UserPassword1 != "" && UserPhonrNum1 != "") {
//调用验证注册方法
saveForm();
} else {
//调用验证登录方法
login();
}
}
}
后台代码:
public ActionResult Logindl(string userName, string userPassword, string identifyingCode)
{
ReturnJsonVo rtjs = new ReturnJsonVo();
//验证码
string Code = Session["ValideCode"].ToString();
try
{
int? count = Myloans.S_User.Count(m => m.TelAndAccount == userName);
if (count != 0)
{
S_User user = Myloans.S_User.Single(m => m.TelAndAccount == userName);
if (user.Password.Trim().Equals(userPassword.Trim()))
{
if (identifyingCode.Equals(Code, System.StringComparison.CurrentCultureIgnoreCase))
{
string usertype = (from tabRD in Myloans.S_UseRD
where tabRD.UserID == user.UserID
select tabRD.UserTypeID).Single().ToString();
///获取本地的IP地址
string AddressIP = string.Empty;
foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
{
if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
{
AddressIP = _IPAddress.ToString();
}
}
S_Log log = new S_Log()
{
UserID = user.UserID,
LogSTime = DateTime.Now,
LogCIP = AddressIP,
};
Myloans.S_Log.Add(log);
Myloans.SaveChanges();
if (usertype == "1" || usertype == "2")
{
//此时用户为超级管理员或管理员
rtjs.Code = "1";
}
else
{
//此时用户为普通用户
rtjs.Code = "2";
}
//设置session的时间
Session.Timeout = 1440;
Session["UserName"] = user.UserName;
Session["UserID"] = user.UserID;
}
else
{
rtjs.Text = "验证码错误!请重新填写";
}
}
else
{
rtjs.Text = "用户密码错误!请检查";
}
}
else
{
rtjs.Text = "用户账号错误!请检查";
}
return Json(rtjs, JsonRequestBehavior.AllowGet);
}
catch (Exception)
{
rtjs.Text = "数据异常";
return Json(rtjs, JsonRequestBehavior.AllowGet);
}
}