Userauth.php 5.9 KB

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