Userauth.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Cache;
  6. use TencentCloud\Common\Credential;
  7. use TencentCloud\Common\Profile\ClientProfile;
  8. use TencentCloud\Common\Profile\HttpProfile;
  9. use TencentCloud\Common\Exception\TencentCloudSDKException;
  10. //use TencentCloud\Faceid\V20180301\FaceidClient;
  11. //use TencentCloud\Faceid\V20180301\Models\IdCardVerificationRequest;
  12. use TencentCloud\Iai\V20200303\IaiClient;
  13. use TencentCloud\Iai\V20200303\Models\CompareFaceRequest;
  14. use fast\Random;
  15. /**
  16. * 实名认证,真人认证相关
  17. */
  18. class Userauth extends Api
  19. {
  20. protected $noNeedLogin = [];
  21. protected $noNeedRight = '*';
  22. //实名认证信息
  23. public function idcard_info(){
  24. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->order('id desc')->find();
  25. $this->success('success',$check);
  26. }
  27. //申请实名认证
  28. public function apply_idcard_confirm(){
  29. $truename = input('truename','');
  30. $idcard = input('idcard' ,'');
  31. if(empty($truename) || empty($idcard)){
  32. $this->error('实名认证信息必填');
  33. }
  34. if($this->auth->idcard_status == 1){
  35. $this->error('您已经完成实名认证');
  36. }
  37. if($this->auth->idcard_status == 0){
  38. $this->error('您已经提交实名认证,请等待审核');
  39. }
  40. Db::startTrans();
  41. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->lock(true)->find();
  42. if(!empty($check)){
  43. if($check['status'] == 0){
  44. Db::rollback();
  45. $this->error('您已经提交实名认证,请等待审核');
  46. }
  47. if($check['status'] == 1){
  48. Db::rollback();
  49. $this->error('您已经完成实名认证');
  50. }
  51. }
  52. $count = Db::name('user_idconfirm')->where(['idcard' => $idcard, 'user_id' => ['neq', $this->auth->id]])->count('id');
  53. if ($count) {
  54. $this->error('该身份证号已被他人使用');
  55. }
  56. //限制每日请求次数
  57. $time = time();
  58. $today_end = strtotime(date('Y-m-d 23:59:59', $time));
  59. $cache_time = $today_end - $time; //缓存时间
  60. $time_count = Cache::get('fourauth' . $this->auth->id);
  61. if (!$time_count) {
  62. Cache::set('fourauth' . $this->auth->id, 1, $cache_time);
  63. } else {
  64. Cache::set('fourauth' . $this->auth->id, $time_count + 1, $cache_time);
  65. if ($time_count > 5) {
  66. $this->error('今日实名次数已到上限,明天再来吧');
  67. }
  68. }
  69. //阿里云身份证二要素认证
  70. $auth_restult = $this->userauth_aliyun_two($idcard, $truename);
  71. if($auth_restult == false){
  72. $this->error('身份证信息与姓名不符');
  73. }
  74. $data = [
  75. 'user_id' => $this->auth->id,
  76. 'truename' => $truename,
  77. 'idcard' => $idcard,
  78. 'status' => 1, //不需要人工刚审核了,直接过审
  79. 'createtime' => time(),
  80. 'updatetime' => time(),
  81. ];
  82. //更新
  83. $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcard_status'=>$data['status']]);
  84. if(!empty($check)){
  85. $rs = Db::name('user_idconfirm')->where('id',$check['id'])->update($data);
  86. }else{
  87. $rs = Db::name('user_idconfirm')->insertGetId($data);
  88. }
  89. if(!$rs || !$update_rs){
  90. Db::rollback();
  91. $this->error('提交失败');
  92. }
  93. Db::commit();
  94. $this->success('认证通过');
  95. $this->success('提交成功,请等待审核');
  96. }
  97. //产品链接:https://market.aliyun.com/products/57000002/cmapi026109.html#sku=yuncode20109000025
  98. //阿里云-数脉api
  99. //姓名+身份证号
  100. private function userauth_aliyun_two($cardNo = '',$realname = ''){
  101. if(!$cardNo || !$realname){
  102. return false;
  103. }
  104. $config = config('aliyun_auth_shumai_two');
  105. $host = "https://eid.shumaidata.com";
  106. $path = "/eid/check";
  107. $method = "POST";
  108. $appcode = $config['app_code'];
  109. $headers = array();
  110. array_push($headers, "Authorization:APPCODE " . $appcode);
  111. $querys = "idcard=".$cardNo."&name=".urlencode($realname);
  112. $bodys = '';
  113. $url = $host . $path . "?" . $querys;
  114. $curl = curl_init();
  115. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  116. curl_setopt($curl, CURLOPT_URL, $url);
  117. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  118. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  119. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  120. //设定返回信息中是否包含响应信息头,启用时会将头文件的信息作为数据流输出,true 表示输出信息头, false表示不输出信息头
  121. //如果需要将字符串转成json,请将 CURLOPT_HEADER 设置成 false
  122. curl_setopt($curl, CURLOPT_HEADER, false);
  123. if (1 == strpos("$".$host, "https://"))
  124. {
  125. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  126. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  127. }
  128. $returnRes = curl_exec($curl);
  129. //var_dump($returnRes);
  130. curl_close($curl);
  131. $result = json_decode($returnRes,true);
  132. //dump($result);
  133. if(is_array($result) && isset($result['code']) && $result['code'] == 0){
  134. if(isset($result['result']) && isset($result['result']['res'])){
  135. if($result['result']['res'] == 1){
  136. //实名过了
  137. return true;
  138. }
  139. }
  140. }
  141. return false;
  142. }
  143. }