index.vue 18 KB

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