|
@@ -3,235 +3,169 @@
|
|
|
namespace app\api\controller;
|
|
|
|
|
|
use app\common\controller\Api;
|
|
|
-
|
|
|
+use think\Db;
|
|
|
+use think\Cache;
|
|
|
/**
|
|
|
* 实名认证
|
|
|
*/
|
|
|
-class Test extends Api
|
|
|
+class Userauth extends Api
|
|
|
{
|
|
|
|
|
|
protected $noNeedLogin = '*';
|
|
|
protected $noNeedRight = '*';
|
|
|
|
|
|
|
|
|
- //申请真人认证
|
|
|
- public function realauth()
|
|
|
- {
|
|
|
- try {
|
|
|
- $realName = $this->request->param('real_name','');
|
|
|
- $idCard = $this->request->param('id_card','');
|
|
|
- if ($this->auth->is_auth == 2) {
|
|
|
- $this->error('您已经真人认证过了~');
|
|
|
- }
|
|
|
- if (empty($realName)) {
|
|
|
- throw new Exception('请输入姓名');
|
|
|
- }
|
|
|
- if (empty($idCard)) {
|
|
|
- throw new Exception('请输入身份证号');
|
|
|
- }
|
|
|
- $userAuthWhere['user_id'] = ['neq',$this->auth->id];
|
|
|
- $userAuthWhere['idcard'] = $idCard;
|
|
|
+ //实名认证信息
|
|
|
+ public function idcard_info(){
|
|
|
+ $check = Db::name('user_auth')->where('user_id',$this->auth->id)->order('id desc')->find();
|
|
|
+ $this->success('success',$check);
|
|
|
+ }
|
|
|
|
|
|
- // 获取用户已经实名认证的数量
|
|
|
- $authenticatedCount = model('UserAuth')->where(['idcard' => $idCard])->count();
|
|
|
+ //申请实名认证
|
|
|
+ public function apply_idcard_confirm(){
|
|
|
+ $truename = input('truename','');
|
|
|
+ $idcard = input('idcard' ,'');
|
|
|
|
|
|
- // 如果已经实名认证的数量不超过3个
|
|
|
- if ($authenticatedCount > 3) {
|
|
|
- throw new Exception('您当前身份证号已经认证过3次');
|
|
|
- }
|
|
|
+ if(empty($truename) || empty($idcard)){
|
|
|
+ $this->error('实名认证信息必填');
|
|
|
+ }
|
|
|
|
|
|
- /*$userAuth = model('UserAuth')->where($userAuthWhere)->find();
|
|
|
- if (!empty($userAuth)) {
|
|
|
- throw new Exception('该身份证号已被其他用户实名验证过');
|
|
|
- }*/
|
|
|
- /*$userService = new UserService();
|
|
|
- $faceParams = [
|
|
|
- 'real_name' => $realName,
|
|
|
- 'id_card' => $idCard,
|
|
|
- 'user_id' => $this->auth->id,
|
|
|
- ];
|
|
|
- $res = $userService->faceAuth($faceParams);
|
|
|
- if (!$res['status']) {
|
|
|
- $this->error('您的网络开小差啦5~');
|
|
|
- }
|
|
|
- $rs = $res['data'];
|
|
|
- if (!$rs || $rs['code'] != 0) {
|
|
|
- $this->error('您的网络开小差啦6~');
|
|
|
- }
|
|
|
+ if($this->auth->is_auth == 2){
|
|
|
|
|
|
- $user_auth = [
|
|
|
- 'user_id' => $this->auth->id,
|
|
|
- 'realname' => $realName,
|
|
|
- 'idcard' => $idCard,
|
|
|
- 'certify_id' => isset($rs['result']['faceId']) ? $rs['result']['faceId'] : '',
|
|
|
- 'out_trade_no' => isset($rs['result']['orderNo']) ? $rs['result']['orderNo'] : '',
|
|
|
- 'status' => 0,
|
|
|
- 'createtime' => time(),
|
|
|
- 'updatetime' => time()
|
|
|
- ];*/
|
|
|
- $userService = new UserService();
|
|
|
- $aliParams = [
|
|
|
- 'id_card' => $idCard,
|
|
|
- 'real_name' => $realName,
|
|
|
- ];
|
|
|
- $aliCheckRes = $userService->aliCheck($aliParams);
|
|
|
- if (!$aliCheckRes['status']) {
|
|
|
- throw new Exception($aliCheckRes['msg']);
|
|
|
- }
|
|
|
- $user_auth = [
|
|
|
- 'user_id' => $this->auth->id,
|
|
|
- 'realname' => $realName,
|
|
|
- 'idcard' => $idCard,
|
|
|
- 'certify_id' => '',
|
|
|
- 'out_trade_no' => '',
|
|
|
- 'status' => 1,
|
|
|
- 'createtime' => time(),
|
|
|
- ];
|
|
|
- //开启事务
|
|
|
- Db::startTrans();
|
|
|
- //查询是否认证过
|
|
|
- $info = Db::name('user_auth')->where(['user_id' => $this->auth->id])->find();
|
|
|
- if ($info) {
|
|
|
- $auth_rs = Db::name('user_auth')->where(['id' => $info['id']])->setField($user_auth);
|
|
|
- } else {
|
|
|
- $auth_rs = Db::name('user_auth')->insertGetId($user_auth);
|
|
|
- }
|
|
|
- if (!$auth_rs) {
|
|
|
+ $this->error('您已经完成实名认证');
|
|
|
+ }
|
|
|
+
|
|
|
+ if($this->auth->is_auth == 1){
|
|
|
+ $this->error('您已经提交实名认证,请等待审核');
|
|
|
+ }
|
|
|
+
|
|
|
+ Db::startTrans();
|
|
|
+ $check = Db::name('user_auth')->where('user_id',$this->auth->id)->lock(true)->find();
|
|
|
+ if(!empty($check)){
|
|
|
+ if($check['status'] == 0){
|
|
|
Db::rollback();
|
|
|
- $this->error('您的网络开小差啦7~');
|
|
|
+ $this->error('您已经提交实名认证,请等待审核');
|
|
|
}
|
|
|
- //修改用户表认证状态
|
|
|
- $user_rs = Db::name('user')->where(['id' => $this->auth->id])->setField('is_auth', 2);
|
|
|
- if ($user_rs === false) {
|
|
|
+ if($check['status'] == 1){
|
|
|
Db::rollback();
|
|
|
- $this->error('您的网络开小差啦8~');
|
|
|
+ $this->error('您已经完成实名认证');
|
|
|
}
|
|
|
-
|
|
|
- Db::commit();
|
|
|
- /*$return_data = [
|
|
|
- 'face_id' => $user_auth['certify_id'],
|
|
|
- 'order_no' => $user_auth['out_trade_no'],
|
|
|
- 'user_id' => (string)$this->auth->id,
|
|
|
- 'nonce' => $rs['nonce'],
|
|
|
- 'sign' => $rs['sign'],
|
|
|
- ];*/
|
|
|
- $this->success('验证成功');
|
|
|
- } catch (Exception $e) {
|
|
|
- $this->error($e->getMessage());
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- //查询真人认证结果
|
|
|
- public function getrealauthresult() {
|
|
|
- $user_auth = Db::name('user_auth')->where(['user_id' => $this->auth->id])->find();
|
|
|
- if (!$user_auth) {
|
|
|
- $this->success('尚未认证');
|
|
|
+ $count = Db::name('user_auth')->where(['idcard' => $idcard, 'user_id' => ['neq', $this->auth->id]])->count('id');
|
|
|
+ if ($count) {
|
|
|
+ $this->error('该身份证号已被他人使用');
|
|
|
}
|
|
|
- if ($user_auth['status'] == 1) {
|
|
|
- $this->success('真人认证通过');
|
|
|
- }
|
|
|
- if (!$user_auth['certify_id']) {
|
|
|
- $this->success('请先进行真人认证');
|
|
|
- }
|
|
|
- $tencentConfig = config('tencent_yun');
|
|
|
- $sercrtId = isset($tencentConfig['SecretId']) ? $tencentConfig['SecretId'] : '';
|
|
|
- $sercrtKey = isset($tencentConfig['SecretKey']) ? $tencentConfig['SecretKey'] : '';
|
|
|
- //获取token
|
|
|
- $token_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/access_token?app_id='.$sercrtId.'&secret='.$sercrtKey.'&grant_type=client_credential&version=1.0.0';
|
|
|
- $token_result = file_get_contents($token_url);
|
|
|
- if (!$token_result) {
|
|
|
- $this->error('您的网络开小差啦1~');
|
|
|
- }
|
|
|
- $token_result = json_decode($token_result, true);
|
|
|
- if ($token_result['code'] != 0) {
|
|
|
- $this->error('您的网络开小差啦2~');
|
|
|
- }
|
|
|
- $token = $token_result['access_token'];
|
|
|
|
|
|
- //获取签名鉴权参数ticket
|
|
|
- $ticket_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/api_ticket?app_id='.$sercrtId.'&access_token='.$token.'&type=SIGN&version=1.0.0';
|
|
|
- $ticket_result = file_get_contents($ticket_url);
|
|
|
- if (!$ticket_result) {
|
|
|
- $this->error('您的网络开小差啦3~');
|
|
|
+ //限制每日请求次数
|
|
|
+ $time = time();
|
|
|
+ $today_end = strtotime(date('Y-m-d 23:59:59', $time));
|
|
|
+ $cache_time = $today_end - $time; //缓存时间
|
|
|
+ $time_count = Cache::get('fourauth' . $this->auth->id);
|
|
|
+ if (!$time_count) {
|
|
|
+ Cache::set('fourauth' . $this->auth->id, 1, $cache_time);
|
|
|
+ } else {
|
|
|
+ Cache::set('fourauth' . $this->auth->id, $time_count + 1, $cache_time);
|
|
|
+ if ($time_count > 5) {
|
|
|
+ $this->error('今日实名次数已到上限,明天再来吧');
|
|
|
+ }
|
|
|
}
|
|
|
- $ticket_result = json_decode($ticket_result, true);
|
|
|
- if ($ticket_result['code'] != 0) {
|
|
|
- $this->error('您的网络开小差啦4~');
|
|
|
+
|
|
|
+ //阿里云身份证三要素认证
|
|
|
+ $auth_restult = $this->userauth_aliyun_three($idcard, $truename,$this->auth->mobile);
|
|
|
+ if($auth_restult == false){
|
|
|
+ $this->error('身份证信息与姓名或注册手机号不符');
|
|
|
}
|
|
|
- $ticket = $ticket_result['tickets'][0]['value'];
|
|
|
-
|
|
|
- //获取签名
|
|
|
- $sign_data = [
|
|
|
- 'wbappid' => $sercrtId,
|
|
|
- 'orderNo' => $user_auth['out_trade_no'],
|
|
|
- 'version' => '1.0.0',
|
|
|
- 'ticket' => $ticket,
|
|
|
- 'nonce' => Random::alnum(32)
|
|
|
- ];//p($sign_data);
|
|
|
- asort($sign_data); //p($sign_data);//排序
|
|
|
- $sign_string = join('', $sign_data);//p($sign_string);
|
|
|
- $sign = sha1($sign_string);//p($sign);
|
|
|
-
|
|
|
- //人脸核身结果查询
|
|
|
- $url = 'https://miniprogram-kyc.tencentcloudapi.com/api/v2/base/queryfacerecord?orderNo=' . $user_auth['out_trade_no'];
|
|
|
+
|
|
|
$data = [
|
|
|
- 'appId' => $sercrtId,
|
|
|
- 'version' => '1.0.0',
|
|
|
- 'nonce' => $sign_data['nonce'],
|
|
|
- 'orderNo' => $user_auth['out_trade_no'],
|
|
|
- 'sign' => $sign
|
|
|
+ 'user_id' => $this->auth->id,
|
|
|
+ 'realname' => $truename,
|
|
|
+ 'idcard' => $idcard,
|
|
|
+ 'status' => 1, //不需要人工刚审核了,直接过审
|
|
|
+ 'createtime' => time(),
|
|
|
];
|
|
|
|
|
|
- $rs = curl_post($url,json_encode($data, 320), ['Content-Type: application/json']);
|
|
|
- if (!$rs) {
|
|
|
- $this->error('您的网络开小差啦5~');
|
|
|
- }
|
|
|
- $rs = json_decode($rs, true);
|
|
|
- if (!$rs || $rs['code'] != 0) {
|
|
|
- $this->error($rs['msg']);
|
|
|
- }
|
|
|
- if ($rs['result']['liveRate'] >= 90 && $rs['result']['similarity'] >= 90) {
|
|
|
- $edit_data['status'] = 2;
|
|
|
- $msg = '真人认证成功';
|
|
|
- } else {
|
|
|
- $edit_data['status'] = -1;
|
|
|
- $edit_data['certify_id'] = '';
|
|
|
- $edit_data['out_trade_no'] = '';
|
|
|
- $msg = '真人认证失败';
|
|
|
+ //更新
|
|
|
+ $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['is_auth'=>2]);//不需要人工刚审核了,直接过审
|
|
|
+ if(!empty($check)){
|
|
|
+ $rs = Db::name('user_auth')->where('id',$check['id'])->update($data);
|
|
|
+ }else{
|
|
|
+ $rs = Db::name('user_auth')->insertGetId($data);
|
|
|
}
|
|
|
|
|
|
- $edit_data['updatetime'] = time();
|
|
|
- //开启事务
|
|
|
- Db::startTrans();
|
|
|
- //修改认证信息
|
|
|
- $result = Db::name('user_auth')->where(['user_id' => $this->auth->id, 'status' => $user_auth['status']])->setField($edit_data);
|
|
|
- if (!$result) {
|
|
|
+ //task任务
|
|
|
+ //实名认证
|
|
|
+ $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,4);
|
|
|
+ if($task_rs === false){
|
|
|
Db::rollback();
|
|
|
- $this->error('查询认证结果失败2');
|
|
|
+ $this->error('完成任务失败');
|
|
|
}
|
|
|
- //修改用户信息
|
|
|
- $rs = Db::name('user')->where(['id' => $this->auth->id])->setField('is_auth', $edit_data['status']);
|
|
|
- if (!$rs) {
|
|
|
+
|
|
|
+ //系统消息
|
|
|
+ $msg_id = \app\common\model\Message::addMessage($this->auth->id,'实名认证','实名认证已经审核通过');
|
|
|
+
|
|
|
+ if(!$rs || !$update_rs){
|
|
|
Db::rollback();
|
|
|
- $this->error('查询认证结果失败3');
|
|
|
- }
|
|
|
- if ($edit_data['status'] == 2) { //通过
|
|
|
- //tag任务赠送金币
|
|
|
- //真人认证奖励
|
|
|
- /*$task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,20);
|
|
|
- if($task_rs === false){
|
|
|
- Db::rollback();
|
|
|
- $this->error('完成任务赠送奖励失败');
|
|
|
- }*/
|
|
|
- //系统消息
|
|
|
- $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证已经审核通过');
|
|
|
- } else {
|
|
|
- //系统消息
|
|
|
- $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证审核不通过');
|
|
|
+ $this->error('提交失败');
|
|
|
}
|
|
|
|
|
|
Db::commit();
|
|
|
- $this->success($msg);
|
|
|
+ $this->success('认证通过');
|
|
|
+
|
|
|
}
|
|
|
|
|
|
+ //产品链接:https://market.aliyun.com/products/57000002/cmapi026100.html?spm=5176.730005.result.8.1dbc123e8ArY19&innerSource=search#sku=yuncode2010000006
|
|
|
+ //阿里云-数脉api
|
|
|
+ //姓名+手机号+身份证号
|
|
|
+ private function userauth_aliyun_three($cardNo = '',$realname = '',$mobile = ''){
|
|
|
+ if(!$cardNo || !$realname || !$mobile){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $config = config('aliyun_auth_shumai');
|
|
|
+
|
|
|
+ $host = "https://mobile3elements.shumaidata.com";
|
|
|
+ $path = "/mobile/verify_real_name";
|
|
|
+ $method = "POST";
|
|
|
+ $appcode = $config['app_code'];
|
|
|
+ $headers = array();
|
|
|
+ array_push($headers, "Authorization:APPCODE " . $appcode);
|
|
|
+ //根据API的要求,定义相对应的Content-Type
|
|
|
+ array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
|
|
|
+ $querys = "";
|
|
|
+ $bodys = "idcard=".$cardNo."&mobile=".$mobile."&name=".urlencode($realname);
|
|
|
+ $url = $host . $path;
|
|
|
+
|
|
|
+ $curl = curl_init();
|
|
|
+ curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
|
|
|
+ curl_setopt($curl, CURLOPT_URL, $url);
|
|
|
+ curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
|
|
+ curl_setopt($curl, CURLOPT_FAILONERROR, false);
|
|
|
+ curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
|
|
+ //设定返回信息中是否包含响应信息头,启用时会将头文件的信息作为数据流输出,true 表示输出信息头, false表示不输出信息头
|
|
|
+ //如果需要将字符串转成json,请将 CURLOPT_HEADER 设置成 false
|
|
|
+ curl_setopt($curl, CURLOPT_HEADER, false);
|
|
|
+ if (1 == strpos("$".$host, "https://"))
|
|
|
+ {
|
|
|
+ curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
+ curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
+ }
|
|
|
+ curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
|
|
|
+ $returnRes = curl_exec($curl);
|
|
|
+ curl_close($curl);
|
|
|
+ $result = json_decode($returnRes,true);
|
|
|
+
|
|
|
+ if(is_array($result) && isset($result['code']) && $result['code'] == 0){
|
|
|
+ if(isset($result['result']) && isset($result['result']['res'])){
|
|
|
+ if($result['result']['res'] == 1){
|
|
|
+ //实名过了
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|