UserService.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. $tencentConfig = config('tencent_im');
  22. $sercrtId = isset($tencentConfig['SecretId']) ? $tencentConfig['SecretId'] : '';
  23. $sercrtKey = isset($tencentConfig['SecretKey']) ? $tencentConfig['SecretKey'] : '';
  24. //获取token
  25. $token_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/access_token?app_id='.$sercrtId.'&secret='.$sercrtKey.'&grant_type=client_credential&version=1.0.0';
  26. $token_result = file_get_contents($token_url);
  27. if (!$token_result) {
  28. throw new Exception('您的网络开小差啦1~');
  29. }
  30. $token_result = json_decode($token_result, true);echo '<pre>';var_dump($token_result);exit;
  31. if ($token_result['code'] != 0) {
  32. throw new Exception('您的网络开小差啦2~');
  33. }
  34. $token = $token_result['access_token'];
  35. //获取签名鉴权参数ticket
  36. $ticket_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/api_ticket?app_id='.$sercrtId.'&access_token='.$token.'&type=SIGN&version=1.0.0';
  37. $ticket_result = file_get_contents($ticket_url);
  38. if (!$ticket_result) {
  39. throw new Exception('您的网络开小差啦3~');
  40. }
  41. $ticket_result = json_decode($ticket_result, true);
  42. if ($ticket_result['code'] != 0) {
  43. throw new Exception('您的网络开小差啦4~');
  44. }
  45. $ticket = $ticket_result['tickets'][0]['value'];
  46. //获取签名
  47. $sign_data = [
  48. 'wbappid' => $sercrtId,
  49. 'userId' => (string)$this->auth->id,
  50. 'version' => '1.0.0',
  51. 'ticket' => $ticket,
  52. 'nonce' => Random::alnum(32)
  53. ];
  54. asort($sign_data);//排序
  55. $sign_string = join('', $sign_data);
  56. $sign = sha1($sign_string);
  57. //上传身份信息
  58. $orderNo = getMillisecond() . $this->auth->id . mt_rand(1, 1000); //商户请求的唯一标识
  59. $url = 'https://miniprogram-kyc.tencentcloudapi.com/api/server/getAdvFaceId?orderNo='.$orderNo;
  60. $data = [
  61. 'webankAppId' => $sercrtId,
  62. 'orderNo' => $orderNo,
  63. 'userId' => (string)$this->auth->id,
  64. 'name' => $realName,//姓名
  65. 'idNo' => $idCard,//证件号
  66. 'version' => '1.0.0',
  67. 'sign' => $sign,
  68. 'nonce' => $sign_data['nonce']
  69. ];
  70. $res = curl_post($url,json_encode($data, 320), ['Content-Type: application/json']);
  71. echo '<pre>';var_dump($res);exit;
  72. $result['data'] = $res;
  73. } catch (Exception $e) {
  74. $result['status'] = 0;
  75. $result['msg'] = $e->getMessage();
  76. }
  77. return $result;
  78. }
  79. }