index.vue 17 KB

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