camera.vue 14 KB

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