baiduUtil.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import config from '@/config.js'
  2. // 获取AccessToken
  3. export function getAccessToken(callback) {
  4. uni.request({
  5. url: '/baiduApi/oauth/2.0/token',
  6. data: {
  7. grant_type: 'client_credentials',
  8. client_id: config.face_client_id,
  9. client_secret: config.face_client_secret
  10. },
  11. method: 'POST',
  12. header: {
  13. 'Content-Type': 'application/x-www-form-urlencoded'
  14. },
  15. success: (res) => {
  16. // res.data.access_token
  17. callback(res.data.access_token)
  18. }
  19. })
  20. }
  21. // 身份证识别
  22. export function idcard(path, token, type, callback) {
  23. if (!type) type = 'front'
  24. uni.request({
  25. url: '/baiduApi/rest/2.0/ocr/v1/idcard',
  26. data: {
  27. image: path,
  28. access_token: token,
  29. id_card_side: type,
  30. detect_photo: true,
  31. detect_risk: true,
  32. detect_card: true,
  33. },
  34. timeout: 30000,
  35. method: 'POST',
  36. header: {
  37. 'Content-Type': 'application/x-www-form-urlencoded'
  38. },
  39. success: (res) => {
  40. // console.log("wwwwwww",res);
  41. callback(res.data)
  42. }
  43. })
  44. }
  45. // 对比
  46. export function match(token, face1, face2, callback) {
  47. let data = [{
  48. image: face1,
  49. image_type: 'BASE64',
  50. liveness_control: 'NORMAL',
  51. }, {
  52. image: face2,
  53. image_type: 'BASE64'
  54. }]
  55. console.log("人脸对比:", data)
  56. uni.request({
  57. url: '/baiduApi/rest/2.0/face/v3/match?access_token=' + token,
  58. data: data,
  59. method: 'POST',
  60. header: {
  61. 'Content-Type': 'application/json'
  62. },
  63. success: (res) => {
  64. callback(res)
  65. },
  66. error: (err) => {
  67. console.log("对比失败,", err);
  68. }
  69. })
  70. }
  71. // 创建组
  72. export function createGroup(token, groupId, callback) {
  73. uni.request({
  74. url: '/baiduApi/rest/2.0/face/v3/faceset/group/add?access_token=' + token,
  75. data: {
  76. group_id: groupId,
  77. },
  78. method: 'POST',
  79. header: {
  80. 'Content-Type': 'application/x-www-form-urlencoded'
  81. },
  82. success: (res) => {
  83. callback(res)
  84. },
  85. error: (err) => {
  86. console.log("创建组失败,", err);
  87. }
  88. })
  89. }
  90. // 人脸注册
  91. export function faceAdd(token, face, groupId, user_id, callback) {
  92. // https://cloud.baidu.com/doc/FACE/s/Gk37c1uzc#%E4%BA%BA%E8%84%B8%E6%B3%A8%E5%86%8C
  93. let data = {
  94. image: face,
  95. image_type: 'BASE64',
  96. group_id: groupId,
  97. user_id: user_id,
  98. action_type: 'REPLACE', // 操作方式 APPEND: 当user_id在库中已经存在时,对此user_id重复注册时,新注册的图片默认会追加到该user_id下 REPLACE : 当对此user_id重复注册时,则会用新图替换库中该user_id下所有图片 默认使用APPEND
  99. }
  100. console.log("人脸注册:", data)
  101. uni.request({
  102. url: '/baiduApi/rest/2.0/face/v3/faceset/user/add?access_token=' + token,
  103. data: data,
  104. method: 'POST',
  105. header: {
  106. 'Content-Type': 'application/json'
  107. },
  108. success: (res) => {
  109. callback(res)
  110. },
  111. error: (err) => {
  112. console.log("人脸注册失败,", err);
  113. }
  114. })
  115. }
  116. // 人脸搜索
  117. export function faceSearch(token, face, groupId, callback) {
  118. let data = {
  119. image: face,
  120. image_type: 'BASE64',
  121. group_id_list: groupId,
  122. match_threshold: config.score,
  123. max_user_num: 50
  124. }
  125. console.log("人脸搜索:", data)
  126. uni.request({
  127. url: '/baiduApi/rest/2.0/face/v3/search?access_token=' + token,
  128. data: data,
  129. method: 'POST',
  130. header: {
  131. 'Content-Type': 'application/json'
  132. },
  133. success: (res) => {
  134. callback(res)
  135. },
  136. error: (err) => {
  137. console.log("人脸搜索失败,", err);
  138. }
  139. })
  140. }