camera.vue 15 KB

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