authentication.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. }, 1000)
  198. },
  199. // 对比人脸请求
  200. handleEncrypt(imgUrl) {
  201. uni.showLoading({
  202. title: '认证中,请稍后...'
  203. })
  204. uni.request({
  205. url: 'https://chtech.ncjti.edu.cn/testingServer/faceVerification/api/identity-comparison-record/comparison',
  206. method: 'post',
  207. data: {
  208. data: RSAencrypt(
  209. encrypt(
  210. JSON.stringify({
  211. url: imgUrl,
  212. name: this.name,
  213. // name: '周祥',
  214. idCard: this.cardNumber,
  215. category: '校园打卡',
  216. studentNumber: this.cardNumber
  217. })
  218. )
  219. )
  220. },
  221. success: res => {
  222. if (res.data.status == 200) {
  223. let result = JSON.parse(decrypt(res.data.authorization))
  224. // console.log(result)
  225. if (result.status == 200) {
  226. uni.showToast({
  227. title: result.desc
  228. })
  229. setTimeout(() => {
  230. this.handleUploading(imgUrl)
  231. }, 1500)
  232. } else {
  233. uni.showToast({
  234. title: `${result.desc},请重新认证`,
  235. icon: 'error',
  236. duration: 2000
  237. })
  238. this.tempImg = ''
  239. this.isAuthCamera = true
  240. // this.initData()
  241. }
  242. }
  243. }
  244. })
  245. },
  246. async handleUploading(imgUrl) {
  247. let res = await this.$myRequest_clockIn({
  248. url: '/attendance/api/sign/check/in/update',
  249. method: 'put',
  250. header: {
  251. Authorization: uni.getStorageSync('token'),
  252. platform: 2,
  253. 'Accept-Language': 'zh-CN,zh;q=0.9'
  254. },
  255. data: {
  256. id: this.id,
  257. lat: this.lat,
  258. lng: this.lng,
  259. location: this.location,
  260. matchFaceImage: imgUrl,
  261. sceneImage: this.flag == 1 ? '' : this.sceneImage
  262. }
  263. })
  264. // console.log(res);
  265. if (res.code == 200) {
  266. this.getNowTime()
  267. this.$refs.popup.open()
  268. }
  269. },
  270. // 点击 我知道了按钮 跳回首页
  271. handleGoHome() {
  272. this.$refs.popup.close()
  273. uni.reLaunch({
  274. url: '/pagesClockIn/home/home'
  275. })
  276. },
  277. // 获取当前时间
  278. getNowTime() {
  279. let date = new Date()
  280. let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  281. let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  282. let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  283. this.nowTime = hours + ':' + minutes + ':' + seconds
  284. }
  285. }
  286. }
  287. </script>
  288. <style lang="scss" scoped>
  289. .container {
  290. .notes {
  291. margin-top: 110rpx;
  292. text-align: center;
  293. font-size: 28rpx;
  294. }
  295. .msg {
  296. margin-top: 10rpx;
  297. text-align: center;
  298. font-size: 28rpx;
  299. }
  300. .info {
  301. margin-top: 100rpx;
  302. height: 40rpx;
  303. text-align: center;
  304. color: #5393ff;
  305. }
  306. .photo {
  307. margin: 0 auto;
  308. margin-top: 180rpx;
  309. width: 375rpx;
  310. height: 375rpx;
  311. border-radius: 188rpx;
  312. overflow: hidden;
  313. .img {
  314. width: 100%;
  315. height: 100%;
  316. border-radius: 50%;
  317. }
  318. img {
  319. width: 100%;
  320. height: 100%;
  321. }
  322. }
  323. .button {
  324. margin: 0 auto;
  325. margin-top: 127rpx;
  326. width: 430rpx;
  327. height: 100rpx;
  328. line-height: 100rpx;
  329. text-align: center;
  330. color: #fff;
  331. border-radius: 21rpx;
  332. font-size: 32rpx;
  333. background: linear-gradient(180deg, rgba(0, 172, 252, 1) 0%, rgba(0, 130, 252, 1) 100%);
  334. }
  335. .popup-content {
  336. display: flex;
  337. flex-direction: column;
  338. justify-content: space-around;
  339. align-items: center;
  340. width: 630rpx;
  341. height: 376rpx;
  342. border-radius: 33rpx;
  343. background-color: #fff;
  344. .title {
  345. display: flex;
  346. justify-content: center;
  347. align-items: center;
  348. .icon {
  349. width: 40rpx;
  350. height: 40rpx;
  351. img {
  352. width: 100%;
  353. height: 100%;
  354. }
  355. }
  356. .title_info {
  357. margin-left: 8rpx;
  358. font-size: 36rpx;
  359. font-weight: 800;
  360. color: #0082fc;
  361. }
  362. }
  363. .time {
  364. font-size: 32rpx;
  365. }
  366. .popup_button {
  367. width: 50%;
  368. text-align: center;
  369. font-size: 32rpx;
  370. color: #0082fc;
  371. }
  372. }
  373. }
  374. </style>