authentication.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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({
  186. url: "/attendance/api/sign/check/in/update",
  187. method: "put",
  188. header: {
  189. 'Authorization': uni.getStorageSync("token") ||
  190. 'eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo1MDQ2LCJ1c2VyX3V1aWQiOjEzNDc3NzE0NzM1NTY5NzE1MiwibmJmIjoxNjcxMTU1ODQzfQ.u4-N762Ijfb9RkuuFOFkeMiJQI9uCi0IaheJlGwi5Ms'
  191. },
  192. data: {
  193. id: this.id,
  194. lat: this.lat,
  195. lng: this.lng,
  196. location: this.location,
  197. matchFaceImage: this.tempImg,
  198. sceneImage: this.sceneImage,
  199. }
  200. })
  201. // console.log(res);
  202. if (res.code == 200) {
  203. this.getNowTime()
  204. this.$refs.popup.open()
  205. }
  206. }, 2000)
  207. },
  208. // 点击 我知道了按钮 跳回首页
  209. handleGoHome() {
  210. this.$refs.popup.close()
  211. uni.reLaunch({
  212. url: "/pages/home/home"
  213. })
  214. },
  215. // 获取当前时间
  216. getNowTime() {
  217. let date = new Date()
  218. let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  219. let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  220. let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  221. this.nowTime = hours + ':' + minutes + ':' + seconds
  222. }
  223. },
  224. }
  225. </script>
  226. <style lang="scss" scoped>
  227. .container {
  228. .notes {
  229. margin-top: 110rpx;
  230. text-align: center;
  231. font-size: 28rpx;
  232. }
  233. .msg {
  234. margin-top: 10rpx;
  235. text-align: center;
  236. font-size: 28rpx;
  237. }
  238. .info {
  239. margin-top: 100rpx;
  240. height: 40rpx;
  241. text-align: center;
  242. }
  243. .photo {
  244. margin: 0 auto;
  245. margin-top: 40rpx;
  246. width: 375rpx;
  247. height: 375rpx;
  248. border-radius: 188rpx;
  249. overflow: hidden;
  250. .img {
  251. width: 100%;
  252. height: 100%;
  253. border-radius: 50%;
  254. }
  255. img {
  256. width: 100%;
  257. height: 100%;
  258. }
  259. }
  260. .button {
  261. margin: 0 auto;
  262. margin-top: 127rpx;
  263. width: 430rpx;
  264. height: 100rpx;
  265. line-height: 100rpx;
  266. text-align: center;
  267. color: #fff;
  268. border-radius: 21rpx;
  269. font-size: 32rpx;
  270. background: linear-gradient(180deg, rgba(0, 172, 252, 1) 0%, rgba(0, 130, 252, 1) 100%);
  271. }
  272. .popup-content {
  273. display: flex;
  274. flex-direction: column;
  275. justify-content: space-around;
  276. align-items: center;
  277. width: 630rpx;
  278. height: 376rpx;
  279. border-radius: 33rpx;
  280. background-color: #fff;
  281. .title {
  282. display: flex;
  283. justify-content: center;
  284. align-items: center;
  285. .icon {
  286. width: 40rpx;
  287. height: 40rpx;
  288. img {
  289. width: 100%;
  290. height: 100%;
  291. }
  292. }
  293. .title_info {
  294. margin-left: 8rpx;
  295. font-size: 36rpx;
  296. font-weight: 800;
  297. color: #0082FC;
  298. }
  299. }
  300. .time {
  301. font-size: 32rpx;
  302. }
  303. .popup_button {
  304. width: 50%;
  305. text-align: center;
  306. font-size: 32rpx;
  307. color: #0082FC;
  308. }
  309. }
  310. }
  311. </style>