index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <template>
  2. <div>
  3. <Transition :enter-active-class="proxy?.animate.mask.enter">
  4. <div class="mask" v-if="faceState"></div>
  5. </Transition>
  6. <Transition :enter-active-class="proxy?.animate.rankingWindow.enter"
  7. :leave-active-class="proxy?.animate.rankingWindow.leave">
  8. <div class="confirmDiaBg" v-if="faceState">
  9. <div class="confirmDiaWindow">
  10. <div class="confirmDiaWindow-con">
  11. <div class="name">
  12. 请举右手看摄像头进行人脸识别
  13. </div>
  14. <div class="pic">
  15. <img src="@/assets/images/common/onlineFace.png" />
  16. </div>
  17. </div>
  18. </div>
  19. <div @click="getCloseFace" class="close"></div>
  20. </div>
  21. </Transition>
  22. </div>
  23. </template>
  24. <script setup lang="ts">
  25. import { initSpeech, speckText, playMusic, controlMusic, speckCancel, chineseNumber } from '@/utils/speech';
  26. import { useWebSocket } from '@/utils/faceWs';
  27. const { proxy } = getCurrentInstance() as any;
  28. const router = useRouter();
  29. const { faceWs, startDevice, checkFace, openFace, terminateFace, suspendFace, resumeFace, getFaceState } = useWebSocket();
  30. const data = reactive<any>({
  31. faceCheckStu: {},//人脸信息
  32. faceState: false,//人脸识别窗口状态
  33. direction: "",//动画方向
  34. parameter: {},//参数
  35. deviceInfo: {},//设备信息
  36. againNum: 0,//再次启动次数
  37. againTimer: null,//定时状态
  38. wsState: false,//WS状态
  39. });
  40. const { faceCheckStu, faceState, direction, parameter, deviceInfo, againNum, againTimer, wsState } = toRefs(data);
  41. onBeforeMount(() => {
  42. })
  43. onMounted(() => {
  44. })
  45. /**
  46. * 初始化
  47. */
  48. const getInit = async () => {
  49. console.log("触发人脸识别")
  50. let deviceid = localStorage.getItem('deviceid') || '';
  51. if (!deviceid) {
  52. proxy?.$modal.msgError(`请重新登录绑定设备号后使用`);
  53. return false;
  54. }
  55. faceState.value = true;
  56. if (wsState.value) {
  57. proxy?.$modal.msgWarning(`操作过快,请稍后重试`);
  58. setTimeout(() => {
  59. faceState.value = false;
  60. }, 1000)
  61. return false;
  62. }
  63. speckText("请举右手看摄像头进行人脸识别");
  64. faceWs((e: any) => {
  65. console.log("faceWS", e)
  66. if (e?.wksid) {
  67. wsState.value = true;
  68. //获取设备信息
  69. startDevice({ deviceid: deviceid });
  70. console.log("获取设备信息")
  71. }
  72. if (e?.type == 'fe_device_init_result') {
  73. //接收设备信息并发送请求
  74. if (e?.device_info) {
  75. deviceInfo.value = e.device_info;
  76. getCheckFace();
  77. console.log("返回设备信息,检查是否支持人脸识别")
  78. } else {
  79. proxy?.$modal.msgError(`设备信息缺失,请重新登录绑定设备号后使用`);
  80. }
  81. }
  82. if (e?.cmd == 'check_facecontroller_available') {
  83. let handcontroller_id = deviceInfo.value.handcontroller_id;
  84. if (e?.code == 0) {
  85. //查看人脸识别状态,如果不处于关闭就先关闭再重新启动(可能会APP退出然后工作站还在运行的可能性)
  86. getFaceState(handcontroller_id);
  87. againNum.value = 0;
  88. againTimer.value = null;
  89. clearTimeout(againTimer.value);
  90. console.log("查看人脸识别状态")
  91. } else {
  92. //尝试多次查询人脸识别状态
  93. if (againNum.value <= 10) {
  94. againTimer.value = setTimeout(() => {
  95. getCheckFace();
  96. }, 100)
  97. againNum.value++;
  98. } else {
  99. let msg = "";
  100. if (e.code == 102402) {
  101. msg = `多次连接失败请重试,人脸识别模块被占用`;
  102. } else {
  103. msg = `多次连接失败请重试,人脸识别模块不可用,code:${e.code}`;
  104. }
  105. proxy?.$modal.msgWarning(msg);
  106. againNum.value = 0;
  107. againTimer.value = null;
  108. clearTimeout(againTimer.value);
  109. getCloseFace();//直接关闭
  110. }
  111. }
  112. }
  113. if (e?.cmd == 'get_facecontroller_state') {
  114. let handcontroller_id = deviceInfo.value.handcontroller_id;
  115. //state说明: 0:关闭,3:空闲,36:工作中
  116. if ([3, 36].includes(e.state)) {
  117. getCloseFace();
  118. proxy?.$modal.msgWarning(`请重新人脸识别`);
  119. } else {
  120. openFace(handcontroller_id);
  121. }
  122. }
  123. if (e?.type == 'facecontroller_result') {
  124. if (e?.data?.student_id) {
  125. //返回人脸信息然后跳转
  126. let id = e.data.student_id;
  127. let name = e.data.student_name;
  128. let pic = e.data.student_icon;
  129. let obj = { id, name, pic }
  130. faceCheckStu.value = obj;
  131. getCloseFace();
  132. router.push({ path: '/score', query: obj });
  133. }
  134. }
  135. if (e?.cmd == 'terminate_facecontroller') {
  136. if (e?.code == 0) {
  137. faceState.value = false;
  138. }
  139. }
  140. if (e?.type == 'disconnect') {
  141. wsState.value = false;
  142. }
  143. });
  144. };
  145. /**
  146. * 查询人脸识别状态
  147. */
  148. const getCheckFace = () => {
  149. let handcontroller_id = deviceInfo.value.handcontroller_id;
  150. //检查是否支持人脸识别
  151. checkFace(handcontroller_id);
  152. };
  153. /**
  154. * 关闭人脸识别
  155. */
  156. const getCloseFace = () => {
  157. let handcontroller_id = deviceInfo.value.handcontroller_id;
  158. terminateFace(handcontroller_id);
  159. faceState.value = false;
  160. speckCancel(); //停止播报
  161. };
  162. //暴露给父组件用
  163. defineExpose({
  164. getInit
  165. })
  166. </script>
  167. <style lang="scss" scoped>
  168. .mask {
  169. position: fixed;
  170. height: 100vh;
  171. width: 100vw;
  172. top: 0;
  173. left: 0;
  174. background: rgba(0, 0, 0, 0.8);
  175. z-index: 998;
  176. }
  177. .confirmDiaBg {
  178. width: 70rem;
  179. position: fixed;
  180. left: 50%;
  181. top: 50%;
  182. margin-left: calc(70rem / -2);
  183. margin-top: calc(((70rem / 2) + 3.2rem) / -2);
  184. display: flex;
  185. flex-direction: column;
  186. z-index: 999;
  187. .confirmDiaWindow {
  188. border-radius: 1.6rem;
  189. opacity: 1;
  190. text-align: center;
  191. display: flex;
  192. align-items: center;
  193. justify-content: center;
  194. margin-bottom: 20px;
  195. background: linear-gradient(62deg, #092941 -85%, #2A484B 96%);
  196. .confirmDiaWindow-con {
  197. padding: 25px;
  198. .pic {
  199. width: 100%;
  200. img {
  201. width: 100%;
  202. }
  203. }
  204. .name {
  205. width: 100%;
  206. color: #ffffff;
  207. font-size: 2.8vh;
  208. margin-bottom: 30px;
  209. }
  210. }
  211. }
  212. .close {
  213. margin: 0 auto;
  214. }
  215. }
  216. </style>