Parcourir la source

fix card_typ error

stanley-king il y a 3 ans
Parent
commit
a9f8b4a6c9
6 fichiers modifiés avec 113 ajouts et 137 suppressions
  1. 99 110
      helper/mtopcard/mtopcard.php
  2. 12 20
      rdispatcher/proxy.php
  3. 0 3
      test/TestAddData.php
  4. 2 2
      test/TestCardNo.php
  5. 0 1
      test/TestCardType.php
  6. 0 1
      test/TestRefill.php

+ 99 - 110
helper/mtopcard/mtopcard.php

@@ -191,7 +191,7 @@ function simple_card_type($cardno)
     }
 }
 
-function card_type($cardno,&$regin_no,$fCoroutine = false)
+function card_type($cardno,&$regin_no)
 {
     $regin_no = -1;
     if(preg_match( '/^1[0-9]{18}$/',$cardno,$matches)) {
@@ -202,66 +202,14 @@ function card_type($cardno,&$regin_no,$fCoroutine = false)
     }
     elseif(preg_match('/^1\d{10}$/',$cardno,$matches))
     {
-        $checker = function ($phone,&$region)
-        {
-//            if (empty($phone)) return false;  //手机号不能为空
-//            $url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel={$phone}";
-//            $resp = http_request($url);  //获取API返回 的数据
-//            if(empty($resp)) return false;
-//
-//            $resp = mb_convert_encoding($resp, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5'); //解决中文乱码
-//            $datas = explode('__GetZoneResult_', $resp);
-//            if(count($datas) == 2)
-//            {
-//                $body = trim($datas[0]);
-//                $res = json_decode($body,true);
-//                if(empty($res)) return false;
-//
-//                $province = formatProvince($res['province']);
-//                $region = array_search($province, ProvinceList);
-//                return $res;
-//            }
-
-            return false;
-        };
-
-        $i = 0;
-        do {
-            $ret = $checker($cardno, $region);
-//            if($ret === false && $fCoroutine) {
-//                Log::record("query card_type sleep one second.",Log::DEBUG);
-//                Swoole\Coroutine::sleep(1);
-//            }
-            $i++;
-        } while (empty($ret) && $i < 3);
-
-        if (empty($ret))
-        {
-            $regin_no = -1;
-            Log::record("淘宝接口没法使用了,用传统办法识别卡类型",Log::ERR);
-            if (preg_match('/^134[0-8]\d{7}$|^(?:13[5-9]|147|15[0-27-9]|17[28]|18[2-478]|19[578])\d{8}$/', $cardno, $matches)) {
-                return ChinaMobileCard;
-            } elseif (preg_match('/^(?:13[0-2]|145|15[56]|166|17[156]|18[56]|196)\d{8}$/', $cardno, $matches)) {
-                return ChinaUnicomCard;
-            } elseif (preg_match('/^(?:133|149|153|177|173|18[019]|19[0139])\d{8}$/', $cardno, $matches)) {
-                return ChinaTelecomCard;
-            } else {
-                return UnknownCard;
-            }
-        }
-        elseif($ret['catName'] == '中国联通') {
-            $regin_no = $region;
+        $regin_no = -1;
+        if (preg_match('/^134[0-8]\d{7}$|^(?:13[5-9]|147|15[0-27-9]|17[28]|18[2-478]|19[578])\d{8}$/', $cardno, $matches)) {
+            return ChinaMobileCard;
+        } elseif (preg_match('/^(?:13[0-2]|145|15[56]|166|17[156]|18[56]|196)\d{8}$/', $cardno, $matches)) {
             return ChinaUnicomCard;
-        }
-        elseif($ret['catName'] == '中国电信') {
-            $regin_no = $region;
+        } elseif (preg_match('/^(?:133|149|153|177|173|18[019]|19[0139])\d{8}$/', $cardno, $matches)) {
             return ChinaTelecomCard;
-        }
-        elseif($ret['catName'] == '中国移动') {
-            $regin_no = $region;
-            return ChinaMobileCard;
-        }
-        else {
+        } else {
             return UnknownCard;
         }
     }
@@ -283,37 +231,86 @@ function oil_type($cardno)
     }
 }
 
-function valid_phone($phone) : bool
+function valid_phone($card_no)
 {
-    $ali = function ($phone,&$net_err) : bool
+    $type_checker = function ($channel)
+    {
+        if($channel == '中国联通') {
+            return ChinaUnicomCard;
+        }
+        elseif($channel == '中国电信') {
+            return ChinaTelecomCard;
+        }
+        elseif($channel == '中国移动') {
+            return ChinaMobileCard;
+        }
+        else {
+            return UnknownCard;
+        }
+    };
+
+    $validate_checker = function ($status)
+    {
+        // "status": 1 //状态 0:空号;1:实号;2:停机;3:库无;4:沉默号;5:风险号
+        if (in_array($status, [0,5])) {
+            return false;
+        } else {
+            return true;
+        }
+    };
+
+    $regin_checker = function ($area)
+    {
+        $areas = explode('-',$area);
+        if(count($areas) > 0)
+        {
+            $province = $areas[0];
+            $endtxts= ["省","市","自治区","特别行政区"];
+
+            foreach ($endtxts as $endtxt)
+            {
+                if(strpos($province, $endtxt) === false) {
+                    continue;
+                } else {
+                    $province = mb_strcut($province, 0, strrpos($province, $endtxt));
+                }
+            }
+
+            $region = array_search($province, ProvinceList);
+            return $region;
+        }
+        else {
+            return -1;
+        }
+    };
+
+    $ali = function ($card_no, &$validate, &$card_type, &$region_no) use ($validate_checker, $type_checker, $regin_checker): bool
     {
         $url = 'https://mobileempty.shumaidata.com/mobileempty';
-        $params['mobile'] = $phone;
+        $params['mobile'] = $card_no;
         $headers = ["Authorization:APPCODE " . '8f92d951293f4d2ea48ff86d5a70c537'];
 
+        $net_err = 0;
         $resp = http_request($url, $params, 'GET', false, $headers,$net_err);
-        if ($resp == false) return true;
+        if ($resp == false) return false;
 
         $resp = json_decode($resp, true);
-        if ($resp == false) return true;
+        if ($resp == false) return false;
 
-        if ($resp['success'] == true && $resp['code'] == 200)
+        if ($resp['code'] == 200)
         {
-            // "status": 1 //状态 0:空号;1:实号;2:停机;3:库无;4:沉默号;5:风险号
-            $status = intval($resp['data']['status']);
-            if (in_array($status, [0,5])) {
-                Log::record("valid_phone phone:{$phone}, status : {$status}", Log::DEBUG);
-                return false;
-            } else {
-                return true;
-            }
-        } else {
-            Log::record("valid_phone phone:{$phone} return msg:{$resp['msg']}", Log::DEBUG);
+            $data = $resp['data'];
+            $validate = $validate_checker(intval($data['status']));
+            $card_type = $type_checker($data['channel']);
+            $region_no = $regin_checker($data['area']);
             return true;
+        } else {
+            Log::record("ali_valid phone:{$card_no} return msg:{$resp['msg']}", Log::DEBUG);
+            return false;
         }
     };
 
-    $tian_yan = function ($phone,&$net_err) : bool
+    $tianyan = function ($card_no, &$validate, &$card_type, &$region_no) use ($validate_checker, $type_checker, $regin_checker): bool
     {
         $url = 'https://api.shumaidata.com/v4/mobile_empty/check';
         $appid = '2Xfa6IFIPv0sVUjy';
@@ -324,54 +321,46 @@ function valid_phone($phone) : bool
         $data['timestamp'] = intval($cur * 1000);
         $content = "{$appid}&{$data['timestamp']}&{$appSecurity}";
         $data['sign'] = md5($content);
-        $data['mobile'] = $phone;
+        $data['mobile'] = $card_no;
 
+        $net_err = 0;
         $resp = http_request($url, $data, 'GET',false, [],$net_err);
-        if ($resp == false) return true;
+        if ($resp == false) return false;
 
         $resp = json_decode($resp, true);
-        if ($resp == false) return true;
+        if ($resp == false) return false;
 
-        if ($resp['success'] == true && $resp['code'] == 200)
+        if ($resp['code'] == 200)
         {
-            // "status": 1 //状态 0:空号;1:实号;2:停机;3:库无;4:沉默号;5:风险号
-            $status = intval($resp['data']['status']);
-            if (in_array($status, [0,5])) {
-                Log::record("tianyan_valid_phone phone:{$phone}, status : {$status}", Log::DEBUG);
-                return false;
-            } else {
-                return true;
-            }
-        } else {
-            Log::record("tianyan_valid_phone phone:{$phone} return msg:{$resp['msg']}", Log::DEBUG);
+            $data = $resp['data'];
+            $validate = $validate_checker(intval($data['status']));
+            $card_type = $type_checker($data['channel']);
+            $region_no = $regin_checker($data['area']);
             return true;
+        } else {
+            Log::record("tianyan_valid phone:{$card_no} return msg:{$resp['msg']}", Log::DEBUG);
+            return false;
         }
     };
 
-    $net_err = 0;
-    $ret = $tian_yan($phone,$net_err);
-    if(empty($net_err)) return $ret;
+    $validate = true;
+    $card_type = card_type($card_no,$region_no);
+    $region_no = -1;
 
-    $net_err = 0;
-    return $ali($phone,$net_err);
-}
-
-function formatProvince(string $province) : string
-{
-    if(empty($province)) {
-        return '';
+    if($card_type == PetroChinaCard || $card_type == SinopecCard) {
+        return [$validate,$card_type,$region_no];
     }
 
-    $checkArr = ["省","市","自治区","特别行政区"];
+    $ret = $tianyan($card_no,$validate,$card_type,$region_no);
+    if($ret) {
+        return [$validate,$card_type,$region_no];
+    }
 
-    for($i = 0; $i < count($checkArr); $i++)
-    {
-        if(strpos($province, $checkArr[$i]) === false) {
-            continue;
-        } else {
-            $province = mb_strcut($province, 0, strrpos($province, $checkArr[$i]));
-        }
+    $ret = $ali($card_no,$validate,$card_type,$region_no);
+    if($ret) {
+        return [$validate,$card_type,$region_no];
     }
 
-    return $province;
+    $card_type = card_type($card_no,$region_no);
+    return [$validate,$card_type,$region_no];
 }

+ 12 - 20
rdispatcher/proxy.php

@@ -37,19 +37,14 @@ class proxy
 
         refill\util::push_queue_order($mchid,$mch_order,ORDER_STATE_SEND);
 
-        $need_check = false;
         $first_comit = false;
         if($card_type == 0)
         {
             $first_comit = true;
-            $card_type = mtopcard\card_type($card_no,$regin_no);
+            [$validate,$card_type,$region] = mtopcard\valid_phone($card_no);
             $params['card_type'] = $card_type;
-            $params['regin_no'] = $regin_no;
-
-            global $config;
-            if($config['phone_life_check'] == true && $card_type != mtopcard\PetroChinaCard && $card_type != mtopcard\SinopecCard) {
-                $need_check = true;
-            }
+            $params['regin_no'] = $region;
+            $regin_no = $region;
         }
 
         Log::record("proxy::add mch_order={$mch_order} card_no = {$card_no} regin_no={$regin_no}",Log::DEBUG);
@@ -58,19 +53,16 @@ class proxy
             refill\util::incr_user_commit($mchid,$card_type,$amount,$org_quality);
         }
 
-        if ($need_check)
+        if (!$validate)
         {
-            $valided = mtopcard\valid_phone($card_no);
-            if (!$valided) {
-                $order_id = refill\RefillFactory::instance()->zero_order($mchid, $buyer_id, $amount, $card_no,
-                    $mch_order, $idcard, $card_name, $notify_url, $quality, $org_quality, $order_time, $commit_times,
-                    "无效的手机号");
-
-                refill\util::pop_queue_order($mchid,$mch_order);
-                QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => false]);
-                util::del_exclude_channel($mchid,$mch_order,$card_type);
-                return;
-            }
+            $order_id = refill\RefillFactory::instance()->zero_order($mchid, $buyer_id, $amount, $card_no,
+                $mch_order, $idcard, $card_name, $notify_url, $quality, $org_quality, $order_time, $commit_times,
+                "无效的手机号");
+
+            refill\util::pop_queue_order($mchid, $mch_order);
+            QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => false]);
+            util::del_exclude_channel($mchid, $mch_order, $card_type);
+            return;
         }
 
         [$errcode, $errmsg, $order_id, $neterr,$net_errno] = refill\RefillFactory::instance()->add($mchid, $buyer_id, $amount, $card_no,

+ 0 - 3
test/TestAddData.php

@@ -17,7 +17,6 @@ require_once(BASE_HELPER_PATH . '/PHPExcel/PHPExcel.php');
 require_once (BASE_CORE_PATH . '/framework/function/http.php');
 
 use PHPUnit\Framework\TestCase;
-use function mtopcard\formatProvince;
 use const mtopcard\ProvinceList;
 
 class TestAddData extends TestCase
@@ -52,8 +51,6 @@ class TestAddData extends TestCase
         {
             $body = trim($datas[0]);
             $res = json_decode($body,true);
-            $province = formatProvince($res['province']);
-            $region = array_search($province, ProvinceList);
             return $res;
         }
     }

+ 2 - 2
test/TestCardNo.php

@@ -32,9 +32,9 @@ class TestCardNo extends TestCase
         $phones = array_unique($phones);
         $count = count($phones);
 
-
         foreach ($phones as $phone) {
-            $x = mtopcard\valid_phone($phone);
+            [$validate,$card_type,$region_no] = mtopcard\valid_phone($phone);
+            Log::record("validate={$validate} card_type={$card_type} region_no={$region_no}",Log::DEBUG);
         }
     }
 

+ 0 - 1
test/TestCardType.php

@@ -2,7 +2,6 @@
 
 
 use PHPUnit\Framework\TestCase;
-use function mtopcard\formatProvince;
 use const mtopcard\ChinaMobileCard;
 use const mtopcard\ChinaTelecomCard;
 use const mtopcard\ChinaUnicomCard;

+ 0 - 1
test/TestRefill.php

@@ -2,7 +2,6 @@
 
 
 use PHPUnit\Framework\TestCase;
-use function mtopcard\formatProvince;
 use const mtopcard\ProvinceList;
 
 define('APP_ID', 'test');