authentication.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <template>
  2. <view class="container">
  3. <view class="notes">拍摄您本人人脸,确保对准手机,光线充足</view>
  4. <view class="msg">{{ name }} {{ cardNumber }}</view>
  5. <!-- <view class="info">
  6. <span v-if="tipsText">{{ tipsText }}</span>
  7. </view> -->
  8. <view class="photo">
  9. <camera class="img" v-if="isAuthCamera" device-position="front" flash="off" resolution="high" />
  10. <img v-else :src="tempImg" />
  11. </view>
  12. <view class="button" @click="handleTakePhotoClick">开启身份认证</view>
  13. <!-- 认证结果弹窗 -->
  14. <uni-popup ref="popup" :is-mask-click="false">
  15. <view class="popup-content">
  16. <view class="title">
  17. <view class="icon"><img src="./imgs/success.png" /></view>
  18. <view class="title_info">打卡成功</view>
  19. </view>
  20. <view class="time">{{ nowTime }}</view>
  21. <view class="popup_button" @click="handleGoHome">我知道了</view>
  22. </view>
  23. </uni-popup>
  24. </view>
  25. </template>
  26. <script>
  27. import { encrypt, decrypt } from '../util/encryp.js'
  28. import { RSAencrypt } from '../util/WxmpRsa.js'
  29. export default {
  30. data() {
  31. return {
  32. // 错误文案提示
  33. tipsText: '人脸检测初始化失败,请退出重试',
  34. // 当前时间
  35. nowTime: '',
  36. // 本地图片路径 被匹对照片
  37. tempImg: '',
  38. // 场景照片
  39. sceneImage: '',
  40. // 经纬度
  41. lat: '',
  42. lng: '',
  43. // 地址
  44. location: '',
  45. // 规则id
  46. id: '',
  47. // 相机引擎
  48. cameraEngine: null,
  49. // 是否拥有相机权限
  50. isAuthCamera: true,
  51. // 姓名
  52. name: '',
  53. // 身份证号
  54. cardNumber: '',
  55. // 是否需要场景照片,值为1时是不需要
  56. flag: ''
  57. }
  58. },
  59. onLoad(options) {
  60. let userInfo = uni.getStorageSync('userInfo')
  61. if (userInfo) {
  62. this.name = userInfo.name
  63. this.cardNumber = userInfo.cardNumber
  64. }
  65. this.id = options.id
  66. this.lat = options.latitude
  67. this.lng = options.longitude
  68. this.location = options.address
  69. if (options.imgUrl) {
  70. this.sceneImage = options.imgUrl
  71. }
  72. if (options.flag) {
  73. this.flag = options.flag
  74. }
  75. this.initData()
  76. },
  77. methods: {
  78. // 初始化相机引擎
  79. initData() {
  80. // #ifdef MP-WEIXIN
  81. // 1、初始化人脸识别
  82. wx.initFaceDetect()
  83. // 2、创建 camera 上下文 CameraContext 对象
  84. this.cameraEngine = wx.createCameraContext()
  85. // 3、获取 Camera 实时帧数据
  86. const listener = this.cameraEngine.onCameraFrame((frame) => {
  87. if (this.tempImg) {
  88. return
  89. }
  90. // 4、人脸识别,使用前需要通过 wx.initFaceDetect 进行一次初始化,推荐使用相机接口返回的帧数据
  91. wx.faceDetect({
  92. frameBuffer: frame.data,
  93. width: frame.width,
  94. height: frame.height,
  95. enablePoint: true,
  96. enableConf: true,
  97. enableAngle: true,
  98. enableMultiFace: true,
  99. success: (faceData) => {
  100. // let face = faceData.faceInfo[0]
  101. // if (faceData.x == -1 || faceData.y == -1) {
  102. // this.tipsText = '检测不到人'
  103. // }
  104. // if (faceData.faceInfo.length > 1) {
  105. // this.tipsText = '请保证只有一个人'
  106. // } else {
  107. // const { pitch, roll, yaw } = face.angleArray
  108. // const standard = 0.5
  109. // if (Math.abs(pitch) >= standard || Math.abs(roll) >= standard || Math.abs(yaw) >= standard) {
  110. // this.tipsText = '请平视摄像头'
  111. // } else if (
  112. // face.confArray.global <= 0.8 ||
  113. // face.confArray.leftEye <= 0.8 ||
  114. // face.confArray.mouth <= 0.8 ||
  115. // face.confArray.nose <= 0.8 ||
  116. // face.confArray.rightEye <= 0.8
  117. // ) {
  118. // this.tipsText = '请勿遮挡五官'
  119. // } else {
  120. // this.tipsText = '请点击下方按钮开始认证'
  121. // // 这里可以写自己的逻辑了
  122. // }
  123. // }
  124. },
  125. fail: (err) => {
  126. if (err.x == -1 || err.y == -1) {
  127. this.tipsText = '检测不到人'
  128. } else {
  129. this.tipsText = '网络错误,请退出页面重试'
  130. }
  131. }
  132. })
  133. })
  134. // 5、开始监听帧数据
  135. listener.start()
  136. // #endif
  137. },
  138. // 拍照点击
  139. handleTakePhotoClick() {
  140. // if (this.tipsText != '' && this.tipsText != '请点击下方按钮开始认证') {
  141. // return
  142. // }
  143. uni.getSetting({
  144. success: (res) => {
  145. if (!res.authSetting['scope.camera']) {
  146. this.isAuthCamera = true
  147. uni.openSetting({
  148. success: (res) => {
  149. if (res.authSetting['scope.camera']) {
  150. this.isAuthCamera = true
  151. }
  152. }
  153. })
  154. }
  155. }
  156. })
  157. this.cameraEngine.takePhoto({
  158. // quality: 'low',
  159. quality: 'high',
  160. success: ({ tempImagePath }) => {
  161. this.tempImg = tempImagePath
  162. this.isAuthCamera = false
  163. this.tipsText = ''
  164. this.handleOkClick()
  165. }
  166. })
  167. },
  168. // 上传
  169. handleOkClick() {
  170. // 这里的 this.tempImg 是经过人脸检测后 拍照拿到的路径
  171. this.upLoadOne(this.tempImg)
  172. },
  173. upLoadOne(url) {
  174. uni.showLoading({
  175. title: '认证中,请稍后...'
  176. })
  177. setTimeout(() => {
  178. // uni.uploadFile({
  179. // url: `https://chtech.ncjti.edu.cn/campusclock/attendance/api/file/upload`,
  180. // filePath: url,
  181. // name: 'file',
  182. // header: {
  183. // platform: 2,
  184. // 'Accept-Language': 'zh-CN,zh;q=0.9'
  185. // },
  186. // success: uploadFileRes => {
  187. // let imgUrl = JSON.parse(uploadFileRes.data).data
  188. // // this.handleUploading(imgUrl)
  189. // this.handleEncrypt(imgUrl)
  190. // },
  191. // fail: () => {
  192. // uni.showToast({
  193. // title: '上传失败',
  194. // icon: 'error'
  195. // })
  196. // }
  197. // })
  198. uni.uploadFile({
  199. url: `https://chtech.ncjti.edu.cn/gxy/facedemo/face-api/ihotel/mapper/face/compareTwoFace`,
  200. // url: `http://192.168.161.220:8089/ihotel/mapper/face/compareTwoFace`,
  201. filePath: url,
  202. name: 'file',
  203. formData: {
  204. mCardNumber: this.cardNumber
  205. },
  206. header: {
  207. platform: 2,
  208. 'Accept-Language': 'zh-CN,zh;q=0.9'
  209. },
  210. success: (uploadFileRes) => {
  211. // console.log(uploadFileRes)
  212. let temRes = JSON.parse(uploadFileRes.data)
  213. if (temRes.code === '200') {
  214. let imgUrl = temRes.data.fileUrl
  215. uni.showToast({
  216. title: '对比成功'
  217. })
  218. setTimeout(() => {
  219. this.handleUploading(imgUrl)
  220. }, 1500)
  221. } else {
  222. uni.showToast({
  223. title: `${temRes.message},请重新认证`,
  224. icon: 'none',
  225. duration: 2000
  226. })
  227. this.tempImg = ''
  228. this.isAuthCamera = true
  229. }
  230. // this.handleEncrypt(imgUrl)
  231. },
  232. fail: () => {
  233. uni.showToast({
  234. title: '上传失败',
  235. icon: 'error'
  236. })
  237. }
  238. })
  239. }, 1000)
  240. },
  241. // 对比人脸请求
  242. // handleEncrypt(imgUrl) {
  243. // uni.showLoading({
  244. // title: '认证中,请稍后...'
  245. // })
  246. // uni.request({
  247. // url: 'https://chtech.ncjti.edu.cn/testingServer/faceVerification/api/identity-comparison-record/comparison',
  248. // method: 'post',
  249. // data: {
  250. // data: RSAencrypt(
  251. // encrypt(
  252. // JSON.stringify({
  253. // url: imgUrl,
  254. // name: this.name,
  255. // // name: '周祥',
  256. // idCard: this.cardNumber,
  257. // category: '校园打卡',
  258. // studentNumber: this.cardNumber
  259. // })
  260. // )
  261. // )
  262. // },
  263. // success: (res) => {
  264. // if (res.data.status == 200) {
  265. // let result = JSON.parse(decrypt(res.data.authorization))
  266. // // console.log(result)
  267. // if (result.status == 200) {
  268. // uni.showToast({
  269. // title: result.desc
  270. // })
  271. // setTimeout(() => {
  272. // this.handleUploading(imgUrl)
  273. // }, 1500)
  274. // } else {
  275. // uni.showToast({
  276. // title: `${result.desc},请重新认证`,
  277. // icon: 'error',
  278. // duration: 2000
  279. // })
  280. // this.tempImg = ''
  281. // this.isAuthCamera = true
  282. // // this.initData()
  283. // }
  284. // }
  285. // }
  286. // })
  287. // },
  288. async handleUploading(imgUrl) {
  289. let res = await this.$myRequest_clockIn({
  290. url: '/attendance/api/sign/check/in/update',
  291. method: 'put',
  292. header: {
  293. Authorization: uni.getStorageSync('token'),
  294. platform: 2,
  295. 'Accept-Language': 'zh-CN,zh;q=0.9'
  296. },
  297. data: {
  298. id: this.id,
  299. lat: this.lat,
  300. lng: this.lng,
  301. location: this.location,
  302. matchFaceImage: imgUrl,
  303. sceneImage: this.flag == 1 ? '' : this.sceneImage
  304. }
  305. })
  306. // console.log(res);
  307. if (res.code == 200) {
  308. this.getNowTime()
  309. this.$refs.popup.open()
  310. }
  311. },
  312. // 点击 我知道了按钮 跳回首页
  313. handleGoHome() {
  314. this.$refs.popup.close()
  315. uni.reLaunch({
  316. url: '/pagesClockIn/home/home'
  317. })
  318. },
  319. // 获取当前时间
  320. getNowTime() {
  321. let date = new Date()
  322. let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  323. let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  324. let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  325. this.nowTime = hours + ':' + minutes + ':' + seconds
  326. }
  327. }
  328. }
  329. </script>
  330. <style lang="scss" scoped>
  331. .container {
  332. .notes {
  333. margin-top: 110rpx;
  334. text-align: center;
  335. font-size: 28rpx;
  336. }
  337. .msg {
  338. margin-top: 10rpx;
  339. text-align: center;
  340. font-size: 28rpx;
  341. }
  342. .info {
  343. margin-top: 100rpx;
  344. height: 40rpx;
  345. text-align: center;
  346. color: #5393ff;
  347. }
  348. .photo {
  349. margin: 0 auto;
  350. margin-top: 180rpx;
  351. width: 375rpx;
  352. height: 375rpx;
  353. border-radius: 188rpx;
  354. overflow: hidden;
  355. .img {
  356. width: 100%;
  357. height: 100%;
  358. border-radius: 50%;
  359. }
  360. img {
  361. width: 100%;
  362. height: 100%;
  363. }
  364. }
  365. .button {
  366. margin: 0 auto;
  367. margin-top: 127rpx;
  368. width: 430rpx;
  369. height: 100rpx;
  370. line-height: 100rpx;
  371. text-align: center;
  372. color: #fff;
  373. border-radius: 21rpx;
  374. font-size: 32rpx;
  375. background: linear-gradient(180deg, rgba(0, 172, 252, 1) 0%, rgba(0, 130, 252, 1) 100%);
  376. }
  377. .popup-content {
  378. display: flex;
  379. flex-direction: column;
  380. justify-content: space-around;
  381. align-items: center;
  382. width: 630rpx;
  383. height: 376rpx;
  384. border-radius: 33rpx;
  385. background-color: #fff;
  386. .title {
  387. display: flex;
  388. justify-content: center;
  389. align-items: center;
  390. .icon {
  391. width: 40rpx;
  392. height: 40rpx;
  393. img {
  394. width: 100%;
  395. height: 100%;
  396. }
  397. }
  398. .title_info {
  399. margin-left: 8rpx;
  400. font-size: 36rpx;
  401. font-weight: 800;
  402. color: #0082fc;
  403. }
  404. }
  405. .time {
  406. font-size: 32rpx;
  407. }
  408. .popup_button {
  409. width: 50%;
  410. text-align: center;
  411. font-size: 32rpx;
  412. color: #0082fc;
  413. }
  414. }
  415. }
  416. </style>