index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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">
  12. <text>{{type == 1 ? '识别中' : type == 2 ? this.nameTwo : '重新识别'}}
  13. <u-icon class="ic" name="reload" color="#8C8C8C" size="40rpx" @click="refresh"></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. if(platform == 'ios') {
  151. this.test()
  152. return
  153. }
  154. //初始化摄像头参数
  155. if (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia) {
  156. navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator
  157. .mozGetUserMedia;
  158. navigator.getUserMedia(videoObj, (stream) => {
  159. this.isnotbtn = true
  160. // 摄像头开启成功
  161. this.$refs["video"].srcObject = stream;
  162. // // 实时拍照效果
  163. this.$refs["video"].play();
  164. this.show = true
  165. this.type = 1
  166. this.test()
  167. this.photograph()
  168. setTimeout(() => {
  169. // debugger
  170. if (!this.faceData && !this.image) {
  171. _this.closeCamera()
  172. _this.show = false
  173. clearInterval(_this.intervalTakeImg)
  174. _this.intervalTakeImg = null
  175. _this.$emit('timeOutLogin')
  176. }
  177. // this.noLogin()
  178. }, 15000)
  179. }, (err) => {
  180. console.log("Video capture error: ", err.code);
  181. this.show = false
  182. this.$emit('err')
  183. });
  184. }
  185. },
  186. test() {
  187. var video = document.querySelector('video');
  188. // 兼容代码
  189. window.URL = (window.URL || window.webkitURL || window.mozURL || window.msURL);
  190. if (navigator.mediaDevices === undefined) {
  191. navigator.mediaDevices = {};
  192. }
  193. if (navigator.mediaDevices.getUserMedia === undefined) {
  194. navigator.mediaDevices.getUserMedia = function(constraints) {
  195. var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator
  196. .msGetUserMedia;
  197. if (!getUserMedia) {
  198. return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
  199. }
  200. return new Promise(function(resolve, reject) {
  201. getUserMedia.call(navigator, constraints, resolve, reject);
  202. });
  203. }
  204. }
  205. //摄像头调用配置
  206. var mediaOpts = {
  207. audio: false,
  208. video: {
  209. facingMode: "user"
  210. }
  211. }
  212. let that = this;
  213. navigator.mediaDevices.getUserMedia(mediaOpts).then(function(stream) {
  214. that.mediaStreamTrack = stream;
  215. video = document.querySelector('video');
  216. if ("srcObject" in video) {
  217. video.srcObject = stream
  218. } else {
  219. video.src = window.URL && window.URL.createObjectURL(stream) || stream
  220. this.canvasCtx.drawImage(window.URL.createObjectURL(stream), 0, 0, 520, 520);
  221. }
  222. video.play();
  223. that.type = 1
  224. that.photograph();
  225. }).catch(function(err) {
  226. console.log(err)
  227. });
  228. },
  229. // 拍照
  230. photograph() {
  231. // let ctx = document.createElement('canvas').getContext("2d")
  232. let ctx = uni.createCanvasContext('cvs', this);
  233. // let ctx = this.$refs.canvas.getContext("2d");
  234. const _this = this
  235. this.intervalTakeImg = setInterval(() => {
  236. // 把当前视频帧内容渲染到canvas上
  237. // ctx.drawImage(document.createElement('canvas'), 0, 0, 520, 520);
  238. html2Cvs(document.querySelector('video'), {
  239. backgroundColor: null,
  240. useCORS: true
  241. }).then(cvs => {
  242. let imgData = cvs.toDataURL('image/png')
  243. this.faceImgData = imgData
  244. // this.type = 2
  245. this.faceRecognition()
  246. // this.intervalTakeImg = null
  247. // clearInterval(this.intervalTakeImg)
  248. // ctx.drawImage(imgData, 0, 0, 520, 520);
  249. })
  250. // 转base64格式、图片格式转换、图片质量压缩
  251. // console.log('canvas', document.createElement('canvas').toDataURL("image/jpeg", 0.7))
  252. // return
  253. let imgBase64 = document.createElement('canvas').toDataURL("image/jpeg", 0.7); // 由字节转换为KB 判断大小
  254. // let imgBase64 = this.$refs["canvas"].toDataURL("image/jpeg", 0.7); // 由字节转换为KB 判断大小
  255. // console.log('canver', this.$refs["canvas"])
  256. // console.log('%cHealthEduOnline.vue line:92 imgBase64', 'color: white; background-color: #26bfa5;', imgBase64);
  257. let str = imgBase64.replace("data:image/jpeg;base64,", "");
  258. let strLength = str.length;
  259. let fileLength = parseInt(strLength - (strLength / 8) * 2); // 图片尺寸 用于判断
  260. let size = (fileLength / 1024).toFixed(2);
  261. console.log(size); // 上传拍照信息 调用接口上传图片 .........
  262. // 保存到本地
  263. this.dialogCamera = false;
  264. // this.faceRecognition()
  265. clearInterval(this.intervalTakeImg)
  266. }, 4000)
  267. // let ADOM = document.createElement("a");
  268. // ADOM.href = imgBase64 ;
  269. // ADOM.download = new Date().getTime() + ".jpeg";
  270. // ADOM.click();
  271. },
  272. // 上传人脸识别图片
  273. faceRecognition() {
  274. this.$request({
  275. url: '/face_check/faceRecognition',
  276. header: {
  277. contentType: "application/json"
  278. },
  279. data: {
  280. image: this.faceImgData,
  281. },
  282. method: 'POST',
  283. }).then(res => {
  284. console.log(res)
  285. if(res.data.length != 0){
  286. if(res.data[0].name != null){
  287. // this.type = 3
  288. this.type = 2
  289. this.name = res.data[0].name
  290. this.getBasics()
  291. return
  292. }else{
  293. // this.toAdd(res.data[0].name)
  294. console.log('w')
  295. this.type = 3
  296. return
  297. }
  298. }
  299. console.log('w')
  300. this.type = 3
  301. })
  302. },
  303. // 人脸识别成功获取基础信息
  304. getBasics() {
  305. this.$request({
  306. url: this.$api.addHandle.student,
  307. data: {
  308. id: this.name,
  309. },
  310. }).then(res => {
  311. console.log(res)
  312. this.nameTwo = res.data.name
  313. })
  314. },
  315. // 关闭摄像头
  316. closeCamera() {
  317. if (!this.$refs["video"].srcObject) {
  318. this.dialogCamera = false;
  319. return;
  320. }
  321. this.this.intervalTakeImg = null
  322. let stream = this.$refs["video"].srcObject;
  323. let tracks = stream.getTracks();
  324. tracks.forEach((track) => {
  325. track.stop();
  326. });
  327. this.$refs["video"].srcObject = null;
  328. this.isnotbtn = false
  329. },
  330. // 刷新人脸
  331. refresh() {
  332. this.faceImgData = null
  333. this.type = 1
  334. this.callCamera()
  335. },
  336. //跳过
  337. skip() {
  338. console.log('跳过')
  339. this.toAdd()
  340. },
  341. // 确认
  342. confirm() {
  343. this.toAdd('w')
  344. },
  345. // 跳转新增
  346. toAdd() {
  347. uni.redirectTo({
  348. url: `/pages/addHandle/index?idt=${this.name}`
  349. })
  350. }
  351. }
  352. }
  353. </script>
  354. <style lang="scss" scoped>
  355. page {
  356. /* background-color: #D9D9D9; */
  357. // background-color: #F5F5F5 !important;
  358. overflow: hidden;
  359. }
  360. .title {
  361. margin-top: 126rpx;
  362. text-align: center;
  363. color: rgba(0, 0, 0, 0.90);
  364. font-size: 40rpx;
  365. }
  366. .img {
  367. width: 280rpx;
  368. height: 280rpx;
  369. border-radius: 100%;
  370. margin: 0 auto;
  371. margin-top: 40rpx;
  372. margin-bottom: 46rpx;
  373. overflow: hidden;
  374. position: relative;
  375. background-color: #ccc;
  376. .video {
  377. width: 380rpx;
  378. height: 380rpx;
  379. top: -50rpx;
  380. position: absolute;
  381. left: 50%;
  382. transform: translateX(-50%);
  383. }
  384. .image {
  385. width: 380rpx;
  386. height: 380rpx;
  387. top: -50rpx;
  388. position: absolute;
  389. left: 50%;
  390. transform: translateX(-50%);
  391. }
  392. }
  393. .reload {
  394. color: #6241D5;
  395. font-size: 40rpx;
  396. display: flex;
  397. justify-content: center;
  398. text {
  399. display: inline-block;
  400. position: relative;
  401. .ic {
  402. position: absolute;
  403. top: 10rpx;
  404. right: -60rpx;
  405. }
  406. }
  407. }
  408. .submit {
  409. margin-top: 580rpx;
  410. }
  411. .skip {
  412. text-align: center;
  413. margin-top: 26rpx;
  414. font-size: 32rpx;
  415. color: rgba(0, 0, 0, 0.90);
  416. }
  417. .active {
  418. background: #fff;
  419. }
  420. .imagbtn {
  421. display: flex;
  422. align-content: center;
  423. justify-content: start;
  424. }
  425. .face-bottom {
  426. position: absolute;
  427. bottom: 21px;
  428. background-color: #fff;
  429. left: 50%;
  430. transform: translateX(-50%);
  431. height: 41px;
  432. width: 306px;
  433. display: flex;
  434. justify-content: center;
  435. align-items: center;
  436. border-radius: 29px;
  437. font-size: 16px;
  438. color: black;
  439. img {
  440. margin-right: 11px;
  441. }
  442. }
  443. .querybtn {
  444. width: 50px;
  445. height: 50px;
  446. margin: auto 0;
  447. border-radius: 15px;
  448. /* font-size:30px; */
  449. }
  450. .face-data {
  451. width: 100%;
  452. height: 100%;
  453. display: flex;
  454. flex-direction: column;
  455. justify-content: center;
  456. align-items: center;
  457. font-size: 20px;
  458. .face-data-name {
  459. margin-top: 24px;
  460. margin-bottom: 48px;
  461. }
  462. }
  463. .btn {
  464. --hue: 190;
  465. --btn-bg-color: hsl(var(--hue), 100%, 50%);
  466. --btn-bg-color-darker: hsl(var(--hue), 100%, 45%);
  467. position: relative;
  468. padding: 0.75rem 1.5rem;
  469. margin: 1rem;
  470. font-size: 1rem;
  471. font-family: Lato, sans-serif;
  472. line-height: 1.5;
  473. color: white;
  474. text-decoration: none;
  475. background-color: var(--btn-bg-color);
  476. border: 1px solid var(--btn-bg-color);
  477. border-radius: 4px;
  478. 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);
  479. outline: transparent;
  480. overflow: hidden;
  481. cursor: pointer;
  482. user-select: none;
  483. white-space: nowrap;
  484. transition: 0.25s;
  485. }
  486. .btn-pink {
  487. --hue: 330;
  488. }
  489. .btn-bubbles {
  490. overflow: visible;
  491. transition: transform ease-in 0.1s, background-color ease-in 0.1s, box-shadow ease-in 0.25s;
  492. }
  493. .btn-bubbles::before {
  494. position: absolute;
  495. content: "";
  496. left: -2em;
  497. right: -2em;
  498. top: -2em;
  499. bottom: -2em;
  500. transition: ease-in-out 0.5s;
  501. background-repeat: no-repeat;
  502. 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%);
  503. 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%;
  504. 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%;
  505. animation: bubbles ease-in-out 0.75s forwards;
  506. }
  507. .btn-bubbles:active {
  508. transform: scale(0.95);
  509. background: var(--btn-bg-color-darker);
  510. }
  511. .btn-bubbles:active::before {
  512. animation: none;
  513. background-size: 0;
  514. }
  515. @keyframes bubbles {
  516. 0% {
  517. 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%;
  518. }
  519. 50% {
  520. 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%;
  521. }
  522. 100% {
  523. 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%;
  524. background-size: 0% 0%;
  525. }
  526. }
  527. .animation {
  528. position: absolute;
  529. top: 0px;
  530. left: 0px;
  531. width: 100%;
  532. height: 100%;
  533. background:
  534. linear-gradient(rgba(167, 223, 255, 0.63), rgba(167, 223, 255, 0.63)),
  535. /* linear-gradient(90deg, #ffffff33 1px,transparent 0,transparent 19px),
  536. linear-gradient( #ffffff33 1px,transparent 0,transparent 19px), */
  537. linear-gradient(transparent, rgba(167, 223, 255, 0.63));
  538. /* background: linear-gradient(170deg,rgba(167,223,255,0.63) 0%, rgba(251,254,111,0.00) 100%); */
  539. background-size: 100% 1.5%, 10% 100%, 100% 8%, 100% 100%;
  540. background-repeat: no-repeat, repeat, repeat, no-repeat;
  541. background-position: 0% 0%, 0 0, 0 0, 0 0;
  542. /* 初始位置 */
  543. clip-path: polygon(0% 0%, 100% 0%, 100% 1.5%, 0% 1.5%);
  544. /* 添加动画效果 */
  545. animation: move 2s infinite linear;
  546. /* animation-direction: alternate; */
  547. }
  548. @keyframes move {
  549. to {
  550. background-position: 0 100%, 0 0, 0 0, 0 0;
  551. /* 终止位置 */
  552. clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 0% 100%);
  553. }
  554. }
  555. .video .uni-video-cover {
  556. display: none !important
  557. }
  558. //播放按钮
  559. video::-webkit-media-controls-play-button {
  560. display: none;
  561. }
  562. //进度条
  563. video::-webkit-media-controls-timeline {
  564. display: none;
  565. }
  566. </style>