mtopcard.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. //[$validate,$card_type,$region_no,$isTransfer,$status,$black]
  220. function valid_phone($card_no)
  221. {
  222. $type_checker = function ($channel)
  223. {
  224. if($channel == '联通') {
  225. return ChinaUnicomCard;
  226. }
  227. elseif($channel == '电信') {
  228. return ChinaTelecomCard;
  229. }
  230. elseif($channel == '移动') {
  231. return ChinaMobileCard;
  232. }
  233. else {
  234. return UnknownCard;
  235. }
  236. };
  237. $validate_checker = function ($status)
  238. {
  239. // "status": 1 //状态 0:空号;1:实号;2:停机;3:库无;4:沉默号;5:风险号
  240. Log::record("status={$status}",Log::DEBUG);
  241. if (in_array($status, [0,5])) {
  242. return false;
  243. } else {
  244. return true;
  245. }
  246. };
  247. $regin_checker = function ($area)
  248. {
  249. if(empty($area)) return -1;
  250. $areas = explode('-',$area);
  251. if(count($areas) > 0)
  252. {
  253. $province = $areas[0];
  254. $endtxts= ["省","市","自治区","特别行政区"];
  255. foreach ($endtxts as $endtxt)
  256. {
  257. if(strpos($province, $endtxt) === false) {
  258. continue;
  259. } else {
  260. $province = mb_strcut($province, 0, strrpos($province, $endtxt));
  261. }
  262. }
  263. $region = array_search($province, ProvinceList);
  264. return $region;
  265. }
  266. else {
  267. return -1;
  268. }
  269. };
  270. $ali = function ($card_no, &$validate, &$region_no,&$status) use ($validate_checker, $regin_checker): bool
  271. {
  272. $url = 'https://mobileempty.shumaidata.com/mobileempty';
  273. $params['mobile'] = $card_no;
  274. $headers = ["Authorization:APPCODE " . '8f92d951293f4d2ea48ff86d5a70c537'];
  275. $net_err = 0;
  276. $resp = http_request($url, $params, 'GET', false, $headers,$net_err);
  277. if ($resp == false) return false;
  278. $resp = json_decode($resp, true);
  279. if ($resp == false) return false;
  280. if ($resp['code'] == 200)
  281. {
  282. $data = $resp['data'];
  283. $status = intval($data['status']);
  284. $validate = $validate_checker(intval($data['status']));
  285. $region_no = $regin_checker($data['area']);
  286. return true;
  287. } else {
  288. Log::record("ali_valid phone:{$card_no} return msg:{$resp['msg']}", Log::DEBUG);
  289. return false;
  290. }
  291. };
  292. $tianyan = function ($card_no, &$validate, &$region_no,&$status) use ($validate_checker, $regin_checker): bool
  293. {
  294. $url = 'https://api.shumaidata.com/v4/mobile_empty/check';
  295. $appid = '2Xfa6IFIPv0sVUjy';
  296. $appSecurity = '2Xfa6IFIPv0sVUjynOddsfh6KXfbyJ84';
  297. $cur = microtime (true);
  298. $data['appid'] = $appid;
  299. $data['timestamp'] = intval($cur * 1000);
  300. $content = "{$appid}&{$data['timestamp']}&{$appSecurity}";
  301. $data['sign'] = md5($content);
  302. $data['mobile'] = $card_no;
  303. $net_err = 0;
  304. $resp = http_request($url, $data, 'GET',false, [],$net_err);
  305. if ($resp == false) return false;
  306. $resp = json_decode($resp, true);
  307. if ($resp == false) return false;
  308. if ($resp['code'] == 200)
  309. {
  310. $data = $resp['data'];
  311. $status = intval($data['status']);
  312. $validate = $validate_checker(intval($data['status']));
  313. $region_no = $regin_checker($data['area']);
  314. return true;
  315. } else {
  316. Log::record("tianyan_valid phone:{$card_no} return msg:{$resp['msg']}", Log::DEBUG);
  317. return false;
  318. }
  319. };
  320. $tianyan_cardtyper = function ($card_no) use ($type_checker)
  321. {
  322. $url = 'https://api.shumaidata.com/v4/mobile-transfer/query';
  323. $appid = '2Xfa6IFIPv0sVUjy';
  324. $appSecurity = '2Xfa6IFIPv0sVUjynOddsfh6KXfbyJ84';
  325. $cur = microtime (true);
  326. $data['appid'] = $appid;
  327. $data['timestamp'] = intval($cur * 1000);
  328. $content = "{$appid}&{$data['timestamp']}&{$appSecurity}";
  329. $data['sign'] = md5($content);
  330. $data['mobile'] = $card_no;
  331. $net_err = 0;
  332. $resp = http_request($url, $data, 'GET',false, [],$net_err);
  333. if ($resp == false) return [false,UnknownCard,UnknownCard,false];
  334. $resp = json_decode($resp, true);
  335. if ($resp == false) return [false,UnknownCard,UnknownCard,false];
  336. if ($resp['code'] == 200)
  337. {
  338. $data = $resp['data'];
  339. $ispType = $data['ispType'];
  340. $newIspType = $data['newIspType'];
  341. Log::record("tianyan_transfer phone:{$card_no} ispType:{$ispType} newIspType:{$newIspType}", Log::DEBUG);
  342. $card_type = $type_checker($newIspType);
  343. $org_type = $type_checker($ispType);
  344. $isTransfer = $org_type != $card_type;
  345. return [true,$card_type,$org_type,$isTransfer];
  346. } else {
  347. Log::record("tianyan_transfer phone:{$card_no} return msg:{$resp['msg']}", Log::DEBUG);
  348. return [false,UnknownCard,UnknownCard,false];
  349. }
  350. };
  351. $time_checker = function ($update_time)
  352. {
  353. //90 天以内数据有效
  354. $delta = time() - intval($update_time);
  355. return $delta < 90 * 86400 && $delta >= 0;
  356. };
  357. $pinfo_getter = function ($card_no) use ($validate_checker,$time_checker)
  358. {
  359. $mod_card = Model('card_info');
  360. $info = $mod_card->getCardInfo($card_no);
  361. if(empty($info)) {
  362. return [false,[]];
  363. }
  364. elseif($time_checker($info['update_time'])) {
  365. $mod_card->where(['card_no' => $card_no])->update(['using_times' => $info['using_times'] + 1]);
  366. $card_type = intval($info['card_type']);
  367. $region_no = intval($info['regin']);
  368. $isTransfer = intval($info['transfer']) == 0 ? false : true;
  369. $status = intval($info['card_state']);
  370. $black = intval($info['black']);
  371. $validate = $validate_checker($status);
  372. $result = [$validate,$card_type,$region_no,$isTransfer,$status,$black];
  373. return [true,$result];
  374. }
  375. else {
  376. return [false,[]];
  377. }
  378. };
  379. $pinfo_updator = function ($card_no,$card_type,$org_type,$region_no,$isTransfer,$status)
  380. {
  381. $mod_card = Model('card_info');
  382. $transfer = $isTransfer == true ? 1 : 0;
  383. $mod_card->replace_card($card_no,$card_type,$org_type,$region_no,$transfer,$status);
  384. };
  385. $validate = true;
  386. $black = 0;
  387. $card_type = card_type($card_no,$region_no);
  388. $region_no = -1;
  389. if($card_type == PetroChinaCard || $card_type == SinopecCard) {
  390. return [$validate,$card_type,$region_no,false,1,$black];
  391. }
  392. [$succ,$result] = $pinfo_getter($card_no);
  393. if($succ) return $result;
  394. [$succ,$_card_type,$_org_type,$_isTransfer] = $tianyan_cardtyper($card_no);
  395. if($succ) {
  396. $card_type = $_card_type;
  397. $org_type = $_org_type;
  398. $isTransfer = $_isTransfer;
  399. }
  400. else {
  401. $isTransfer = false;
  402. }
  403. $status = 6;
  404. $ret = $tianyan($card_no,$validate,$region_no,$status);
  405. if($ret)
  406. {
  407. if($succ) $pinfo_updator($card_no,$card_type,$org_type,$region_no,$isTransfer,$status);
  408. return [$validate,$card_type,$region_no,$isTransfer,$status,$black];
  409. }
  410. $ret = $ali($card_no,$validate,$region_no,$status);
  411. if($ret)
  412. {
  413. if($succ) $pinfo_updator($card_no,$card_type,$org_type,$region_no,$isTransfer,$status);
  414. return [$validate,$card_type,$region_no,$isTransfer,$status,$black];
  415. }
  416. return [true,$card_type,-1,false,$status,$black];
  417. }
  418. function is_validate($status)
  419. {
  420. if (in_array($status, [0,5])) {
  421. return false;
  422. } else {
  423. return true;
  424. }
  425. }