authentication.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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: 'high',
  159. success: ({ tempImagePath }) => {
  160. this.tempImg = tempImagePath
  161. this.isAuthCamera = false
  162. this.tipsText = ''
  163. this.handleOkClick()
  164. }
  165. })
  166. },
  167. // 上传
  168. handleOkClick() {
  169. // 这里的 this.tempImg 是经过人脸检测后 拍照拿到的路径
  170. this.upLoadOne(this.tempImg)
  171. },
  172. upLoadOne(url) {
  173. uni.showLoading({
  174. title: '认证中,请稍后...'
  175. })
  176. setTimeout(() => {
  177. // uni.uploadFile({
  178. // url: `https://chtech.ncjti.edu.cn/campusclock/attendance/api/file/upload`,
  179. // filePath: url,
  180. // name: 'file',
  181. // header: {
  182. // platform: 2,
  183. // 'Accept-Language': 'zh-CN,zh;q=0.9'
  184. // },
  185. // success: uploadFileRes => {
  186. // let imgUrl = JSON.parse(uploadFileRes.data).data
  187. // // this.handleUploading(imgUrl)
  188. // this.handleEncrypt(imgUrl)
  189. // },
  190. // fail: () => {
  191. // uni.showToast({
  192. // title: '上传失败',
  193. // icon: 'error'
  194. // })
  195. // }
  196. // })
  197. uni.uploadFile({
  198. url: `https://chtech.ncjti.edu.cn/gxy/facedemo/face-api/ihotel/mapper/face/compareTwoFace`,
  199. // url: `http://192.168.161.220:8089/ihotel/mapper/face/compareTwoFace`,
  200. filePath: url,
  201. name: 'file',
  202. formData: {
  203. mIdCard: this.cardNumber
  204. },
  205. header: {
  206. platform: 2,
  207. 'Accept-Language': 'zh-CN,zh;q=0.9'
  208. },
  209. success: (uploadFileRes) => {
  210. // console.log(uploadFileRes)
  211. let temRes = JSON.parse(uploadFileRes.data)
  212. if (temRes.code === '200') {
  213. let imgUrl = temRes.data.fileUrl
  214. uni.showToast({
  215. title: '对比成功'
  216. })
  217. setTimeout(() => {
  218. this.handleUploading(imgUrl)
  219. }, 1500)
  220. } else {
  221. uni.showToast({
  222. title: `${temRes.message},请重新认证`,
  223. icon: 'none',
  224. duration: 2000
  225. })
  226. this.tempImg = ''
  227. this.isAuthCamera = true
  228. }
  229. // this.handleEncrypt(imgUrl)
  230. },
  231. fail: () => {
  232. uni.showToast({
  233. title: '上传失败',
  234. icon: 'error'
  235. })
  236. }
  237. })
  238. }, 1000)
  239. },
  240. // 对比人脸请求
  241. handleEncrypt(imgUrl) {
  242. uni.showLoading({
  243. title: '认证中,请稍后...'
  244. })
  245. uni.request({
  246. url: 'https://chtech.ncjti.edu.cn/testingServer/faceVerification/api/identity-comparison-record/comparison',
  247. method: 'post',
  248. data: {
  249. data: RSAencrypt(
  250. encrypt(
  251. JSON.stringify({
  252. url: imgUrl,
  253. name: this.name,
  254. // name: '周祥',
  255. idCard: this.cardNumber,
  256. category: '校园打卡',
  257. studentNumber: this.cardNumber
  258. })
  259. )
  260. )
  261. },
  262. success: (res) => {
  263. if (res.data.status == 200) {
  264. let result = JSON.parse(decrypt(res.data.authorization))
  265. // console.log(result)
  266. if (result.status == 200) {
  267. uni.showToast({
  268. title: result.desc
  269. })
  270. setTimeout(() => {
  271. this.handleUploading(imgUrl)
  272. }, 1500)
  273. } else {
  274. uni.showToast({
  275. title: `${result.desc},请重新认证`,
  276. icon: 'error',
  277. duration: 2000
  278. })
  279. this.tempImg = ''
  280. this.isAuthCamera = true
  281. // this.initData()
  282. }
  283. }
  284. }
  285. })
  286. },
  287. async handleUploading(imgUrl) {
  288. let res = await this.$myRequest_clockIn({
  289. url: '/attendance/api/sign/check/in/update',
  290. method: 'put',
  291. header: {
  292. Authorization: uni.getStorageSync('token'),
  293. platform: 2,
  294. 'Accept-Language': 'zh-CN,zh;q=0.9'
  295. },
  296. data: {
  297. id: this.id,
  298. lat: this.lat,
  299. lng: this.lng,
  300. location: this.location,
  301. matchFaceImage: imgUrl,
  302. sceneImage: this.flag == 1 ? '' : this.sceneImage
  303. }
  304. })
  305. // console.log(res);
  306. if (res.code == 200) {
  307. this.getNowTime()
  308. this.$refs.popup.open()
  309. }
  310. },
  311. // 点击 我知道了按钮 跳回首页
  312. handleGoHome() {
  313. this.$refs.popup.close()
  314. uni.reLaunch({
  315. url: '/pagesClockIn/home/home'
  316. })
  317. },
  318. // 获取当前时间
  319. getNowTime() {
  320. let date = new Date()
  321. let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  322. let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  323. let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  324. this.nowTime = hours + ':' + minutes + ':' + seconds
  325. }
  326. }
  327. }
  328. </script>
  329. <style lang="scss" scoped>
  330. .container {
  331. .notes {
  332. margin-top: 110rpx;
  333. text-align: center;
  334. font-size: 28rpx;
  335. }
  336. .msg {
  337. margin-top: 10rpx;
  338. text-align: center;
  339. font-size: 28rpx;
  340. }
  341. .info {
  342. margin-top: 100rpx;
  343. height: 40rpx;
  344. text-align: center;
  345. color: #5393ff;
  346. }
  347. .photo {
  348. margin: 0 auto;
  349. margin-top: 180rpx;
  350. width: 375rpx;
  351. height: 375rpx;
  352. border-radius: 188rpx;
  353. overflow: hidden;
  354. .img {
  355. width: 100%;
  356. height: 100%;
  357. border-radius: 50%;
  358. }
  359. img {
  360. width: 100%;
  361. height: 100%;
  362. }
  363. }
  364. .button {
  365. margin: 0 auto;
  366. margin-top: 127rpx;
  367. width: 430rpx;
  368. height: 100rpx;
  369. line-height: 100rpx;
  370. text-align: center;
  371. color: #fff;
  372. border-radius: 21rpx;
  373. font-size: 32rpx;
  374. background: linear-gradient(180deg, rgba(0, 172, 252, 1) 0%, rgba(0, 130, 252, 1) 100%);
  375. }
  376. .popup-content {
  377. display: flex;
  378. flex-direction: column;
  379. justify-content: space-around;
  380. align-items: center;
  381. width: 630rpx;
  382. height: 376rpx;
  383. border-radius: 33rpx;
  384. background-color: #fff;
  385. .title {
  386. display: flex;
  387. justify-content: center;
  388. align-items: center;
  389. .icon {
  390. width: 40rpx;
  391. height: 40rpx;
  392. img {
  393. width: 100%;
  394. height: 100%;
  395. }
  396. }
  397. .title_info {
  398. margin-left: 8rpx;
  399. font-size: 36rpx;
  400. font-weight: 800;
  401. color: #0082fc;
  402. }
  403. }
  404. .time {
  405. font-size: 32rpx;
  406. }
  407. .popup_button {
  408. width: 50%;
  409. text-align: center;
  410. font-size: 32rpx;
  411. color: #0082fc;
  412. }
  413. }
  414. }
  415. </style>