dynamicFaceCheck.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. <template>
  2. <view>
  3. <view class="top">
  4. <NavigateBar title="身份核验" bgcolor="#a09cc4" control="back"></NavigateBar>
  5. </view>
  6. <view class="face-area">
  7. <view class="face-area--tips">
  8. <view class="face-area--tips--title">
  9. <u-text text="身份核验" color="#333333" bold size="34rpx"></u-text>
  10. </view>
  11. <view class="face-area--tips--context">
  12. <u-text text="请根据页面提示进行操作" color="#CBCBCB" size="28rpx"></u-text>
  13. </view>
  14. </view>
  15. <view class="face-area--line"></view>
  16. <view class="camera-container">
  17. <camera v-if="showcamera" class="camera" device-position="front" flash="off" resolution="low"></camera>
  18. </view>
  19. <view class="info">
  20. <u-text v-if="showcamera" :text="tipsText" align="center"></u-text>
  21. </view>
  22. </view>
  23. <view class="precautions">
  24. <view class="precautions--tips">
  25. <view class="precautions--tips--title">
  26. <u-text text="注意事项" color="#333333" bold size="34rpx"></u-text>
  27. </view>
  28. <view class="precautions--tips--context">
  29. <u-text text="请遵守需注意事项" color="#CBCBCB" size="28rpx"></u-text>
  30. </view>
  31. </view>
  32. <view class="precautions--line"></view>
  33. <view class="precautions--context">
  34. <u-text :text="'确认'+name+'本人操作;保持正脸在取景框中根据屏幕指示完成'" color="#666666" size="24rpx"></u-text>
  35. </view>
  36. <view class="img-list">
  37. <view class="img-list--item" v-for="img in imgList">
  38. <view class="img">
  39. <u-image :src="img.src" :height="img.height" :width="img.width"></u-image>
  40. </view>
  41. <view class="tips">
  42. <u-text :text="img.tips" color="#666666" size="20rpx"></u-text>
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. <view class="btn">
  48. <u-button text="同意并开始验证" @click="start" color="#9e97c3"></u-button>
  49. </view>
  50. </view>
  51. </template>
  52. <script>
  53. import {
  54. mapState,
  55. mapMutations
  56. } from "vuex";
  57. export default {
  58. data() {
  59. return {
  60. cameraEngine: null,
  61. VKSession: null,
  62. showcamera: false,
  63. tipsText: "开始身份核验",
  64. idNumber: '',
  65. name: '',
  66. duringCameraAuth: false,
  67. isVKSupport: false,
  68. imgList: [{
  69. src: '/static/check-face-phone.png',
  70. width: '48rpx',
  71. height: '70rpx',
  72. tips: '正对手机'
  73. },
  74. {
  75. src: '/static/check-face-light.png',
  76. width: '78rpx',
  77. height: '70rpx',
  78. tips: '光线充足'
  79. },
  80. {
  81. src: '/static/check-face-face.png',
  82. width: '80rpx',
  83. height: '80rpx',
  84. tips: '脸部无遮挡'
  85. }
  86. ]
  87. };
  88. },
  89. computed: {
  90. ...mapState('m_user', ['userInfo']),
  91. ...mapState('m_business', ['currentHotel', 'checkinInfo']),
  92. },
  93. methods: {
  94. ...mapMutations('m_business', ['updateCheckinInfo']),
  95. start() {
  96. if (!this.duringCameraAuth && this.isVKSupport) {
  97. this.getCameraAuth()
  98. }
  99. },
  100. getCameraAuth() {
  101. console.log('getCameraAuth')
  102. this.duringCameraAuth = true
  103. let that = this
  104. wx.getSetting({
  105. success: (res) => {
  106. console.log('getSetting, res : ')
  107. console.log(res)
  108. if (!res.authSetting["scope.camera"]) {
  109. wx.authorize({
  110. scope: "scope.camera",
  111. success(res) {
  112. console.log('authorize success, res : ')
  113. console.log(res)
  114. that.showcamera = true
  115. },
  116. fail: (res) => {
  117. console.log('authorize fail, res : ')
  118. console.log(res)
  119. wx.showModal({
  120. title: "提示",
  121. content: "尚未进行授权,功能将无法使用",
  122. cancelText: "取消",
  123. confirmText: "授权",
  124. success(res) {
  125. console.log(res);
  126. if (res.confirm) {
  127. wx.openSetting({
  128. //这里的方法是调到一个添加权限的页面,这里可以测试在拒绝授权的情况下设置中是否存在相机选项
  129. success: (res) => {
  130. if (!res.authSetting[
  131. "scope.camera"
  132. ]) {
  133. this.getCameraAuth()
  134. } else {
  135. that.showcamera =
  136. true
  137. }
  138. },
  139. fail: function() {
  140. console.log(
  141. "相机授权设置失败");
  142. },
  143. });
  144. } else if (res.cancel) {
  145. console.log("用户点击取消");
  146. wx.showToast({
  147. title: "您已取消授权",
  148. icon: "error",
  149. duration: 1000,
  150. });
  151. setTimeout(function() {
  152. wx.navigateBack();
  153. }, 1000);
  154. }
  155. }
  156. })
  157. }
  158. })
  159. } else {
  160. that.showcamera = true
  161. }
  162. that.initData()
  163. }
  164. })
  165. this.duringCameraAuth = false
  166. },
  167. //visionkit人脸必须函数
  168. async detectFace(frame) {
  169. console.log("frame", frame.data);
  170. this.VKSession.detectFace({
  171. frameBuffer: frame.data,
  172. width: frame.width,
  173. height: frame.height,
  174. scoreThreshold: 0.8,
  175. sourceType: 0,
  176. modelMode: 1,
  177. });
  178. },
  179. //初始化相机引擎
  180. initData() {
  181. let that = this;
  182. console.log('before createCameraContext')
  183. //使用visionkit
  184. that.cameraEngine = wx.createCameraContext();
  185. console.log('before createCameraContext')
  186. let count = 0;
  187. //初始化相机
  188. const listener = that.cameraEngine.onCameraFrame((frame) => {
  189. count++;
  190. //每十帧分析一次
  191. if (count === 10) {
  192. this.detectFace(frame);
  193. count = 0;
  194. }
  195. });
  196. //开始监听数据帧
  197. listener.start();
  198. //创建VK对象
  199. that.VKSession = wx.createVKSession({
  200. version: "v1",
  201. track: {
  202. plane: {
  203. mode: 1,
  204. },
  205. face: {
  206. mode: 2,
  207. },
  208. },
  209. });
  210. //启动VK对象
  211. that.VKSession.start((errno) => {
  212. console.log("VKSession.start errno", errno);
  213. if (errno) {
  214. wx.showModal({
  215. title: "提示",
  216. content: "网络错误,请退出后重试",
  217. showCancel: false,
  218. confirmText: "确定",
  219. success(res) {
  220. console.log(res);
  221. if (res.confirm) {
  222. wx.navigateBack();
  223. }
  224. },
  225. });
  226. return
  227. } else {
  228. console.log('VKSession start')
  229. that.VKSession.on("updateAnchors", (anchors) => {
  230. console.log("anchors", anchors);
  231. console.log("anchors.length", anchors.length);
  232. anchors.forEach((anchor) => {
  233. console.log("anchor.points", anchor.points);
  234. console.log("anchor.origin", anchor.origin);
  235. console.log("anchor.size", anchor.size);
  236. console.log("anchor.angle", anchor.angle);
  237. console.log("anchor", anchor.confidence);
  238. if (anchors.length > 1) {
  239. that.tipsText = "请保证只有一个人";
  240. } else {
  241. const {
  242. pitch,
  243. roll,
  244. yaw
  245. } = anchor.angle;
  246. const standard = 0.3;
  247. if (
  248. Math.abs(pitch) >= standard ||
  249. Math.abs(roll) >= standard ||
  250. Math.abs(yaw) >= standard
  251. ) {
  252. that.tipsText = "请平视摄像头";
  253. } else if (
  254. anchor.origin.x < 0.15 ||
  255. anchor.origin.x > 0.3 ||
  256. anchor.origin.y < 0.2 ||
  257. anchor.origin.y > 0.45
  258. ) {
  259. this.tipsText = "请将人脸对准中心位置";
  260. } else if (
  261. anchor.confidence[0] <= 0.8 ||
  262. anchor.confidence[1] <= 0.8 ||
  263. anchor.confidence[2] <= 0.8 ||
  264. anchor.confidence[3] <= 0.8 ||
  265. anchor.confidence[4] <= 0.8
  266. ) {
  267. that.tipsText = "请勿遮挡五官";
  268. } else {
  269. listener.stop();
  270. that.tipsText = "即将拍照,请保持!";
  271. setTimeout(function() {
  272. that.handleTakePhotoClick();
  273. }, 1000);
  274. return;
  275. }
  276. }
  277. });
  278. });
  279. }
  280. });
  281. },
  282. // 拍照
  283. handleTakePhotoClick() {
  284. this.tipsText = "";
  285. uni.showLoading({
  286. title: "正在身份核验,请稍后",
  287. });
  288. this.cameraEngine.takePhoto({
  289. quality: "normal",
  290. success: ({
  291. tempImagePath
  292. }) => {
  293. let mybase64 = wx
  294. .getFileSystemManager()
  295. .readFileSync(tempImagePath, "base64");
  296. this.afterRead(mybase64);
  297. },
  298. });
  299. },
  300. //上传
  301. async afterRead(mybase64) {
  302. let that = this;
  303. uni.$http.post("/faceVerification/checkPic", {
  304. hotelId: that.currentHotel.hotelId,
  305. faceData: mybase64,
  306. })
  307. .then(async (res) => {
  308. uni.hideLoading();
  309. console.log("mybase64", mybase64);
  310. if (res.data.code === 200 && res.data.success === true) {
  311. if (that.userInfo.skipIdMatching === true) {
  312. that.showcamera = false
  313. uni.showModal({
  314. content: "图片检查通过!",
  315. showCancel: false,
  316. success: function(res) {
  317. if (res.confirm) {
  318. console.log("用户点击确定");
  319. let guestInfo = {
  320. name: that.name,
  321. idNumber: that.idNumber,
  322. faceData: mybase64
  323. }
  324. that.checkinInfo.push(guestInfo)
  325. that.updateCheckinInfo(that.checkinInfo)
  326. setTimeout(() => uni.reLaunch({
  327. url: '/subpkg_checkin/confirmOrder/confirmOrder'
  328. }), 1000);
  329. }
  330. },
  331. });
  332. } else {
  333. uni.showLoading({
  334. title: "正在身份核验,请稍后",
  335. });
  336. let yunjiFaceCheckInfo = {
  337. idNo: that.idNumber,
  338. name: that.name,
  339. image: mybase64,
  340. };
  341. let res = await uni.$http.post(
  342. "/faceVerification/yunjiVerification",
  343. yunjiFaceCheckInfo
  344. );
  345. uni.hideLoading();
  346. if (res.data.success === true) {
  347. let guestInfo = {
  348. name: that.name,
  349. idNumber: that.idNumber,
  350. faceData: mybase64
  351. }
  352. that.checkinInfo.push(guestInfo)
  353. that.updateCheckinInfo(that.checkinInfo)
  354. uni.$showMsg('身份核验通过!')
  355. setTimeout(() => uni.reLaunch({
  356. url: '/subpkg_checkin/confirmOrder/confirmOrder'
  357. }), 1000)
  358. } else {
  359. uni.showModal({
  360. content: "身份核验失败,请重试",
  361. showCancel: false,
  362. success: function(res) {
  363. if (res.confirm) {
  364. console.log("用户点击确定");
  365. that.initData();
  366. }
  367. },
  368. });
  369. }
  370. }
  371. } else {
  372. uni.showModal({
  373. content: "身份核验失败,请重试",
  374. showCancel: false,
  375. success: function(res) {
  376. if (res.confirm) {
  377. console.log("用户点击确定");
  378. that.initData();
  379. }
  380. },
  381. });
  382. }
  383. console.log("checkPicRes", res);
  384. });
  385. },
  386. },
  387. onShow() {
  388. },
  389. onLoad(options) {
  390. this.isVKSupport = wx.isVKSupport('v1')
  391. if (!this.isVKSupport) {
  392. uni.showModal({
  393. content: '微信版本过低,请升级至最新版本',
  394. showCancel: false,
  395. success() {
  396. uni.navigateBack()
  397. },
  398. })
  399. } else {
  400. this.name = options.name
  401. this.idNumber = options.idNumber
  402. }
  403. },
  404. // #ifdef MP-WEIXIN
  405. onShareAppMessage(info) {
  406. return {
  407. title: '源享住',
  408. path: 'pages/login/login',
  409. imageUrl: "/static/logo.png"
  410. }
  411. }
  412. // #endif
  413. };
  414. </script>
  415. <style lang="scss">
  416. page {
  417. background-color: #EFEFF4;
  418. .face-area {
  419. background-color: #FFFFFF;
  420. border-radius: 50rpx;
  421. overflow: hidden;
  422. .face-area--tips {
  423. display: flex;
  424. padding: 10rpx 20rpx;
  425. align-items: flex-end;
  426. .face-area--tips--title {
  427. margin: 10rpx;
  428. }
  429. .face-area--tips--context {
  430. margin: 10rpx;
  431. }
  432. }
  433. .face-area--line {
  434. margin-left: 40rpx;
  435. margin-bottom: 20rpx;
  436. width: 30rpx;
  437. border-bottom: solid 6rpx #9e97c3;
  438. }
  439. .camera-container {
  440. width: 80%;
  441. height: 80vw;
  442. margin: 0 auto;
  443. margin-bottom: 30rpx;
  444. .camera {
  445. width: 100%;
  446. height: 80vw;
  447. margin: 0 auto;
  448. margin-bottom: 30rpx;
  449. border: 2rpx solid black;
  450. border-radius: 50%;
  451. // box-sizing: border-box;
  452. }
  453. }
  454. }
  455. .precautions {
  456. overflow: hidden;
  457. .precautions--tips {
  458. display: flex;
  459. padding: 10rpx 20rpx;
  460. align-items: flex-end;
  461. .precautions--tips--title {
  462. margin: 10rpx;
  463. }
  464. .precautions--tips--context {
  465. margin: 10rpx;
  466. }
  467. }
  468. .precautions--line {
  469. margin-left: 40rpx;
  470. margin-bottom: 20rpx;
  471. width: 30rpx;
  472. border-bottom: solid 6rpx #9e97c3;
  473. }
  474. .precautions--context {
  475. margin: 0 20rpx;
  476. margin-bottom: 20rpx;
  477. }
  478. .img-list {
  479. display: flex;
  480. justify-content: space-around;
  481. .img-list--item {
  482. display: flex;
  483. flex-direction: column;
  484. align-items: center;
  485. width: 20%;
  486. .img {
  487. display: flex;
  488. flex-direction: row;
  489. border-radius: 20rpx;
  490. background-color: #dcdce9;
  491. width: 100%;
  492. height: 140rpx;
  493. justify-content: center;
  494. align-items: center;
  495. }
  496. }
  497. }
  498. }
  499. .btn {
  500. margin: 20rpx;
  501. }
  502. }
  503. .content {
  504. position: absolute;
  505. top: 0;
  506. right: 0;
  507. bottom: 0;
  508. left: 0;
  509. .camera-box {
  510. position: relative;
  511. width: 100%;
  512. height: 100%;
  513. }
  514. .camera-bg-box {
  515. position: relative;
  516. width: 100%;
  517. height: 100%;
  518. overflow: hidden;
  519. &::after {
  520. content: "";
  521. position: absolute;
  522. left: 50%;
  523. top: 50%;
  524. transform: translateX(-50%) translateY(-50%);
  525. border-radius: 100%;
  526. width: 600rpx;
  527. height: 600rpx;
  528. border: 1000rpx solid rgba(255, 255, 255, 0.998);
  529. }
  530. }
  531. .camera {
  532. width: 100%;
  533. height: 100%;
  534. border-top: 200rpx solid black;
  535. border-bottom: 200rpx solid black;
  536. box-sizing: border-box;
  537. }
  538. .camera-tip {
  539. position: absolute;
  540. bottom: 220rpx;
  541. left: 50%;
  542. color: rgb(0, 0, 0);
  543. transform: translateX(-50%);
  544. // .text-40-fff-600();
  545. }
  546. }
  547. </style>