FaceId.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <div class="row" style="margin: 0;">
  3. <div class="col-md-12 text-center" style="padding:8px;">
  4. <div style="font-size: 30px;">
  5. <span>人脸检测</span>
  6. <span v-if="showIframe">({{timeCount}})</span>
  7. </div>
  8. <div v-if="showIframe" class="text-center" style="color: red; font-size: 16px;">
  9. (注意:请点击下方“开始比对”按钮并在60秒内完成人脸检测,超时将退出考试)
  10. </div>
  11. </div>
  12. <div id="faceIdDiv" style="position: relative;
  13. height:620px; background-color: #6e6f72!important;
  14. background-image: radial-gradient(circle at 50% 0,#a9a9a9,#34363c);">
  15. <div v-show="!showIframe" width="100%" height="200px" style="text-align: center;line-height:100px;margin-top:5px;">
  16. <div style="color:white;font-weight: bold;font-size:20px;">
  17. {{redoBtnMsg}}
  18. </div>
  19. <button v-if="redoBtnShow" type="button" class="qm-primary-button" :disabled="redoBtnDisabled" @click="startFaceVerify">重试</button>
  20. </div>
  21. <iframe v-show="showIframe" allow="camera *" allowusermedia id="myFrame" scrolling="no" width="100%" height="620px" frameborder="0"></iframe>
  22. </div>
  23. </div>
  24. </template>
  25. <script>
  26. import {
  27. FACEID_LINENESS_URL,
  28. VUE_APP_WK_SERVER_SOCKET
  29. } from "@/constants/constants.js";
  30. export default {
  31. name: "FaceId",
  32. data() {
  33. return {
  34. showIframe: false,
  35. redoBtnShow: false,
  36. timeCount: 60,
  37. redoBtnMsg: ""
  38. };
  39. },
  40. mounted() {
  41. console.debug("startFaceVerify");
  42. this.startFaceVerify();
  43. },
  44. methods: {
  45. showRedo(redoMsg) {
  46. this.showIframe = false;
  47. this.redoBtnDisabled = false;
  48. this.redoBtnShow = true;
  49. if (redoMsg) {
  50. this.redoBtnMsg = redoMsg;
  51. } else {
  52. this.redoBtnMsg = "系统繁忙,请手动点击重试";
  53. }
  54. },
  55. updateFaceVerify(errorMsg, redoMsg) {
  56. this.showRedo(redoMsg);
  57. this.$http.get(
  58. "/api/ecs_oe_student/examFaceLivenessVerify/updateFaceLivenessVerify/" +
  59. this.$route.params.examRecordDataId,
  60. { params: { errorMsg: errorMsg } }
  61. );
  62. },
  63. checkIframeOnload() {
  64. var iframe = document.getElementById("myFrame");
  65. if (!iframe) {
  66. return null;
  67. }
  68. var app = iframe.contentWindow.document.getElementById("app");
  69. if (app) {
  70. return "success";
  71. } else {
  72. var preLabel = iframe.contentWindow.document.getElementsByTagName(
  73. "pre"
  74. )[0];
  75. if (
  76. preLabel &&
  77. preLabel.innerText &&
  78. preLabel.innerText.indexOf("error_message") > -1
  79. ) {
  80. return preLabel.innerText;
  81. }
  82. }
  83. return null;
  84. },
  85. iframeLoadSuccess() {
  86. try {
  87. var iframe = document.getElementById("myFrame");
  88. var app = iframe.contentWindow.document.getElementById("app");
  89. app.querySelector(".footer").remove();
  90. } catch (err) {
  91. console.error(err);
  92. }
  93. const examRecordId = this.$route.params.examRecordDataId;
  94. this.timeCount = 60; //人脸检测倒计时60秒
  95. var timeCountInterval = setInterval(() => {
  96. --this.timeCount;
  97. if (this.timeCount === 0) {
  98. clearInterval(timeCountInterval);
  99. }
  100. }, 1000);
  101. this.logout = () => {
  102. this.$router.push("/login/" + localStorage.getItem("domain"));
  103. };
  104. //定时事件,如果1分钟内未完成人脸检测,执行内部程序
  105. var faceIdTime = setTimeout(() => {
  106. ws.close();
  107. var that = this;
  108. this.$http
  109. .get(
  110. "/api/ecs_oe_student/examFaceLivenessVerify/faceLivenessVerifyTimeOut/" +
  111. examRecordId
  112. )
  113. .then(function success(response) {
  114. if (response.status == 200) {
  115. var receivedMsg = response.data;
  116. that.faceTestEnd(receivedMsg);
  117. }
  118. })
  119. .catch(() => {
  120. clearInterval(timeCountInterval);
  121. this.logout();
  122. });
  123. }, 60000); //60000
  124. /**
  125. * 人脸检测结果返回后台处理
  126. */
  127. this.faceTestEndHandle = result => {
  128. this.$http
  129. .get(
  130. "/api/ecs_oe_student/examFaceLivenessVerify/faceLivenessVerifyEnd/" +
  131. examRecordId +
  132. "?result=" +
  133. result
  134. )
  135. .then(() => {
  136. if (result != "SUCCESS") {
  137. this.logout();
  138. }
  139. });
  140. };
  141. /**
  142. * 人脸检测结束
  143. */
  144. this.faceTestEnd = receivedMsg => {
  145. if (receivedMsg.verifyCount == 1) {
  146. if (receivedMsg.verifyResult == "TIME_OUT") {
  147. this.$Message.error({
  148. content: "第一次人脸检测超时,检测失败,系统退出,请重新登录",
  149. duration: 15
  150. });
  151. this.logout();
  152. } else if (receivedMsg.verifyResult == "VERIFY_FAILED") {
  153. this.$Message.error({
  154. content: "第一次人脸检测失败,系统退出,请重新登录",
  155. duration: 15
  156. });
  157. this.logout();
  158. } else if (receivedMsg.verifyResult == "NOT_ONESELF") {
  159. this.$Message.error({
  160. content: "人脸检测不合格,结束考试",
  161. duration: 15
  162. });
  163. this.faceTestEndHandle("FAILED");
  164. } else if (receivedMsg.verifyResult == "VERIFY_SUCCESS") {
  165. this.$Message.info({
  166. content: "人脸检测成功,请继续完成考试",
  167. duration: 15
  168. });
  169. this.faceTestEndHandle("SUCCESS");
  170. }
  171. } else if (receivedMsg.verifyCount >= 2) {
  172. if (receivedMsg.verifyResult == "VERIFY_SUCCESS") {
  173. // FIXME: 什么逻辑?
  174. this.$Message.info({
  175. content: "人脸检测成功,请继续完成考试",
  176. duration: 15
  177. });
  178. this.faceTestEndHandle("SUCCESS");
  179. } else {
  180. this.$Message.error({
  181. content: "人脸检测不合格,结束考试",
  182. duration: 15
  183. });
  184. this.faceTestEndHandle("FAILED");
  185. }
  186. }
  187. };
  188. // open websocket
  189. var ws = new WebSocket(VUE_APP_WK_SERVER_SOCKET + examRecordId);
  190. ws.onopen = function() {
  191. console.log("websocket已连接");
  192. };
  193. ws.onmessage = response => {
  194. if (response.data.indexOf("verifyResult") > -1) {
  195. var receivedMsg = JSON.parse(response.data);
  196. this.faceTestEnd(receivedMsg);
  197. clearTimeout(faceIdTime);
  198. this.$emit("closeFaceId");
  199. ws.close();
  200. }
  201. };
  202. ws.onclose = function() {
  203. console.log("websocket连接已关闭...");
  204. };
  205. },
  206. async startFaceVerify() {
  207. this.redoBtnDisabled = true;
  208. this.redoBtnMsg = "正在进入人脸检测...";
  209. const examRecordId = this.$route.params.examRecordDataId;
  210. const response = await this.$http.get(
  211. "/api/ecs_oe_student/examFaceLivenessVerify/getFaceLivenessVerifyToken/" +
  212. examRecordId
  213. );
  214. if (!response.data.success) {
  215. console.log(response.data.errorMsg);
  216. this.$Message.error({
  217. content: "您上传的底照不适合做活体检测,请联系老师!",
  218. duration: 15
  219. });
  220. return;
  221. }
  222. this.showIframe = true;
  223. var iframe = document.getElementById("myFrame");
  224. try {
  225. iframe.src = FACEID_LINENESS_URL + response.data.faceLivenessToken;
  226. } catch (err) {
  227. console.error(err);
  228. }
  229. var index = 0;
  230. var iframeLoadTime = setInterval(() => {
  231. var iframeLoadMsg = this.checkIframeOnload();
  232. if (!iframeLoadMsg) {
  233. index++;
  234. if (index == 20) {
  235. //检测达到20次
  236. clearInterval(iframeLoadTime);
  237. this.showRedo("网络异常,请手动点击重试");
  238. }
  239. } else if (iframeLoadMsg.indexOf("error_message") > -1) {
  240. clearInterval(iframeLoadTime);
  241. this.updateFaceVerify(iframeLoadMsg, null);
  242. } else if (iframeLoadMsg == "success") {
  243. clearInterval(iframeLoadTime);
  244. // this.iframeLoadSuccess();
  245. setTimeout(() => {
  246. this.iframeLoadSuccess();
  247. }, 300); // 延迟确保能删除footer
  248. }
  249. }, 500);
  250. }
  251. }
  252. };
  253. </script>