UserService.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace app\common\service;
  3. use fast\Random;
  4. use think\Exception;
  5. class UserService
  6. {
  7. /**
  8. * 获取请求参数
  9. * @return void
  10. */
  11. public function faceAuth($params=[])
  12. {
  13. $result = [
  14. 'status' => 1,
  15. 'msg' => '获取成功',
  16. 'data' => [],
  17. ];
  18. try {
  19. $idCard = isset($params['id_card']) ? $params['id_card'] : '';
  20. $realName = isset($params['real_name']) ? $params['real_name'] : '';
  21. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  22. $tencentConfig = config('tencent_yun');
  23. $sercrtId = isset($tencentConfig['SecretId']) ? $tencentConfig['SecretId'] : '';
  24. $sercrtKey = isset($tencentConfig['SecretKey']) ? $tencentConfig['SecretKey'] : '';
  25. //获取token
  26. $token_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/access_token?app_id='.$sercrtId.'&secret='.$sercrtKey.'&grant_type=client_credential&version=1.0.0';
  27. $token_result = file_get_contents($token_url);
  28. if (!$token_result) {
  29. throw new Exception('您的网络开小差啦1~');
  30. }
  31. $token_result = json_decode($token_result, true);
  32. if ($token_result['code'] != 0) {
  33. throw new Exception('您的网络开小差啦2~');
  34. }
  35. $token = $token_result['access_token'];
  36. //获取签名鉴权参数ticket
  37. $ticket_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/api_ticket?app_id='.$sercrtId.'&access_token='.$token.'&type=SIGN&version=1.0.0';
  38. $ticket_result = file_get_contents($ticket_url);
  39. if (!$ticket_result) {
  40. throw new Exception('您的网络开小差啦3~');
  41. }
  42. $ticket_result = json_decode($ticket_result, true);
  43. if ($ticket_result['code'] != 0) {
  44. throw new Exception('您的网络开小差啦4~');
  45. }
  46. $ticket = $ticket_result['tickets'][0]['value'];
  47. //获取签名
  48. $sign_data = [
  49. 'wbappid' => $sercrtId,
  50. 'userId' => (string)$userId,
  51. 'version' => '1.0.0',
  52. 'ticket' => $ticket,
  53. 'nonce' => Random::alnum(32)
  54. ];
  55. asort($sign_data);//排序
  56. $sign_string = join('', $sign_data);
  57. $sign = sha1($sign_string);
  58. //上传身份信息
  59. $orderNo = getMillisecond() . $userId . mt_rand(1, 1000); //商户请求的唯一标识
  60. $url = 'https://miniprogram-kyc.tencentcloudapi.com/api/server/getAdvFaceId?orderNo='.$orderNo;
  61. $data = [
  62. 'webankAppId' => $sercrtId,
  63. 'orderNo' => $orderNo,
  64. 'userId' => (string)$userId,
  65. 'name' => $realName,//姓名
  66. 'idNo' => $idCard,//证件号
  67. 'version' => '1.0.0',
  68. 'sign' => $sign,
  69. 'nonce' => $sign_data['nonce']
  70. ];
  71. $res = curl_post($url,json_encode($data, 320), ['Content-Type: application/json']);
  72. $result['data'] = json_decode($res,true);
  73. $result['data']['nonce'] = $sign_data['nonce'];
  74. $result['data']['sign'] = $sign;
  75. } catch (Exception $e) {
  76. $result['status'] = 0;
  77. $result['msg'] = $e->getMessage();
  78. }
  79. return $result;
  80. }
  81. }