media.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. <template>
  2. <view>
  3. <u-popup v-model="isShow" mode="bottom" border-radius="40" :mask-close-able="false">
  4. <view class="photoBox" v-if="isShow">
  5. <view class="photoTop">
  6. <view class="centersq">请正视手机屏幕</view>
  7. </view>
  8. <view class="photoCenter">
  9. <view class="center_camera" v-if="isTaking">
  10. <!-- #ifdef MP-WEIXIN -->
  11. <camera device-position="front" flash="off" @error="error"
  12. style="width: 400px; height: 400px;margin: 0 auto;">
  13. <!-- 加人脸框 -->
  14. </camera>
  15. <!-- #endif -->
  16. <!-- #ifdef H5 -->
  17. <video :controls="false" v-show="status !== 3" id="video" width="400" height="400"
  18. class="photo_v"></video>
  19. <video id="video1" v-show="status === 3" class="photo_v2" autoplay controls :src="url"></video>
  20. <view class="mask" v-if="false"></view>
  21. <!-- #endif -->
  22. </view>
  23. <view class="custom" v-if="!isTaking">
  24. <!-- #ifdef MP-WEIXIN -->
  25. <image :src="avatarUrl" mode=""></image>
  26. <!-- #endif -->
  27. <!-- #ifdef H5 -->
  28. <image :src="faceUrl" mode=""></image>
  29. <!-- #endif -->
  30. </view>
  31. </view>
  32. <view class="btns">
  33. <!-- <view class="btnResult" v-if="isTaking" @click="takePhoto"
  34. >拍照</view
  35. > -->
  36. <view v-if="isTaking" class="takePhoto_btn">
  37. <view style="width: 100rpx; height: 2rpx"></view>
  38. <view class="">
  39. <button style="float: left;" :type="status === 3 ? 'primary' : 'primary'" class="pos"
  40. @click="luzhi" :disabled="status === 2">{{
  41. status === 1 ? "开始录制3秒" : status === 2 ? "录制中" : "提交"
  42. }}</button>
  43. <button @click="again" type="primary" style="float: left;
  44. margin-left: 20px;" v-if="status === 3">重录</button>
  45. <!-- <view class="btnResult" @click="luzhi()">重拍</view> -->
  46. <!-- <view class="square"></view> -->
  47. </view>
  48. <view class="rights" @click="takePhTips()">
  49. <text>抽查提示</text>
  50. <u-icon name="arrow-right" color="#FFFFFF" size="30"></u-icon>
  51. </view>
  52. </view>
  53. <view class="btnResult" v-if="!isTaking" @click="reTake">重拍</view>
  54. <view class="btnResult" v-if="!isTaking" @click="submit">确认</view>
  55. </view>
  56. </view>
  57. </u-popup>
  58. <!-- 播放前拍照end -->
  59. <u-popup v-model="showSet" :mask-close-able="false" mode="center" border-radius="24">
  60. <view style="
  61. align-items: center;
  62. padding: 0 40rpx;
  63. display: flex;
  64. flex-direction: column;
  65. justify-content: center;
  66. ">
  67. <view style="
  68. font-weight: bold;
  69. color: #333333;
  70. font-size: 30rpx;
  71. margin-top: 30rpx;
  72. ">温馨提示</view>
  73. <view style="
  74. width: 457rpx;
  75. color: #666666;
  76. font-size: 30rpx;
  77. margin-top: 30rpx;
  78. ">学习过程中需要拍照验证学员身份, 拍照功能需要使用您的相机。
  79. 是否授权使用?</view>
  80. <view style="margin: 40rpx 0">
  81. <button open-type="openSetting" @bindopensetting="openSetting" class="btnSet">
  82. 去授权
  83. </button>
  84. </view>
  85. </view>
  86. </u-popup>
  87. <!-- 拍照提示 -->
  88. <u-popup v-model="popupPhotoShow" mode="bottom" border-radius="30" :mask-close-able='false'>
  89. <view class="popup_box">
  90. <view class="head">
  91. <image src="/static/learn/photo_head.png" class="photo_head"></image>
  92. </view>
  93. <view class="warn">温馨提示</view>
  94. <view class="contents">
  95. <view class="words">1、本次为抽查检测,需要录制3秒的视频。</view>
  96. <view class="words">2、请保证摄像头正对自己,避免头像偏左或者偏右。</view>
  97. <view class="words">3、请保证拍照环境光线充足(照片太暗或曝光会降低验证通过率)。</view>
  98. <view class="words">4、请保证整个头像在人脸识别区域内,脸部无遮挡装饰物(佩戴眼镜会降低通过率)。</view>
  99. <view class="words">5、如果下面视频中出现黑屏,摄像头可能被其他进程占用,请关闭其他调用摄像头的程序,重新刷新当前页面重新拍照识别。</view>
  100. </view>
  101. <view class="take_photo" @click="openCamera">
  102. 去录像
  103. </view>
  104. </view>
  105. </u-popup>
  106. </view>
  107. </template>
  108. <script>
  109. import myCompressImage from "@/common/compressPhoto.js";
  110. import PopupPhoto from "./index.vue";
  111. export default {
  112. name: "SaasMiniprogramCamera",
  113. inject: ["paramsFn"],
  114. props: {
  115. visible: {
  116. type: Boolean,
  117. default: false,
  118. },
  119. },
  120. data() {
  121. return {
  122. isShow: false,
  123. isTaking: true,
  124. avatarUrl: "",
  125. isCameraAuth: false,
  126. showSet: false,
  127. popupPhotoShow: false,
  128. status: 1, //1未录制 2录制中 3录制完成
  129. takeVideoModal: false,
  130. mediaRecorder: null,
  131. videoblob: null,
  132. url: null,
  133. faceUrl: null,
  134. };
  135. },
  136. methods: {
  137. error(err) {
  138. console.log(err)
  139. },
  140. async submit() {
  141. if (this.uploadLock) {
  142. return;
  143. }
  144. this.uploadLock = true;
  145. let imgUrl = await this.imageInfos()
  146. let compareFaceData = await this.faceRecognition(imgUrl);
  147. if (compareFaceData.code === 200) {
  148. this.$emit("submitTakeVideo", this.videoblob);
  149. this.closeCamera();
  150. } else {
  151. uni.showToast({
  152. title: compareFaceData.msg,
  153. icon: "none",
  154. duration: 2000,
  155. });
  156. setTimeout(() => {
  157. if (!this.isTaking) {
  158. this.reTake();
  159. }
  160. }, 2000);
  161. }
  162. },
  163. async imageInfos() {
  164. let resPath = await myCompressImage(this.avatarUrl || this.faceUrl, 50);
  165. const waitUpload = await this.$method.uploadFile(resPath, 0);
  166. return waitUpload;
  167. },
  168. closeCamera() {
  169. this.isShow = false;
  170. this.showSet = false;
  171. this.popupPhotoShow = false;
  172. this.status=1;
  173. },
  174. openCamera() {
  175. console.log("openCamera");
  176. this.uploadLock = false;
  177. this.popupPhotoShow = false;
  178. // 同一个商品只谈一次提示
  179. // let popupList = uni.getStorageSync("popupList") || [];
  180. // if (!popupList.includes(this.goodsId)) {
  181. // popupList.push(this.goodsId);
  182. // uni.setStorageSync("popupList", popupList);
  183. // this.popupPhotoShow = true;
  184. // return;
  185. // }
  186. // #ifdef MP-WEIXIN
  187. // 屏幕亮度
  188. uni.setKeepScreenOn({
  189. keepScreenOn: true,
  190. });
  191. uni.getSetting({
  192. success: (res) => {
  193. if (res.authSetting["scope.camera"]) {
  194. this.isCameraAuth = true;
  195. this.showCamera();
  196. } else {
  197. wx.authorize({
  198. scope: "scope.camera",
  199. success: () => {
  200. this.isCameraAuth = true;
  201. this.showCamera();
  202. },
  203. fail: () => {
  204. this.isCameraAuth = false;
  205. this.showSet = true;
  206. },
  207. });
  208. }
  209. },
  210. fail: (res) => {},
  211. });
  212. // #endif
  213. // #ifdef H5
  214. if (
  215. (window.navigator.mediaDevices &&
  216. window.navigator.mediaDevices.getUserMedia) ||
  217. window.navigator.getUserMedia ||
  218. window.navigator.webkitGetUserMedia ||
  219. window.navigator.mozGetUserMedia
  220. ) {
  221. console.log("getUserMedia----");
  222. // 调用用户媒体设备, 访问摄像头
  223. this.getUserMedia({
  224. video: {
  225. width: 400,
  226. height: 400,
  227. facingMode: "user",
  228. },
  229. },
  230. this.photographSuccess,
  231. this.photographError
  232. );
  233. } else {
  234. console.log("1111没有摄像");
  235. this.photographError();
  236. }
  237. // #endif
  238. },
  239. showCamera() {
  240. this.isTaking = true;
  241. this.isShow = true;
  242. },
  243. //确认拍照
  244. takePhoto() {
  245. // #ifdef MP-WEIXIN
  246. const ctx = uni.createCameraContext();
  247. ctx.startRecord
  248. ctx.takePhoto({
  249. quality: "high",
  250. success: (res) => {
  251. this.avatarUrl = res.tempImagePath;
  252. console.log("开始拍照this.avatarUrl:", this.avatarUrl);
  253. this.isTaking = false;
  254. },
  255. fail: (err) => {},
  256. });
  257. // #endif
  258. // #ifdef H5
  259. this.luzhi();
  260. // const canvas = document.createElement("canvas");
  261. // canvas.width = 400;
  262. // canvas.height = 400;
  263. // const context = canvas.getContext("2d");
  264. // const box = document.querySelector(".photo_v");
  265. // const video = box.querySelector("video");
  266. // context.drawImage(video, 0, 0, 400, 400);
  267. // this.faceUrl = canvas.toDataURL("image/png");
  268. // this.isTaking = false;
  269. // #endif
  270. },
  271. takePhTips() {
  272. this.popupPhotoShow = true;
  273. this.isShow = false;
  274. this.isTaking = false;
  275. },
  276. /**
  277. * 人脸匹配
  278. */
  279. faceRecognition(url) {
  280. return new Promise((resolve) => {
  281. // // #ifdef MP-WEIXIN
  282. // let fileSystem = uni.getFileSystemManager();
  283. // fileSystem.readFile({
  284. // filePath: `${this.avatarUrl}`,
  285. // encoding: "base64",
  286. // position: 0,
  287. // success: (res) => {
  288. // let base64 = "data:image/jpg;base64," + res.data;
  289. // this.CompareFace(base64, resolve);
  290. // },
  291. // fail(err) {
  292. // // this.$u.toast('人脸识别错误!')
  293. // console.error(err, "err-----人脸识别错误");
  294. // },
  295. // });
  296. // // #endif
  297. // // #ifdef H5
  298. // this.CompareFace(this.faceUrl, resolve);
  299. // // #endif
  300. this.CompareFace(url, resolve);
  301. });
  302. },
  303. CompareFace(url, resolve) {
  304. let timer = setTimeout(() => {
  305. uni.showToast({
  306. icon: "none",
  307. title: "录像超时,请重新录制视频",
  308. duration: 2000,
  309. success: () => {
  310. setTimeout(() => {
  311. uni.navigateBack();
  312. }, 1000);
  313. },
  314. });
  315. }, 10 * 1000);
  316. this.$api
  317. .faceCertificationCompareFace({
  318. urlA: url,
  319. // imageA: url,
  320. orderGoodsId: this.params.orderGoodsId,
  321. gradeId: this.params.gradeId,
  322. })
  323. .then((res) => {
  324. clearTimeout(timer);
  325. console.log(res, "人脸识别成功res");
  326. resolve(res.data);
  327. })
  328. .catch((err) => {
  329. clearTimeout(timer);
  330. // 当前网络延迟,
  331. console.log("人脸识别错误:", err);
  332. uni.showModal({
  333. content: "当前网络延迟",
  334. showCancel: false,
  335. success: (resultst) => {
  336. if (resultst.confirm) {
  337. uni.navigateBack();
  338. }
  339. },
  340. });
  341. });
  342. },
  343. photographSuccess(stream) {
  344. console.log("有摄像头---", stream);
  345. this.isCameraAuth = true;
  346. this.showCamera();
  347. this.$nextTick(() => {
  348. const box = document.querySelector(".photo_v");
  349. const video = box.querySelector("video");
  350. video.srcObject = stream;
  351. this.mediaRecorder = new MediaRecorder(stream);
  352. video.play();
  353. });
  354. },
  355. photographError(err) {
  356. console.log("没有摄像头:", err);
  357. uni.showModal({
  358. title: "提示",
  359. content: "课程学习需要开启摄像头进行拍照,经检测您的设备无摄像头可使用,请检测环境是否支持。",
  360. cancelText: "取消",
  361. confirmText: "确定",
  362. showCancel: false,
  363. success: (res) => {
  364. if (res.confirm) {
  365. uni.navigateBack();
  366. } else if (res.cancel) {}
  367. },
  368. });
  369. },
  370. getUserMedia(constraints, success, error) {
  371. console.log("getUserMedia===", constraints);
  372. if (window.navigator.mediaDevices.getUserMedia) {
  373. // 最新的标准API
  374. window.navigator.mediaDevices
  375. .getUserMedia(constraints)
  376. .then(success)
  377. .catch(error);
  378. } else if (window.navigator.webkitGetUserMedia) {
  379. // webkit核心浏览器
  380. window.navigator.webkitGetUserMedia(constraints, success, error);
  381. } else if (window.navigator.mozGetUserMedia) {
  382. // firfox浏览器
  383. window.navigator.mozGetUserMedia(constraints, success, error);
  384. } else if (window.navigator.getUserMedia) {
  385. // 旧版API
  386. window.navigator.getUserMedia(constraints, success, error);
  387. }
  388. },
  389. again() {
  390. this.url = null;
  391. this.status = 1;
  392. },
  393. luzhi() {
  394. if (this.status === 3) {
  395. //完成提交
  396. this.submit();
  397. return;
  398. }
  399. this.onCatchPhoto();
  400. this.mediaRecorder.start();
  401. this.status = 2;
  402. setTimeout(() => {
  403. this.stop();
  404. this.status = 3;
  405. }, 3500);
  406. },
  407. //拍照
  408. onCatchPhoto() {
  409. const canvas = document.createElement("canvas");
  410. canvas.width = 400;
  411. canvas.height = 400;
  412. const context = canvas.getContext("2d");
  413. const box = document.querySelector(".photo_v");
  414. const video = box.querySelector("video");
  415. context.drawImage(video, 0, 0, 400, 400);
  416. this.faceUrl = canvas.toDataURL("image/png");
  417. // console.log(this.faceUrl,'faceUrl')
  418. },
  419. stop() {
  420. this.mediaRecorder.stop();
  421. this.mediaRecorder.ondataavailable = (event) => {
  422. //this.$message.success("录制完成");
  423. // console.log(event.data, 'data')
  424. // 尝试转换为mp4类型
  425. const blob = event.data.slice(0, event.data.size, "video/mp4");
  426. this.url = URL.createObjectURL(blob);
  427. this.videoblob = blob;
  428. console.log(event.data, 'url')
  429. // const file = new window.File([blob], "videoss.mp4", {
  430. // type: 'video/mp4',
  431. // });
  432. // console.log(file, 'file')
  433. };
  434. },
  435. },
  436. computed: {
  437. params() {
  438. return this.paramsFn();
  439. },
  440. goodsId() {
  441. return this.params.goodsId;
  442. },
  443. },
  444. components: {
  445. PopupPhoto,
  446. },
  447. watch: {
  448. visible(val) {
  449. if (val) {
  450. this.takePhTips()//this.openCamera();
  451. } else {
  452. this.closeCamera();
  453. }
  454. },
  455. },
  456. };
  457. </script>
  458. <style lang="scss" scoped>
  459. .btnSet {
  460. width: 440rpx;
  461. height: 80rpx;
  462. background: #007aff;
  463. border-radius: 40rpx;
  464. color: #ffffff;
  465. font-size: 28rpx;
  466. line-height: 80rpx;
  467. }
  468. .photoBox {
  469. width: 100%;
  470. .photoTop {
  471. width: 100%;
  472. height: 74rpx;
  473. border-radius: 20px 20px 0px 0px;
  474. background-color: #ffffff;
  475. display: flex;
  476. align-items: center;
  477. justify-content: center;
  478. padding: 0rpx 38rpx;
  479. .centersq {
  480. color: #333;
  481. font-size: 30rpx;
  482. font-weight: 500;
  483. }
  484. }
  485. .photoCenter {
  486. width: 750rpx;
  487. height: 75vh;
  488. position: relative;
  489. .center_camera {
  490. width: 100%;
  491. height: 75vh;
  492. position: fixed;
  493. background-color: rgba(0, 0, 0, .8);
  494. .head_take {
  495. width: 100%;
  496. height: 75vh;
  497. display: flex;
  498. flex-direction: column;
  499. }
  500. .headTake_up {
  501. width: 100%;
  502. height: 100rpx;
  503. }
  504. .headTake_minddle {
  505. display: flex;
  506. .min_img {
  507. width: 500rpx;
  508. height: 550rpx;
  509. }
  510. .min_left,
  511. .min_right {
  512. flex: 1;
  513. height: 550rpx;
  514. }
  515. }
  516. .headTake_down {
  517. width: 100%;
  518. flex: 1;
  519. }
  520. .color {
  521. background-color: #333;
  522. opacity: 0.5;
  523. }
  524. .photo_v {
  525. display: block;
  526. margin: 0 auto;
  527. width: 400px;
  528. height: 400px;
  529. }
  530. .photo_v2 {
  531. display: block;
  532. margin: 0 auto;
  533. width: 400px;
  534. height: 400px;
  535. }
  536. .mask {
  537. width: 500rpx;
  538. height: 550rpx;
  539. position: absolute;
  540. top: 100rpx;
  541. left: 0;
  542. right: 0;
  543. bottom: 0;
  544. margin: 0 auto;
  545. box-shadow: 0 0 0 2000px rgba(0, 0, 0, 0.4);
  546. }
  547. }
  548. .custom {
  549. width: 750rpx;
  550. height: 75vh;
  551. position: absolute;
  552. z-index: 1000;
  553. top: 0;
  554. left: 0;
  555. background-color: rgba(0, 0, 0, .8);
  556. image {
  557. display: block;
  558. margin: 0 auto;
  559. width: 400px;
  560. height: 400px;
  561. }
  562. }
  563. }
  564. .btns {
  565. display: flex;
  566. .takePhoto_btn {
  567. width: 100%;
  568. display: flex;
  569. align-items: center;
  570. justify-content: space-between;
  571. background: #a9a7a9;
  572. padding: 40rpx 26rpx;
  573. .middle_btn {
  574. width: 120rpx;
  575. height: 120rpx;
  576. border-radius: 40rpx;
  577. border: 4rpx solid #ffffff;
  578. display: flex;
  579. align-items: center;
  580. justify-content: center;
  581. }
  582. .square {
  583. width: 96rpx;
  584. height: 96rpx;
  585. background: #ffffff;
  586. border-radius: 28rpx;
  587. }
  588. .rights {
  589. font-size: 32rpx;
  590. font-weight: 500;
  591. color: #ffffff;
  592. }
  593. }
  594. .btnResult {
  595. height: 100rpx;
  596. flex: 1;
  597. background-color: #07c160;
  598. text-align: center;
  599. line-height: 100rpx;
  600. color: #fff;
  601. font-size: 32rpx;
  602. font-weight: bold;
  603. }
  604. }
  605. }
  606. .popup_box {
  607. width: 100%;
  608. height: 1100rpx;
  609. display: flex;
  610. align-items: center;
  611. flex-direction: column;
  612. padding: 0rpx 40rpx;
  613. .photo_head {
  614. width: 240rpx;
  615. height: 240rpx;
  616. margin: 80rpx 0rpx 48rpx;
  617. }
  618. .warn {
  619. width: 100%;
  620. font-size: 36rpx;
  621. font-weight: bold;
  622. color: #222222;
  623. margin-bottom: 40rpx;
  624. }
  625. .words {
  626. font-size: 28rpx;
  627. color: #666666;
  628. line-height: 33rpx;
  629. margin-bottom: 24rpx;
  630. }
  631. .take_photo {
  632. width: 412rpx;
  633. height: 84rpx;
  634. line-height: 84rpx;
  635. text-align: center;
  636. background: #01C65A;
  637. border-radius: 16rpx;
  638. font-size: 36rpx;
  639. font-weight: 500;
  640. color: #FFFFFF;
  641. margin-top: 100rpx;
  642. }
  643. }
  644. </style>