mtopcard.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace mtopcard;
  3. use Log;
  4. #用户卡的类型
  5. const UnknownCard = 0;
  6. const PetroChinaCard = 1; //中石油
  7. const SinopecCard = 2; //中石化
  8. const PhoneCard = 3; //手机卡
  9. const ChinaMobileCard = 4; //手机卡
  10. const ChinaUnicomCard = 5; //手机卡
  11. const ChinaTelecomCard = 6; //手机卡
  12. #卡的状态
  13. const CardNormal = 1;
  14. const CardDeled = 2;
  15. #限制参数
  16. const UserMaxCards = 10;
  17. const CardMaxUsers = 5;
  18. #card_key参数设置
  19. const UnusedCard = 0;
  20. const ReserveCard = 1;
  21. const AssignedCard = 2;
  22. const FreezedCard = 3;
  23. #充值卡类型
  24. const OilCardPaper = 1;
  25. const PhoneCardPaper = 2;
  26. function month_stamp($time=null) : int {
  27. $date = getdate($time);
  28. $stamp = $date['year'] * 100 + $date['mon'];
  29. return $stamp;
  30. }
  31. function topcard_type($str_type)
  32. {
  33. if(empty($str_type)) {
  34. return UnknownCard;
  35. }
  36. $str_type = trim(strtolower($str_type));
  37. if($str_type == 'petrochina') { //中石油
  38. return PetroChinaCard;
  39. }
  40. elseif ($str_type == 'sinopec') { //中石化
  41. return SinopecCard;
  42. }
  43. elseif($str_type == 'phone') { //手机卡
  44. return PhoneCard;
  45. }
  46. else {
  47. return UnknownCard;
  48. }
  49. }
  50. function scard_type($card_type)
  51. {
  52. if($card_type == PetroChinaCard) { //中石油
  53. return 'petrochina';
  54. }
  55. elseif ($card_type == SinopecCard) { //中石化
  56. return 'sinopec';
  57. }
  58. elseif($card_type == PhoneCard) { //手机卡
  59. return 'phone';
  60. }
  61. else {
  62. return 'unknown';
  63. }
  64. }
  65. require_once(BASE_HELPER_PATH . '/mtopcard/topcard.php');
  66. require_once(BASE_HELPER_PATH . '/mtopcard/user_topcards.php');
  67. require_once(BASE_HELPER_PATH . '/mtopcard/CardPaper.php');
  68. require_once(BASE_HELPER_PATH . '/mtopcard/cards_helper.php');
  69. function priority_cards($member_id, $page_type = '')
  70. {
  71. if($page_type == 'oil') {
  72. $types = [PetroChinaCard,SinopecCard];
  73. }
  74. elseif($page_type == 'phone') {
  75. $types = [PhoneCard];
  76. }
  77. else {
  78. $types = [PetroChinaCard,SinopecCard,PhoneCard];
  79. }
  80. $user_cards = new user_topcards($member_id);
  81. return $user_cards->priority_cards($types);
  82. }
  83. function topcard_format($card_list)
  84. {
  85. $ret = [];
  86. foreach ($card_list as $item) {
  87. $card = new topcard($item);
  88. $item = $card->format();
  89. $item['card_type'] = scard_type($item['card_type']);
  90. $ret[] = $item;
  91. }
  92. return $ret;
  93. }
  94. function simple_card_type($cardno)
  95. {
  96. if(preg_match( '/^1[0-9]{18}$/',$cardno,$matches)) {
  97. return SinopecCard;
  98. }
  99. elseif(preg_match( '/^9[0-9]{15}$/',$cardno,$matches)) {
  100. return PetroChinaCard;
  101. }
  102. 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)) {
  103. return ChinaMobileCard;
  104. } elseif (preg_match('/^(?:13[0-2]|145|15[56]|166|17[156]|18[56]|196)\d{8}$/', $cardno, $matches)) {
  105. return ChinaUnicomCard;
  106. } elseif (preg_match('/^(?:133|149|153|177|173|18[019]|19[0139])\d{8}$/', $cardno, $matches)) {
  107. return ChinaTelecomCard;
  108. } else {
  109. return UnknownCard;
  110. }
  111. }
  112. function card_type($cardno)
  113. {
  114. if(preg_match( '/^1[0-9]{18}$/',$cardno,$matches)) {
  115. return SinopecCard;
  116. }
  117. elseif(preg_match( '/^9[0-9]{15}$/',$cardno,$matches)) {
  118. return PetroChinaCard;
  119. }
  120. elseif(preg_match('/^1\d{10}$/',$cardno,$matches))
  121. {
  122. $checker = function ($phone)
  123. {
  124. if (empty($phone)) return false; //手机号不能为空
  125. $url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel={$phone}";
  126. $resp = http_request($url); //获取API返回 的数据
  127. $resp = mb_convert_encoding($resp, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5'); //解决中文乱码
  128. $datas = explode('=', $resp);
  129. if(count($datas) == 2)
  130. {
  131. $body = trim($datas[1]);
  132. if(preg_match_all("/(\w+):'([^']+)/", $body, $m)) {
  133. $res = array_combine($m[1], $m[2]);
  134. return $res;
  135. }
  136. }
  137. return false;
  138. };
  139. $ret = $checker($cardno);
  140. if (empty($ret))
  141. {
  142. Log::record("淘宝接口没法使用了,用传统办法识别卡类型",Log::DEBUG);
  143. 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)) {
  144. return ChinaMobileCard;
  145. } elseif (preg_match('/^(?:13[0-2]|145|15[56]|166|17[156]|18[56]|196)\d{8}$/', $cardno, $matches)) {
  146. return ChinaUnicomCard;
  147. } elseif (preg_match('/^(?:133|149|153|177|173|18[019]|19[0139])\d{8}$/', $cardno, $matches)) {
  148. return ChinaTelecomCard;
  149. } else {
  150. return UnknownCard;
  151. }
  152. }
  153. elseif($ret['catName'] == '中国联通') {
  154. return ChinaUnicomCard;
  155. }
  156. elseif($ret['catName'] == '中国电信') {
  157. return ChinaTelecomCard;
  158. }
  159. elseif($ret['catName'] == '中国移动') {
  160. return ChinaMobileCard;
  161. }
  162. else {
  163. return UnknownCard;
  164. }
  165. }
  166. else {
  167. return UnknownCard;
  168. }
  169. }
  170. function oil_type($cardno)
  171. {
  172. if(preg_match( '/^1[0-9]{18}$/',$cardno,$matches)) {
  173. return SinopecCard;
  174. }
  175. elseif(preg_match( '/^9[0-9]{15}$/',$cardno,$matches)) {
  176. return PetroChinaCard;
  177. }
  178. else {
  179. return UnknownCard;
  180. }
  181. }