login.js
3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
layui.config({
base: "/js/"
}).use(['form', 'I18nPage', 'element', 'layer'], function () {
if (self !== top) {
//如果在iframe中,则跳转
top.location.replace("/Login/Index");
}
var form = layui.form,
layer = parent.layer === undefined ? layui.layer : parent.layer,
$ = layui.jquery,
element = layui.element,
I18nPage = layui.I18nPage;
var Lang = I18nPage.GetLang();
var MessageContainer = {};
//登录页面
if (1 == 1) {
MessageContainer["LoginPage_cn"] = "登录页面";
MessageContainer["InputAccount_cn"] = "请输入用户名";
MessageContainer["InputPassword_cn"] = "请输入密码";
MessageContainer["Login_cn"] = "登录";
MessageContainer["ErrorInfo_cn"] = "错误信息";
MessageContainer["LoginPage_us"] = "Login Page";
MessageContainer["InputAccount_us"] = "Please Input Account";
MessageContainer["InputPassword_us"] = "Please Input Password";
MessageContainer["Login_us"] = "Login";
MessageContainer["ErrorInfo_us"] = "Error Info";
}
$(":radio[name='language'][value='" + Lang + "']").prop("checked", "checked");
form.render();
ChangeLanguage(Lang);
form.on('radio(language)', function (data) {
Lang = data.value;
ChangeLanguage(Lang);
});
function ChangeLanguage(lang) {
document.title = MessageContainer["LoginPage_" + lang];
$("#Lang_SystemName").html($("#SystemName_" + lang).html());
$("input[name='username']").prop("lay-reqtext", MessageContainer["InputAccount_" + lang]);
$("input[name='username']").prop("placeholder", MessageContainer["InputAccount_" + lang]);
$("input[name='password']").prop("lay-reqtext", MessageContainer["InputPassword_" + lang]);
$("input[name='password']").prop("placeholder", MessageContainer["InputPassword_" + lang]);
$("#Lang_Login").html(MessageContainer["Login_" + lang]);
form.render();
}
//自定义验证规则
form.verify({
username: function (value) {
if (value == "") {
return MessageContainer["InputAccount_" + Lang];
}
}
, password: function (value) {
if (value == "") {
return MessageContainer["InputPassword_" + Lang]
}
}
});
//登录按钮事件
form.on("submit(login)", function (data) {
$.ajax({
url: "/Login/Login",
type: "post",
data: data.field,
dataType: "json",
success: function (data) {
if (data.Code === 200) {
window.localStorage.setItem("Account", data.Result[0]);
window.localStorage.setItem("Name", data.Result[1]);
window.localStorage.setItem("Lang", Lang);
window.location.href = "/Home/Index";
} else {
layer.msg(data.Message);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
layer.alert(errorThrown, { icon: 2, shadeClose: true, title: MessageContainer["ErrorInfo_" + Lang] });
}
});
return false;
})
$("#username").on('keydown', function (e) {
//CTRL+ENTER 提交
if (e.keyCode == 13) {
$("#password").focus();
}
})
$("#password").on('keydown', function (e) {
//CTRL+ENTER 提交
if (e.keyCode == 13) {
$("#login").click();
}
})
})