FaceId.vue 10 KB

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