index.vue 18 KB

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