Userauth.php 6.2 KB

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