camera.vue 15 KB

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