baiduUtil.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. callback(res.data)
  41. }
  42. })
  43. }
  44. // 对比
  45. export function match(token, face1, face2, callback) {
  46. let data = [{
  47. image: face1,
  48. image_type: 'BASE64',
  49. liveness_control: 'NORMAL',
  50. }, {
  51. image: face2,
  52. image_type: 'BASE64'
  53. }]
  54. uni.request({
  55. url: '/baiduApi/rest/2.0/face/v3/match?access_token=' + token,
  56. data: data,
  57. method: 'POST',
  58. header: {
  59. 'Content-Type': 'application/json'
  60. },
  61. success: (res) => {
  62. callback(res)
  63. },
  64. error: (err) => {
  65. console.log("对比失败,", err);
  66. }
  67. })
  68. }
  69. // 创建组
  70. export function createGroup(token, groupId, callback) {
  71. uni.request({
  72. url: '/baiduApi/rest/2.0/face/v3/faceset/group/add?access_token=' + token,
  73. data: {
  74. group_id: groupId,
  75. },
  76. method: 'POST',
  77. header: {
  78. 'Content-Type': 'application/x-www-form-urlencoded'
  79. },
  80. success: (res) => {
  81. callback(res)
  82. },
  83. error: (err) => {
  84. console.log("创建组失败,", err);
  85. }
  86. })
  87. }
  88. // 人脸注册
  89. export function faceAdd(token, face, groupId, user_id, callback) {
  90. // https://cloud.baidu.com/doc/FACE/s/Gk37c1uzc#%E4%BA%BA%E8%84%B8%E6%B3%A8%E5%86%8C
  91. let data = {
  92. image: face,
  93. image_type: 'BASE64',
  94. group_id: groupId,
  95. user_id: user_id,
  96. action_type: 'REPLACE', // 操作方式 APPEND: 当user_id在库中已经存在时,对此user_id重复注册时,新注册的图片默认会追加到该user_id下 REPLACE : 当对此user_id重复注册时,则会用新图替换库中该user_id下所有图片 默认使用APPEND
  97. }
  98. uni.request({
  99. url: '/baiduApi/rest/2.0/face/v3/faceset/user/add?access_token=' + token,
  100. data: data,
  101. method: 'POST',
  102. header: {
  103. 'Content-Type': 'application/json'
  104. },
  105. success: (res) => {
  106. callback(res)
  107. },
  108. error: (err) => {
  109. console.log("人脸注册失败,", err);
  110. }
  111. })
  112. }
  113. // 人脸搜索
  114. export function faceSearch(token, face, groupId, callback) {
  115. let data = {
  116. image: face,
  117. image_type: 'BASE64',
  118. group_id_list: groupId,
  119. match_threshold: config.score,
  120. max_user_num: 50
  121. }
  122. uni.request({
  123. url: '/baiduApi/rest/2.0/face/v3/search?access_token=' + token,
  124. data: data,
  125. method: 'POST',
  126. header: {
  127. 'Content-Type': 'application/json'
  128. },
  129. success: (res) => {
  130. callback(res)
  131. },
  132. error: (err) => {
  133. console.log("人脸搜索失败,", err);
  134. }
  135. })
  136. }