index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. }else{
  292. // this.toAdd(res.data[0].name)
  293. console.log('w')
  294. this.type = 3
  295. }
  296. }else{
  297. console.log('w')
  298. this.type = 3
  299. }
  300. })
  301. },
  302. // 人脸识别成功获取基础信息
  303. getBasics() {
  304. this.$request({
  305. url: this.$api.addHandle.student,
  306. data: {
  307. id: this.name,
  308. },
  309. }).then(res => {
  310. console.log(res)
  311. this.nameTwo = res.data.name
  312. })
  313. },
  314. // 关闭摄像头
  315. closeCamera() {
  316. if (!this.$refs["video"].srcObject) {
  317. this.dialogCamera = false;
  318. return;
  319. }
  320. this.this.intervalTakeImg = null
  321. let stream = this.$refs["video"].srcObject;
  322. let tracks = stream.getTracks();
  323. tracks.forEach((track) => {
  324. track.stop();
  325. });
  326. this.$refs["video"].srcObject = null;
  327. this.isnotbtn = false
  328. },
  329. // 刷新人脸
  330. refresh() {
  331. this.faceImgData = null
  332. this.type = 1
  333. this.callCamera()
  334. },
  335. //跳过
  336. skip() {
  337. console.log('跳过')
  338. this.toAdd()
  339. },
  340. // 确认
  341. confirm() {
  342. this.toAdd('w')
  343. },
  344. // 跳转新增
  345. toAdd() {
  346. uni.redirectTo({
  347. url: `/pages/addHandle/index?idt=${this.name}`
  348. })
  349. }
  350. }
  351. }
  352. </script>
  353. <style lang="scss" scoped>
  354. page {
  355. /* background-color: #D9D9D9; */
  356. // background-color: #F5F5F5 !important;
  357. overflow: hidden;
  358. }
  359. .title {
  360. margin-top: 126rpx;
  361. text-align: center;
  362. color: rgba(0, 0, 0, 0.90);
  363. font-size: 40rpx;
  364. }
  365. .img {
  366. width: 280rpx;
  367. height: 280rpx;
  368. border-radius: 100%;
  369. margin: 0 auto;
  370. margin-top: 40rpx;
  371. margin-bottom: 46rpx;
  372. overflow: hidden;
  373. position: relative;
  374. background-color: #ccc;
  375. .video {
  376. width: 380rpx;
  377. height: 380rpx;
  378. top: -50rpx;
  379. position: absolute;
  380. left: 50%;
  381. transform: translateX(-50%);
  382. }
  383. .image {
  384. width: 380rpx;
  385. height: 380rpx;
  386. top: -50rpx;
  387. position: absolute;
  388. left: 50%;
  389. transform: translateX(-50%);
  390. }
  391. }
  392. .reload {
  393. color: #6241D5;
  394. font-size: 40rpx;
  395. display: flex;
  396. justify-content: center;
  397. text {
  398. display: inline-block;
  399. position: relative;
  400. .ic {
  401. position: absolute;
  402. top: 10rpx;
  403. right: -60rpx;
  404. }
  405. }
  406. }
  407. .submit {
  408. margin-top: 580rpx;
  409. }
  410. .skip {
  411. text-align: center;
  412. margin-top: 26rpx;
  413. font-size: 32rpx;
  414. color: rgba(0, 0, 0, 0.90);
  415. }
  416. .active {
  417. background: #fff;
  418. }
  419. .imagbtn {
  420. display: flex;
  421. align-content: center;
  422. justify-content: start;
  423. }
  424. .face-bottom {
  425. position: absolute;
  426. bottom: 21px;
  427. background-color: #fff;
  428. left: 50%;
  429. transform: translateX(-50%);
  430. height: 41px;
  431. width: 306px;
  432. display: flex;
  433. justify-content: center;
  434. align-items: center;
  435. border-radius: 29px;
  436. font-size: 16px;
  437. color: black;
  438. img {
  439. margin-right: 11px;
  440. }
  441. }
  442. .querybtn {
  443. width: 50px;
  444. height: 50px;
  445. margin: auto 0;
  446. border-radius: 15px;
  447. /* font-size:30px; */
  448. }
  449. .face-data {
  450. width: 100%;
  451. height: 100%;
  452. display: flex;
  453. flex-direction: column;
  454. justify-content: center;
  455. align-items: center;
  456. font-size: 20px;
  457. .face-data-name {
  458. margin-top: 24px;
  459. margin-bottom: 48px;
  460. }
  461. }
  462. .btn {
  463. --hue: 190;
  464. --btn-bg-color: hsl(var(--hue), 100%, 50%);
  465. --btn-bg-color-darker: hsl(var(--hue), 100%, 45%);
  466. position: relative;
  467. padding: 0.75rem 1.5rem;
  468. margin: 1rem;
  469. font-size: 1rem;
  470. font-family: Lato, sans-serif;
  471. line-height: 1.5;
  472. color: white;
  473. text-decoration: none;
  474. background-color: var(--btn-bg-color);
  475. border: 1px solid var(--btn-bg-color);
  476. border-radius: 4px;
  477. 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);
  478. outline: transparent;
  479. overflow: hidden;
  480. cursor: pointer;
  481. user-select: none;
  482. white-space: nowrap;
  483. transition: 0.25s;
  484. }
  485. .btn-pink {
  486. --hue: 330;
  487. }
  488. .btn-bubbles {
  489. overflow: visible;
  490. transition: transform ease-in 0.1s, background-color ease-in 0.1s, box-shadow ease-in 0.25s;
  491. }
  492. .btn-bubbles::before {
  493. position: absolute;
  494. content: "";
  495. left: -2em;
  496. right: -2em;
  497. top: -2em;
  498. bottom: -2em;
  499. transition: ease-in-out 0.5s;
  500. background-repeat: no-repeat;
  501. 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%);
  502. 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%;
  503. 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%;
  504. animation: bubbles ease-in-out 0.75s forwards;
  505. }
  506. .btn-bubbles:active {
  507. transform: scale(0.95);
  508. background: var(--btn-bg-color-darker);
  509. }
  510. .btn-bubbles:active::before {
  511. animation: none;
  512. background-size: 0;
  513. }
  514. @keyframes bubbles {
  515. 0% {
  516. 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%;
  517. }
  518. 50% {
  519. 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%;
  520. }
  521. 100% {
  522. 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%;
  523. background-size: 0% 0%;
  524. }
  525. }
  526. .animation {
  527. position: absolute;
  528. top: 0px;
  529. left: 0px;
  530. width: 100%;
  531. height: 100%;
  532. background:
  533. linear-gradient(rgba(167, 223, 255, 0.63), rgba(167, 223, 255, 0.63)),
  534. /* linear-gradient(90deg, #ffffff33 1px,transparent 0,transparent 19px),
  535. linear-gradient( #ffffff33 1px,transparent 0,transparent 19px), */
  536. linear-gradient(transparent, rgba(167, 223, 255, 0.63));
  537. /* background: linear-gradient(170deg,rgba(167,223,255,0.63) 0%, rgba(251,254,111,0.00) 100%); */
  538. background-size: 100% 1.5%, 10% 100%, 100% 8%, 100% 100%;
  539. background-repeat: no-repeat, repeat, repeat, no-repeat;
  540. background-position: 0% 0%, 0 0, 0 0, 0 0;
  541. /* 初始位置 */
  542. clip-path: polygon(0% 0%, 100% 0%, 100% 1.5%, 0% 1.5%);
  543. /* 添加动画效果 */
  544. animation: move 2s infinite linear;
  545. /* animation-direction: alternate; */
  546. }
  547. @keyframes move {
  548. to {
  549. background-position: 0 100%, 0 0, 0 0, 0 0;
  550. /* 终止位置 */
  551. clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 0% 100%);
  552. }
  553. }
  554. .video .uni-video-cover {
  555. display: none !important
  556. }
  557. //播放按钮
  558. video::-webkit-media-controls-play-button {
  559. display: none;
  560. }
  561. //进度条
  562. video::-webkit-media-controls-timeline {
  563. display: none;
  564. }
  565. </style>