| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008 |
- <%@ page import="ss.page.PageC" %>
- <%@ page language="java" pageEncoding="UTF-8" isELIgnored="false" %>
- <%@ taglib uri="/ssTag" prefix="ss" %>
- <% pageContext.setAttribute(PageC.PAGE_objName, "xy"); %>
- <!DOCTYPE html>
- <html>
- <head>
- <%@ include file="/page/clip/header.jsp" %>
- <script>window.xyZjzBatchLoginStatus="${empty sessionScope['ssUser']?'0':'1'}";</script>
- <link rel="stylesheet" type="text/css" href="/ss/window/theme/dhtmlxwindows.css">
- <link rel="stylesheet" type="text/css" href="/ss/window/theme/dhx_blue/dhtmlxwindows_dhx_blue.css">
- <script type="text/javascript" src="/ss/window/dhtmlxcommon.js"></script>
- <script type="text/javascript" src="/ss/window/dhtmlxwindows.js"></script>
- <script type="text/javascript" src="/ss/window/dhtmlxcontainer.js"></script>
- <script type="text/javascript" src="/ss/js/display.js"></script>
- <script type="text/javascript" src="/ss/js/upload.js"></script>
- <style>
- html, body, body.env-input-body {
- height: 100%;
- margin: 0;
- overflow: hidden;
- background: #f5f7fb;
- font-family: Arial, "Microsoft YaHei", sans-serif;
- color: #333;
- }
- .form-container {
- height: 100%;
- min-height: 0;
- margin: 0;
- }
- .batch-photo-page {
- display: flex;
- height: 100%;
- min-height: 0;
- overflow: hidden;
- }
- .batch-photo-main {
- flex: 0 0 55%;
- display: flex;
- min-width: 0;
- min-height: 0;
- flex-direction: column;
- align-items: center;
- padding: 20px 42px 24px;
- box-sizing: border-box;
- background: #fff;
- border-right: 1px solid #e1e5ec;
- }
- .batch-photo-preview-shell {
- display: flex;
- flex: 1;
- width: 100%;
- min-height: 0;
- align-items: flex-start;
- justify-content: center;
- }
- .batch-photo-preview {
- flex: 0 0 auto;
- position: relative;
- width: min(100%, 540px);
- height: auto;
- max-width: 100%;
- max-height: 100%;
- aspect-ratio: 1000 / 1515;
- overflow: hidden;
- background: #858585;
- border: 1px solid #d8dce4;
- box-sizing: border-box;
- }
- .batch-photo-preview video,
- .batch-photo-preview > img.batch-photo-captured {
- position: absolute;
- inset: 0;
- z-index: 1;
- display: none;
- width: 100%;
- height: 100%;
- object-fit: cover;
- }
- .batch-photo-preview.is-live {
- border-color: #409eff;
- }
- .batch-photo-placeholder {
- position: absolute;
- inset: 0;
- z-index: 2;
- display: flex;
- align-items: center;
- justify-content: center;
- padding: 32px;
- box-sizing: border-box;
- color: rgba(255, 255, 255, .86);
- text-align: center;
- font-size: 18px;
- line-height: 1.6;
- background: linear-gradient(180deg, rgba(0,0,0,.08), rgba(0,0,0,.22));
- }
- .batch-photo-guide {
- position: absolute;
- inset: 0;
- z-index: 3;
- display: none;
- pointer-events: none;
- box-sizing: border-box;
- background: transparent url("/image/xyZjz/batch-face-guide.png?v=20260827-face3") center center / 100% 100% no-repeat;
- }
- .batch-photo-action-bar {
- display: flex;
- flex-wrap: wrap;
- gap: 12px;
- justify-content: center;
- width: min(100%, 430px);
- padding-top: 20px;
- }
- .batch-photo-action-bar button,
- .batch-photo-toolbar button {
- min-width: 138px;
- height: 42px;
- padding: 0 22px;
- border: 1px solid #d7dce5;
- border-radius: 4px;
- background: #fff;
- color: #555;
- cursor: pointer;
- font-size: 16px;
- }
- .batch-photo-action-bar button:hover,
- .batch-photo-toolbar button:hover {
- border-color: #409eff;
- color: #409eff;
- }
- .batch-photo-action-bar button.primary,
- .batch-photo-toolbar button.primary {
- border-color: #1f2937;
- background: #1f2937;
- color: #fff;
- }
- .batch-photo-action-bar button.primary:hover,
- .batch-photo-toolbar button.primary:hover {
- border-color: #111827;
- background: #111827;
- color: #fff;
- }
- .batch-photo-action-bar button:disabled,
- .batch-photo-toolbar button:disabled {
- cursor: not-allowed;
- opacity: .55;
- }
- .batch-photo-toolbar {
- display: flex;
- align-items: center;
- gap: 8px;
- width: 100%;
- margin-bottom: 0;
- }
- .batch-photo-toolbar select {
- max-width: 220px;
- height: 34px;
- padding: 0 8px;
- border: 1px solid #d7dce5;
- border-radius: 4px;
- background: #fff;
- }
- .batch-photo-right {
- display: grid;
- grid-template-rows: 248px 1px minmax(0, 1fr);
- flex: 1 1 42%;
- min-width: 380px;
- min-height: 0;
- overflow: hidden;
- background: #f7f8fa;
- }
- .batch-photo-student-card {
- margin: 20px 32px;
- padding: 16px 20px;
- overflow: hidden;
- border: 1px solid #e0e4eb;
- border-radius: 5px;
- background: #fff;
- box-sizing: border-box;
- }
- .batch-photo-student-name {
- margin-bottom: 18px;
- color: #111;
- font-size: 21px;
- font-weight: 600;
- }
- .batch-photo-student-body {
- display: flex;
- align-items: flex-start;
- gap: 18px;
- }
- .batch-photo-avatar {
- width: 80px;
- height: 121px;
- flex: 0 0 80px;
- overflow: hidden;
- border: 1px solid #e3e6ec;
- border-radius: 3px;
- background: #f1f3f6;
- }
- .batch-photo-avatar img {
- display: block;
- width: 100%;
- height: 100%;
- object-fit: cover;
- }
- .batch-photo-student-lines {
- min-width: 0;
- color: #555;
- font-size: 16px;
- line-height: 1.8;
- }
- .batch-photo-student-lines div {
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
- .batch-photo-right-divider {
- height: 1px;
- background: #e1e5ec;
- }
- .batch-photo-list-panel {
- display: flex;
- min-width: 0;
- min-height: 0;
- flex-direction: column;
- padding: 16px 18px 12px;
- box-sizing: border-box;
- }
- .batch-photo-list-header {
- display: flex;
- align-items: center;
- min-width: 0;
- gap: 10px;
- margin-bottom: 12px;
- padding: 0 8px;
- }
- .batch-photo-list-labels {
- display: grid;
- grid-template-columns: 34px auto;
- align-items: center;
- flex: 0 0 auto;
- gap: 12px;
- color: #4b5563;
- font-size: 17px;
- font-weight: 600;
- white-space: nowrap;
- }
- .batch-photo-list-index-label {
- text-align: center;
- }
- .batch-photo-list-name-label {
- overflow: hidden;
- text-overflow: ellipsis;
- }
- .batch-photo-progress {
- color: #6b7280;
- font-size: 14px;
- white-space: nowrap;
- margin-left: auto;
- }
- .batch-photo-search {
- flex: 0 1 200px;
- width: 200px;
- min-width: 180px;
- height: 32px;
- padding: 0 10px;
- border: 1px solid #d7dce5;
- border-radius: 4px;
- box-sizing: border-box;
- }
- .batch-photo-list {
- flex: 1;
- min-width: 0;
- min-height: 0;
- overflow-y: auto;
- border-top: 1px solid #e8ebf0;
- }
- .batch-photo-student-row {
- display: grid;
- min-width: 0;
- grid-template-columns: 34px max-content max-content 48px;
- align-items: center;
- gap: 12px;
- min-height: 42px;
- padding: 0 8px;
- border-radius: 4px;
- cursor: pointer;
- font-size: 14px;
- justify-content: start;
- }
- .batch-photo-student-row:hover {
- background: #eef4fb;
- }
- .batch-photo-student-row.active {
- background: #e0e7f0;
- }
- .batch-photo-student-row.completed .batch-photo-status {
- color: #16834b;
- }
- .batch-photo-student-row.failed .batch-photo-status {
- color: #d14343;
- }
- .batch-photo-index {
- color: #9ca3af;
- text-align: center;
- }
- .batch-photo-student-name-cell,
- .batch-photo-student-id-cell {
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
- .batch-photo-student-name-cell {
- color: #1f2937;
- font-weight: 600;
- }
- .batch-photo-student-id-cell {
- color: #6b7280;
- font-variant-numeric: tabular-nums;
- }
- .batch-photo-status {
- color: #9ca3af;
- font-size: 12px;
- text-align: right;
- white-space: nowrap;
- }
- .batch-photo-message {
- min-height: 24px;
- width: min(100%, 520px);
- padding-top: 8px;
- color: #6b7280;
- font-size: 14px;
- text-align: center;
- }
- .batch-photo-message.error {
- color: #c0392b;
- }
- @media (max-width: 900px) {
- .batch-photo-main {
- flex-basis: 55%;
- padding: 20px;
- }
- .batch-photo-right {
- flex-basis: 42%;
- min-width: 360px;
- }
- .batch-photo-student-card {
- margin: 20px 16px;
- }
- .batch-photo-list-panel {
- padding-right: 12px;
- padding-left: 12px;
- }
- .batch-photo-list-header {
- gap: 6px;
- padding: 0 4px;
- }
- .batch-photo-list-labels {
- grid-template-columns: 28px auto;
- gap: 12px;
- font-size: 15px;
- }
- .batch-photo-search {
- flex-basis: 160px;
- width: 160px;
- min-width: 160px;
- }
- .batch-photo-student-row {
- grid-template-columns: 28px max-content max-content 42px;
- gap: 12px;
- padding-right: 4px;
- padding-left: 4px;
- }
- }
- </style>
- </head>
- <body class="env-input-body">
- <form id="app" class="form-container" onsubmit="return false;">
- <input id="batchBjid" type="hidden" value="${bjid}">
- <div class="batch-photo-page" id="batchPhotoPage">
- <section class="batch-photo-main">
- <div class="batch-photo-toolbar">
- <select id="batchPhotoDeviceSelect" hidden></select>
- <button type="button" id="batchPhotoDeviceSwitch" hidden>切换摄像头</button>
- </div>
- <div class="batch-photo-preview-shell">
- <div class="batch-photo-preview" id="batchPhotoPreview">
- <div class="batch-photo-placeholder" id="batchPhotoPlaceholder">正在加载班级学生名单...</div>
- <video id="batchPhotoVideo" autoplay muted playsinline></video>
- <img id="batchPhotoImage" class="batch-photo-captured" alt="当前拍摄照片">
- <div class="batch-photo-guide" id="batchPhotoGuide"></div>
- <canvas id="batchPhotoCanvas"></canvas>
- </div>
- </div>
- <div class="batch-photo-action-bar" id="batchPhotoActions">
- <button type="button" id="batchPhotoConnectButton">连接摄像头</button>
- <button type="button" id="batchPhotoCaptureButton" hidden>拍照</button>
- <button type="button" id="batchPhotoRetakeButton" hidden>重新拍照</button>
- <button type="button" id="batchPhotoSaveButton" class="primary" hidden>保存并提交</button>
- </div>
- <div class="batch-photo-message" id="batchPhotoMessage">正在加载学生名单...</div>
- </section>
- <aside class="batch-photo-right">
- <section class="batch-photo-student-card">
- <div class="batch-photo-student-name" id="batchPhotoStudentName">--</div>
- <div class="batch-photo-student-body">
- <div class="batch-photo-avatar"><img id="batchPhotoStudentAvatar" alt="学生照片"></div>
- <div class="batch-photo-student-lines">
- <div id="batchPhotoStudentGender">性别:--</div>
- <div id="batchPhotoStudentStudy">就读方式:--</div>
- <div>班级:<span id="batchPhotoClassName">${bjmc}</span></div>
- <div id="batchPhotoStudentNumber">学号:--</div>
- </div>
- </div>
- </section>
- <div class="batch-photo-right-divider"></div>
- <section class="batch-photo-list-panel">
- <div class="batch-photo-list-header">
- <div class="batch-photo-list-labels">
- <div class="batch-photo-list-index-label">序号</div>
- <div class="batch-photo-list-name-label">姓名</div>
- </div>
- <div class="batch-photo-progress" id="batchPhotoProgress">0/0</div>
- <input class="batch-photo-search" id="batchPhotoSearch" type="text" placeholder="姓名/身份证号">
- </div>
- <div class="batch-photo-list" id="batchPhotoStudentList"></div>
- </section>
- </aside>
- </div>
- </form>
- <script>
- (function () {
- "use strict";
- var state = {
- mode: "loading",
- stream: null,
- students: [],
- currentIndex: -1,
- capturedBlob: null,
- capturedUrl: "",
- capturedRyid: null,
- saving: false,
- devices: [],
- requestSequence: 0,
- previewResizeObserver: null
- };
- var elements = {};
- var emptyImage = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='80' height='121'%3E%3Crect width='80' height='121' fill='%23f1f3f6'/%3E%3Ctext x='40' y='66' font-size='11' fill='%23999' text-anchor='middle'%3E无照片%3C/text%3E%3C/svg%3E";
- var batchPhotoWidth = 1000;
- var batchPhotoHeight = 1515;
- var batchPhotoRatio = batchPhotoWidth / batchPhotoHeight;
- function getElement(id) {
- return document.getElementById(id);
- }
- function initElements() {
- elements.bjid = getElement("batchBjid");
- elements.preview = getElement("batchPhotoPreview");
- elements.previewShell = elements.preview.parentNode;
- elements.placeholder = getElement("batchPhotoPlaceholder");
- elements.video = getElement("batchPhotoVideo");
- elements.image = getElement("batchPhotoImage");
- elements.guide = getElement("batchPhotoGuide");
- elements.canvas = getElement("batchPhotoCanvas");
- elements.connect = getElement("batchPhotoConnectButton");
- elements.capture = getElement("batchPhotoCaptureButton");
- elements.retake = getElement("batchPhotoRetakeButton");
- elements.save = getElement("batchPhotoSaveButton");
- elements.deviceSelect = getElement("batchPhotoDeviceSelect");
- elements.deviceSwitch = getElement("batchPhotoDeviceSwitch");
- elements.message = getElement("batchPhotoMessage");
- elements.name = getElement("batchPhotoStudentName");
- elements.avatar = getElement("batchPhotoStudentAvatar");
- elements.gender = getElement("batchPhotoStudentGender");
- elements.study = getElement("batchPhotoStudentStudy");
- elements.number = getElement("batchPhotoStudentNumber");
- elements.className = getElement("batchPhotoClassName");
- elements.progress = getElement("batchPhotoProgress");
- elements.list = getElement("batchPhotoStudentList");
- elements.search = getElement("batchPhotoSearch");
- }
- function resizePreview() {
- if (!elements.preview || !elements.previewShell) {
- return;
- }
- var availableWidth = elements.previewShell.clientWidth;
- var availableHeight = elements.previewShell.clientHeight;
- if (!availableWidth || !availableHeight) {
- return;
- }
- var maxWidth = 540;
- var width = Math.min(maxWidth, availableWidth, availableHeight * batchPhotoRatio);
- var height = width / batchPhotoRatio;
- elements.preview.style.width = width + "px";
- elements.preview.style.height = height + "px";
- }
- function watchPreviewResize() {
- resizePreview();
- if (window.ResizeObserver) {
- state.previewResizeObserver = new ResizeObserver(resizePreview);
- state.previewResizeObserver.observe(elements.previewShell);
- } else {
- window.addEventListener("resize", resizePreview);
- }
- }
- function setMessage(message, isError) {
- elements.message.textContent = message || "";
- elements.message.className = isError ? "batch-photo-message error" : "batch-photo-message";
- }
- function setPlaceholder(message) {
- elements.placeholder.textContent = message || "";
- elements.placeholder.style.display = "flex";
- }
- function hidePlaceholder() {
- elements.placeholder.style.display = "none";
- }
- function stopStream() {
- if (state.stream && state.stream.getTracks) {
- state.stream.getTracks().forEach(function (track) {
- track.stop();
- });
- }
- state.stream = null;
- elements.video.pause();
- elements.video.srcObject = null;
- }
- function revokeCapturedUrl() {
- if (state.capturedUrl) {
- URL.revokeObjectURL(state.capturedUrl);
- state.capturedUrl = "";
- }
- }
- function clearCapturedPhoto() {
- revokeCapturedUrl();
- state.capturedBlob = null;
- state.capturedRyid = null;
- elements.image.removeAttribute("src");
- }
- function setMode(mode) {
- state.mode = mode;
- var live = mode === "live";
- var captured = mode === "captured" || mode === "saveError";
- var saving = mode === "saving";
- var cameraError = mode === "cameraError";
- elements.connect.hidden = !(mode === "loading" || mode === "cameraDisconnected" || cameraError);
- elements.capture.hidden = !live;
- elements.retake.hidden = !captured;
- elements.save.hidden = !captured;
- elements.capture.disabled = saving;
- elements.retake.disabled = saving;
- elements.save.disabled = saving;
- elements.preview.classList.toggle("is-live", live);
- elements.video.style.display = live ? "block" : "none";
- elements.image.style.display = captured ? "block" : "none";
- elements.guide.style.display = live ? "block" : "none";
- if (live) {
- hidePlaceholder();
- } else if (!captured && !cameraError && mode !== "completed") {
- setPlaceholder(mode === "saving" ? "正在保存照片..." : "点击“连接摄像头”开始拍照");
- } else if (cameraError) {
- setPlaceholder("摄像头连接失败,请检查权限或设备后重试");
- }
- if (mode === "listError") {
- setPlaceholder("学生名单加载失败,请刷新页面后重试");
- } else if (mode === "completed") {
- setPlaceholder("全班学生证件照已完成,可从右侧选择学生补拍");
- }
- }
- function currentStudent() {
- return state.currentIndex < 0 ? null : state.students[state.currentIndex];
- }
- function imageUrl(path) {
- return path ? "/service?ssServ=dlByHttp&type=img&path=" + encodeURIComponent(path) : emptyImage;
- }
- function updateStudentCard() {
- var student = currentStudent();
- if (!student) {
- elements.name.textContent = "--";
- elements.avatar.src = emptyImage;
- elements.gender.textContent = "性别:--";
- elements.study.textContent = "就读方式:--";
- elements.number.textContent = "学号:--";
- return;
- }
- elements.name.textContent = student.xm || "--";
- var temporaryPhoto = state.capturedUrl && state.capturedRyid != null &&
- String(state.capturedRyid) === String(student.ryid) ? state.capturedUrl : "";
- elements.avatar.src = temporaryPhoto || imageUrl(student.zjzwj);
- elements.gender.textContent = "性别:" + (student.xbmc || student.xbm || "--");
- elements.study.textContent = "就读方式:" + (student.jdfsmc || student.jdfsm || "--");
- elements.number.textContent = "学号:" + (student.ryh || "--");
- }
- function updateProgress() {
- var completedCount = state.students.filter(function (student) { return !!student.sfypzjz; }).length;
- elements.progress.textContent = completedCount + "/" + state.students.length;
- }
- function renderList() {
- var keyword = (elements.search.value || "").trim().toLowerCase();
- elements.list.innerHTML = "";
- state.students.forEach(function (student, index) {
- var searchable = ((student.xm || "") + " " + (student.sfzh || "")).toLowerCase();
- if (keyword && searchable.indexOf(keyword) < 0) {
- return;
- }
- var row = document.createElement("div");
- row.className = "batch-photo-student-row";
- if (index === state.currentIndex) {
- row.classList.add("active");
- }
- if (student.sfypzjz) {
- row.classList.add("completed");
- }
- if (student.saveError) {
- row.classList.add("failed");
- }
- row.innerHTML = "<div class='batch-photo-index'></div><div class='batch-photo-student-name-cell'></div><div class='batch-photo-student-id-cell'></div><div class='batch-photo-status'></div>";
- row.children[0].textContent = String(index + 1);
- row.children[1].textContent = student.xm || "未命名学生";
- row.children[2].textContent = student.sfzh || "未录入身份证号";
- row.children[3].textContent = student.saveError ? "失败" : (student.sfypzjz ? "已拍" : "未拍");
- row.addEventListener("click", function () { selectStudent(index, false); });
- elements.list.appendChild(row);
- });
- updateProgress();
- }
- function scrollCurrentStudentIntoView() {
- if (!elements.list) {
- return;
- }
- var scroll = function () {
- var activeRow = elements.list.querySelector(".active");
- if (activeRow) {
- activeRow.scrollIntoView({block: "nearest"});
- }
- };
- if (window.requestAnimationFrame) {
- window.requestAnimationFrame(scroll);
- } else {
- scroll();
- }
- }
- function resetPreviewForStudent() {
- clearCapturedPhoto();
- var student = currentStudent();
- if (state.stream) {
- elements.image.style.display = "none";
- elements.video.style.display = "block";
- setMode("live");
- } else {
- elements.video.style.display = "none";
- setMode("cameraDisconnected");
- elements.image.src = imageUrl(student && student.zjzwj);
- elements.image.style.display = student && student.zjzwj ? "block" : "none";
- }
- }
- function selectStudent(index, skipConfirmation) {
- if (index < 0 || index >= state.students.length || index === state.currentIndex) {
- return;
- }
- if (!skipConfirmation && state.capturedBlob && !window.confirm("当前照片尚未保存,切换学生将放弃本次拍照,是否继续?")) {
- return;
- }
- state.currentIndex = index;
- updateStudentCard();
- renderList();
- resetPreviewForStudent();
- scrollCurrentStudentIntoView();
- }
- function firstIncompleteIndex() {
- for (var index = 0; index < state.students.length; index++) {
- if (!state.students[index].sfypzjz) {
- return index;
- }
- }
- return -1;
- }
- function parseJson(value) {
- if (typeof value === "string") {
- return JSON.parse(value);
- }
- return value;
- }
- function loadStudents() {
- var bjid = elements.bjid.value;
- fetch("/service?ssServ=xyZjz_batchListByA&bjid=" + encodeURIComponent(bjid), {credentials: "same-origin"})
- .then(function (response) { return response.text(); })
- .then(parseJson)
- .then(function (result) {
- if (!result || result.ssCode !== 0) {
- throw new Error(result && result.ssMsg ? result.ssMsg : "学生列表加载失败");
- }
- var data = result.ssData || {};
- state.students = Array.isArray(data.students) ? data.students : [];
- if (data.bjmc) {
- elements.className.textContent = data.bjmc;
- }
- var firstIndex = firstIncompleteIndex();
- state.currentIndex = firstIndex >= 0 ? firstIndex : (state.students.length ? 0 : -1);
- updateStudentCard();
- renderList();
- scrollCurrentStudentIntoView();
- if (firstIndex < 0) {
- setMode("completed");
- setMessage(state.students.length ? "当前班级学生证件照已全部完成" : "当前班级没有可拍照学生");
- } else {
- resetPreviewForStudent();
- setMessage("请连接摄像头后开始拍照");
- }
- })
- .catch(function (error) {
- setMode("listError");
- setMessage(error.message || "学生列表加载失败", true);
- });
- }
- function enumerateCameras() {
- if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
- throw new Error("当前浏览器不支持摄像头访问");
- }
- return navigator.mediaDevices.getUserMedia({video: true, audio: false})
- .then(function (permissionStream) {
- return navigator.mediaDevices.enumerateDevices().then(function (devices) {
- permissionStream.getTracks().forEach(function (track) { track.stop(); });
- state.devices = devices.filter(function (device) { return device.kind === "videoinput"; });
- if (!state.devices.length) {
- throw new Error("未检测到摄像头");
- }
- elements.deviceSelect.innerHTML = "";
- state.devices.forEach(function (device, index) {
- var option = document.createElement("option");
- option.value = device.deviceId;
- option.textContent = device.label || ("摄像头" + (index + 1));
- elements.deviceSelect.appendChild(option);
- });
- elements.deviceSelect.hidden = state.devices.length < 2;
- elements.deviceSwitch.hidden = state.devices.length < 2;
- return elements.deviceSelect.value || state.devices[0].deviceId;
- });
- });
- }
- function connectCamera() {
- if (state.mode === "saving") {
- return;
- }
- setMode("connecting");
- setMessage("正在请求摄像头权限...");
- enumerateCameras()
- .then(function (deviceId) {
- stopStream();
- return navigator.mediaDevices.getUserMedia({video: {deviceId: {exact: deviceId}}, audio: false});
- })
- .then(function (stream) {
- state.stream = stream;
- elements.video.srcObject = stream;
- return elements.video.play();
- })
- .then(function () {
- var videoTrack = state.stream && state.stream.getVideoTracks()[0];
- if (videoTrack) {
- videoTrack.addEventListener("ended", function () {
- stopStream();
- setMode("cameraError");
- setMessage("摄像头已断开,请重新连接", true);
- });
- }
- setMode("live");
- setMessage("请对准取景框,按 Enter 或点击“拍照”");
- })
- .catch(function (error) {
- stopStream();
- setMode("cameraError");
- setMessage(error.message || "摄像头连接失败", true);
- });
- }
- function capturePhoto() {
- var student = currentStudent();
- if (state.mode !== "live" || !state.stream || state.saving || !student) {
- return;
- }
- var videoWidth = elements.video.videoWidth;
- var videoHeight = elements.video.videoHeight;
- if (!videoWidth || !videoHeight) {
- setMessage("摄像头画面尚未准备好,请稍后重试", true);
- return;
- }
- var targetRatio = batchPhotoRatio;
- var sourceRatio = videoWidth / videoHeight;
- var cropWidth = sourceRatio > targetRatio ? Math.round(videoHeight * targetRatio) : videoWidth;
- var cropHeight = sourceRatio > targetRatio ? videoHeight : Math.round(videoWidth / targetRatio);
- var sourceX = Math.round((videoWidth - cropWidth) / 2);
- var sourceY = Math.round((videoHeight - cropHeight) / 2);
- elements.canvas.width = batchPhotoWidth;
- elements.canvas.height = batchPhotoHeight;
- elements.canvas.getContext("2d").drawImage(elements.video, sourceX, sourceY, cropWidth, cropHeight, 0, 0, batchPhotoWidth, batchPhotoHeight);
- elements.canvas.toBlob(function (blob) {
- if (!blob) {
- setMessage("照片截图失败,请重试", true);
- return;
- }
- clearCapturedPhoto();
- state.capturedBlob = blob;
- state.capturedUrl = URL.createObjectURL(blob);
- state.capturedRyid = student.ryid;
- elements.image.src = state.capturedUrl;
- updateStudentCard();
- setMode("captured");
- setMessage("请确认照片,按 Enter 或点击“保存并提交”");
- }, "image/jpeg", .92);
- }
- function retakePhoto() {
- if (state.saving) {
- return;
- }
- clearCapturedPhoto();
- updateStudentCard();
- if (state.stream) {
- setMode("live");
- setMessage("请对准取景框,按 Enter 或点击“拍照”");
- } else {
- setMode("cameraDisconnected");
- }
- }
- function uploadCapturedPhoto(blob) {
- var formData = new FormData();
- formData.append("application", "");
- formData.append("fileEdit", new File([blob], "batch_" + Date.now() + ".jpg", {type: "image/jpeg"}));
- return fetch("/service?ssServ=ulByHttp&type=img", {
- method: "POST",
- body: formData,
- credentials: "same-origin"
- }).then(function (response) { return response.text(); })
- .then(parseJson)
- .then(function (result) {
- var path = result && result.fileList && result.fileList[0] && result.fileList[0].path;
- if (!path) {
- throw new Error("照片上传后未返回文件路径");
- }
- return path;
- });
- }
- function savePhoto() {
- if (state.mode !== "captured" && state.mode !== "saveError") {
- return;
- }
- var student = currentStudent();
- if (!student || !state.capturedBlob || state.saving) {
- return;
- }
- state.saving = true;
- state.requestSequence += 1;
- var requestId = Date.now() + "_" + state.requestSequence;
- setMode("saving");
- setMessage("正在上传并处理照片...");
- uploadCapturedPhoto(state.capturedBlob)
- .then(function (path) {
- var body = new URLSearchParams();
- body.set("bjid", elements.bjid.value);
- body.set("ryid", String(student.ryid));
- body.set("zjzwj", path);
- body.set("requestId", requestId);
- return fetch("/service?ssServ=xyZjz_batchSaveByA", {
- method: "POST",
- headers: {"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"},
- body: body.toString(),
- credentials: "same-origin"
- }).then(function (response) { return response.text(); }).then(parseJson);
- })
- .then(function (result) {
- if (!result || result.ssCode !== 0) {
- throw new Error(result && result.ssMsg ? result.ssMsg : "照片保存失败");
- }
- var data = result.ssData || {};
- student.sfypzjz = true;
- if (data.savedPhoto) {
- student.zjzwj = data.savedPhoto;
- }
- student.saveError = false;
- clearCapturedPhoto();
- state.saving = false;
- renderList();
- updateStudentCard();
- if (data.allCompleted) {
- setMode("completed");
- setMessage("全班学生证件照已完成");
- return;
- }
- var nextIndex = state.students.findIndex(function (item) {
- return data.nextStudent && String(item.ryid) === String(data.nextStudent.ryid);
- });
- if (nextIndex < 0) {
- nextIndex = firstIncompleteIndex();
- }
- if (nextIndex >= 0) {
- state.currentIndex = nextIndex;
- updateStudentCard();
- renderList();
- scrollCurrentStudentIntoView();
- setMode(state.stream ? "live" : "cameraDisconnected");
- setMessage(state.stream ? "请对准取景框,按 Enter 或点击“拍照”" : "请重新连接摄像头");
- } else {
- setMode("completed");
- setMessage("全班学生证件照已完成");
- }
- })
- .catch(function (error) {
- state.saving = false;
- if (student) {
- student.saveError = true;
- }
- setMode("saveError");
- renderList();
- setMessage(error.message || "照片保存失败,请重试", true);
- });
- }
- function handleKeydown(event) {
- var target = event.target || {};
- var tagName = (target.tagName || "").toLowerCase();
- if (tagName === "input" || tagName === "textarea" || tagName === "select" || target.isContentEditable || event.repeat) {
- return;
- }
- if (event.key === "Enter") {
- event.preventDefault();
- if (state.mode === "live") {
- capturePhoto();
- } else if (state.mode === "captured" || state.mode === "saveError") {
- savePhoto();
- } else if (state.mode === "cameraDisconnected" || state.mode === "cameraError") {
- connectCamera();
- }
- } else if (event.key === "ArrowLeft" || event.key === "Left" || event.keyCode === 37 || event.which === 37) {
- if (state.mode === "captured" || state.mode === "saveError") {
- event.preventDefault();
- retakePhoto();
- }
- } else if (event.key === "ArrowUp" || event.key === "ArrowDown") {
- event.preventDefault();
- if (state.mode === "saving" || state.currentIndex < 0) {
- return;
- }
- var nextIndex = state.currentIndex + (event.key === "ArrowUp" ? -1 : 1);
- selectStudent(nextIndex, false);
- }
- }
- function initBatchPhotoPage() {
- elements = {};
- initElements();
- elements.connect.addEventListener("click", connectCamera);
- elements.capture.addEventListener("click", capturePhoto);
- elements.retake.addEventListener("click", retakePhoto);
- elements.save.addEventListener("click", savePhoto);
- elements.deviceSwitch.addEventListener("click", connectCamera);
- elements.search.addEventListener("input", renderList);
- document.addEventListener("keydown", handleKeydown);
- window.addEventListener("beforeunload", function (event) {
- if (state.capturedBlob) {
- event.preventDefault();
- event.returnValue = "当前照片尚未保存";
- }
- stopStream();
- if (state.previewResizeObserver) {
- state.previewResizeObserver.disconnect();
- }
- });
- watchPreviewResize();
- loadStudents();
- }
- if (window.SS && window.SS.__appMounted) {
- initBatchPhotoPage();
- } else {
- document.addEventListener("ssAppMounted", initBatchPhotoPage, {once: true});
- }
- })();
- </script>
- <%@ include file="/page/clip/footer.jsp" %>
- </body>
- </html>
|