authentication.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <template>
  2. <view class="container">
  3. <view class="msg">{{ name }} {{ cardNumber }}</view>
  4. <view class="photo">
  5. <camera class="img" v-if="isAuthCamera" device-position="front" flash="off" resolution="high" />
  6. <img v-else :src="tempImg" />
  7. </view>
  8. <view class="button" @click="handleTakePhotoClick">开始拍照</view>
  9. <!-- 认证结果弹窗 -->
  10. <uni-popup ref="popup" :is-mask-click="false">
  11. <view class="popup-content">
  12. <view class="title">
  13. <view class="icon"><img src="./imgs/success.png" /></view>
  14. <view class="title_info">打卡成功</view>
  15. </view>
  16. <view class="time">{{ nowTime }}</view>
  17. <view class="popup_button" @click="handleGoHome">我知道了</view>
  18. </view>
  19. </uni-popup>
  20. </view>
  21. </template>
  22. <script>
  23. import { encrypt, decrypt } from '../util/encryp.js'
  24. import { RSAencrypt } from '../util/WxmpRsa.js'
  25. export default {
  26. data() {
  27. return {
  28. // 当前时间
  29. nowTime: '',
  30. // 本地图片路径 被匹对照片
  31. tempImg: '',
  32. // 场景照片
  33. sceneImage: '',
  34. // 经纬度
  35. lat: '',
  36. lng: '',
  37. // 地址
  38. location: '',
  39. // 规则id
  40. id: '',
  41. // 相机引擎
  42. cameraEngine: null,
  43. // 是否拥有相机权限
  44. isAuthCamera: true,
  45. // 姓名
  46. name: '',
  47. // 身份证号
  48. cardNumber: '',
  49. // 是否需要场景照片,值为1时是不需要
  50. flag: ''
  51. }
  52. },
  53. onLoad(options) {
  54. let userInfo = uni.getStorageSync('userInfo')
  55. if (userInfo) {
  56. this.name = userInfo.name
  57. this.cardNumber = userInfo.cardNumber
  58. }
  59. this.id = options.id
  60. this.lat = options.latitude
  61. this.lng = options.longitude
  62. this.location = options.address
  63. if (options.imgUrl) {
  64. this.sceneImage = options.imgUrl
  65. }
  66. if (options.flag) {
  67. this.flag = options.flag
  68. }
  69. this.initData()
  70. },
  71. methods: {
  72. // 初始化相机引擎
  73. initData() {
  74. // 创建 camera 上下文 CameraContext 对象
  75. this.cameraEngine = uni.createCameraContext()
  76. // 获取 Camera 实时帧数据
  77. const listener = this.cameraEngine.onCameraFrame((frame) => {
  78. if (this.tempImg) {
  79. return
  80. }
  81. })
  82. // 5、开始监听帧数据
  83. listener.start()
  84. },
  85. // 拍照点击
  86. handleTakePhotoClick() {
  87. uni.getSetting({
  88. success: (res) => {
  89. if (!res.authSetting['scope.camera']) {
  90. this.isAuthCamera = true
  91. uni.openSetting({
  92. success: (res) => {
  93. if (res.authSetting['scope.camera']) {
  94. this.isAuthCamera = true
  95. }
  96. }
  97. })
  98. }
  99. }
  100. })
  101. this.cameraEngine.takePhoto({
  102. quality: 'high',
  103. success: ({ tempImagePath }) => {
  104. this.tempImg = tempImagePath
  105. this.isAuthCamera = false
  106. this.handleOkClick()
  107. }
  108. })
  109. },
  110. // 上传
  111. handleOkClick() {
  112. // 这里的 this.tempImg 是拍照拿到的图片路径
  113. this.upLoadOne(this.tempImg)
  114. },
  115. upLoadOne(url) {
  116. uni.showLoading({
  117. title: '认证中,请稍后...'
  118. })
  119. setTimeout(() => {
  120. uni.uploadFile({
  121. url: `https://chtech.ncjti.edu.cn/gxy/facedemo/face-api/ihotel/mapper/face/compareTwoFace`,
  122. filePath: url,
  123. name: 'file',
  124. formData: {
  125. mCardNumber: this.cardNumber
  126. },
  127. header: {
  128. platform: 2,
  129. 'Accept-Language': 'zh-CN,zh;q=0.9'
  130. },
  131. success: (uploadFileRes) => {
  132. // console.log(uploadFileRes)
  133. let temRes = JSON.parse(uploadFileRes.data)
  134. if (temRes.code === '200') {
  135. let imgUrl = temRes.data.fileUrl
  136. uni.showToast({
  137. title: '对比成功'
  138. })
  139. setTimeout(() => {
  140. this.handleUploading(imgUrl)
  141. }, 1500)
  142. } else {
  143. uni.showToast({
  144. title: `${temRes.message},请重新认证`,
  145. icon: 'none',
  146. duration: 2000
  147. })
  148. this.tempImg = ''
  149. this.isAuthCamera = true
  150. }
  151. },
  152. fail: () => {
  153. uni.showToast({
  154. title: '上传失败',
  155. icon: 'error'
  156. })
  157. }
  158. })
  159. }, 1000)
  160. },
  161. async handleUploading(imgUrl) {
  162. let res = await this.$myRequest_clockIn({
  163. url: '/attendance/api/sign/check/in/update',
  164. method: 'put',
  165. header: {
  166. Authorization: uni.getStorageSync('token'),
  167. platform: 2,
  168. 'Accept-Language': 'zh-CN,zh;q=0.9'
  169. },
  170. data: {
  171. id: this.id,
  172. lat: this.lat,
  173. lng: this.lng,
  174. location: this.location,
  175. matchFaceImage: imgUrl,
  176. sceneImage: this.flag == 1 ? '' : this.sceneImage
  177. }
  178. })
  179. // console.log(res);
  180. if (res.code == 200) {
  181. this.getNowTime()
  182. this.$refs.popup.open()
  183. }
  184. },
  185. // 点击 我知道了按钮 跳回首页
  186. handleGoHome() {
  187. this.$refs.popup.close()
  188. uni.reLaunch({
  189. url: '/pagesClockIn/home/home'
  190. })
  191. },
  192. // 获取当前时间
  193. getNowTime() {
  194. let date = new Date()
  195. let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  196. let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  197. let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  198. this.nowTime = hours + ':' + minutes + ':' + seconds
  199. }
  200. }
  201. }
  202. </script>
  203. <style lang="scss" scoped>
  204. .container {
  205. // .notes {
  206. // margin-top: 110rpx;
  207. // text-align: center;
  208. // font-size: 28rpx;
  209. // }
  210. .msg {
  211. margin-top: 120rpx;
  212. text-align: center;
  213. font-size: 28rpx;
  214. }
  215. .photo {
  216. margin: 0 auto;
  217. margin-top: 180rpx;
  218. width: 375rpx;
  219. height: 375rpx;
  220. border-radius: 188rpx;
  221. overflow: hidden;
  222. .img {
  223. width: 100%;
  224. height: 100%;
  225. border-radius: 50%;
  226. }
  227. img {
  228. width: 100%;
  229. height: 100%;
  230. }
  231. }
  232. .button {
  233. margin: 0 auto;
  234. margin-top: 127rpx;
  235. width: 430rpx;
  236. height: 100rpx;
  237. line-height: 100rpx;
  238. text-align: center;
  239. color: #fff;
  240. border-radius: 21rpx;
  241. font-size: 32rpx;
  242. background: linear-gradient(180deg, rgba(0, 172, 252, 1) 0%, rgba(0, 130, 252, 1) 100%);
  243. }
  244. .popup-content {
  245. display: flex;
  246. flex-direction: column;
  247. justify-content: space-around;
  248. align-items: center;
  249. width: 630rpx;
  250. height: 376rpx;
  251. border-radius: 33rpx;
  252. background-color: #fff;
  253. .title {
  254. display: flex;
  255. justify-content: center;
  256. align-items: center;
  257. .icon {
  258. width: 40rpx;
  259. height: 40rpx;
  260. img {
  261. width: 100%;
  262. height: 100%;
  263. }
  264. }
  265. .title_info {
  266. margin-left: 8rpx;
  267. font-size: 36rpx;
  268. font-weight: 800;
  269. color: #0082fc;
  270. }
  271. }
  272. .time {
  273. font-size: 32rpx;
  274. }
  275. .popup_button {
  276. width: 50%;
  277. text-align: center;
  278. font-size: 32rpx;
  279. color: #0082fc;
  280. }
  281. }
  282. }
  283. </style>