<template>
|
<div class="login-container">
|
<div class="login-header">
|
<img src="../assets/login/login-text.png" alt="" />
|
<div>
|
<img
|
v-if="!isFullscreen"
|
style="
|
width: 1.88rem;
|
height: 1.88rem;
|
margin-right: 1.81rem;
|
cursor: pointer;
|
"
|
src="@/assets/screen.png"
|
alt=""
|
@click="screenonToggle"
|
/>
|
<img
|
v-if="isFullscreen"
|
style="
|
width: 1.88rem;
|
height: 1.88rem;
|
margin-right: 1.81rem;
|
cursor: pointer;
|
"
|
src="@/assets/exitscreen.png"
|
alt=""
|
@click="screenonToggle"
|
/>
|
</div>
|
</div>
|
<div class="login-box">
|
<!-- 左侧图片 -->
|
<div class="left-img">
|
<img src="../assets/login/bg.png" alt="" />
|
</div>
|
<!-- 右侧登录 -->
|
<div class="login-formbox">
|
<el-form
|
v-if="show"
|
ref="loginRef"
|
:model="loginForm"
|
:rules="loginRules"
|
class="login-form"
|
>
|
<div class="title">欢迎登录...</div>
|
<div style="color: #666666; font-size: 0.8rem; margin: 0.5rem 0">
|
WELCOME TO LOGIN
|
</div>
|
|
<el-form-item prop="userName">
|
<el-input
|
v-model="loginForm.userName"
|
style="height: 3.13rem; width: 20.19rem"
|
ref="userNameRef"
|
type="text"
|
size="large"
|
autocomplete="off"
|
placeholder="请输入您的登录账号"
|
/>
|
</el-form-item>
|
<el-form-item prop="password">
|
<el-input
|
show-password
|
v-model="loginForm.password"
|
style="height: 3.13rem; width: 20.19rem"
|
type="password"
|
size="large"
|
auto-complete="off"
|
placeholder="请输入密码"
|
>
|
</el-input>
|
</el-form-item>
|
|
<!-- <el-form-item prop="verificationCode">
|
<div style="display: flex">
|
<el-input
|
v-model="loginForm.verificationCode"
|
size="large"
|
@keyup.enter="handleLogin"
|
style="height: 3.13rem; width: 17.19rem; margin-right: 0.63rem"
|
auto-complete="off"
|
placeholder="验证码"
|
>
|
</el-input>
|
<div class="login-code" style="">
|
<img
|
:src="codeUrl"
|
@click="getCode"
|
style="width: 7.19rem; height: 3.13rem"
|
class="login-code-img"
|
/>
|
</div>
|
</div>
|
</el-form-item> -->
|
<el-form-item>
|
<el-button
|
:loading="loading"
|
color="#1F63FF"
|
size="large"
|
style="width: 100%; height: 3.13rem"
|
@click.prevent="handleLogin"
|
>
|
<span v-if="!loading">登 录</span>
|
<span v-else>登 录 中...</span>
|
</el-button>
|
<div
|
style="
|
display: flex;
|
justify-content: center;
|
align-items: center;
|
width: 100%;
|
margin-top: 1.25rem;
|
"
|
>
|
<span
|
style="
|
color: #4386ff;
|
border-bottom: 1px solid #4386ff;
|
font-weight: bold;
|
cursor: pointer;
|
font-size: 1rem;
|
"
|
@click="recognition"
|
>人脸识别登录</span
|
>
|
</div>
|
</el-form-item>
|
</el-form>
|
<div class="face-login" v-else>
|
<span
|
style="
|
text-align: center;
|
font-size: 0.88rem;
|
font-weight: bold;
|
color: #333333;
|
"
|
>请将脸部正对蓝色显示框内,并保持光线充足</span
|
>
|
<div style="width: 100%; display: flex; justify-content: center">
|
<div
|
style="
|
display: flex;
|
justify-content: center;
|
align-items: center;
|
border: 1px solid #4386ff;
|
border-radius: 50%;
|
width: 18.75rem;
|
height: 18.75rem;
|
margin: 2.06rem 0;
|
background-color: #f1fcff;
|
"
|
>
|
<img
|
style="width: 16rem; height: 16rem"
|
v-if="!face"
|
src="@/assets/login/face.png"
|
alt=""
|
/>
|
<!-- <canvas v-else ref="canvasDom" /> -->
|
<div v-else>
|
<!-- 播放器,用来播放拍摄的视频 -->
|
<video class="camera_video" ref="videoDom" />
|
<!-- 显示照片 -->
|
<!-- <img
|
style="border-radius: 50%; width: 18.75rem; height: 18.75rem"
|
v-else
|
:src="imgurl"
|
/> -->
|
</div>
|
</div>
|
</div>
|
<!-- <el-button
|
v-if="!reBtn"
|
type="primary"
|
size="small"
|
style="width: 100%"
|
@click="takePhoto"
|
>开始识别</el-button
|
>
|
<el-button
|
v-else
|
type="primary"
|
size="small"
|
style="width: 100%"
|
@click="REtakePhoto"
|
>重新识别</el-button
|
> -->
|
<div style="width: 100%; margin-top: 1rem; display: flex">
|
<el-button
|
v-if="show"
|
type="primary"
|
size="small"
|
style="width: 100%"
|
@click="recognition"
|
>开始识别</el-button
|
>
|
<el-button
|
v-else
|
type="primary"
|
size="small"
|
style="width: 100%"
|
@click="recognition"
|
>重新获取摄像头</el-button
|
>
|
<el-select
|
v-if="videoArr.length > 1"
|
v-model="constraints"
|
placeholder="请选择摄像头"
|
size="samll"
|
@change="changeconstraints"
|
style="height: 2rem"
|
:disabled="!videoArr.length > 1"
|
>
|
<el-option
|
v-for="item in videoArr"
|
:key="item.id"
|
:label="item.label"
|
:value="item.id"
|
/>
|
</el-select>
|
</div>
|
<div
|
style="
|
display: flex;
|
justify-content: center;
|
align-items: center;
|
width: 100%;
|
margin-top: 1.25rem;
|
"
|
>
|
<span
|
style="
|
color: #4386ff;
|
border-bottom: 1px solid #4386ff;
|
font-weight: bold;
|
cursor: pointer;
|
font-size: 1rem;
|
"
|
@click="accountlogin"
|
>账号登录</span
|
>
|
</div>
|
</div>
|
</div>
|
</div>
|
</div>
|
</template>
|
|
<script setup>
|
import { getCodeImg, login, CleanUnusedImages } from "@/api/login";
|
import { useRouter, useRoute } from "vue-router";
|
import { getCurrentInstance, ref, nextTick, onMounted, onUnmounted } from "vue";
|
import { ElMessage } from "element-plus";
|
import screenfull from "screenfull";
|
import store from "@/store";
|
import axios from "axios";
|
|
const router = useRouter();
|
const route = useRoute();
|
|
const { proxy } = getCurrentInstance();
|
|
const show = ref(true); // 是否显示登录框
|
|
const codeUrl = ref(""); // 验证码
|
const loading = ref(false); // 登录加载状态
|
const face = ref(false); // 人脸识别登录
|
// 照片路径
|
const imgurl = ref(null);
|
// canvas控件对象
|
const canvasDom = ref(null);
|
// video 控件对象
|
const videoDom = ref(null);
|
const reBtn = ref(false);
|
const loginForm = ref({
|
userName: "",
|
password: "",
|
verificationCode: "1234",
|
UUID: undefined,
|
tenantId: "0",
|
});
|
// tenantId: route.query.tenantId ? route.query.tenantId : "",
|
const loginRules = {
|
userName: [
|
{ required: true, trigger: "blur", message: "请输入您的登录账号" },
|
],
|
password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
|
verificationCode: [
|
{ required: true, trigger: "blur", message: "请输入验证码" },
|
],
|
// tenantId: [{ required: true, trigger: "blur", message: "请输入公司唯一码" }],
|
};
|
|
function handleLogin() {
|
proxy.$refs.loginRef.validate((valid) => {
|
if (valid) {
|
loading.value = true;
|
login(loginForm.value)
|
.then((res) => {
|
if (res.status) {
|
store.commit("setUserInfo", { ...res.data });
|
router.push({ path: "/" });
|
}
|
})
|
.catch((err) => {
|
loginForm.value.verificationCode = "";
|
getCode();
|
loading.value = false;
|
return proxy.$message.error(res.message);
|
});
|
setTimeout(() => {
|
loading.value = false;
|
}, 1000);
|
}
|
});
|
}
|
|
//获取验证码方法
|
function getCode() {
|
getCodeImg().then((res) => {
|
if (res.Code == 500) {
|
getCode();
|
return;
|
}
|
codeUrl.value = "data:image/gif;base64," + res.img;
|
loginForm.value.UUID = res.uuid;
|
});
|
}
|
//绑定输入框焦点对象
|
const userNameRef = ref();
|
nextTick(() => {
|
userNameRef.value.focus();
|
});
|
const videoArr = ref([]);
|
//选择的摄像头的id
|
const constraints = ref("");
|
const videoConstraints = ref({});
|
//人脸识别登录
|
const recognition = () => {
|
show.value = false;
|
videoArr.value = [];
|
setTimeout(async () => {
|
face.value = true;
|
navigator.mediaDevices
|
.enumerateDevices()
|
.then((devices) => {
|
devices.forEach(function (device) {
|
if (device.kind == "videoinput") {
|
videoArr.value.push({
|
label: device.label,
|
id: device.deviceId,
|
});
|
constraints.value = videoArr.value[0].id;
|
}
|
});
|
openCamera();
|
})
|
.catch(function (err) {
|
layer.msg(err.name + ": " + err.message);
|
});
|
}, 1000);
|
ElMessage({
|
message: "正在调用摄像头,请稍等...",
|
type: "warning",
|
plain: true,
|
duration: 1000,
|
});
|
// 人脸识别登录逻辑
|
};
|
|
//账号登录
|
function accountlogin() {
|
stop();
|
ElMessage({
|
message: "正在关闭摄像头,请稍等...",
|
type: "warning",
|
plain: true,
|
duration: 2000,
|
});
|
setTimeout(() => {
|
ElMessage({
|
message: "关闭摄像头成功",
|
type: "success",
|
plain: true,
|
});
|
}, 3000);
|
setTimeout(() => {
|
show.value = true;
|
face.value = false;
|
}, 2000);
|
}
|
//打开摄像头
|
const openCamera = async () => {
|
videoConstraints.value.deviceId = { exact: constraints.value };
|
|
// 检测浏览器是否支持mediaDevices
|
if (navigator.mediaDevices) {
|
navigator.mediaDevices
|
// 开启视频,关闭音频
|
.getUserMedia({
|
audio: false,
|
video: {
|
width: 300, // 设置视频宽度
|
height: 300, // 设置视频高度
|
facingMode: "user", // 使用前置摄像头
|
deviceId: videoConstraints.value.deviceId,
|
},
|
})
|
.then((stream) => {
|
// 将视频流传入viedo控件
|
videoDom.value.srcObject = stream;
|
// 播放
|
videoDom.value.play();
|
Facerecognition();
|
ElMessage({
|
message: "摄像头调用成功",
|
type: "success",
|
plain: true,
|
});
|
})
|
.catch((err) => {
|
console.log(err);
|
});
|
} else {
|
window.alert("该浏览器不支持开启摄像头,请更换最新版浏览器");
|
}
|
};
|
const changeconstraints = (res) => {
|
openCamera();
|
};
|
|
// 开始识别
|
const takePhoto = async () => {
|
// 如果已经拍照了就重新启动摄像头
|
// if (imgurl.value) {
|
// imgurl.value = null;
|
// openCamera();
|
// return;
|
// }
|
|
// console.log(videoDom.value.videoWidth, videoDom.value.videoHeight);
|
// 创建一个画布元素,设置画布尺寸为视频流的尺寸
|
const canvas = document.createElement("canvas");
|
// 设置画布大小与摄像大小一致
|
canvas.width = videoDom.value.videoWidth;
|
canvas.height = videoDom.value.videoHeight;
|
// 获取画布上下文对象
|
const ctx = canvas.getContext("2d");
|
// 绘制当前视频帧到画布上
|
ctx.drawImage(videoDom.value, 0, 0, canvas.width, canvas.height);
|
// 将画布内容转为 Base64 数据
|
const imageDataUrl = canvas.toDataURL("image/png");
|
// 存储图片路径
|
imgurl.value = imageDataUrl;
|
|
// // 创建一个图片元素
|
// const imageElement = new Image();
|
// // 将 Base64 数据设置为图片的 src 属性
|
// imageElement.src = imageDataUrl;
|
// console.log(imageElement, imageDataUrl, "图片路径");
|
// return;
|
// canvasDom.value.width = videoDom.value.videoWidth;
|
// canvasDom.value.height = videoDom.value.videoHeight;
|
// // 执行画的操作
|
// canvasDom.value.getContext("2d").drawImage(videoDom.value, 0, 0);
|
// // 将结果转换为可展示的格式
|
// imgurl.value = canvasDom.value.toDataURL("image/webp");
|
// 关闭摄像头
|
let files = dataURLtoFile(imgurl.value, new Date().getTime() + ".png");
|
const formdata = new FormData();
|
formdata.append("files", files);
|
let response = await axios.post("/api/User/SaveFiles", formdata, {
|
headers: {
|
"Content-Type": "multipart/form-data",
|
},
|
});
|
ElMessage({
|
message: "开始识别中,请稍等...",
|
type: "warning",
|
plain: true,
|
duration: 2000,
|
});
|
setTimeout(() => {
|
login({
|
userName: "",
|
password: "",
|
path: response.data.data,
|
})
|
.then((res) => {
|
if (res.status) {
|
store.commit("setUserInfo", { ...res.data });
|
ElMessage({
|
message: "识别成功,开始登录",
|
type: "success",
|
plain: true,
|
duration: 2000,
|
});
|
CleanUnusedImages();
|
stop();
|
setTimeout(() => {
|
router.push({ path: "/" });
|
}, 1000);
|
}
|
})
|
.catch((err) => {
|
loading.value = false;
|
return proxy.$message.error(err.message);
|
});
|
setTimeout(() => {
|
loading.value = false;
|
}, 1000);
|
}, 1000);
|
reBtn.value = true;
|
};
|
//重新识别
|
// const REtakePhoto = () => {
|
// reBtn.value = false;
|
// takePhoto();
|
// };
|
// 将 base64 转换为 Blob
|
const dataURLtoFile = (dataurl, filename) => {
|
let arr = dataurl.split(","),
|
mime = arr[0].match(/:(.*?);/)[1],
|
bstr = atob(arr[1]),
|
n = bstr.length,
|
u8arr = new Uint8Array(n);
|
while (n--) {
|
u8arr[n] = bstr.charCodeAt(n);
|
}
|
return new File([u8arr], filename, {
|
type: mime,
|
});
|
};
|
// 关闭摄像头
|
const stop = () => {
|
let stream = videoDom.value.srcObject;
|
if (!stream) return;
|
let tracks = stream.getTracks();
|
tracks.forEach((x) => {
|
x.stop();
|
});
|
clearInterval(timer.value);
|
};
|
|
//获取验证码
|
getCode();
|
//定时人脸识别
|
const timer = ref(null);
|
const Facerecognition = () => {
|
clearInterval(timer.value);
|
if (!show.value) {
|
timer.value = setInterval(() => {
|
takePhoto();
|
}, 3000);
|
}
|
};
|
|
// 是否全屏
|
const isFullscreen = ref(false);
|
|
// 监听变化
|
const screenchange = () => {
|
isFullscreen.value = screenfull.isFullscreen;
|
};
|
|
// 切换事件
|
const screenonToggle = () => {
|
screenfull.toggle();
|
};
|
// 设置侦听器
|
onMounted(() => {
|
screenfull.on("screenchange", screenchange);
|
});
|
|
// 删除侦听器
|
onUnmounted(() => {
|
screenfull.off("screenchange", screenchange);
|
});
|
</script>
|
|
<style lang="less" scoped>
|
// * {
|
// box-sizing: border-box;
|
// padding: 0;
|
// margin: 0;
|
// }
|
.login-container {
|
width: 100%;
|
min-height: 100vh;
|
display: flex;
|
flex-direction: column;
|
.login-header {
|
display: flex;
|
justify-content: space-between;
|
align-items: center;
|
height: 4.38rem;
|
background-color: #006eff;
|
padding-left: 2.06rem;
|
box-sizing: border-box;
|
img {
|
width: 16rem;
|
height: 3.38rem;
|
}
|
}
|
.login-box {
|
width: 100%;
|
height: calc(100vh - 4.57rem);
|
display: flex;
|
background-color: #f6f7fc;
|
.left-img {
|
width: 100%;
|
height: 100%;
|
img {
|
width: 100%;
|
height: 100%;
|
}
|
}
|
.login-formbox {
|
width: 100%;
|
height: 100%;
|
display: flex;
|
justify-content: center;
|
align-items: center;
|
.title {
|
color: rgb(16, 16, 16);
|
font-size: 2rem;
|
font-weight: 600;
|
position: relative;
|
}
|
.title::after {
|
position: absolute;
|
bottom: 0;
|
content: "";
|
display: block;
|
width: 9rem;
|
height: 1rem;
|
background: linear-gradient(to right, #006eff, #ffffff);
|
opacity: 0.5;
|
}
|
}
|
.face-login {
|
width: 30rem;
|
display: flex;
|
flex-direction: column;
|
justify-content: center;
|
align-content: center;
|
text-align: center;
|
}
|
}
|
}
|
.camera_video {
|
width: 18.75rem;
|
height: 18.75rem;
|
border: 1px black solid;
|
border-radius: 50% 50%;
|
}
|
</style>
|