media.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. },
  173. openCamera() {
  174. console.log("openCamera");
  175. this.uploadLock = false;
  176. this.popupPhotoShow = false;
  177. // 同一个商品只谈一次提示
  178. // let popupList = uni.getStorageSync("popupList") || [];
  179. // if (!popupList.includes(this.goodsId)) {
  180. // popupList.push(this.goodsId);
  181. // uni.setStorageSync("popupList", popupList);
  182. // this.popupPhotoShow = true;
  183. // return;
  184. // }
  185. // #ifdef MP-WEIXIN
  186. // 屏幕亮度
  187. uni.setKeepScreenOn({
  188. keepScreenOn: true,
  189. });
  190. uni.getSetting({
  191. success: (res) => {
  192. if (res.authSetting["scope.camera"]) {
  193. this.isCameraAuth = true;
  194. this.showCamera();
  195. } else {
  196. wx.authorize({
  197. scope: "scope.camera",
  198. success: () => {
  199. this.isCameraAuth = true;
  200. this.showCamera();
  201. },
  202. fail: () => {
  203. this.isCameraAuth = false;
  204. this.showSet = true;
  205. },
  206. });
  207. }
  208. },
  209. fail: (res) => {},
  210. });
  211. // #endif
  212. // #ifdef H5
  213. if (
  214. (window.navigator.mediaDevices &&
  215. window.navigator.mediaDevices.getUserMedia) ||
  216. window.navigator.getUserMedia ||
  217. window.navigator.webkitGetUserMedia ||
  218. window.navigator.mozGetUserMedia
  219. ) {
  220. console.log("getUserMedia----");
  221. // 调用用户媒体设备, 访问摄像头
  222. this.getUserMedia({
  223. video: {
  224. width: 400,
  225. height: 400,
  226. facingMode: "user",
  227. },
  228. },
  229. this.photographSuccess,
  230. this.photographError
  231. );
  232. } else {
  233. console.log("1111没有摄像");
  234. this.photographError();
  235. }
  236. // #endif
  237. },
  238. showCamera() {
  239. this.isTaking = true;
  240. this.isShow = true;
  241. },
  242. //确认拍照
  243. takePhoto() {
  244. // #ifdef MP-WEIXIN
  245. const ctx = uni.createCameraContext();
  246. ctx.startRecord
  247. ctx.takePhoto({
  248. quality: "high",
  249. success: (res) => {
  250. this.avatarUrl = res.tempImagePath;
  251. console.log("开始拍照this.avatarUrl:", this.avatarUrl);
  252. this.isTaking = false;
  253. },
  254. fail: (err) => {},
  255. });
  256. // #endif
  257. // #ifdef H5
  258. this.luzhi();
  259. // const canvas = document.createElement("canvas");
  260. // canvas.width = 400;
  261. // canvas.height = 400;
  262. // const context = canvas.getContext("2d");
  263. // const box = document.querySelector(".photo_v");
  264. // const video = box.querySelector("video");
  265. // context.drawImage(video, 0, 0, 400, 400);
  266. // this.faceUrl = canvas.toDataURL("image/png");
  267. // this.isTaking = false;
  268. // #endif
  269. },
  270. takePhTips() {
  271. this.popupPhotoShow = true;
  272. this.isShow = false;
  273. this.isTaking = false;
  274. },
  275. /**
  276. * 人脸匹配
  277. */
  278. faceRecognition(url) {
  279. return new Promise((resolve) => {
  280. // // #ifdef MP-WEIXIN
  281. // let fileSystem = uni.getFileSystemManager();
  282. // fileSystem.readFile({
  283. // filePath: `${this.avatarUrl}`,
  284. // encoding: "base64",
  285. // position: 0,
  286. // success: (res) => {
  287. // let base64 = "data:image/jpg;base64," + res.data;
  288. // this.CompareFace(base64, resolve);
  289. // },
  290. // fail(err) {
  291. // // this.$u.toast('人脸识别错误!')
  292. // console.error(err, "err-----人脸识别错误");
  293. // },
  294. // });
  295. // // #endif
  296. // // #ifdef H5
  297. // this.CompareFace(this.faceUrl, resolve);
  298. // // #endif
  299. this.CompareFace(url, resolve);
  300. });
  301. },
  302. CompareFace(url, resolve) {
  303. let timer = setTimeout(() => {
  304. uni.showToast({
  305. icon: "none",
  306. title: "录像超时,请重新录制视频",
  307. duration: 2000,
  308. success: () => {
  309. setTimeout(() => {
  310. uni.navigateBack();
  311. }, 1000);
  312. },
  313. });
  314. }, 10 * 1000);
  315. this.$api
  316. .faceCertificationCompareFace({
  317. urlA: url,
  318. // imageA: url,
  319. orderGoodsId: this.params.orderGoodsId,
  320. gradeId: this.params.gradeId,
  321. })
  322. .then((res) => {
  323. clearTimeout(timer);
  324. console.log(res, "人脸识别成功res");
  325. resolve(res.data);
  326. })
  327. .catch((err) => {
  328. clearTimeout(timer);
  329. // 当前网络延迟,
  330. console.log("人脸识别错误:", err);
  331. uni.showModal({
  332. content: "当前网络延迟",
  333. showCancel: false,
  334. success: (resultst) => {
  335. if (resultst.confirm) {
  336. uni.navigateBack();
  337. }
  338. },
  339. });
  340. });
  341. },
  342. photographSuccess(stream) {
  343. console.log("有摄像头---", stream);
  344. this.isCameraAuth = true;
  345. this.showCamera();
  346. this.$nextTick(() => {
  347. const box = document.querySelector(".photo_v");
  348. const video = box.querySelector("video");
  349. video.srcObject = stream;
  350. this.mediaRecorder = new MediaRecorder(stream);
  351. video.play();
  352. });
  353. },
  354. photographError(err) {
  355. console.log("没有摄像头:", err);
  356. uni.showModal({
  357. title: "提示",
  358. content: "课程学习需要开启摄像头进行拍照,经检测您的设备无摄像头可使用,请检测环境是否支持。",
  359. cancelText: "取消",
  360. confirmText: "确定",
  361. showCancel: false,
  362. success: (res) => {
  363. if (res.confirm) {
  364. uni.navigateBack();
  365. } else if (res.cancel) {}
  366. },
  367. });
  368. },
  369. getUserMedia(constraints, success, error) {
  370. console.log("getUserMedia===", constraints);
  371. if (window.navigator.mediaDevices.getUserMedia) {
  372. // 最新的标准API
  373. window.navigator.mediaDevices
  374. .getUserMedia(constraints)
  375. .then(success)
  376. .catch(error);
  377. } else if (window.navigator.webkitGetUserMedia) {
  378. // webkit核心浏览器
  379. window.navigator.webkitGetUserMedia(constraints, success, error);
  380. } else if (window.navigator.mozGetUserMedia) {
  381. // firfox浏览器
  382. window.navigator.mozGetUserMedia(constraints, success, error);
  383. } else if (window.navigator.getUserMedia) {
  384. // 旧版API
  385. window.navigator.getUserMedia(constraints, success, error);
  386. }
  387. },
  388. again() {
  389. this.url = null;
  390. this.status = 1;
  391. },
  392. luzhi() {
  393. if (this.status === 3) {
  394. //完成提交
  395. this.submit();
  396. return;
  397. }
  398. this.onCatchPhoto();
  399. this.mediaRecorder.start();
  400. this.status = 2;
  401. setTimeout(() => {
  402. this.stop();
  403. this.status = 3;
  404. }, 3500);
  405. },
  406. //拍照
  407. onCatchPhoto() {
  408. const canvas = document.createElement("canvas");
  409. canvas.width = 400;
  410. canvas.height = 400;
  411. const context = canvas.getContext("2d");
  412. const box = document.querySelector(".photo_v");
  413. const video = box.querySelector("video");
  414. context.drawImage(video, 0, 0, 400, 400);
  415. this.faceUrl = canvas.toDataURL("image/png");
  416. // console.log(this.faceUrl,'faceUrl')
  417. },
  418. stop() {
  419. this.mediaRecorder.stop();
  420. this.mediaRecorder.ondataavailable = (event) => {
  421. //this.$message.success("录制完成");
  422. // console.log(event.data, 'data')
  423. // 尝试转换为mp4类型
  424. const blob = event.data.slice(0, event.data.size, "video/mp4");
  425. this.url = URL.createObjectURL(blob);
  426. this.videoblob = blob;
  427. console.log(event.data, 'url')
  428. // const file = new window.File([blob], "videoss.mp4", {
  429. // type: 'video/mp4',
  430. // });
  431. // console.log(file, 'file')
  432. };
  433. },
  434. },
  435. computed: {
  436. params() {
  437. return this.paramsFn();
  438. },
  439. goodsId() {
  440. return this.params.goodsId;
  441. },
  442. },
  443. components: {
  444. PopupPhoto,
  445. },
  446. watch: {
  447. visible(val) {
  448. if (val) {
  449. this.takePhTips()//this.openCamera();
  450. } else {
  451. this.closeCamera();
  452. }
  453. },
  454. },
  455. };
  456. </script>
  457. <style lang="scss" scoped>
  458. .btnSet {
  459. width: 440rpx;
  460. height: 80rpx;
  461. background: #007aff;
  462. border-radius: 40rpx;
  463. color: #ffffff;
  464. font-size: 28rpx;
  465. line-height: 80rpx;
  466. }
  467. .photoBox {
  468. width: 100%;
  469. .photoTop {
  470. width: 100%;
  471. height: 74rpx;
  472. border-radius: 20px 20px 0px 0px;
  473. background-color: #ffffff;
  474. display: flex;
  475. align-items: center;
  476. justify-content: center;
  477. padding: 0rpx 38rpx;
  478. .centersq {
  479. color: #333;
  480. font-size: 30rpx;
  481. font-weight: 500;
  482. }
  483. }
  484. .photoCenter {
  485. width: 750rpx;
  486. height: 75vh;
  487. position: relative;
  488. .center_camera {
  489. width: 100%;
  490. height: 75vh;
  491. position: fixed;
  492. background-color: rgba(0, 0, 0, .8);
  493. .head_take {
  494. width: 100%;
  495. height: 75vh;
  496. display: flex;
  497. flex-direction: column;
  498. }
  499. .headTake_up {
  500. width: 100%;
  501. height: 100rpx;
  502. }
  503. .headTake_minddle {
  504. display: flex;
  505. .min_img {
  506. width: 500rpx;
  507. height: 550rpx;
  508. }
  509. .min_left,
  510. .min_right {
  511. flex: 1;
  512. height: 550rpx;
  513. }
  514. }
  515. .headTake_down {
  516. width: 100%;
  517. flex: 1;
  518. }
  519. .color {
  520. background-color: #333;
  521. opacity: 0.5;
  522. }
  523. .photo_v {
  524. display: block;
  525. margin: 0 auto;
  526. width: 400px;
  527. height: 400px;
  528. }
  529. .photo_v2 {
  530. display: block;
  531. margin: 0 auto;
  532. width: 400px;
  533. height: 400px;
  534. }
  535. .mask {
  536. width: 500rpx;
  537. height: 550rpx;
  538. position: absolute;
  539. top: 100rpx;
  540. left: 0;
  541. right: 0;
  542. bottom: 0;
  543. margin: 0 auto;
  544. box-shadow: 0 0 0 2000px rgba(0, 0, 0, 0.4);
  545. }
  546. }
  547. .custom {
  548. width: 750rpx;
  549. height: 75vh;
  550. position: absolute;
  551. z-index: 1000;
  552. top: 0;
  553. left: 0;
  554. background-color: rgba(0, 0, 0, .8);
  555. image {
  556. display: block;
  557. margin: 0 auto;
  558. width: 400px;
  559. height: 400px;
  560. }
  561. }
  562. }
  563. .btns {
  564. display: flex;
  565. .takePhoto_btn {
  566. width: 100%;
  567. display: flex;
  568. align-items: center;
  569. justify-content: space-between;
  570. background: #a9a7a9;
  571. padding: 40rpx 26rpx;
  572. .middle_btn {
  573. width: 120rpx;
  574. height: 120rpx;
  575. border-radius: 40rpx;
  576. border: 4rpx solid #ffffff;
  577. display: flex;
  578. align-items: center;
  579. justify-content: center;
  580. }
  581. .square {
  582. width: 96rpx;
  583. height: 96rpx;
  584. background: #ffffff;
  585. border-radius: 28rpx;
  586. }
  587. .rights {
  588. font-size: 32rpx;
  589. font-weight: 500;
  590. color: #ffffff;
  591. }
  592. }
  593. .btnResult {
  594. height: 100rpx;
  595. flex: 1;
  596. background-color: #07c160;
  597. text-align: center;
  598. line-height: 100rpx;
  599. color: #fff;
  600. font-size: 32rpx;
  601. font-weight: bold;
  602. }
  603. }
  604. }
  605. .popup_box {
  606. width: 100%;
  607. height: 1100rpx;
  608. display: flex;
  609. align-items: center;
  610. flex-direction: column;
  611. padding: 0rpx 40rpx;
  612. .photo_head {
  613. width: 240rpx;
  614. height: 240rpx;
  615. margin: 80rpx 0rpx 48rpx;
  616. }
  617. .warn {
  618. width: 100%;
  619. font-size: 36rpx;
  620. font-weight: bold;
  621. color: #222222;
  622. margin-bottom: 40rpx;
  623. }
  624. .words {
  625. font-size: 28rpx;
  626. color: #666666;
  627. line-height: 33rpx;
  628. margin-bottom: 24rpx;
  629. }
  630. .take_photo {
  631. width: 412rpx;
  632. height: 84rpx;
  633. line-height: 84rpx;
  634. text-align: center;
  635. background: #01C65A;
  636. border-radius: 16rpx;
  637. font-size: 36rpx;
  638. font-weight: 500;
  639. color: #FFFFFF;
  640. margin-top: 100rpx;
  641. }
  642. }
  643. </style>