123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?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 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;
- 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 == 'phone') { //手机卡
- return PhoneCard;
- }
- else {
- return UnknownCard;
- }
- }
- function scard_type($card_type)
- {
- if($card_type == PetroChinaCard) { //中石油
- return 'petrochina';
- }
- elseif ($card_type == SinopecCard) { //中石化
- return 'sinopec';
- }
- elseif($card_type == PhoneCard) { //手机卡
- return 'phone';
- }
- 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;
- } else {
- return UnknownCard;
- }
- }
- function 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('/^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 = http_request($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))
- {
- Log::record("淘宝接口没法使用了,用传统办法识别卡类型",Log::DEBUG);
- 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'] == '中国联通') {
- return ChinaUnicomCard;
- }
- elseif($ret['catName'] == '中国电信') {
- return ChinaTelecomCard;
- }
- elseif($ret['catName'] == '中国移动') {
- return ChinaMobileCard;
- }
- 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;
- }
- }
|