index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <!-- <web-view src="../../static/index.html"></web-view> -->
  3. </template>
  4. <script>
  5. import {
  6. getUser
  7. } from '@/common/auth.js';
  8. import config from '@/config.js';
  9. import {
  10. translate,
  11. base64ToUrl
  12. } from '@/common/image.js'
  13. export default {
  14. data() {
  15. return {
  16. face: '',
  17. groupId: '',
  18. userInfo: {},
  19. // 百度云access_token
  20. token: '',
  21. client_id: config.face_client_id,
  22. client_secret: config.face_client_secret,
  23. }
  24. },
  25. onLoad() {
  26. this.getImage()
  27. this.userInfo = getUser()
  28. this.groupId = this.userInfo.dept.locationCode.substring(0, 6)
  29. },
  30. methods: {
  31. getImage() {
  32. uni.chooseImage({
  33. count: 1,
  34. sourceType: ['camera'],
  35. sizeType: ['compressed'],
  36. success: (res) => {
  37. uni.showLoading({
  38. title: '正在上传中...'
  39. })
  40. let size = res.tempFiles[0].size
  41. let scale = 1
  42. if (size / 1024 / 1024 > 0.9) scale = 0.6
  43. translate(res.tempFilePaths[0], scale, 'blob', this.setSrc)
  44. }
  45. })
  46. },
  47. setSrc(e, blobUrl) {
  48. this.src = blobUrl
  49. uni.getFileInfo({
  50. filePath: blobUrl,
  51. success: (res) => {
  52. console.log(res);
  53. let size = res.size
  54. let scale = 1
  55. if (size / 1024 / 1024 > 0.9) {
  56. scale = 0.6
  57. translate(this.src, scale, 'blob', this.setSrc)
  58. } else {
  59. e = e.replace('data:image/jpeg;base64,', "");
  60. this.face = e
  61. this.getAccessToken()
  62. }
  63. },
  64. fail: (err) => {
  65. console.log("err:", err);
  66. }
  67. })
  68. },
  69. // 人脸搜索
  70. faceSearch() {
  71. let face = this.face
  72. let data = {
  73. image: face,
  74. image_type: 'BASE64',
  75. group_id_list: this.groupId,
  76. match_threshold: config.score
  77. }
  78. console.log("人脸搜索:", data)
  79. uni.request({
  80. url: '/baiduApi/rest/2.0/face/v3/search?access_token=' + this.token,
  81. data: data,
  82. method: 'POST',
  83. header: {
  84. 'Content-Type': 'application/json'
  85. },
  86. success: (res) => {
  87. uni.hideLoading()
  88. console.log("人脸搜索:", res)
  89. if (res.data.error_msg == 'SUCCESS') {
  90. // this.isSearch = true
  91. if (res.data.result.user_list.length < 1) {
  92. console.log("人脸不存在");
  93. } else {
  94. }
  95. } else {
  96. console.log("人脸不存在1111");
  97. }
  98. },
  99. error: (err) => {
  100. uni.hideLoading()
  101. }
  102. })
  103. },
  104. getAccessToken() {
  105. uni.request({
  106. url: '/baiduApi/oauth/2.0/token',
  107. data: {
  108. grant_type: 'client_credentials',
  109. client_id: this.client_id,
  110. client_secret: this.client_secret
  111. },
  112. method: 'POST',
  113. header: {
  114. 'Content-Type': 'application/x-www-form-urlencoded'
  115. },
  116. success: (res) => {
  117. if (res.statusCode == 200) {
  118. this.token = res.data.access_token
  119. this.faceSearch()
  120. }
  121. },
  122. error: (err) => {
  123. uni.hideLoading()
  124. }
  125. })
  126. },
  127. }
  128. }
  129. </script>
  130. <style>
  131. </style>