camera.vue 14 KB

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