authentication.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <template>
  2. <view class="container">
  3. <view class="notes">
  4. 拍摄您本人人脸,确保对准手机,光线充足
  5. </view>
  6. <view class="msg">
  7. {{name}} {{cardNumber}}
  8. </view>
  9. <view class="info">
  10. <span v-if="tipsText">{{tipsText}}</span>
  11. </view>
  12. <view class="photo">
  13. <camera class="img" v-if='isAuthCamera' device-position="front" flash="off" resolution='high' />
  14. <img v-else :src="tempImg">
  15. </view>
  16. <view class="button" @click="handleTakePhotoClick">
  17. 开启身份认证
  18. </view>
  19. <!-- 认证结果弹窗 -->
  20. <uni-popup ref="popup" :is-mask-click="false">
  21. <view class="popup-content">
  22. <view class="title">
  23. <view class="icon">
  24. <img src="./imgs/success.png">
  25. </view>
  26. <view class="title_info">
  27. 打卡成功
  28. </view>
  29. </view>
  30. <view class="time">
  31. {{nowTime}}
  32. </view>
  33. <view class="popup_button" @click="handleGoHome">
  34. 我知道了
  35. </view>
  36. </view>
  37. </uni-popup>
  38. </view>
  39. </template>
  40. <script>
  41. export default {
  42. data() {
  43. return {
  44. // 错误文案提示
  45. tipsText: '',
  46. // 当前时间
  47. nowTime: "",
  48. // 本地图片路径 被匹对照片
  49. tempImg: '',
  50. // 场景照片
  51. sceneImage: "",
  52. // 经纬度
  53. lat: "",
  54. lng: "",
  55. // 地址
  56. location: "",
  57. // 规则id
  58. id: "",
  59. // 相机引擎
  60. cameraEngine: null,
  61. // 是否拥有相机权限
  62. isAuthCamera: true,
  63. // 姓名
  64. name: "",
  65. // 身份证号
  66. cardNumber: ""
  67. };
  68. },
  69. onLoad(options) {
  70. this.id = options.id
  71. this.lat = options.latitude
  72. this.lng = options.longitude
  73. this.location = options.address
  74. this.sceneImage = options.imgUrl
  75. this.initData()
  76. let userInfo = uni.getStorageSync("userInfo")
  77. if (userInfo) {
  78. this.name = userInfo.name
  79. this.cardNumber = userInfo.cardNumber
  80. }
  81. },
  82. methods: {
  83. // 初始化相机引擎
  84. initData() {
  85. // #ifdef MP-WEIXIN
  86. // 1、初始化人脸识别
  87. wx.initFaceDetect()
  88. // 2、创建 camera 上下文 CameraContext 对象
  89. this.cameraEngine = wx.createCameraContext()
  90. // 3、获取 Camera 实时帧数据
  91. const listener = this.cameraEngine.onCameraFrame((frame) => {
  92. if (this.tempImg) {
  93. return;
  94. }
  95. // 4、人脸识别,使用前需要通过 wx.initFaceDetect 进行一次初始化,推荐使用相机接口返回的帧数据
  96. wx.faceDetect({
  97. frameBuffer: frame.data,
  98. width: frame.width,
  99. height: frame.height,
  100. enablePoint: true,
  101. enableConf: true,
  102. enableAngle: true,
  103. enableMultiFace: true,
  104. success: (faceData) => {
  105. let face = faceData.faceInfo[0]
  106. if (faceData.x == -1 || faceData.y == -1) {
  107. this.tipsText = '检测不到人'
  108. }
  109. if (faceData.faceInfo.length > 1) {
  110. this.tipsText = '请保证只有一个人'
  111. } else {
  112. const {
  113. pitch,
  114. roll,
  115. yaw
  116. } = face.angleArray;
  117. const standard = 0.5
  118. if (Math.abs(pitch) >= standard || Math.abs(roll) >= standard ||
  119. Math.abs(yaw) >= standard) {
  120. this.tipsText = '请平视摄像头'
  121. } else if (face.confArray.global <= 0.8 || face.confArray.leftEye <=
  122. 0.8 || face.confArray.mouth <= 0.8 || face.confArray.nose <= 0.8 ||
  123. face.confArray.rightEye <= 0.8) {
  124. this.tipsText = '请勿遮挡五官'
  125. } else {
  126. this.tipsText = '请点击下方按钮开始认证'
  127. // 这里可以写自己的逻辑了
  128. }
  129. }
  130. },
  131. fail: (err) => {
  132. if (err.x == -1 || err.y == -1) {
  133. this.tipsText = '检测不到人'
  134. } else {
  135. this.tipsText = err.errMsg || '网络错误,请退出页面重试'
  136. }
  137. },
  138. })
  139. })
  140. // 5、开始监听帧数据
  141. listener.start()
  142. // #endif
  143. },
  144. // 拍照点击
  145. handleTakePhotoClick() {
  146. if (this.tipsText != "" && this.tipsText != "请点击下方按钮开始认证") {
  147. return;
  148. }
  149. uni.getSetting({
  150. success: (res) => {
  151. if (!res.authSetting['scope.camera']) {
  152. this.isAuthCamera = true
  153. uni.openSetting({
  154. success: (res) => {
  155. if (res.authSetting['scope.camera']) {
  156. this.isAuthCamera = true;
  157. }
  158. }
  159. })
  160. }
  161. }
  162. })
  163. this.cameraEngine.takePhoto({
  164. quality: "high",
  165. success: ({
  166. tempImagePath
  167. }) => {
  168. this.tempImg = tempImagePath
  169. this.isAuthCamera = false
  170. this.tipsText = ""
  171. this.handleOkClick()
  172. }
  173. })
  174. },
  175. // 上传
  176. handleOkClick() {
  177. // 这里的 this.tempImg 是经过人脸检测后 拍照拿到的路径
  178. this.upLoadOne()
  179. },
  180. upLoadOne() {
  181. uni.showLoading({
  182. title: "检测中,请稍后...",
  183. });
  184. setTimeout(async () => {
  185. let res = await this.$myRequest_clockIn({
  186. url: "/attendance/api/sign/check/in/update",
  187. method: "put",
  188. header: {
  189. 'Authorization': uni.getStorageSync("token")
  190. },
  191. data: {
  192. id: this.id,
  193. lat: this.lat,
  194. lng: this.lng,
  195. location: this.location,
  196. matchFaceImage: this.tempImg,
  197. sceneImage: this.sceneImage,
  198. }
  199. })
  200. // console.log(res);
  201. if (res.code == 200) {
  202. this.getNowTime()
  203. this.$refs.popup.open()
  204. }
  205. }, 2000)
  206. },
  207. // 点击 我知道了按钮 跳回首页
  208. handleGoHome() {
  209. this.$refs.popup.close()
  210. uni.reLaunch({
  211. url: "/pagesClockIn/home/home"
  212. })
  213. },
  214. // 获取当前时间
  215. getNowTime() {
  216. let date = new Date()
  217. let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  218. let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  219. let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  220. this.nowTime = hours + ':' + minutes + ':' + seconds
  221. }
  222. },
  223. }
  224. </script>
  225. <style lang="scss" scoped>
  226. .container {
  227. .notes {
  228. margin-top: 110rpx;
  229. text-align: center;
  230. font-size: 28rpx;
  231. }
  232. .msg {
  233. margin-top: 10rpx;
  234. text-align: center;
  235. font-size: 28rpx;
  236. }
  237. .info {
  238. margin-top: 100rpx;
  239. height: 40rpx;
  240. text-align: center;
  241. }
  242. .photo {
  243. margin: 0 auto;
  244. margin-top: 40rpx;
  245. width: 375rpx;
  246. height: 375rpx;
  247. border-radius: 188rpx;
  248. overflow: hidden;
  249. .img {
  250. width: 100%;
  251. height: 100%;
  252. border-radius: 50%;
  253. }
  254. img {
  255. width: 100%;
  256. height: 100%;
  257. }
  258. }
  259. .button {
  260. margin: 0 auto;
  261. margin-top: 127rpx;
  262. width: 430rpx;
  263. height: 100rpx;
  264. line-height: 100rpx;
  265. text-align: center;
  266. color: #fff;
  267. border-radius: 21rpx;
  268. font-size: 32rpx;
  269. background: linear-gradient(180deg, rgba(0, 172, 252, 1) 0%, rgba(0, 130, 252, 1) 100%);
  270. }
  271. .popup-content {
  272. display: flex;
  273. flex-direction: column;
  274. justify-content: space-around;
  275. align-items: center;
  276. width: 630rpx;
  277. height: 376rpx;
  278. border-radius: 33rpx;
  279. background-color: #fff;
  280. .title {
  281. display: flex;
  282. justify-content: center;
  283. align-items: center;
  284. .icon {
  285. width: 40rpx;
  286. height: 40rpx;
  287. img {
  288. width: 100%;
  289. height: 100%;
  290. }
  291. }
  292. .title_info {
  293. margin-left: 8rpx;
  294. font-size: 36rpx;
  295. font-weight: 800;
  296. color: #0082FC;
  297. }
  298. }
  299. .time {
  300. font-size: 32rpx;
  301. }
  302. .popup_button {
  303. width: 50%;
  304. text-align: center;
  305. font-size: 32rpx;
  306. color: #0082FC;
  307. }
  308. }
  309. }
  310. </style>