Procházet zdrojové kódy

use taobao check card zone

stanley-king před 4 roky
rodič
revize
df5844b463

+ 39 - 8
helper/mtopcard/mtopcard.php

@@ -109,6 +109,8 @@ function topcard_format($card_list)
     return $ret;
 }
 
+
+
 function card_type($cardno)
 {
     if(preg_match( '/^1[0-9]{18}$/',$cardno,$matches)) {
@@ -117,14 +119,43 @@ function card_type($cardno)
     elseif(preg_match( '/^9[0-9]{15}$/',$cardno,$matches)) {
         return PetroChinaCard;
     }
-    elseif(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;
+    elseif(preg_match('/^1\d{10}$/',$cardno,$matches))
+    {
+        $checker = function ($phone) {
+            if (empty($phone)) return false;  //手机号不能为空
+            $url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel={$phone}";
+            $resp = file_get_contents($url);  //获取API返回 的数据
+            $resp = mb_convert_encoding($resp, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5'); //解决中文乱码
+
+            $datas = explode('=', $resp);
+            if(count($datas) == 2)
+            {
+                $body = trim($datas[1]);
+                if(preg_match_all("/(\w+):'([^']+)/", $body, $m)) {
+                    $res = array_combine($m[1], $m[2]);
+                    return $res;
+                }
+            }
+
+            return false;
+        };
+        
+        $ret = $checker($cardno);
+        if(empty($ret)) {
+            return UnknownCard;
+        }
+        elseif($ret['catName'] == '中国联通') {
+            return ChinaUnicomCard;
+        }
+        elseif($ret['catName'] == '中国电信') {
+            return ChinaTelecomCard;
+        }
+        elseif($ret['catName'] == '中国移动') {
+            return ChinaMobileCard;
+        }
+        else {
+            return UnknownCard;
+        }
     }
     else {
         return UnknownCard;

+ 1 - 6
mobile/control/merchant_refill.php

@@ -50,7 +50,7 @@ class merchant_refillControl extends mbMerchantControl
         $card_type = mtopcard\card_type($card_no);
 
         if($card_type == mtopcard\UnknownCard) {
-            return [false,"充值卡不是电话和中石油中石化类型."];
+            return [false,'卡类型无法识别'];
         }
 
         if($card_type === mtopcard\PetroChinaCard)
@@ -63,11 +63,6 @@ class merchant_refillControl extends mbMerchantControl
             }
         }
 
-        $card_type = mtopcard\card_type($card_no);
-        if($card_type === mtopcard\UnknownCard) {
-            return [false,'卡类型无法识别'];
-        }
-
         return [true,""];
     }
 

+ 1 - 5
mobile/control/refill.php

@@ -46,7 +46,7 @@ class refillControl extends merchantControl
         $card_type = mtopcard\card_type($card_no);
 
         if($card_type == mtopcard\UnknownCard) {
-            return [false,"充值卡不是电话和中石油中石化类型."];
+            return [false,'卡类型无法识别'];
         }
         
         if($card_type === mtopcard\PetroChinaCard)
@@ -65,10 +65,6 @@ class refillControl extends merchantControl
         if(empty($params['order_sn'])) {
             return [false,'参数没有包含贵方唯一订单号:order_sn'];
         }
-        $card_type = mtopcard\card_type($card_no);
-        if($card_type === mtopcard\UnknownCard) {
-            return [false,'卡类型无法识别'];
-        }
 
         return [true,""];
     }

+ 27 - 12
test/TestMemberCard.php

@@ -436,29 +436,44 @@ class TestMemberCard extends TestCase
         $checker = function ($phone) {
             if (empty($phone)) return false;  //手机号不能为空
             $url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel={$phone}";
-            $response = file_get_contents($url);  //获取API返回 的数据
-            $list = mb_convert_encoding($response, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5'); //解决中文乱码
-            //解决返回数据格式问题
-            $res = trim(explode('=', $list)[1]);
-            preg_match_all("/(\w+):'([^']+)/", $res, $m);
-            $res = array_combine($m[1], $m[2]);
-            return $res;
+            $resp = file_get_contents($url);  //获取API返回 的数据
+            $resp = mb_convert_encoding($resp, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5'); //解决中文乱码
+
+            $datas = explode('=', $resp);
+            if(count($datas) == 2)
+            {
+                $body = trim($datas[1]);
+                if(preg_match_all("/(\w+):'([^']+)/", $body, $m)) {
+                    $res = array_combine($m[1], $m[2]);
+                    return $res;
+                }
+            }
+
+            return false;
         };
 
-        $ret = $checker($phone);
 
+        $ret = $checker($phone);
         if(empty($ret)) {
-            return 0;
+            return mtopcard\UnknownCard;
         }
         elseif($ret['catName'] == '中国联通') {
-            return 1;
+            return mtopcard\ChinaUnicomCard;
         }
         elseif($ret['catName'] == '中国电信') {
-            return 2;
+            return mtopcard\ChinaTelecomCard;
         }
         elseif($ret['catName'] == '中国移动') {
-            return 3;
+            return mtopcard\ChinaMobileCard;
         }
+        else {
+            return mtopcard\UnknownCard;
+        }
+    }
+
+    public function testPhoneZone()
+    {
+        $x = $this->carrier_type('13911129867');
     }
     public function testCheckMobileChannel(){
         $mobile = 17801048874;