authentication.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <template>
  2. <view class="container">
  3. <view class="notes">
  4. 拍摄您本人人脸,确保对准手机,光线充足
  5. </view>
  6. <view class="msg">
  7. 罗东 265656862656521
  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. tempImg: '',
  48. // 相机引擎
  49. cameraEngine: null,
  50. // 是否拥有相机权限
  51. isAuthCamera: true,
  52. // 当前时间
  53. nowTime: "",
  54. };
  55. },
  56. onLoad(options) {
  57. this.initData()
  58. },
  59. methods: {
  60. // 初始化相机引擎
  61. initData() {
  62. // #ifdef MP-WEIXIN
  63. // 1、初始化人脸识别
  64. wx.initFaceDetect()
  65. // 2、创建 camera 上下文 CameraContext 对象
  66. this.cameraEngine = wx.createCameraContext()
  67. // 3、获取 Camera 实时帧数据
  68. const listener = this.cameraEngine.onCameraFrame((frame) => {
  69. if (this.tempImg) {
  70. return;
  71. }
  72. // 4、人脸识别,使用前需要通过 wx.initFaceDetect 进行一次初始化,推荐使用相机接口返回的帧数据
  73. wx.faceDetect({
  74. frameBuffer: frame.data,
  75. width: frame.width,
  76. height: frame.height,
  77. enablePoint: true,
  78. enableConf: true,
  79. enableAngle: true,
  80. enableMultiFace: true,
  81. success: (faceData) => {
  82. let face = faceData.faceInfo[0]
  83. if (faceData.x == -1 || faceData.y == -1) {
  84. this.tipsText = '检测不到人'
  85. }
  86. if (faceData.faceInfo.length > 1) {
  87. this.tipsText = '请保证只有一个人'
  88. } else {
  89. const {
  90. pitch,
  91. roll,
  92. yaw
  93. } = face.angleArray;
  94. const standard = 0.5
  95. if (Math.abs(pitch) >= standard || Math.abs(roll) >= standard ||
  96. Math.abs(yaw) >= standard) {
  97. this.tipsText = '请平视摄像头'
  98. } else if (face.confArray.global <= 0.8 || face.confArray.leftEye <=
  99. 0.8 || face.confArray.mouth <= 0.8 || face.confArray.nose <= 0.8 ||
  100. face.confArray.rightEye <= 0.8) {
  101. this.tipsText = '请勿遮挡五官'
  102. } else {
  103. this.tipsText = '请点击下方按钮开始认证'
  104. // 这里可以写自己的逻辑了
  105. }
  106. }
  107. },
  108. fail: (err) => {
  109. if (err.x == -1 || err.y == -1) {
  110. this.tipsText = '检测不到人'
  111. } else {
  112. this.tipsText = err.errMsg || '网络错误,请退出页面重试'
  113. }
  114. },
  115. })
  116. })
  117. // 5、开始监听帧数据
  118. listener.start()
  119. // #endif
  120. },
  121. // 拍照点击
  122. handleTakePhotoClick() {
  123. if (this.tipsText != "" && this.tipsText != "请点击下方按钮开始认证") {
  124. return;
  125. }
  126. uni.getSetting({
  127. success: (res) => {
  128. if (!res.authSetting['scope.camera']) {
  129. this.isAuthCamera = true
  130. uni.openSetting({
  131. success: (res) => {
  132. if (res.authSetting['scope.camera']) {
  133. this.isAuthCamera = true;
  134. }
  135. }
  136. })
  137. }
  138. }
  139. })
  140. this.cameraEngine.takePhoto({
  141. quality: "high",
  142. success: ({
  143. tempImagePath
  144. }) => {
  145. this.tempImg = tempImagePath
  146. this.isAuthCamera = false
  147. this.tipsText = ""
  148. this.handleOkClick()
  149. }
  150. })
  151. },
  152. // 上传
  153. handleOkClick() {
  154. // 这里的 this.tempImg 是经过人脸检测后 拍照拿到的路径
  155. this.upLoadOne(this.tempImg)
  156. },
  157. upLoadOne(imgPath) {
  158. uni.showLoading({
  159. title: "检测中,请稍后...",
  160. });
  161. setTimeout(() => {
  162. uni.hideLoading();
  163. this.getNowTime()
  164. this.$refs.popup.open()
  165. }, 2000)
  166. // 然后这里imgPath 传过来的是 要上传的临时本地图片的路径
  167. // 具体上传方法根据自己的请求方式 请求自己的接口
  168. },
  169. // 点击 我知道了按钮 跳回首页
  170. handleGoHome() {
  171. this.$refs.popup.close()
  172. uni.reLaunch({
  173. url: "/pages/home/home"
  174. })
  175. },
  176. // 获取当前时间
  177. getNowTime() {
  178. let date = new Date()
  179. let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  180. let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  181. let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  182. this.nowTime = hours + ':' + minutes + ':' + seconds
  183. // console.log(this.nowTime);
  184. }
  185. },
  186. }
  187. </script>
  188. <style lang="scss" scoped>
  189. .container {
  190. .notes {
  191. margin-top: 110rpx;
  192. text-align: center;
  193. font-size: 28rpx;
  194. }
  195. .msg {
  196. margin-top: 10rpx;
  197. text-align: center;
  198. font-size: 28rpx;
  199. }
  200. .info {
  201. margin-top: 100rpx;
  202. height: 40rpx;
  203. text-align: center;
  204. }
  205. .photo {
  206. margin: 0 auto;
  207. margin-top: 40rpx;
  208. width: 375rpx;
  209. height: 375rpx;
  210. border-radius: 188rpx;
  211. overflow: hidden;
  212. .img {
  213. width: 100%;
  214. height: 100%;
  215. border-radius: 50%;
  216. }
  217. img {
  218. width: 100%;
  219. height: 100%;
  220. }
  221. }
  222. .button {
  223. margin: 0 auto;
  224. margin-top: 127rpx;
  225. width: 430rpx;
  226. height: 100rpx;
  227. line-height: 100rpx;
  228. text-align: center;
  229. color: #fff;
  230. border-radius: 21rpx;
  231. font-size: 32rpx;
  232. background: linear-gradient(180deg, rgba(0, 172, 252, 1) 0%, rgba(0, 130, 252, 1) 100%);
  233. }
  234. .popup-content {
  235. display: flex;
  236. flex-direction: column;
  237. justify-content: space-around;
  238. align-items: center;
  239. width: 630rpx;
  240. height: 376rpx;
  241. border-radius: 33rpx;
  242. background-color: #fff;
  243. .title {
  244. display: flex;
  245. justify-content: center;
  246. align-items: center;
  247. .icon {
  248. width: 40rpx;
  249. height: 40rpx;
  250. img {
  251. width: 100%;
  252. height: 100%;
  253. }
  254. }
  255. .title_info {
  256. margin-left: 8rpx;
  257. font-size: 36rpx;
  258. font-weight: 800;
  259. color: #0082FC;
  260. }
  261. }
  262. .time {
  263. font-size: 32rpx;
  264. }
  265. .popup_button {
  266. width: 50%;
  267. text-align: center;
  268. font-size: 32rpx;
  269. color: #0082FC;
  270. }
  271. }
  272. }
  273. </style>