UserService.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. /**
  82. * 阿里实名认证二要素
  83. * https://market.aliyun.com/products/57000002/cmapi026109.html
  84. * @return void
  85. */
  86. public function aliCheck($params=[])
  87. {
  88. $result = [
  89. 'status' => 1,
  90. 'msg' => '',
  91. 'data' => [],
  92. ];
  93. try {
  94. $idCard = isset($params['id_card']) ? $params['id_card'] : '';
  95. $realName = isset($params['real_name']) ? $params['real_name'] : '';
  96. $aliyunConfig = config('ali_yun');
  97. $host = "https://eid.shumaidata.com";
  98. $path = "/eid/check";
  99. $method = "POST";
  100. $appcode = isset($aliyunConfig['app_code']) ? $aliyunConfig['app_code'] : '';
  101. $headers = ['Content-Type: application/json'];
  102. array_push($headers, "Authorization:APPCODE " . $appcode);
  103. $querys = "idcard=".$idCard."&name=".urlencode($realName);
  104. $bodys = "";
  105. $url = $host . $path . "?" . $querys;
  106. $curl = curl_init();
  107. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  108. curl_setopt($curl, CURLOPT_URL, $url);
  109. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  110. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  111. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  112. //设定返回信息中是否包含响应信息头,启用时会将头文件的信息作为数据流输出,true 表示输出信息头, false表示不输出信息头
  113. //如果需要将字符串转成json,请将 CURLOPT_HEADER 设置成 false
  114. curl_setopt($curl, CURLOPT_HEADER, false);
  115. if (1 == strpos("$".$host, "https://"))
  116. {
  117. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  118. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  119. }
  120. $returnRes = curl_exec($curl);
  121. curl_close($curl);
  122. $resultData = json_decode($returnRes,true);
  123. if (isset($resultData['code']) && !empty($resultData['code'])) {
  124. throw new Exception($resultData['message']);
  125. }
  126. if (isset($resultData['result']['res']) && $resultData['result']['res'] != 1) {
  127. throw new Exception($resultData['result']['description']);
  128. }
  129. $result['data'] = $resultData;
  130. } catch (Exception $e) {
  131. $result['status'] = 0;
  132. $result['msg'] = $e->getMessage();
  133. }
  134. return $result;
  135. }
  136. /**
  137. * 阿里银行卡三要素
  138. * https://market.aliyun.com/products/57000002/cmapi00063283.html
  139. * @return void
  140. */
  141. public function bankCheck($params=[])
  142. {
  143. $result = [
  144. 'status' => 1,
  145. 'msg' => '',
  146. 'data' => [],
  147. ];
  148. try {
  149. $bankcard = isset($params['bank_no']) ? $params['bank_no'] : '';//银行卡
  150. $idCard = isset($params['id_card']) ? $params['id_card'] : '';//身份证号
  151. $realName = isset($params['real_name']) ? $params['real_name'] : '';//姓名
  152. $aliyunConfig = config('ali_yun');
  153. $host = "https://sxbank3v2.market.alicloudapi.com";
  154. $path = "/bankcard3/check";
  155. $method = "POST";
  156. $appcode = isset($aliyunConfig['app_code']) ? $aliyunConfig['app_code'] : '';
  157. $headers = [];
  158. array_push($headers, "Authorization:APPCODE " . $appcode);
  159. $querys = "bankcard=".$bankcard."&idCard=".$idCard."&name=".urlencode($realName);
  160. $bodys = "";
  161. $url = $host . $path . "?" . $querys;
  162. $curl = curl_init();
  163. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  164. curl_setopt($curl, CURLOPT_URL, $url);
  165. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  166. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  167. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  168. //设定返回信息中是否包含响应信息头,启用时会将头文件的信息作为数据流输出,true 表示输出信息头, false表示不输出信息头
  169. //如果需要将字符串转成json,请将 CURLOPT_HEADER 设置成 false
  170. curl_setopt($curl, CURLOPT_HEADER, false);
  171. if (1 == strpos("$".$host, "https://"))
  172. {
  173. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  174. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  175. }
  176. $returnRes = curl_exec($curl);
  177. curl_close($curl);
  178. $resultData = json_decode($returnRes,true);
  179. if (isset($resultData['code']) && !empty($resultData['code'])) {
  180. throw new Exception($resultData['msg']);
  181. }
  182. $aliResult = isset($resultData['data']['result']) ? $resultData['data']['result'] : 0;
  183. if ($aliResult != 1) {
  184. //核查结果(1:一致,2:不一致,3:无效卡号或卡状态异常)
  185. $aliMsg = '';
  186. if ($aliResult == 2) {
  187. $aliMsg = '不一致';
  188. } elseif ($aliResult == 3) {
  189. $aliMsg = '无效卡号或卡状态异常';
  190. }
  191. throw new Exception($aliMsg);
  192. }
  193. $result['data'] = $resultData;
  194. } catch (Exception $e) {
  195. $result['status'] = 0;
  196. $result['msg'] = $e->getMessage();
  197. }
  198. return $result;
  199. }
  200. }