123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- <?php
- namespace mtopcard;
- use Log;
- #用户卡的类型
- const UnknownCard = 0;
- const PetroChinaCard = 1; //中石油
- const SinopecCard = 2; //中石化
- const PhoneCard = 3; //手机卡
- const ChinaMobileCard = 4; //手机卡
- const ChinaUnicomCard = 5; //手机卡
- const ChinaTelecomCard = 6; //手机卡
- const ThirdRefillCard = 7; //三方账号充值
- #卡的状态
- const CardNormal = 1;
- const CardDeled = 2;
- #限制参数
- const UserMaxCards = 10;
- const CardMaxUsers = 5;
- #card_key参数设置
- const UnusedCard = 0;
- const ReserveCard = 1;
- const AssignedCard = 2;
- const FreezedCard = 3;
- #充值卡类型
- const OilCardPaper = 1;
- const PhoneCardPaper = 2;
- const ThirdCardPaper = 3;
- #省份列表
- const ProvinceList = [
- 1 => '北京',
- 2 => '天津',
- 3 => '河北',
- 4 => '山西',
- 5 => '内蒙古',
- 6 => '辽宁',
- 7 => '吉林',
- 8 => '黑龙江',
- 9 => '上海',
- 10 => '江苏',
- 11 => '浙江',
- 12 => '安徽',
- 13 => '福建',
- 14 => '江西',
- 15 => '山东',
- 16 => '河南',
- 17 => '湖北',
- 18 => '湖南',
- 19 => '广东',
- 20 => '广西',
- 21 => '海南',
- 22 => '重庆',
- 23 => '四川',
- 24 => '贵州',
- 25 => '云南',
- 26 => '西藏',
- 27 => '陕西',
- 28 => '甘肃',
- 29 => '青海',
- 30 => '宁夏',
- 31 => '新疆'
- ];
- #空号拦截状态
- const CardState = [
- 0 => '空号',
- 1 => '实号',
- 2 => '停机',
- 3 => '库无',
- 4 => '沉默号',
- 5 => '风险号'
- ];
- function month_stamp($time=null) : int {
- $date = getdate($time);
- $stamp = $date['year'] * 100 + $date['mon'];
- return $stamp;
- }
- function topcard_type($str_type)
- {
- if(empty($str_type)) {
- return UnknownCard;
- }
- $str_type = trim(strtolower($str_type));
- if($str_type == 'petrochina') { //中石油
- return PetroChinaCard;
- }
- elseif ($str_type == 'sinopec') { //中石化
- return SinopecCard;
- }
- elseif ($str_type == 'chinamobile') { //中石化
- return ChinaMobileCard;
- }
- elseif($str_type == 'chinaunicom') { //手机卡
- return ChinaUnicomCard;
- }
- elseif($str_type == 'chinatelecom') { //手机卡
- return ChinaTelecomCard;
- }
- elseif($str_type == 'phone') { //手机卡
- return PhoneCard;
- }
- elseif($str_type == 'third') {
- return ThirdRefillCard;
- }
- else {
- return UnknownCard;
- }
- }
- function scard_type($card_type)
- {
- if($card_type == PetroChinaCard) { //中石油
- return 'petrochina';
- }
- elseif ($card_type == SinopecCard) { //中石化
- return 'sinopec';
- }
- elseif ($card_type == ChinaMobileCard) { //中石化
- return 'chinamobile';
- }
- elseif($card_type == ChinaUnicomCard ) { //手机卡
- return 'chinaunicom';
- }
- elseif($card_type == ChinaTelecomCard) { //手机卡
- return 'chinatelecom';
- }
- elseif($card_type == PhoneCard) { //手机卡
- return 'phone';
- }
- elseif($card_type == ThirdRefillCard) {
- return 'third';
- }
- else {
- return 'unknown';
- }
- }
- require_once(BASE_HELPER_PATH . '/mtopcard/topcard.php');
- require_once(BASE_HELPER_PATH . '/mtopcard/user_topcards.php');
- require_once(BASE_HELPER_PATH . '/mtopcard/CardPaper.php');
- require_once(BASE_HELPER_PATH . '/mtopcard/cards_helper.php');
- function priority_cards($member_id, $page_type = '')
- {
- if($page_type == 'oil') {
- $types = [PetroChinaCard,SinopecCard];
- }
- elseif($page_type == 'phone') {
- $types = [PhoneCard];
- }
- else {
- $types = [PetroChinaCard,SinopecCard,PhoneCard];
- }
- $user_cards = new user_topcards($member_id);
- return $user_cards->priority_cards($types);
- }
- function topcard_format($card_list)
- {
- $ret = [];
- foreach ($card_list as $item) {
- $card = new topcard($item);
- $item = $card->format();
- $item['card_type'] = scard_type($item['card_type']);
- $ret[] = $item;
- }
- return $ret;
- }
- function simple_card_type($cardno)
- {
- if (preg_match('/^1[0-9]{18}$/', $cardno, $matches)) {
- return SinopecCard;
- } 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)) {
- return PhoneCard;
- } else {
- return UnknownCard;
- }
- }
- function card_type($cardno,&$regin_no)
- {
- $regin_no = -1;
- if(preg_match( '/^1[0-9]{18}$/',$cardno,$matches)) {
- return SinopecCard;
- }
- elseif(preg_match( '/^9[0-9]{15}$/',$cardno,$matches)) {
- return PetroChinaCard;
- }
- elseif(preg_match('/^1\d{10}$/',$cardno,$matches))
- {
- $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 (preg_match('/^(?:133|149|153|177|173|18[019]|19[0139])\d{8}$/', $cardno, $matches)) {
- return ChinaTelecomCard;
- } else {
- return UnknownCard;
- }
- }
- else {
- return UnknownCard;
- }
- }
- function oil_type($cardno)
- {
- if(preg_match( '/^1[0-9]{18}$/',$cardno,$matches)) {
- return SinopecCard;
- }
- elseif(preg_match( '/^9[0-9]{15}$/',$cardno,$matches)) {
- return PetroChinaCard;
- }
- else {
- return UnknownCard;
- }
- }
- function valid_phone($card_no)
- {
- $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:风险号
- Log::record("status={$status}",Log::DEBUG);
- if (in_array($status, [0,5])) {
- return false;
- } else {
- return true;
- }
- };
- $regin_checker = function ($area)
- {
- if(empty($area)) return -1;
- $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, &$region_no,&$status) use ($validate_checker, $regin_checker): bool
- {
- $url = 'https://mobileempty.shumaidata.com/mobileempty';
- $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 false;
- $resp = json_decode($resp, true);
- if ($resp == false) return false;
- if ($resp['code'] == 200)
- {
- $data = $resp['data'];
- $status = intval($data['status']);
- $validate = $validate_checker(intval($data['status']));
- $region_no = $regin_checker($data['area']);
- return true;
- } else {
- Log::record("ali_valid phone:{$card_no} return msg:{$resp['msg']}", Log::DEBUG);
- return false;
- }
- };
- $tianyan = function ($card_no, &$validate, &$region_no,&$status) use ($validate_checker, $regin_checker): bool
- {
- $url = 'https://api.shumaidata.com/v4/mobile_empty/check';
- $appid = '2Xfa6IFIPv0sVUjy';
- $appSecurity = '2Xfa6IFIPv0sVUjynOddsfh6KXfbyJ84';
- $cur = microtime (true);
- $data['appid'] = $appid;
- $data['timestamp'] = intval($cur * 1000);
- $content = "{$appid}&{$data['timestamp']}&{$appSecurity}";
- $data['sign'] = md5($content);
- $data['mobile'] = $card_no;
- $net_err = 0;
- $resp = http_request($url, $data, 'GET',false, [],$net_err);
- if ($resp == false) return false;
- $resp = json_decode($resp, true);
- if ($resp == false) return false;
- if ($resp['code'] == 200)
- {
- $data = $resp['data'];
- $status = intval($data['status']);
- $validate = $validate_checker(intval($data['status']));
- $region_no = $regin_checker($data['area']);
- return true;
- } else {
- Log::record("tianyan_valid phone:{$card_no} return msg:{$resp['msg']}", Log::DEBUG);
- return false;
- }
- };
- $tianyan_cardtyper = function ($card_no) use ($type_checker)
- {
- $url = 'https://api.shumaidata.com/v4/mobile-transfer/query';
- $appid = '2Xfa6IFIPv0sVUjy';
- $appSecurity = '2Xfa6IFIPv0sVUjynOddsfh6KXfbyJ84';
- $cur = microtime (true);
- $data['appid'] = $appid;
- $data['timestamp'] = intval($cur * 1000);
- $content = "{$appid}&{$data['timestamp']}&{$appSecurity}";
- $data['sign'] = md5($content);
- $data['mobile'] = $card_no;
- $net_err = 0;
- $resp = http_request($url, $data, 'GET',false, [],$net_err);
- if ($resp == false) return false;
- $resp = json_decode($resp, true);
- if ($resp == false) return false;
- if ($resp['code'] == 200)
- {
- $data = $resp['data'];
- $ispType = $data['ispType'];
- $newIspType = $data['newIspType'];
- Log::record("tianyan_transfer phone:{$card_no} ispType:{$ispType} newIspType:{$newIspType}", Log::DEBUG);
- $card_type = $type_checker($newIspType);
- $isTransfer = $ispType != $newIspType;
- return [true,$card_type,$isTransfer];
- } else {
- Log::record("tianyan_transfer phone:{$card_no} return msg:{$resp['msg']}", Log::DEBUG);
- return [false,UnknownCard,false];
- }
- };
- $validate = true;
- $card_type = card_type($card_no,$region_no);
- $region_no = -1;
- if($card_type == PetroChinaCard || $card_type == SinopecCard) {
- return [$validate,$card_type,$region_no,false,1];
- }
- [$succ,$_card_type,$_isTransfer] = $tianyan_cardtyper($card_no);
- if($succ) {
- $card_type = $_card_type;
- $isTransfer = $_isTransfer;
- }
- else {
- $isTransfer = false;
- }
- $status = 6;
- $ret = $tianyan($card_no,$validate,$region_no,$status);
- if($ret) {
- return [$validate,$card_type,$region_no,$isTransfer,$status];
- }
- $ret = $ali($card_no,$validate,$region_no,$status);
- if($ret) {
- return [$validate,$card_type,$region_no,$isTransfer,$status];
- }
- return [true,$card_type,-1,false,$status];
- }
- function is_validate($status)
- {
- if (in_array($status, [0,5])) {
- return false;
- } else {
- return true;
- }
- }
|