authentication.vue 6.1 KB

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