mtopcard.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 == 'chinamobile') { //中石化
  44. return ChinaMobileCard;
  45. }
  46. elseif($str_type == 'chinaunicom') { //手机卡
  47. return ChinaUnicomCard;
  48. }
  49. elseif($str_type == 'chinatelecom') { //手机卡
  50. return ChinaTelecomCard;
  51. }
  52. elseif($str_type == 'phone') { //手机卡
  53. return PhoneCard;
  54. }
  55. else {
  56. return UnknownCard;
  57. }
  58. }
  59. function scard_type($card_type)
  60. {
  61. if($card_type == PetroChinaCard) { //中石油
  62. return 'petrochina';
  63. }
  64. elseif ($card_type == SinopecCard) { //中石化
  65. return 'sinopec';
  66. }
  67. elseif ($card_type == ChinaMobileCard) { //中石化
  68. return 'chinamobile';
  69. }
  70. elseif($card_type == ChinaUnicomCard ) { //手机卡
  71. return 'chinaunicom';
  72. }
  73. elseif($card_type == ChinaTelecomCard) { //手机卡
  74. return 'chinatelecom';
  75. }
  76. elseif($card_type == PhoneCard) { //手机卡
  77. return 'phone';
  78. }
  79. else {
  80. return 'unknown';
  81. }
  82. }
  83. require_once(BASE_HELPER_PATH . '/mtopcard/topcard.php');
  84. require_once(BASE_HELPER_PATH . '/mtopcard/user_topcards.php');
  85. require_once(BASE_HELPER_PATH . '/mtopcard/CardPaper.php');
  86. require_once(BASE_HELPER_PATH . '/mtopcard/cards_helper.php');
  87. function priority_cards($member_id, $page_type = '')
  88. {
  89. if($page_type == 'oil') {
  90. $types = [PetroChinaCard,SinopecCard];
  91. }
  92. elseif($page_type == 'phone') {
  93. $types = [PhoneCard];
  94. }
  95. else {
  96. $types = [PetroChinaCard,SinopecCard,PhoneCard];
  97. }
  98. $user_cards = new user_topcards($member_id);
  99. return $user_cards->priority_cards($types);
  100. }
  101. function topcard_format($card_list)
  102. {
  103. $ret = [];
  104. foreach ($card_list as $item) {
  105. $card = new topcard($item);
  106. $item = $card->format();
  107. $item['card_type'] = scard_type($item['card_type']);
  108. $ret[] = $item;
  109. }
  110. return $ret;
  111. }
  112. function simple_card_type($cardno)
  113. {
  114. if (preg_match('/^1[0-9]{18}$/', $cardno, $matches)) {
  115. return SinopecCard;
  116. } elseif (preg_match('/^9[0-9]{15}$/', $cardno, $matches)) {
  117. return PetroChinaCard;
  118. } 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)) {
  119. return ChinaMobileCard;
  120. } elseif (preg_match('/^(?:13[0-2]|145|15[56]|166|17[156]|18[56]|196)\d{8}$/', $cardno, $matches)) {
  121. return ChinaUnicomCard;
  122. } elseif (preg_match('/^(?:133|149|153|177|173|18[019]|19[0139])\d{8}$/', $cardno, $matches)) {
  123. return ChinaTelecomCard;
  124. } elseif (preg_match('/^1\d{10}$/', $cardno, $matches)) {
  125. return PhoneCard;
  126. } else {
  127. return UnknownCard;
  128. }
  129. }
  130. function card_type($cardno)
  131. {
  132. if(preg_match( '/^1[0-9]{18}$/',$cardno,$matches)) {
  133. return SinopecCard;
  134. }
  135. elseif(preg_match( '/^9[0-9]{15}$/',$cardno,$matches)) {
  136. return PetroChinaCard;
  137. }
  138. elseif(preg_match('/^1\d{10}$/',$cardno,$matches))
  139. {
  140. $checker = function ($phone)
  141. {
  142. if (empty($phone)) return false; //手机号不能为空
  143. $url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel={$phone}";
  144. $resp = http_request($url); //获取API返回 的数据
  145. $resp = mb_convert_encoding($resp, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5'); //解决中文乱码
  146. $datas = explode('=', $resp);
  147. if(count($datas) == 2)
  148. {
  149. $body = trim($datas[1]);
  150. if(preg_match_all("/(\w+):'([^']+)/", $body, $m)) {
  151. $res = array_combine($m[1], $m[2]);
  152. return $res;
  153. }
  154. }
  155. return false;
  156. };
  157. $ret = $checker($cardno);
  158. if (empty($ret))
  159. {
  160. Log::record("淘宝接口没法使用了,用传统办法识别卡类型",Log::DEBUG);
  161. 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)) {
  162. return ChinaMobileCard;
  163. } elseif (preg_match('/^(?:13[0-2]|145|15[56]|166|17[156]|18[56]|196)\d{8}$/', $cardno, $matches)) {
  164. return ChinaUnicomCard;
  165. } elseif (preg_match('/^(?:133|149|153|177|173|18[019]|19[0139])\d{8}$/', $cardno, $matches)) {
  166. return ChinaTelecomCard;
  167. } else {
  168. return UnknownCard;
  169. }
  170. }
  171. elseif($ret['catName'] == '中国联通') {
  172. return ChinaUnicomCard;
  173. }
  174. elseif($ret['catName'] == '中国电信') {
  175. return ChinaTelecomCard;
  176. }
  177. elseif($ret['catName'] == '中国移动') {
  178. return ChinaMobileCard;
  179. }
  180. else {
  181. return UnknownCard;
  182. }
  183. }
  184. else {
  185. return UnknownCard;
  186. }
  187. }
  188. function oil_type($cardno)
  189. {
  190. if(preg_match( '/^1[0-9]{18}$/',$cardno,$matches)) {
  191. return SinopecCard;
  192. }
  193. elseif(preg_match( '/^9[0-9]{15}$/',$cardno,$matches)) {
  194. return PetroChinaCard;
  195. }
  196. else {
  197. return UnknownCard;
  198. }
  199. }
  200. function valid_phone($phone) : bool
  201. {
  202. $url = 'https://mobileempty.shumaidata.com/mobileempty';
  203. $params['mobile'] = $phone;
  204. $headers = ["Authorization:APPCODE " . '8f92d951293f4d2ea48ff86d5a70c537'];
  205. $resp = http_request($url, $params, 'GET', false, $headers);
  206. if ($resp == false) return true;
  207. $resp = json_decode($resp, true);
  208. if ($resp == false) return true;
  209. if ($resp['success'] == true && $resp['code'] == 200)
  210. {
  211. // "status": 1 //状态 0:空号;1:实号;2:停机;3:库无;4:沉默号;5:风险号
  212. $status = intval($resp['data']['status']);
  213. if (in_array($status, [0])) {
  214. Log::record("valid_phone phone:{$phone}, status : {$status}", Log::DEBUG);
  215. return false;
  216. } else {
  217. return true;
  218. }
  219. } else {
  220. Log::record("valid_phone phone:{$phone} return msg:{$resp['msg']}", Log::DEBUG);
  221. return true;
  222. }
  223. }