mtopcard.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. const ThirdCardPaper = 3;
  28. #省份列表
  29. const ProvinceList = [
  30. 1 => '北京',
  31. 2 => '天津',
  32. 3 => '河北',
  33. 4 => '山西',
  34. 5 => '内蒙古',
  35. 6 => '辽宁',
  36. 7 => '吉林',
  37. 8 => '黑龙江',
  38. 9 => '上海',
  39. 10 => '江苏',
  40. 11 => '浙江',
  41. 12 => '安徽',
  42. 13 => '福建',
  43. 14 => '江西',
  44. 15 => '山东',
  45. 16 => '河南',
  46. 17 => '湖北',
  47. 18 => '湖南',
  48. 19 => '广东',
  49. 20 => '广西',
  50. 21 => '海南',
  51. 22 => '重庆',
  52. 23 => '四川',
  53. 24 => '贵州',
  54. 25 => '云南',
  55. 26 => '西藏',
  56. 27 => '陕西',
  57. 28 => '甘肃',
  58. 29 => '青海',
  59. 30 => '宁夏',
  60. 31 => '新疆'
  61. ];
  62. #空号拦截状态
  63. const CardState = [
  64. 0 => '空号',
  65. 1 => '实号',
  66. 2 => '停机',
  67. 3 => '库无',
  68. 4 => '沉默号',
  69. 5 => '风险号'
  70. ];
  71. function month_stamp($time=null) : int {
  72. $date = getdate($time);
  73. $stamp = $date['year'] * 100 + $date['mon'];
  74. return $stamp;
  75. }
  76. function topcard_type($str_type)
  77. {
  78. if(empty($str_type)) {
  79. return UnknownCard;
  80. }
  81. $str_type = trim(strtolower($str_type));
  82. if($str_type == 'petrochina') { //中石油
  83. return PetroChinaCard;
  84. }
  85. elseif ($str_type == 'sinopec') { //中石化
  86. return SinopecCard;
  87. }
  88. elseif ($str_type == 'chinamobile') { //中石化
  89. return ChinaMobileCard;
  90. }
  91. elseif($str_type == 'chinaunicom') { //手机卡
  92. return ChinaUnicomCard;
  93. }
  94. elseif($str_type == 'chinatelecom') { //手机卡
  95. return ChinaTelecomCard;
  96. }
  97. elseif($str_type == 'phone') { //手机卡
  98. return PhoneCard;
  99. }
  100. elseif($str_type == 'third') {
  101. return ThirdRefillCard;
  102. }
  103. else {
  104. return UnknownCard;
  105. }
  106. }
  107. function scard_type($card_type)
  108. {
  109. if($card_type == PetroChinaCard) { //中石油
  110. return 'petrochina';
  111. }
  112. elseif ($card_type == SinopecCard) { //中石化
  113. return 'sinopec';
  114. }
  115. elseif ($card_type == ChinaMobileCard) { //中石化
  116. return 'chinamobile';
  117. }
  118. elseif($card_type == ChinaUnicomCard ) { //手机卡
  119. return 'chinaunicom';
  120. }
  121. elseif($card_type == ChinaTelecomCard) { //手机卡
  122. return 'chinatelecom';
  123. }
  124. elseif($card_type == PhoneCard) { //手机卡
  125. return 'phone';
  126. }
  127. elseif($card_type == ThirdRefillCard) {
  128. return 'third';
  129. }
  130. else {
  131. return 'unknown';
  132. }
  133. }
  134. require_once(BASE_HELPER_PATH . '/mtopcard/topcard.php');
  135. require_once(BASE_HELPER_PATH . '/mtopcard/user_topcards.php');
  136. require_once(BASE_HELPER_PATH . '/mtopcard/CardPaper.php');
  137. require_once(BASE_HELPER_PATH . '/mtopcard/cards_helper.php');
  138. function priority_cards($member_id, $page_type = '')
  139. {
  140. if($page_type == 'oil') {
  141. $types = [PetroChinaCard,SinopecCard];
  142. }
  143. elseif($page_type == 'phone') {
  144. $types = [PhoneCard];
  145. }
  146. else {
  147. $types = [PetroChinaCard,SinopecCard,PhoneCard];
  148. }
  149. $user_cards = new user_topcards($member_id);
  150. return $user_cards->priority_cards($types);
  151. }
  152. function topcard_format($card_list)
  153. {
  154. $ret = [];
  155. foreach ($card_list as $item) {
  156. $card = new topcard($item);
  157. $item = $card->format();
  158. $item['card_type'] = scard_type($item['card_type']);
  159. $ret[] = $item;
  160. }
  161. return $ret;
  162. }
  163. function simple_card_type($cardno)
  164. {
  165. if (preg_match('/^1[0-9]{18}$/', $cardno, $matches)) {
  166. return SinopecCard;
  167. } elseif (preg_match('/^9[0-9]{15}$/', $cardno, $matches)) {
  168. return PetroChinaCard;
  169. } 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)) {
  170. return ChinaMobileCard;
  171. } elseif (preg_match('/^(?:13[0-2]|145|15[56]|166|17[156]|18[56]|196)\d{8}$/', $cardno, $matches)) {
  172. return ChinaUnicomCard;
  173. } elseif (preg_match('/^(?:133|149|153|177|173|18[019]|19[0139])\d{8}$/', $cardno, $matches)) {
  174. return ChinaTelecomCard;
  175. } elseif (preg_match('/^1\d{10}$/', $cardno, $matches)) {
  176. return PhoneCard;
  177. } else {
  178. return UnknownCard;
  179. }
  180. }
  181. function card_type($cardno,&$regin_no)
  182. {
  183. $regin_no = -1;
  184. if(preg_match( '/^1[0-9]{18}$/',$cardno,$matches)) {
  185. return SinopecCard;
  186. }
  187. elseif(preg_match( '/^9[0-9]{15}$/',$cardno,$matches)) {
  188. return PetroChinaCard;
  189. }
  190. elseif(preg_match('/^1\d{10}$/',$cardno,$matches))
  191. {
  192. $regin_no = -1;
  193. 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)) {
  194. return ChinaMobileCard;
  195. } elseif (preg_match('/^(?:13[0-2]|145|15[56]|166|17[156]|18[56]|196)\d{8}$/', $cardno, $matches)) {
  196. return ChinaUnicomCard;
  197. } elseif (preg_match('/^(?:133|149|153|177|173|18[019]|19[0139])\d{8}$/', $cardno, $matches)) {
  198. return ChinaTelecomCard;
  199. } else {
  200. return UnknownCard;
  201. }
  202. }
  203. else {
  204. return UnknownCard;
  205. }
  206. }
  207. function oil_type($cardno)
  208. {
  209. if(preg_match( '/^1[0-9]{18}$/',$cardno,$matches)) {
  210. return SinopecCard;
  211. }
  212. elseif(preg_match( '/^9[0-9]{15}$/',$cardno,$matches)) {
  213. return PetroChinaCard;
  214. }
  215. else {
  216. return UnknownCard;
  217. }
  218. }
  219. function valid_phone($card_no)
  220. {
  221. $type_checker = function ($channel)
  222. {
  223. if($channel == '联通') {
  224. return ChinaUnicomCard;
  225. }
  226. elseif($channel == '电信') {
  227. return ChinaTelecomCard;
  228. }
  229. elseif($channel == '移动') {
  230. return ChinaMobileCard;
  231. }
  232. else {
  233. return UnknownCard;
  234. }
  235. };
  236. $validate_checker = function ($status)
  237. {
  238. // "status": 1 //状态 0:空号;1:实号;2:停机;3:库无;4:沉默号;5:风险号
  239. Log::record("status={$status}",Log::DEBUG);
  240. if (in_array($status, [0,5])) {
  241. return false;
  242. } else {
  243. return true;
  244. }
  245. };
  246. $regin_checker = function ($area)
  247. {
  248. if(empty($area)) return -1;
  249. $areas = explode('-',$area);
  250. if(count($areas) > 0)
  251. {
  252. $province = $areas[0];
  253. $endtxts= ["省","市","自治区","特别行政区"];
  254. foreach ($endtxts as $endtxt)
  255. {
  256. if(strpos($province, $endtxt) === false) {
  257. continue;
  258. } else {
  259. $province = mb_strcut($province, 0, strrpos($province, $endtxt));
  260. }
  261. }
  262. $region = array_search($province, ProvinceList);
  263. return $region;
  264. }
  265. else {
  266. return -1;
  267. }
  268. };
  269. $ali = function ($card_no, &$validate, &$region_no,&$status) use ($validate_checker, $regin_checker): bool
  270. {
  271. $url = 'https://mobileempty.shumaidata.com/mobileempty';
  272. $params['mobile'] = $card_no;
  273. $headers = ["Authorization:APPCODE " . '8f92d951293f4d2ea48ff86d5a70c537'];
  274. $net_err = 0;
  275. $resp = http_request($url, $params, 'GET', false, $headers,$net_err);
  276. if ($resp == false) return false;
  277. $resp = json_decode($resp, true);
  278. if ($resp == false) return false;
  279. if ($resp['code'] == 200)
  280. {
  281. $data = $resp['data'];
  282. $status = intval($data['status']);
  283. $validate = $validate_checker(intval($data['status']));
  284. $region_no = $regin_checker($data['area']);
  285. return true;
  286. } else {
  287. Log::record("ali_valid phone:{$card_no} return msg:{$resp['msg']}", Log::DEBUG);
  288. return false;
  289. }
  290. };
  291. $tianyan = function ($card_no, &$validate, &$region_no,&$status) use ($validate_checker, $regin_checker): bool
  292. {
  293. $url = 'https://api.shumaidata.com/v4/mobile_empty/check';
  294. $appid = '2Xfa6IFIPv0sVUjy';
  295. $appSecurity = '2Xfa6IFIPv0sVUjynOddsfh6KXfbyJ84';
  296. $cur = microtime (true);
  297. $data['appid'] = $appid;
  298. $data['timestamp'] = intval($cur * 1000);
  299. $content = "{$appid}&{$data['timestamp']}&{$appSecurity}";
  300. $data['sign'] = md5($content);
  301. $data['mobile'] = $card_no;
  302. $net_err = 0;
  303. $resp = http_request($url, $data, 'GET',false, [],$net_err);
  304. if ($resp == false) return false;
  305. $resp = json_decode($resp, true);
  306. if ($resp == false) return false;
  307. if ($resp['code'] == 200)
  308. {
  309. $data = $resp['data'];
  310. $status = intval($data['status']);
  311. $validate = $validate_checker(intval($data['status']));
  312. $region_no = $regin_checker($data['area']);
  313. return true;
  314. } else {
  315. Log::record("tianyan_valid phone:{$card_no} return msg:{$resp['msg']}", Log::DEBUG);
  316. return false;
  317. }
  318. };
  319. $tianyan_cardtyper = function ($card_no) use ($type_checker)
  320. {
  321. $url = 'https://api.shumaidata.com/v4/mobile-transfer/query';
  322. $appid = '2Xfa6IFIPv0sVUjy';
  323. $appSecurity = '2Xfa6IFIPv0sVUjynOddsfh6KXfbyJ84';
  324. $cur = microtime (true);
  325. $data['appid'] = $appid;
  326. $data['timestamp'] = intval($cur * 1000);
  327. $content = "{$appid}&{$data['timestamp']}&{$appSecurity}";
  328. $data['sign'] = md5($content);
  329. $data['mobile'] = $card_no;
  330. $net_err = 0;
  331. $resp = http_request($url, $data, 'GET',false, [],$net_err);
  332. if ($resp == false) return false;
  333. $resp = json_decode($resp, true);
  334. if ($resp == false) return false;
  335. if ($resp['code'] == 200)
  336. {
  337. $data = $resp['data'];
  338. $ispType = $data['ispType'];
  339. $newIspType = $data['newIspType'];
  340. Log::record("tianyan_transfer phone:{$card_no} ispType:{$ispType} newIspType:{$newIspType}", Log::DEBUG);
  341. $card_type = $type_checker($newIspType);
  342. $isTransfer = $ispType != $newIspType;
  343. return [true,$card_type,$isTransfer];
  344. } else {
  345. Log::record("tianyan_transfer phone:{$card_no} return msg:{$resp['msg']}", Log::DEBUG);
  346. return [false,UnknownCard,false];
  347. }
  348. };
  349. $validate = true;
  350. $card_type = card_type($card_no,$region_no);
  351. $region_no = -1;
  352. if($card_type == PetroChinaCard || $card_type == SinopecCard) {
  353. return [$validate,$card_type,$region_no,false,1];
  354. }
  355. [$succ,$_card_type,$_isTransfer] = $tianyan_cardtyper($card_no);
  356. if($succ) {
  357. $card_type = $_card_type;
  358. $isTransfer = $_isTransfer;
  359. }
  360. else {
  361. $isTransfer = false;
  362. }
  363. $status = 6;
  364. $ret = $tianyan($card_no,$validate,$region_no,$status);
  365. if($ret) {
  366. return [$validate,$card_type,$region_no,$isTransfer,$status];
  367. }
  368. $ret = $ali($card_no,$validate,$region_no,$status);
  369. if($ret) {
  370. return [$validate,$card_type,$region_no,$isTransfer,$status];
  371. }
  372. return [true,$card_type,-1,false,$status];
  373. }
  374. function is_validate($status)
  375. {
  376. if (in_array($status, [0,5])) {
  377. return false;
  378. } else {
  379. return true;
  380. }
  381. }