Userauth.php 7.0 KB

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