艺术家
2025-06-04 772190e7b2e3f6ef0695ba54d9209324acdcb30a
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
<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>