index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. <template>
  2. <view>
  3. <view class="title" >
  4. {{type == 1 ? '正在识别中,请看向摄像头!' : type == 2 ? '人脸识别成功' : '人脸识别失败'}}
  5. </view>
  6. <view class="img">{
  7. <video id="video_1" ref="video" class="video" stylautoplay
  8. :show-center-play-btn="false"
  9. :show-play-btn="false"
  10. :controls="false"></video>
  11. <image v-show="faceImgData" :src="faceImgData" class="image"></image>
  12. </view>
  13. <view class="reload">
  14. <text>{{type == 3? '重新识别' : '识别成功'}} <u-icon class="ic" name="reload" color="#8C8C8C" size="40rpx" @click="refresh"></u-icon></text>
  15. </view>
  16. <view class="reload">
  17. <!-- <image v-show="faceImgData" :src="faceImgData" style="width: 100px;height: 100px;"></image> -->
  18. <!-- <canvas ref="canvas" canvas-id="cvs" v-show="true" width="520" height="520"></canvas> -->
  19. <!-- <text>Messis <u-icon class="ic" name="reload" color="#8C8C8C" size="40rpx"></u-icon></text> -->
  20. </view>
  21. <view class="submit">
  22. <u-button :disabled="name == ''" shape="circle" type="primary" text="确定" color="#7B5DF0" style="width: 550rpx;" @click="toAdd">
  23. </u-button>
  24. </view>
  25. <view class="skip">
  26. <text @click="skip">跳过</text>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. import html2Cvs from 'html2canvas'
  32. export default {
  33. data() {
  34. return {
  35. isnotbtn: true,
  36. isnotcanvas: false,
  37. intervalTakeImg: null,
  38. image: '',
  39. faceData: '',
  40. faceImgData: '',
  41. proxy: null,
  42. show: false,
  43. msg: '',
  44. canvasCtx: null,
  45. type: '1',
  46. name: '', // 人脸识别成功数据
  47. }
  48. },
  49. mounted() {
  50. this.canvasCtx = uni.createCanvasContext('cvs', this);
  51. if (window.frames.length != parent.frames.length) {
  52. this.iFramePostMessage()
  53. window.addEventListener('message', (msg) => { //接受数据
  54. alert(msg)
  55. console.log(msg)
  56. const _this = this
  57. this.msg = JSON.stringify(msg.data)
  58. if (msg.data.isIframe) {}
  59. if (msg.data.userId) {
  60. _this.isnotbtn = false
  61. // _this.image = imgBase64
  62. Api.faceData(msg.data.userId)
  63. .then(reslove => {
  64. // console.log('%cHealthEduOnline.vue line:115 reslove', 'color: white; background-color: #26bfa5;', reslove);
  65. if (reslove.data.data === null) {
  66. // imgBase64 = ''
  67. // _this.noLogin()
  68. _this.$emit('timeOutLogin')
  69. this.show = false
  70. return
  71. }
  72. _this.faceData = reslove.data.data
  73. this.inLogin()
  74. this.show = false
  75. // _this.closeCamera()
  76. })
  77. .catch(err => {
  78. this.show = false
  79. // console.log('%cHealthEduOnline.vue line:200 err', 'color: white; background-color: #26bfa5;', err);
  80. })
  81. // clearInterval(_this.intervalTakeImg)
  82. // _this.intervalTakeImg = null
  83. }
  84. }, false)
  85. } else {
  86. this.callCamera()
  87. }
  88. },
  89. methods: {
  90. //开启融梦人脸识别
  91. iFramePostMessage(name = 'toFaceLogin') { //调用父页面方法
  92. window.parent.postMessage(name, '*')
  93. },
  94. inLogin() {
  95. Api.getToken({
  96. name: this.faceData.name,
  97. sn: this.faceData.childNo,
  98. gender: this.faceData.sex == 1 ? '男' : '女',
  99. grant_type: 'student'
  100. }).then((res) => {
  101. if (res.data != undefined) {
  102. window.localStorage.setItem('Blade-Auth', res.data.access_token)
  103. Api.getStudentInfo(res.data.user_id).then((user) => {
  104. if (user.data && user.data.code == 200 && user.data.data) {
  105. this.$store.dispatch('setUserInfo', user.data.data)
  106. window.localStorage.setItem('userInfo', JSON.stringify(user.data.data))
  107. const userSig = genTestUserSig(user.data.data.id).userSig
  108. // this.$cookies.set('userId',)
  109. const userId = user.data.data.id
  110. window.localStorage.setItem('userId', userId)
  111. window.localStorage.setItem('userSig', userSig)
  112. this.$store.commit('userLoginSuccess')
  113. this.$store.commit('setLoginUserInfo', {
  114. userId,
  115. userSig
  116. })
  117. Api.loginSuccess(getCookie('SCREENID')).then(res => {
  118. window.location.reload()
  119. })
  120. .catch(err => {
  121. window.location.reload()
  122. })
  123. // 登录 trtcCalling
  124. // console.log(this.proxy);
  125. this.$emit('successLogin')
  126. } else {
  127. localStorage.clear()
  128. }
  129. })
  130. }
  131. }).catch(ss => {})
  132. },
  133. /**
  134. * 重新识别
  135. */
  136. noLogin() {
  137. this.faceData = ''
  138. this.image = ''
  139. this.callCamera()
  140. },
  141. callCamera() {
  142. const _this = this
  143. var videoObj = {
  144. "video": true
  145. };
  146. //初始化摄像头参数
  147. if (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia) {
  148. navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator
  149. .mozGetUserMedia;
  150. navigator.getUserMedia(videoObj, (stream) => {
  151. this.isnotbtn = true
  152. // 摄像头开启成功
  153. this.$refs["video"].srcObject = stream;
  154. // // 实时拍照效果
  155. this.$refs["video"].play();
  156. this.show = true
  157. this.type = 1
  158. this.test()
  159. this.photograph()
  160. setTimeout(() => {
  161. // debugger
  162. if (!this.faceData && !this.image) {
  163. _this.closeCamera()
  164. _this.show = false
  165. clearInterval(_this.intervalTakeImg)
  166. _this.intervalTakeImg = null
  167. _this.$emit('timeOutLogin')
  168. }
  169. // this.noLogin()
  170. }, 15000)
  171. }, (err) => {
  172. console.log("Video capture error: ", err.code);
  173. this.show = false
  174. this.$emit('err')
  175. });
  176. }
  177. },
  178. test() {
  179. var video = document.querySelector('video');
  180. // 兼容代码
  181. window.URL = (window.URL || window.webkitURL || window.mozURL || window.msURL);
  182. if (navigator.mediaDevices === undefined) {
  183. navigator.mediaDevices = {};
  184. }
  185. if (navigator.mediaDevices.getUserMedia === undefined) {
  186. navigator.mediaDevices.getUserMedia = function(constraints) {
  187. var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator
  188. .msGetUserMedia;
  189. if (!getUserMedia) {
  190. return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
  191. }
  192. return new Promise(function(resolve, reject) {
  193. getUserMedia.call(navigator, constraints, resolve, reject);
  194. });
  195. }
  196. }
  197. //摄像头调用配置
  198. var mediaOpts = {
  199. audio: false,
  200. video: {
  201. facingMode: "user"
  202. }
  203. }
  204. let that = this;
  205. navigator.mediaDevices.getUserMedia(mediaOpts).then(function(stream) {
  206. that.mediaStreamTrack = stream;
  207. video = document.querySelector('video');
  208. if ("srcObject" in video) {
  209. video.srcObject = stream
  210. } else {
  211. video.src = window.URL && window.URL.createObjectURL(stream) || stream
  212. this.canvasCtx.drawImage(window.URL.createObjectURL(stream), 0, 0, 520, 520);
  213. }
  214. video.play();
  215. this.photograph();
  216. }).catch(function(err) {
  217. console.log(err)
  218. });
  219. },
  220. // 拍照
  221. photograph() {
  222. // let ctx = document.createElement('canvas').getContext("2d")
  223. let ctx = uni.createCanvasContext('cvs', this);
  224. // let ctx = this.$refs.canvas.getContext("2d");
  225. const _this = this
  226. this.intervalTakeImg = setInterval(() => {
  227. // 把当前视频帧内容渲染到canvas上
  228. // ctx.drawImage(document.createElement('canvas'), 0, 0, 520, 520);
  229. html2Cvs(document.querySelector('video'),{
  230. backgroundColor: null,
  231. useCORS: true
  232. }).then(cvs=>{
  233. let imgData = cvs.toDataURL('image/png')
  234. this.faceImgData = imgData
  235. this.type = 2
  236. this.faceRecognition()
  237. // this.intervalTakeImg = null
  238. // clearInterval(this.intervalTakeImg)
  239. // ctx.drawImage(imgData, 0, 0, 520, 520);
  240. })
  241. // 转base64格式、图片格式转换、图片质量压缩
  242. // console.log('canvas', document.createElement('canvas').toDataURL("image/jpeg", 0.7))
  243. // return
  244. let imgBase64 = document.createElement('canvas').toDataURL("image/jpeg", 0.7); // 由字节转换为KB 判断大小
  245. // let imgBase64 = this.$refs["canvas"].toDataURL("image/jpeg", 0.7); // 由字节转换为KB 判断大小
  246. // console.log('canver', this.$refs["canvas"])
  247. // console.log('%cHealthEduOnline.vue line:92 imgBase64', 'color: white; background-color: #26bfa5;', imgBase64);
  248. let str = imgBase64.replace("data:image/jpeg;base64,", "");
  249. let strLength = str.length;
  250. let fileLength = parseInt(strLength - (strLength / 8) * 2); // 图片尺寸 用于判断
  251. let size = (fileLength / 1024).toFixed(2);
  252. console.log(size); // 上传拍照信息 调用接口上传图片 .........
  253. // 保存到本地
  254. this.dialogCamera = false;
  255. // this.faceRecognition()
  256. clearInterval(this.intervalTakeImg)
  257. }, 4000)
  258. // let ADOM = document.createElement("a");
  259. // ADOM.href = imgBase64 ;
  260. // ADOM.download = new Date().getTime() + ".jpeg";
  261. // ADOM.click();
  262. },
  263. // 上传人脸识别图片
  264. faceRecognition(){
  265. this.$request({
  266. url: '/face_check/faceRecognition',
  267. header: {
  268. contentType: "application/json"
  269. },
  270. data: {
  271. image: this.faceImgData,
  272. },
  273. method: 'POST',
  274. }).then(res => {
  275. console.log(res)
  276. if(res.data[0].name == null){
  277. this.type = 3
  278. }else{
  279. // this.toAdd(res.data[0].name)
  280. this.name = res.data[0].name
  281. }
  282. })
  283. },
  284. // 关闭摄像头
  285. closeCamera() {
  286. if (!this.$refs["video"].srcObject) {
  287. this.dialogCamera = false;
  288. return;
  289. }
  290. this.this.intervalTakeImg = null
  291. let stream = this.$refs["video"].srcObject;
  292. let tracks = stream.getTracks();
  293. tracks.forEach((track) => {
  294. track.stop();
  295. });
  296. this.$refs["video"].srcObject = null;
  297. this.isnotbtn = false
  298. },
  299. // 刷新人脸
  300. refresh(){
  301. this.faceImgData = null
  302. this.callCamera()
  303. },
  304. //跳过
  305. skip() {
  306. console.log('跳过')
  307. this.toAdd()
  308. },
  309. // 确认
  310. confirm() {
  311. this.toAdd('w')
  312. },
  313. // 跳转新增
  314. toAdd() {
  315. uni.navigateTo({
  316. url: `/pages/addHandle/index?id=${this.name}`
  317. })
  318. }
  319. }
  320. }
  321. </script>
  322. <style lang="scss" scoped>
  323. page {
  324. /* background-color: #D9D9D9; */
  325. // background-color: #F5F5F5 !important;
  326. }
  327. .title {
  328. margin-top: 126rpx;
  329. text-align: center;
  330. color: rgba(0, 0, 0, 0.90);
  331. font-size: 40rpx;
  332. }
  333. .img {
  334. width: 280rpx;
  335. height: 280rpx;
  336. border-radius: 100%;
  337. margin: 0 auto;
  338. margin-top: 40rpx;
  339. margin-bottom: 46rpx;
  340. overflow: hidden;
  341. position: relative;
  342. background-color: #ccc;
  343. .video{
  344. width: 380rpx;
  345. height: 380rpx;
  346. top: -50rpx;
  347. position: absolute;
  348. left: 50%;
  349. transform: translateX(-50%);
  350. }
  351. .image{
  352. width: 370rpx;
  353. height: 280rpx;
  354. position: absolute;
  355. top: 0rpx;
  356. left: 50%;
  357. transform: translateX(-50%);
  358. }
  359. }
  360. .reload {
  361. color: #6241D5;
  362. font-size: 40rpx;
  363. display: flex;
  364. justify-content: center;
  365. text {
  366. display: inline-block;
  367. position: relative;
  368. .ic {
  369. position: absolute;
  370. top: 10rpx;
  371. right: -60rpx;
  372. }
  373. }
  374. }
  375. .submit {
  376. margin-top: 580rpx;
  377. }
  378. .skip {
  379. text-align: center;
  380. margin-top: 26rpx;
  381. font-size: 32rpx;
  382. color: rgba(0, 0, 0, 0.90);
  383. }
  384. .active {
  385. background: #fff;
  386. }
  387. .imagbtn {
  388. display: flex;
  389. align-content: center;
  390. justify-content: start;
  391. }
  392. .face-bottom {
  393. position: absolute;
  394. bottom: 21px;
  395. background-color: #fff;
  396. left: 50%;
  397. transform: translateX(-50%);
  398. height: 41px;
  399. width: 306px;
  400. display: flex;
  401. justify-content: center;
  402. align-items: center;
  403. border-radius: 29px;
  404. font-size: 16px;
  405. color: black;
  406. img {
  407. margin-right: 11px;
  408. }
  409. }
  410. .querybtn {
  411. width: 50px;
  412. height: 50px;
  413. margin: auto 0;
  414. border-radius: 15px;
  415. /* font-size:30px; */
  416. }
  417. .face-data {
  418. width: 100%;
  419. height: 100%;
  420. display: flex;
  421. flex-direction: column;
  422. justify-content: center;
  423. align-items: center;
  424. font-size: 20px;
  425. .face-data-name {
  426. margin-top: 24px;
  427. margin-bottom: 48px;
  428. }
  429. }
  430. .btn {
  431. --hue: 190;
  432. --btn-bg-color: hsl(var(--hue), 100%, 50%);
  433. --btn-bg-color-darker: hsl(var(--hue), 100%, 45%);
  434. position: relative;
  435. padding: 0.75rem 1.5rem;
  436. margin: 1rem;
  437. font-size: 1rem;
  438. font-family: Lato, sans-serif;
  439. line-height: 1.5;
  440. color: white;
  441. text-decoration: none;
  442. background-color: var(--btn-bg-color);
  443. border: 1px solid var(--btn-bg-color);
  444. border-radius: 4px;
  445. box-shadow: 0 0.1px 0.7px rgba(233, 30, 99, 0.141), 0 0.1px 1.7px rgba(233, 30, 99, 0.202), 0 0.3px 3.1px rgba(233, 30, 99, 0.25), 0 0.4px 5.6px rgba(233, 30, 99, 0.298), 0 0.8px 10.4px rgba(233, 30, 99, 0.359), 0 2px 25px rgba(233, 30, 99, 0.5);
  446. outline: transparent;
  447. overflow: hidden;
  448. cursor: pointer;
  449. user-select: none;
  450. white-space: nowrap;
  451. transition: 0.25s;
  452. }
  453. .btn-pink {
  454. --hue: 330;
  455. }
  456. .btn-bubbles {
  457. overflow: visible;
  458. transition: transform ease-in 0.1s, background-color ease-in 0.1s, box-shadow ease-in 0.25s;
  459. }
  460. .btn-bubbles::before {
  461. position: absolute;
  462. content: "";
  463. left: -2em;
  464. right: -2em;
  465. top: -2em;
  466. bottom: -2em;
  467. transition: ease-in-out 0.5s;
  468. background-repeat: no-repeat;
  469. background-image: radial-gradient(circle, var(--btn-bg-color) 20%, transparent 20%), radial-gradient(circle, var(--btn-bg-color) 20%, transparent 20%), radial-gradient(circle, transparent 20%, var(--btn-bg-color) 20%, transparent 30%), radial-gradient(circle, var(--btn-bg-color) 20%, transparent 20%), radial-gradient(circle, var(--btn-bg-color) 20%, transparent 20%), radial-gradient(circle, var(--btn-bg-color) 20%, transparent 20%), radial-gradient(circle, var(--btn-bg-color) 20%, transparent 20%), radial-gradient(circle, transparent 20%, var(--btn-bg-color) 20%, transparent 30%), radial-gradient(circle, transparent 20%, var(--btn-bg-color) 20%, transparent 30%), radial-gradient(circle, var(--btn-bg-color) 20%, transparent 20%), radial-gradient(circle, var(--btn-bg-color) 20%, transparent 20%), radial-gradient(circle, transparent 20%, var(--btn-bg-color) 20%, transparent 30%), radial-gradient(circle, transparent 20%, var(--btn-bg-color) 20%, transparent 30%), radial-gradient(circle, var(--btn-bg-color) 20%, transparent 20%), radial-gradient(circle, var(--btn-bg-color) 20%, transparent 20%), radial-gradient(circle, var(--btn-bg-color) 20%, transparent 20%);
  470. background-size: 13% 14%, 15% 19%, 11% 19%, 17% 14%, 16% 20%, 11% 18%, 16% 10%, 19% 17%, 18% 19%, 18% 18%, 15% 11%, 17% 19%, 17% 11%, 16% 16%, 16% 15%, 20% 11%;
  471. background-position: 18% 40%, 20% 31%, 30% 30%, 40% 30%, 50% 30%, 57% 30%, 65% 30%, 80% 32%, 15% 60%, 83% 60%, 18% 70%, 25% 70%, 41% 70%, 50% 70%, 64% 70%, 80% 71%;
  472. animation: bubbles ease-in-out 0.75s forwards;
  473. }
  474. .btn-bubbles:active {
  475. transform: scale(0.95);
  476. background: var(--btn-bg-color-darker);
  477. }
  478. .btn-bubbles:active::before {
  479. animation: none;
  480. background-size: 0;
  481. }
  482. @keyframes bubbles {
  483. 0% {
  484. background-position: 18% 40%, 20% 31%, 30% 30%, 40% 30%, 50% 30%, 57% 30%, 65% 30%, 80% 32%, 15% 60%, 83% 60%, 18% 70%, 25% 70%, 41% 70%, 50% 70%, 64% 70%, 80% 71%;
  485. }
  486. 50% {
  487. background-position: 10% 44%, 0% 20%, 15% 5%, 30% 0%, 42% 0%, 62% -2%, 75% 0%, 95% -2%, 0% 80%, 95% 55%, 7% 100%, 24% 100%, 41% 100%, 55% 95%, 68% 96%, 95% 100%;
  488. }
  489. 100% {
  490. background-position: 5% 44%, -5% 20%, 7% 5%, 23% 0%, 37% 0, 58% -2%, 80% 0%, 100% -2%, -5% 80%, 100% 55%, 2% 100%, 23% 100%, 42% 100%, 60% 95%, 70% 96%, 100% 100%;
  491. background-size: 0% 0%;
  492. }
  493. }
  494. .animation {
  495. position: absolute;
  496. top: 0px;
  497. left: 0px;
  498. width: 100%;
  499. height: 100%;
  500. background:
  501. linear-gradient(rgba(167, 223, 255, 0.63), rgba(167, 223, 255, 0.63)),
  502. /* linear-gradient(90deg, #ffffff33 1px,transparent 0,transparent 19px),
  503. linear-gradient( #ffffff33 1px,transparent 0,transparent 19px), */
  504. linear-gradient(transparent, rgba(167, 223, 255, 0.63));
  505. /* background: linear-gradient(170deg,rgba(167,223,255,0.63) 0%, rgba(251,254,111,0.00) 100%); */
  506. background-size: 100% 1.5%, 10% 100%, 100% 8%, 100% 100%;
  507. background-repeat: no-repeat, repeat, repeat, no-repeat;
  508. background-position: 0% 0%, 0 0, 0 0, 0 0;
  509. /* 初始位置 */
  510. clip-path: polygon(0% 0%, 100% 0%, 100% 1.5%, 0% 1.5%);
  511. /* 添加动画效果 */
  512. animation: move 2s infinite linear;
  513. /* animation-direction: alternate; */
  514. }
  515. @keyframes move {
  516. to {
  517. background-position: 0 100%, 0 0, 0 0, 0 0;
  518. /* 终止位置 */
  519. clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 0% 100%);
  520. }
  521. }
  522. .video .uni-video-cover{
  523. display: none !important
  524. }
  525. //播放按钮
  526. video::-webkit-media-controls-play-button {
  527. display: none;
  528. }
  529. //进度条
  530. video::-webkit-media-controls-timeline {
  531. display: none;
  532. }
  533. </style>