baiduUtil.js 3.1 KB

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