Procházet zdrojové kódy

feat(ocr): 实名认证增加港澳台证件识别功能

shichen před 10 měsíci
rodič
revize
bc3a6be9f5

+ 86 - 15
app/controller/api/user/User.php

@@ -44,6 +44,7 @@ use app\common\common;
44 44
 use phpseclib3\Math\BigInteger\Engines\BCMath;
45 45
 
46 46
 class User extends BaseController
47
+// class User
47 48
 {
48 49
     use HuiPay;
49 50
 
@@ -56,6 +57,17 @@ class User extends BaseController
56 57
         -1 => '失效'
57 58
     ];
58 59
 
60
+    // private $request;
61
+
62
+    // public function __construct(App $app, UserRepository $repository)
63
+    // {
64
+    //     $this->request = $app->request;
65
+    //     // parent::__construct($app);
66
+    //     $this->repository = $repository;
67
+    //     $this->source = $this->request->header('source');
68
+    //     $this->merId = $this->request->header('merId');
69
+    // }
70
+
59 71
     public function __construct(App $app, UserRepository $repository)
60 72
     {
61 73
         parent::__construct($app);
@@ -927,6 +939,7 @@ class User extends BaseController
927 939
     {
928 940
         $member_id = $this->request->uid();
929 941
         $type = input("type", 0);
942
+        $cardType = input('card_type',1);
930 943
         if ($type == 1) {
931 944
             $data = $this->repository->get_cer(["uid" => $member_id]);
932 945
             return app('json')->success($data);
@@ -942,19 +955,45 @@ class User extends BaseController
942 955
         $params['create_time'] = time();
943 956
         $params['uid'] = $member_id;
944 957
         $baidu = new Ocr();
945
-        $image = file_get_contents($params['card_positive']);
946
-        $idCardSide = "front";
947
-        $result = $baidu->idcard($image, $idCardSide);
948
-        $real_user_name = $result['words_result']['姓名']['words'] ?? '';
949
-        $real_user_card = $result['words_result']['公民身份号码']['words'] ?? '';
950
-        if ($real_user_name != $params['user_name']) {
951
-            return app('json')->fail('姓名或身份证号输入有误~');
952
-        }
953
-        if ($real_user_card != $params['user_card']) {
954
-            return app('json')->fail('姓名或身份证号输入有误~');
955
-        }
956
-        if ($result['image_status'] == "normal" && $result['idcard_number_type'] == 1) {
958
+
959
+        // 兼容港澳台证件
960
+        if ($cardType != 1) {
961
+            // 设置exitentrypermit_type
962
+            $exType = '';
963
+            if ($cardType == 2) {
964
+                $exType = 'tw_return_passport_front'; //台胞证
965
+            }
966
+            if ($cardType == 3) {
967
+                $exType = 'hk_mc_return_passport_front'; // 返乡证
968
+            }
969
+
970
+            if (empty($exType)) {
971
+                return app('json')->fail('证件号类型错误~');
972
+            }
973
+
974
+            $postData['url'] = $params['card_positive'];
975
+            $postData['exitentrypermit_type'] = $exType;
976
+            $result = $baidu->hkMacauTaiwan($postData);
977
+            $real_user_name = $result['words_result']['name_chn'][0]['word']??'';
978
+            $real_user_card = $result['words_result']['card_number'][0]['word']??'';
979
+            if ($real_user_name != $params['user_name']) {
980
+                return app('json')->fail('姓名或身份证号输入有误~');
981
+            }
982
+            if ($real_user_card != $params['user_card']) {
983
+                return app('json')->fail('姓名或身份证号输入有误~');
984
+            }
985
+
986
+            $params['reason'] = '';
987
+            $params['audit_time'] = 0;
988
+            $params['update_time'] = 0;
957 989
             $params['status'] = 1;
990
+            $params['card_type'] = $cardType;
991
+            if ($result['words_result']['sex'][0]['word'] == '男') {
992
+                $gender = 1;
993
+            }else{
994
+                $gender = 2;
995
+            }
996
+            $params['gender'] = $gender;
958 997
             $res = $this->repository->add_cer($params, $member_id);
959 998
             if ($res) {
960 999
                 $userRepository = app()->make(UserRepository::class);
@@ -973,10 +1012,42 @@ class User extends BaseController
973 1012
             } else {
974 1013
                 return app('json')->fail('认证失败');
975 1014
             }
976
-        } else {
977
-            return app('json')->fail('认证失败,请重新认证');
1015
+        }else{
1016
+            $image = file_get_contents($params['card_positive']);
1017
+            $idCardSide = "front";
1018
+            $result = $baidu->idcard($image, $idCardSide);
1019
+            $real_user_name = $result['words_result']['姓名']['words'] ?? '';
1020
+            $real_user_card = $result['words_result']['公民身份号码']['words'] ?? '';
1021
+            if ($real_user_name != $params['user_name']) {
1022
+                return app('json')->fail('姓名或身份证号输入有误~');
1023
+            }
1024
+            if ($real_user_card != $params['user_card']) {
1025
+                return app('json')->fail('姓名或身份证号输入有误~');
1026
+            }
1027
+            if ($result['image_status'] == "normal" && $result['idcard_number_type'] == 1) {
1028
+                $params['status'] = 1;
1029
+                $res = $this->repository->add_cer($params, $member_id);
1030
+                if ($res) {
1031
+                    $userRepository = app()->make(UserRepository::class);
1032
+                    // 赠送积分 完善实名送90个积分,同时送邀请人50个积分
1033
+                    $userRepository->sign_add($member_id, 90,1,'完善实名系统赠送90个积分','','', '', 1, '', 1);
1034
+                    $member_info = Db::name('user')->where( array('uid' => $member_id) )->find();
1035
+                    Db::name('user')
1036
+                        ->where('uid', $member_id)
1037
+                        ->update(['real_name' => $params['user_name'], 'nickname' => $params['user_name']]);
1038
+                    if($member_info['spread_uid']){
1039
+                        // 好友完善实名系统赠送50个积分
1040
+                        $spreadUid = $member_info['spread_uid'];
1041
+                        $userRepository->sign_add($spreadUid, 50,1,'好友完善实名系统赠送50个积分','','', '', 1, '', 1);
1042
+                    }
1043
+                    return app('json')->success($res);
1044
+                } else {
1045
+                    return app('json')->fail('认证失败');
1046
+                }
1047
+            } else {
1048
+                return app('json')->fail('认证失败,请重新认证');
1049
+            }
978 1050
         }
979
-
980 1051
     }
981 1052
 
982 1053
 

+ 1 - 1
extend/BaiduAi/controller/Ocr.php

@@ -23,7 +23,7 @@ class Ocr
23 23
 
24 24
     public function __construct()
25 25
     {
26
-        $this->client = new \AipOcr('24267127', 'Utg2bxLY4is7h5DPehVZ63Q4', 'yfGMEAO6YQXIE2pqYMiH4MNBE1DcNhyO ');
26
+        $this->client = new \AipOcr('24267127', 'Utg2bxLY4is7h5DPehVZ63Q4', 'yfGMEAO6YQXIE2pqYMiH4MNBE1DcNhyO');
27 27
     }
28 28
 
29 29
     public function setConnectionTimeoutInMillis($ms)

+ 13 - 0
extend/BaiduAi/sdk/AipOcr.php

@@ -61,6 +61,8 @@ class AipOcr extends AipBase {
61 61
      */
62 62
     private $idcardUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard';
63 63
 
64
+    private $hkMacauTaiwanUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/hk_macau_taiwan_exitentrypermit';
65
+
64 66
     /**
65 67
      * 银行卡识别 bankcard api url
66 68
      * @var string
@@ -368,6 +370,17 @@ class AipOcr extends AipBase {
368 370
         return $this->request($this->idcardUrl, $data);
369 371
     }
370 372
 
373
+    public function hkMacauTaiwan($data)
374
+    {
375
+        // $data = array();
376
+        //
377
+        // $data['url'] =$data['url'];
378
+        //
379
+        // $data = array_merge($data, $options);
380
+
381
+        return $this->request($this->hkMacauTaiwanUrl, $data);
382
+    }
383
+
371 384
     /**
372 385
      * 银行卡识别接口
373 386
      *