camera.vue 15 KB

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