mtopcard.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. const ThirdRefillCard = 7; //三方账号充值
  13. #卡的状态
  14. const CardNormal = 1;
  15. const CardDeled = 2;
  16. #限制参数
  17. const UserMaxCards = 10;
  18. const CardMaxUsers = 5;
  19. #card_key参数设置
  20. const UnusedCard = 0;
  21. const ReserveCard = 1;
  22. const AssignedCard = 2;
  23. const FreezedCard = 3;
  24. #充值卡类型
  25. const OilCardPaper = 1;
  26. const PhoneCardPaper = 2;
  27. #省份列表
  28. const ProvinceList = [
  29. 1 => '北京',
  30. 2 => '天津',
  31. 3 => '河北',
  32. 4 => '山西',
  33. 5 => '内蒙古',
  34. 6 => '辽宁',
  35. 7 => '吉林',
  36. 8 => '黑龙江',
  37. 9 => '上海',
  38. 10 => '江苏',
  39. 11 => '浙江',
  40. 12 => '安徽',
  41. 13 => '福建',
  42. 14 => '江西',
  43. 15 => '山东',
  44. 16 => '河南',
  45. 17 => '湖北',
  46. 18 => '湖南',
  47. 19 => '广东',
  48. 20 => '广西',
  49. 21 => '海南',
  50. 22 => '重庆',
  51. 23 => '四川',
  52. 24 => '贵州',
  53. 25 => '云南',
  54. 26 => '西藏',
  55. 27 => '陕西',
  56. 28 => '甘肃',
  57. 29 => '青海',
  58. 30 => '宁夏',
  59. 31 => '新疆'
  60. ];
  61. function month_stamp($time=null) : int {
  62. $date = getdate($time);
  63. $stamp = $date['year'] * 100 + $date['mon'];
  64. return $stamp;
  65. }
  66. function topcard_type($str_type)
  67. {
  68. if(empty($str_type)) {
  69. return UnknownCard;
  70. }
  71. $str_type = trim(strtolower($str_type));
  72. if($str_type == 'petrochina') { //中石油
  73. return PetroChinaCard;
  74. }
  75. elseif ($str_type == 'sinopec') { //中石化
  76. return SinopecCard;
  77. }
  78. elseif ($str_type == 'chinamobile') { //中石化
  79. return ChinaMobileCard;
  80. }
  81. elseif($str_type == 'chinaunicom') { //手机卡
  82. return ChinaUnicomCard;
  83. }
  84. elseif($str_type == 'chinatelecom') { //手机卡
  85. return ChinaTelecomCard;
  86. }
  87. elseif($str_type == 'phone') { //手机卡
  88. return PhoneCard;
  89. }
  90. else {
  91. return UnknownCard;
  92. }
  93. }
  94. function scard_type($card_type)
  95. {
  96. if($card_type == PetroChinaCard) { //中石油
  97. return 'petrochina';
  98. }
  99. elseif ($card_type == SinopecCard) { //中石化
  100. return 'sinopec';
  101. }
  102. elseif ($card_type == ChinaMobileCard) { //中石化
  103. return 'chinamobile';
  104. }
  105. elseif($card_type == ChinaUnicomCard ) { //手机卡
  106. return 'chinaunicom';
  107. }
  108. elseif($card_type == ChinaTelecomCard) { //手机卡
  109. return 'chinatelecom';
  110. }
  111. elseif($card_type == PhoneCard) { //手机卡
  112. return 'phone';
  113. }
  114. else {
  115. return 'unknown';
  116. }
  117. }
  118. require_once(BASE_HELPER_PATH . '/mtopcard/topcard.php');
  119. require_once(BASE_HELPER_PATH . '/mtopcard/user_topcards.php');
  120. require_once(BASE_HELPER_PATH . '/mtopcard/CardPaper.php');
  121. require_once(BASE_HELPER_PATH . '/mtopcard/cards_helper.php');
  122. function priority_cards($member_id, $page_type = '')
  123. {
  124. if($page_type == 'oil') {
  125. $types = [PetroChinaCard,SinopecCard];
  126. }
  127. elseif($page_type == 'phone') {
  128. $types = [PhoneCard];
  129. }
  130. else {
  131. $types = [PetroChinaCard,SinopecCard,PhoneCard];
  132. }
  133. $user_cards = new user_topcards($member_id);
  134. return $user_cards->priority_cards($types);
  135. }
  136. function topcard_format($card_list)
  137. {
  138. $ret = [];
  139. foreach ($card_list as $item) {
  140. $card = new topcard($item);
  141. $item = $card->format();
  142. $item['card_type'] = scard_type($item['card_type']);
  143. $ret[] = $item;
  144. }
  145. return $ret;
  146. }
  147. function simple_card_type($cardno)
  148. {
  149. if (preg_match('/^1[0-9]{18}$/', $cardno, $matches)) {
  150. return SinopecCard;
  151. } elseif (preg_match('/^9[0-9]{15}$/', $cardno, $matches)) {
  152. return PetroChinaCard;
  153. } 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)) {
  154. return ChinaMobileCard;
  155. } elseif (preg_match('/^(?:13[0-2]|145|15[56]|166|17[156]|18[56]|196)\d{8}$/', $cardno, $matches)) {
  156. return ChinaUnicomCard;
  157. } elseif (preg_match('/^(?:133|149|153|177|173|18[019]|19[0139])\d{8}$/', $cardno, $matches)) {
  158. return ChinaTelecomCard;
  159. } elseif (preg_match('/^1\d{10}$/', $cardno, $matches)) {
  160. return PhoneCard;
  161. } else {
  162. return UnknownCard;
  163. }
  164. }
  165. function card_type($cardno,&$regin_no)
  166. {
  167. $regin_no = -1;
  168. if(preg_match( '/^1[0-9]{18}$/',$cardno,$matches)) {
  169. return SinopecCard;
  170. }
  171. elseif(preg_match( '/^9[0-9]{15}$/',$cardno,$matches)) {
  172. return PetroChinaCard;
  173. }
  174. elseif(preg_match('/^1\d{10}$/',$cardno,$matches))
  175. {
  176. $checker = function ($phone,&$region)
  177. {
  178. if (empty($phone)) return false; //手机号不能为空
  179. $url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel={$phone}";
  180. $resp = http_request($url); //获取API返回 的数据
  181. if(empty($resp)) return false;
  182. $resp = mb_convert_encoding($resp, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5'); //解决中文乱码
  183. $datas = explode('=', $resp);
  184. if(count($datas) == 2)
  185. {
  186. $body = trim($datas[1]);
  187. if(preg_match_all("/(\w+):'([^']+)/", $body, $m)) {
  188. $res = array_combine($m[1], $m[2]);
  189. $province = formatProvince($res['province']);
  190. $region = array_search($province,ProvinceList);
  191. return $res;
  192. }
  193. }
  194. return false;
  195. };
  196. $ret = $checker($cardno,$region);
  197. if (empty($ret))
  198. {
  199. $regin_no = -1;
  200. Log::record("淘宝接口没法使用了,用传统办法识别卡类型",Log::DEBUG);
  201. 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)) {
  202. return ChinaMobileCard;
  203. } elseif (preg_match('/^(?:13[0-2]|145|15[56]|166|17[156]|18[56]|196)\d{8}$/', $cardno, $matches)) {
  204. return ChinaUnicomCard;
  205. } elseif (preg_match('/^(?:133|149|153|177|173|18[019]|19[0139])\d{8}$/', $cardno, $matches)) {
  206. return ChinaTelecomCard;
  207. } else {
  208. return UnknownCard;
  209. }
  210. }
  211. elseif($ret['catName'] == '中国联通') {
  212. $regin_no = $region;
  213. return ChinaUnicomCard;
  214. }
  215. elseif($ret['catName'] == '中国电信') {
  216. $regin_no = $region;
  217. return ChinaTelecomCard;
  218. }
  219. elseif($ret['catName'] == '中国移动') {
  220. $regin_no = $region;
  221. return ChinaMobileCard;
  222. }
  223. else {
  224. return UnknownCard;
  225. }
  226. }
  227. else {
  228. return UnknownCard;
  229. }
  230. }
  231. function oil_type($cardno)
  232. {
  233. if(preg_match( '/^1[0-9]{18}$/',$cardno,$matches)) {
  234. return SinopecCard;
  235. }
  236. elseif(preg_match( '/^9[0-9]{15}$/',$cardno,$matches)) {
  237. return PetroChinaCard;
  238. }
  239. else {
  240. return UnknownCard;
  241. }
  242. }
  243. function valid_phone($phone) : bool
  244. {
  245. $url = 'https://mobileempty.shumaidata.com/mobileempty';
  246. $params['mobile'] = $phone;
  247. $headers = ["Authorization:APPCODE " . '8f92d951293f4d2ea48ff86d5a70c537'];
  248. $resp = http_request($url, $params, 'GET', false, $headers);
  249. if ($resp == false) return true;
  250. $resp = json_decode($resp, true);
  251. if ($resp == false) return true;
  252. if ($resp['success'] == true && $resp['code'] == 200)
  253. {
  254. // "status": 1 //状态 0:空号;1:实号;2:停机;3:库无;4:沉默号;5:风险号
  255. $status = intval($resp['data']['status']);
  256. if (in_array($status, [0])) {
  257. Log::record("valid_phone phone:{$phone}, status : {$status}", Log::DEBUG);
  258. return false;
  259. } else {
  260. return true;
  261. }
  262. } else {
  263. Log::record("valid_phone phone:{$phone} return msg:{$resp['msg']}", Log::DEBUG);
  264. return true;
  265. }
  266. }
  267. function formatProvince(string $province) : string
  268. {
  269. if(empty($province)) {
  270. return '';
  271. }
  272. $checkArr = ["省","市","自治区","特别行政区"];
  273. for($i = 0; $i < count($checkArr); $i++) {
  274. if(strpos($province, $checkArr[$i]) === false) {
  275. continue;
  276. } else {
  277. $province = mb_strcut($province, 0, strrpos($province, $checkArr[$i]));
  278. }
  279. }
  280. return $province;
  281. }