Переглянути джерело

实名认证,工具方法

lizhen_gitee 8 місяців тому
батько
коміт
6b7a7c99b7

+ 180 - 0
application/api/controller/Userauth.php

@@ -0,0 +1,180 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+use think\Cache;
+
+use TencentCloud\Common\Credential;
+use TencentCloud\Common\Profile\ClientProfile;
+use TencentCloud\Common\Profile\HttpProfile;
+use TencentCloud\Common\Exception\TencentCloudSDKException;
+//use TencentCloud\Faceid\V20180301\FaceidClient;
+//use TencentCloud\Faceid\V20180301\Models\IdCardVerificationRequest;
+use TencentCloud\Iai\V20200303\IaiClient;
+use TencentCloud\Iai\V20200303\Models\CompareFaceRequest;
+use fast\Random;
+
+/**
+ * 实名认证,真人认证相关
+ */
+class Userauth extends Api
+{
+    protected $noNeedLogin = [];
+    protected $noNeedRight = '*';
+
+
+
+
+    //实名认证信息
+    public function idcard_info(){
+        $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->order('id desc')->find();
+        $this->success('success',$check);
+    }
+
+    //申请实名认证
+    public function apply_idcard_confirm(){
+        $truename = input('truename','');
+        $idcard   = input('idcard'  ,'');
+
+        if(empty($truename) || empty($idcard)){
+            $this->error('实名认证信息必填');
+        }
+
+        if($this->auth->idcard_status == 1){
+
+            $this->error('您已经完成实名认证');
+        }
+
+        if($this->auth->idcard_status == 0){
+            $this->error('您已经提交实名认证,请等待审核');
+        }
+
+
+        Db::startTrans();
+        $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->lock(true)->find();
+        if(!empty($check)){
+            if($check['status'] == 0){
+                Db::rollback();
+                $this->error('您已经提交实名认证,请等待审核');
+            }
+            if($check['status'] == 1){
+                Db::rollback();
+                $this->error('您已经完成实名认证');
+            }
+        }
+
+        $count = Db::name('user_idconfirm')->where(['idcard' => $idcard, 'user_id' => ['neq', $this->auth->id]])->count('id');
+        if ($count) {
+            $this->error('该身份证号已被他人使用');
+        }
+
+        //限制每日请求次数
+        $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('今日实名次数已到上限,明天再来吧');
+            }
+        }
+
+        //阿里云身份证二要素认证
+        $auth_restult = $this->userauth_aliyun_two($idcard, $truename);
+        if($auth_restult == false){
+            $this->error('身份证信息与姓名不符');
+        }
+
+        $data = [
+            'user_id' => $this->auth->id,
+            'truename' => $truename,
+            'idcard' => $idcard,
+            'status' => 1, //不需要人工刚审核了,直接过审
+            'createtime' => time(),
+            'updatetime' => time(),
+        ];
+
+
+        //更新
+        $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcard_status'=>$data['status']]);
+        if(!empty($check)){
+            $rs = Db::name('user_idconfirm')->where('id',$check['id'])->update($data);
+        }else{
+            $rs = Db::name('user_idconfirm')->insertGetId($data);
+        }
+
+        if(!$rs || !$update_rs){
+            Db::rollback();
+            $this->error('提交失败');
+        }
+
+        Db::commit();
+
+
+        $this->success('认证通过');
+        $this->success('提交成功,请等待审核');
+
+    }
+
+
+    //产品链接:https://market.aliyun.com/products/57000002/cmapi026109.html#sku=yuncode20109000025
+    //阿里云-数脉api
+    //姓名+身份证号
+    private function userauth_aliyun_two($cardNo = '',$realname = ''){
+        if(!$cardNo || !$realname){
+            return false;
+        }
+
+        $config = config('aliyun_auth_shumai_two');
+
+        $host = "https://eid.shumaidata.com";
+        $path = "/eid/check";
+        $method = "POST";
+        $appcode = $config['app_code'];
+        $headers = array();
+        array_push($headers, "Authorization:APPCODE " . $appcode);
+
+        $querys = "idcard=".$cardNo."&name=".urlencode($realname);
+        $bodys  = '';
+        $url = $host . $path . "?" . $querys;
+
+        $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);
+        }
+        $returnRes = curl_exec($curl);
+        //var_dump($returnRes);
+        curl_close($curl);
+        $result = json_decode($returnRes,true);
+        //dump($result);
+
+        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;
+    }
+
+
+
+
+}

+ 18 - 0
application/api/controller/Usercenter.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+/**
+ * 用户中心
+ */
+class Usercenter extends Api
+{
+
+    protected $noNeedLogin = [];
+    protected $noNeedRight = ['*'];
+
+
+
+}

+ 190 - 0
application/common/library/Wechat.php

@@ -0,0 +1,190 @@
+<?php
+
+namespace app\common\library;
+
+use fast\Http;
+use think\Cache;
+use think\Session;
+
+/**
+ * 微信授权
+ *
+ */
+class Wechat
+{
+    private $app_id = '';
+    private $app_secret = '';
+
+    private $scope = 'snsapi_userinfo';
+
+    public function __construct($app_id, $app_secret)
+    {
+        $this->app_id = $app_id;
+        $this->app_secret = $app_secret;
+    }
+
+    /**
+     * 获取微信授权链接
+     *
+     * @return string
+     */
+    public function getAuthorizeUrl()
+    {
+        $redirect_uri = addon_url('epay/api/wechat', [], true, true);
+        $redirect_uri = urlencode($redirect_uri);
+        $state = \fast\Random::alnum();
+        Session::set('state', $state);
+        return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope={$this->scope}&state={$state}#wechat_redirect";
+    }
+
+    /**
+     * 获取微信openid
+     *
+     * @return mixed|string
+     */
+
+    /*
+     array(5) {
+                  ["access_token"] => string(89) "49_r4I-StVANS8uYWTUHG86GJ-g1iH6mMFWy-9LeOta--2l6Bzg8LVDQFu8VSARu87atETzVJFZ-fndy-aQqEb8wQ"
+                  ["expires_in"] => int(7200)
+                  ["refresh_token"] => string(89) "49_8Vfa-imGcRmUwcESvpvMcEkiuh8kmD_movl9bIz9DV5GonZrqfIya5NgT7G-NMeJ7KTuCjMrSJi4BtojjDxnCQ"
+                  ["openid"] => string(28) "o8lxjwRjlDortQKhTk1dpHjQxcBU"
+                  ["scope"] => string(15) "snsapi_userinfo"
+            }
+     * */
+
+    public function getOpenid($code = '')
+    {
+        $openid = Session::get('openid');
+        if (!$openid) {
+
+            $token = $this->getAccessToken($code);
+            $openid = isset($token['openid']) ? $token['openid'] : '';
+            if ($openid) {
+                Session::set("openid", $openid);
+            }
+        }
+        return $openid;
+    }
+
+    /*
+    array(2) {
+              ["ret"] => bool(true)
+              ["msg"] => string(307) "{"openid":"o8lxjwRjlDortQKhTk1dpHjQxcBU","nickname":"科","sex":1,"language":"zh_CN","city":"临沂","province":"山东","country":"中国","headimgurl":"https:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/LGYWIv4F5vxZ2zCM9GEUynoQeJ6ibX9IfoKPAWLlGIugn1mgaAMPuqxzPBDQ3ktLEv2ia7HmOeJYTg5LofG8YlwQ\/132","privilege":[]}"
+            }
+     */
+    public function getwxuserinfo($code = '')
+    {
+        $wxuserinfo = Session::get('wxuserinfo');
+        if (!$wxuserinfo) {
+            /*if (!isset($_REQUEST['code'])) {
+                return '';
+            } else {
+                $code = $_REQUEST['code'];*/
+                $token = $this->getAccessToken($code);
+
+                $openid = isset($token['openid']) ? $token['openid'] : '';
+
+                $access_token = isset($token['access_token']) ? $token['access_token'] : '';
+
+                $ret = Http::sendRequest('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN');
+                if ($ret['ret']) {
+                    $wxuserinfo = json_decode($ret['msg'], true);
+                    Session::set('wxuserinfo', $wxuserinfo);
+                }else{
+                    $wxuserinfo = [];
+                }
+
+            /*}*/
+        }
+        return $wxuserinfo;
+    }
+
+
+    /**
+     * 获取授权token网页授权
+     *
+     * @param string $code
+     * @return mixed|string
+     */
+    public function getAccessToken($code = '')
+    {
+        $params = [
+            'appid'      => $this->app_id,
+            'secret'     => $this->app_secret,
+            'code'       => $code,
+            'grant_type' => 'authorization_code'
+        ];
+        $ret = Http::sendRequest('https://api.weixin.qq.com/sns/oauth2/access_token', $params, 'GET');
+        if ($ret['ret']) {
+            $ar = json_decode($ret['msg'], true);
+            return $ar;
+        }
+        return [];
+    }
+
+    public function getuserphonenumber($code = ''){
+        $access_token = $this->getPublicAccessToken();
+
+        $params = [
+            'code' => $code,
+        ];
+
+        $ret = Http::sendRequest('https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$access_token, json_encode($params), 'POST');
+
+        if ($ret['ret']) {
+            $ar = json_decode($ret['msg'], true);
+            return $ar;
+        }
+        return [];
+    }
+
+    public function getPublicAccessToken(){
+        $ret = Http::sendRequest('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->app_id.'&secret='.$this->app_secret);
+
+        if ($ret['ret']) {
+            $ar = json_decode($ret['msg'], true);
+
+            return $ar['access_token'];
+        }
+    }
+
+    //{"errcode":0,"errmsg":"ok","msgid":2054095443608862720}
+    public function send($ac,$openid,$first,$keyword1,$keyword2,$keyword3,$remark,$color){
+
+        $params = [
+            "touser"     => $openid,
+            "template_id"       => "lEUyDmLgwIaFDi9SNlIosXe-4fD43SiqSOZigIPOfJ8",
+            'url' => 'https://***.lanmaonet.com',
+            "data" => [
+                "first" => ["value"=>$first,"color"=>$color],
+                "keyword1" => ["value"=>$keyword1,"color"=>"#173177"],
+                "keyword2" => ["value"=>$keyword2,"color"=>"#173177"],
+                "keyword3" => ["value"=>$keyword3,"color"=>"#173177"],
+                "remark" => ["value"=>$remark,"color"=>$color],
+            ],
+            'miniprogram' => [],
+        ];
+
+        $rest = curl_post('https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$ac,json_encode($params));
+
+    }
+
+    public function getJsticket($code = '')
+    {
+        $jsticket = Session::get('jsticket');
+        if (!$jsticket) {
+            $token = $this->getAccessToken($code);
+            $params = [
+                'access_token' => 'token',
+                'type'         => 'jsapi',
+            ];
+            $ret = Http::sendRequest('https://api.weixin.qq.com/cgi-bin/ticket/getticket', $params, 'GET');
+            if ($ret['ret']) {
+                $ar = json_decode($ret['msg'], true);
+                return $ar;
+            }
+        }
+        return $jsticket;
+    }
+}