ZimFace.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * 身份认证服务
  4. * @author wesmiler
  5. */
  6. namespace app\weixin\service;
  7. class ZimFace
  8. {
  9. // 文档 https://market.aliyun.com/products/57000002/cmapi00043344.html#sku=yuncode3734400001
  10. protected static $apiUrl = 'https://zimfaceid1.market.alicloudapi.com';
  11. protected static $checkUrl = 'https://api.megvii.com/faceid/lite/videoverify';
  12. protected static $appKey = '203953968';
  13. protected static $appSecret = 'nbP0eTttVbGFjXSI8GbY04tE89Mnw9ow';
  14. protected static $appCode = '689f5d471a2140ea8091c8f4e979f8dd';
  15. protected static $apiUrls = [
  16. 'init'=>'/comms/zfi/init',
  17. ];
  18. protected static $errors = [
  19. '0000'=> '成功',
  20. 'T005'=> '业务异常',
  21. '9999'=> '系统异常',
  22. '1999'=> '服务异常',
  23. ];
  24. protected static $resultErrors = [
  25. 'SUCCESS'=> '待比对照片与权威数据照片或参考照片对比是同一个人',
  26. 'PASS_LIVING_NOT_THE_SAME'=> '待比对照片与权威数据照片或参考照片对比不是同一个人',
  27. 'NO_ID_CARD_NUMBER'=> '无此身份证号',
  28. 'ID_NUMBER_NAME_NOT_MATCH'=> '身份证号与姓名不相符',
  29. 'IDCARD_PHOTO_FRONTSIDE'=> '身份证人像面识别错误或信息不匹配',
  30. 'IDCARD_BACKSIDE_BLURRED'=> '身份证国徽面识别错误',
  31. 'NO_FACE_FOUND_IDCARD'=> '身份证人像面找不到人脸',
  32. 'IDCARD_PHOTO_NOTFRONTSIDE'=> '非身份证人像面',
  33. 'IDCARD_PHOTO_NOTBACKSIDE'=> '非身份证国徽面',
  34. 'FAIL_OCR_FAKE_IDCARD'=> '假证',
  35. 'FAIL_LIVING_FACE_ATTACK'=> '云端活体验证失败',
  36. 'CHANGE_FACE_ATTACK'=> '活体验证视频中发生了换脸攻击',
  37. 'NO_FACE_FOUND'=> '活体验证视频中没有检测到人脸',
  38. 'FACE_QUALITY_TOO_LOW'=> '活体验证视频中质量太差',
  39. 'INVALID_VIDEO_DURATION'=> '活体验证视频中长度不符合要求(2s~20s)',
  40. 'VIDEO_TOO_LARGE'=> '活体验证视频过大',
  41. 'SR_ERROR'=> '活体验证视频中,用户读数语音不符合要求',
  42. 'NOT_SYNCHRONIZED'=> '活体验证视频中,用户读数唇语不符合要求',
  43. 'NO_AUDIO'=> '活体验证视频无声音',
  44. 'VIDEO_FORMAT_UNSUPPORTED'=> '活体验证视频格式无法识别',
  45. 'LIP_VOICE_NOT_SYNC'=> '活体验证视频中语音唇语不同步',
  46. 'VIDEO_OK'=> '活体验证视频可用',
  47. 'VIDEO_MANY_TIMES'=> '活体验证视频上传超过阈值',
  48. 'VIDEO_INTERNAL_ERROR'=> '活体验证内部错误',
  49. 'NON_ENTERPRISE_CERTIFICATION'=> '客户未进行企业认证',
  50. 'BALANCE_NOT_ENOUGH'=> '余额不足',
  51. 'ACCOUNT_DISABLED'=> '账户已停用',
  52. 'USER_CANCEL'=> '用户主动退出流程',
  53. 'LIVING_NOT_START'=> '验证流程尚未开始',
  54. ];
  55. /**
  56. * 获取验证参数Token
  57. * @param $params
  58. * @return array|int
  59. */
  60. public static function check($userId, $params){
  61. $realname = isset($params['realname'])? trim($params['realname']) : '';
  62. $idcard = isset($params['idcard'])? trim($params['idcard']) : '';
  63. if(empty($realname) || empty($idcard) || empty($userId)){
  64. return 2012;
  65. }
  66. $uuid = 'U'.date('Ymd').rand(10,99).$userId;
  67. $data = [
  68. 'bizId'=> $uuid,
  69. 'idName'=> $realname,
  70. 'idNumber'=> $idcard,
  71. 'livingType'=> rand(100,999)>500? 4 : 3, // 1-远近,3-摇头,4-点头,6炫彩
  72. // 'type'=> 'true', // 0:(默认值)优先选择静默活体,如果静默活体不支持那么用交互活体;1:只用静默活体(有浏览器兼容性问题);2:只用交互活体
  73. 'needVideo'=> 'false', // 是否返回视频
  74. 'notifyUrl'=> request()->domain().'/api/notify/face', // 异步回调地址
  75. 'returnUrl'=> request()->domain().'/weixin/auth/authResult', // 回调跳转页面
  76. ];
  77. // 参数配置
  78. $smsConfig = cmf_get_option('idenauth_config');
  79. $appKey = isset($params['appKey']) && $params['appKey']?trim($params['appKey']) : self::$appKey;
  80. $appCode = isset($params['appCode'])&&$params['appCode']?trim($params['appCode']) : self::$appCode;
  81. $appSecret = isset($params['appSecret'])&&$params['appSecret']?trim($params['appSecret']) : self::$appSecret;
  82. // 构建请求参数
  83. $headers = [
  84. "Authorization:APPCODE {$appCode}",
  85. "X-Ca-Nonce:{$uuid}",
  86. "Content-Type:application/x-www-form-urlencoded; charset=UTF-8",
  87. ];
  88. $url = self::$apiUrl.self::$apiUrls['init'];
  89. $query = ZimFace::getParams($data);
  90. PRedis::set("caches:zimAuth:{$userId}:request", ['url'=> $url,'query'=> $query,'headers'=> $headers], 600);
  91. $result = httpHeaderRequest($url,$query,'post',$headers);
  92. $code = isset($result['code'])? $result['code'] : '';
  93. $msg = isset($result['msg'])? $result['msg'] : '';
  94. PRedis::set("caches:faceAuth:{$userId}:result", $result, 600);
  95. $url = isset($result['url'])? $result['url'] : '';
  96. if($code!='0000' || empty($url)){
  97. $msg = $msg? $msg : (isset(self::$errors[$code])? self::$errors[$code] : '获取在线验证参数失败,请刷新重试');
  98. return ['code'=> 'error', 'msg'=> $msg, 'result'=> $result];
  99. }
  100. //$url = str_replace('https://edis.esandcloud.com/faceId/lite/ocridcard/fronthd', self::$checkUrl, $url);
  101. return ['code'=> 'success', 'msg'=> '获取验证参数成功', 'result'=> ['result'=> $result, 'url'=> $url]];
  102. }
  103. public static function getResult($token){
  104. if(empty($token)){
  105. return false;
  106. }
  107. // 参数配置
  108. $smsConfig = cmf_get_option('idenauth_config');
  109. $appKey = isset($params['appKey']) && $params['appKey']?trim($params['appKey']) : self::$appKey;
  110. $appCode = isset($params['appCode'])&&$params['appCode']?trim($params['appCode']) : self::$appCode;
  111. $appSecret = isset($params['appSecret'])&&$params['appSecret']?trim($params['appSecret']) : self::$appSecret;
  112. // 构建请求参数
  113. $headers = [
  114. "Authorization:APPCODE {$appCode}",
  115. "Content-Type:application/x-www-form-urlencoded; charset=UTF-8",
  116. ];
  117. $url = sprintf(self::$apiUrl.self::$apiUrls['getResult'], $token);
  118. PRedis::set("caches:faceAuth:result:request_{$token}", ['url'=> $url,'headers'=> $headers], 600);
  119. $result = httpHeaderRequest($url, '','get',$headers);
  120. $resultCode = isset($result['result_code'])? $result['result_code'] : '';
  121. $resultMsg = isset($result['result_message'])? $result['result_message'] : '';
  122. $bizToken = isset($result['biz_token'])? $result['biz_token'] : '';
  123. $images = isset($result['images'])? $result['images'] : [];
  124. PRedis::set("caches:faceAuth:result:result_{$token}", $result, 600);
  125. if($resultCode != 'SUCCESS'){
  126. $msg = isset(self::$resultErrors[$resultMsg])? self::$resultErrors[$resultMsg] : '获取验证结果失败';
  127. return ['code'=> 'error', 'msg'=> $msg, 'result'=> $result];
  128. }
  129. return ['code'=> 'success', 'msg'=> '获取验证结果', 'result'=> $result];
  130. }
  131. /**
  132. * 格式化请求参数
  133. * @param array $params
  134. * @return false|string
  135. */
  136. public static function getParams(array $params){
  137. $data = [];
  138. if(empty($params)){
  139. return false;
  140. }
  141. foreach ($params as $key => $val){
  142. $data[] = $key.'='.$val;
  143. }
  144. return $data? implode('&', $data) : '';
  145. }
  146. /**
  147. * 处理回调
  148. * @param $params
  149. */
  150. public static function catchNotify($params){
  151. }
  152. }