camera.vue 15 KB

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