login.js
6.1 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
var layer = null,
$ = null,
IdCard = document.getElementById("IdCard"),
canvas=document.createElement("canvas"),
btnTakeSnapshot = document.getElementById("btnTakeSnapshot"),
isSendFlag = false,
tabIndex=0,
WebcamObj = null;
var login = {
initWebCam: () => {
Webcam.set({
width: 320,
height: 240,
image_format: "jpeg",
jpeg_quality: 90
});
Webcam.attach("#webcam");
WebcamObj = true;
},
//销毁摄像头
cancalCloseVideo: () => {
Webcam.reset("#webcam");
Webcam.reset();
},
//登入方法
LoginMethod: (bodyValue) => {
isSendFlag = true;
var index = 0;
if (tabIndex != 1) index = layer.load();
fetch("/Login/Login", {
method: "post",
headers: new Headers({
"Content-Type": "application/x-www-form-urlencoded"
}),
body: bodyValue
}).then(function (res) {
return res.json();
}).then(function (data) {
isSendFlag = false;
if (data.Code === 200) {
layer.msg(data.Result.Account + " " + data.Result.Name, { icon: 6, shade: 0.4, time: 10000 });
localStorage.setItem("Account", data.Result.Account);
localStorage.setItem("Name", data.Result.Name);
setTimeout(function () {
window.location.href = "/Home/Index";
}, 2000);
} else {
if (layer != null) document.getElementById("loginMessage").innerHTML = data.Message;
}
layer.close(index);
}).catch(function (error) {
isSendFlag = false;
if (layer != null) document.getElementById("loginMessage").innerHTML = "登入失败";
});
},
//自动刷脸登入
initWebcamLogin: () => {
setInterval(() => {
if (WebcamObj == null) return;
if (isSendFlag) {
console.log("请求中 ");
return;
};
if (tabIndex != 1) return;
var v = document.querySelector('#webcam video');
if (v == null) return;
var ctx = canvas.getContext('2d');
var height = v.videoHeight;
var width = v.videoWidth;
if (height == 0 || width == 0) return;
canvas.height = height;
canvas.width = width;
ctx.drawImage(v, 0, 0, width, height);
var comp = ccv.detect_objects({
"canvas": ccv.grayscale(ccv.pre(canvas)),
"cascade": cascade,
"interval": 5,
"min_neighbors": 1
});
if (comp.length > 0) {
btnTakeSnapshot.click();
}
document.getElementById("ccvMessage").innerHTML = "检测图片结果: " + (comp.length ? '有人脸' : "没人脸");
}, 1500);
},
//注册事件
initEvents: () => {
//刷卡登入
IdCard.addEventListener('input', debounce(handle, 500));
//拍照
btnTakeSnapshot.addEventListener("click", function () {
if (WebcamObj == null) {
alert("请等待摄像启动完成后再拍照登入");
return;
}
Webcam.snap(function (data_uri) {
if (data_uri == null) {
alert("请等待摄像启动完成后再拍照登入");
return;
}
var value = `username=''&password=''&webcam=${data_uri}&idcard=`;
login.LoginMethod(value);
});
});
}
}
var isChrome = window.navigator.userAgent.indexOf("Chrome") > -1;
if (!isChrome) {
alert("推荐使用谷歌浏览器登录使用,以免产生浏览器不兼容出现的错误");
}
login.initEvents();
login.initWebcamLogin();
// 防抖
function debounce(fn, delay) {
let timeout = null;
return function () {
clearTimeout(timeout);
timeout = setTimeout(() => {
fn.apply(this, arguments);
}, delay);
};
}
function handle() {
if (IdCard.value == "") return;
var value = `username=''&password=''&webcam=&idcard=${IdCard.value}`;
login.LoginMethod(value);
}
layui.config({ base: "/js/" }).use(["form", "layer", "element"], function () {
//如果在iframe中,则跳转
if (self !== top) top.location.replace("/Login/Index");
layer = parent.layer === undefined ? layui.layer : parent.layer;
$ = layui.jquery;
var form = layui.form,
element = layui.element;
element.on("tab(loginWebcam)", function (data) {
tabIndex = data.index;
if (data.index===1) {
login.initWebCam();
} else if (data.index === 2) {
IdCard.focus();
} else {
document.getElementById("loginMessage").innerHTML = "";
}
});
localStorage.setItem("sysWhereLineSelect","all");
form.on("select", function (data) {
localStorage.setItem("sysWhereLineSelect", data.value);
});
//登录按钮事件
form.on("submit(login)", function (data) {
debugger;
data.field.password = data.field.password.encryptPwd() ;
data["file"] = "";
$.ajax({
url: "/Login/Login",
type: "post",
data: data.field,
dataType: "json",
success: function (data) {
if (data.Code === 200) {
localStorage.setItem("Account", data.Result.Account);
localStorage.setItem("Name", data.Result.Name);
window.location.href = "/Home/Index";
} else {
layer.msg(data.Message, { time: 4000 });
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
layer.alert("【服务器响应超时,请检查WiFi网络Url地址、服务器网关服务是否启动,反复出现请联系管理员!】", { icon: 2, shadeClose: true, title: "错误信息" });
}
});
return false;
});
});