camera.vue 16 KB

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