card_query.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. namespace mtopcard;
  3. use Log;
  4. class card_query
  5. {
  6. private function validate_checker($status)
  7. {
  8. // "status": 1 //状态 0:空号;1:实号;2:停机;3:库无;4:沉默号;5:风险号 6: 未知 7: 流量卡
  9. Log::record("status={$status}",Log::DEBUG);
  10. if (in_array($status, [0, 5, 7])) {
  11. return false;
  12. } else {
  13. return true;
  14. }
  15. }
  16. //[success,$card_type,$region_no,$isTransfer,$status]
  17. private function tencent_query($card_no)
  18. {
  19. $type_checker = function ($channel)
  20. {
  21. if($channel == 'CMCC') {
  22. return ChinaMobileCard;
  23. }
  24. elseif($channel == 'CTCC') {
  25. return ChinaTelecomCard;
  26. }
  27. elseif($channel == 'CUCC') {
  28. return ChinaUnicomCard;
  29. }
  30. else {
  31. return UnknownCard;
  32. }
  33. };
  34. $status_converter = function ($code)
  35. {
  36. // "status": 1 //状态 0:空号;1:实号;2:停机;3:库无;4:沉默号;5:风险号 6: 未知 7: 流量卡
  37. if($code === 1) {
  38. return 1;
  39. } elseif($code === 2) {
  40. return 0;
  41. } elseif ($code === 3) {
  42. return 2;
  43. } elseif ($code === 4) {
  44. return 7;
  45. } elseif($code === 5) {
  46. return 4;
  47. } else {
  48. return 6;
  49. }
  50. };
  51. $regin_checker = function ($province)
  52. {
  53. if(empty($province)) return -1;
  54. $endtxts= ["省","市","自治区","特别行政区"];
  55. foreach ($endtxts as $endtxt)
  56. {
  57. if(strpos($province, $endtxt) === false) {
  58. continue;
  59. } else {
  60. $province = mb_strcut($province, 0, strrpos($province, $endtxt));
  61. }
  62. }
  63. if(array_key_exists($province,Prov2Noes)) {
  64. return Prov2Noes[$province];
  65. } else {
  66. return -1;
  67. }
  68. };
  69. $query_fun = function ($card_no, &$card_type, &$region_no, &$status) use ($type_checker, $regin_checker, $status_converter)
  70. {
  71. $secretId = 'AKIDkd6sT6C4765LtYN1W2fN2WC2Ynr42acemjQW';
  72. $secretKey = 'dupQeU838dhuQ8uDP2bi81nQNb5wt9KW1qLP7eWu';
  73. $source = 'market';
  74. // 签名
  75. $datetime = gmdate('D, d M Y H:i:s T');
  76. $signStr = sprintf("x-date: %s\nx-source: %s", $datetime, $source);
  77. $sign = base64_encode(hash_hmac('sha1', $signStr, $secretKey, true));
  78. $auth = sprintf('hmac id="%s", algorithm="hmac-sha1", headers="x-date x-source", signature="%s"', $secretId, $sign);
  79. $header = [
  80. "X-Source: {$source}",
  81. "X-Date: {$datetime}",
  82. "Authorization: {$auth}",
  83. ];
  84. $data['number'] = $card_no;
  85. $url = 'https://service-empgc6lp-1256724964.sh.apigw.tencentcs.com/release/query';
  86. $net_err = 0;
  87. $resp = http_request($url, $data, 'GET',false, $header,$net_err);
  88. if ($resp == false) return false;
  89. $resp = json_decode($resp, true);
  90. if ($resp == false) return false;
  91. if ($resp['ret'] == 200)
  92. {
  93. $data = $resp['data'];
  94. $code = intval($data['code']);
  95. $extend = $data['extend'];
  96. $status = $status_converter($code);
  97. $card_type = $type_checker($extend['isp']);
  98. $region_no = $regin_checker($extend['prov']);
  99. Log::record("tencent_query: card_no={$card_no} card_type={$card_type} region_no={$region_no}",Log::DEBUG);
  100. return true;
  101. } else {
  102. Log::record("tencent_valid phone:{$card_no}", Log::DEBUG);
  103. return false;
  104. }
  105. };
  106. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  107. $org_type = card_type($card_no,$region_no);
  108. $region_no = -1;
  109. $status = 6;
  110. //[succ,$card_type,$region_no,$isTransfer,$status]
  111. $ret = $query_fun($card_no, $card_type, $region_no, $status);
  112. if ($ret) {
  113. $isTransfer = $org_type != $card_type;
  114. return [true, $card_type, $region_no, $isTransfer, $status];
  115. }
  116. else {
  117. return [false, $org_type, -1, false, $status];
  118. }
  119. }
  120. //[succ,$card_type,$region_no,$isTransfer,$status]
  121. public function tianyan_query($card_no)
  122. {
  123. $type_checker = function ($channel)
  124. {
  125. if($channel == '联通') {
  126. return ChinaUnicomCard;
  127. }
  128. elseif($channel == '电信') {
  129. return ChinaTelecomCard;
  130. }
  131. elseif($channel == '移动') {
  132. return ChinaMobileCard;
  133. }
  134. else {
  135. return UnknownCard;
  136. }
  137. };
  138. $regin_checker = function ($area)
  139. {
  140. if(empty($area)) return -1;
  141. $areas = explode('-',$area);
  142. if(count($areas) > 0)
  143. {
  144. $province = $areas[0];
  145. $endtxts= ["省","市","自治区","特别行政区"];
  146. foreach ($endtxts as $endtxt)
  147. {
  148. if(strpos($province, $endtxt) === false) {
  149. continue;
  150. } else {
  151. $province = mb_strcut($province, 0, strrpos($province, $endtxt));
  152. }
  153. }
  154. if(array_key_exists($province,Prov2Noes)) {
  155. return Prov2Noes[$province];
  156. }
  157. }
  158. return -1;
  159. };
  160. $ali = function ($card_no, &$region_no, &$status) use ($regin_checker): bool
  161. {
  162. $url = 'https://mobileempty.shumaidata.com/mobileempty';
  163. $params['mobile'] = $card_no;
  164. $headers = ["Authorization:APPCODE " . '8f92d951293f4d2ea48ff86d5a70c537'];
  165. $net_err = 0;
  166. $resp = http_request($url, $params, 'GET', false, $headers,$net_err);
  167. if ($resp == false) return false;
  168. $resp = json_decode($resp, true);
  169. if ($resp == false) return false;
  170. if ($resp['code'] == 200)
  171. {
  172. $data = $resp['data'];
  173. $status = intval($data['status']);
  174. $region_no = $regin_checker($data['area']);
  175. return true;
  176. } else {
  177. Log::record("ali_valid phone:{$card_no} return msg:{$resp['msg']}", Log::DEBUG);
  178. return false;
  179. }
  180. };
  181. $tianyan = function ($card_no, &$region_no, &$status) use ($regin_checker): bool
  182. {
  183. $url = 'https://api.shumaidata.com/v4/mobile_empty/check';
  184. $appid = '2Xfa6IFIPv0sVUjy';
  185. $appSecurity = '2Xfa6IFIPv0sVUjynOddsfh6KXfbyJ84';
  186. $cur = microtime (true);
  187. $data['appid'] = $appid;
  188. $data['timestamp'] = intval($cur * 1000);
  189. $content = "{$appid}&{$data['timestamp']}&{$appSecurity}";
  190. $data['sign'] = md5($content);
  191. $data['mobile'] = $card_no;
  192. $net_err = 0;
  193. $resp = http_request($url, $data, 'GET',false, [],$net_err);
  194. if ($resp == false) return false;
  195. $resp = json_decode($resp, true);
  196. if ($resp == false) return false;
  197. if ($resp['code'] == 200)
  198. {
  199. $data = $resp['data'];
  200. $status = intval($data['status']);
  201. $region_no = $regin_checker($data['area']);
  202. return true;
  203. } else {
  204. Log::record("tianyan_valid phone:{$card_no} return msg:{$resp['msg']}", Log::DEBUG);
  205. return false;
  206. }
  207. };
  208. $tianyan_cardtyper = function ($card_no) use ($type_checker)
  209. {
  210. $url = 'https://api.shumaidata.com/v4/mobile-transfer/query';
  211. $appid = '2Xfa6IFIPv0sVUjy';
  212. $appSecurity = '2Xfa6IFIPv0sVUjynOddsfh6KXfbyJ84';
  213. $cur = microtime (true);
  214. $data['appid'] = $appid;
  215. $data['timestamp'] = intval($cur * 1000);
  216. $content = "{$appid}&{$data['timestamp']}&{$appSecurity}";
  217. $data['sign'] = md5($content);
  218. $data['mobile'] = $card_no;
  219. $net_err = 0;
  220. $resp = http_request($url, $data, 'GET',false, [],$net_err);
  221. if ($resp == false) return [false,UnknownCard,UnknownCard,false];
  222. $resp = json_decode($resp, true);
  223. if ($resp == false) return [false,UnknownCard,UnknownCard,false];
  224. if ($resp['code'] == 200)
  225. {
  226. $data = $resp['data'];
  227. $ispType = $data['ispType'];
  228. $newIspType = $data['newIspType'];
  229. $card_type = $type_checker($newIspType);
  230. $org_type = $type_checker($ispType);
  231. $isTransfer = $org_type != $card_type;
  232. Log::record("tianyan_transfer phone:{$card_no} card_type={$card_type} ispType:{$ispType} newIspType:{$newIspType}", Log::DEBUG);
  233. return [true,$card_type,$org_type,$isTransfer];
  234. } else {
  235. Log::record("tianyan_transfer phone:{$card_no} return msg:{$resp['msg']}", Log::DEBUG);
  236. return [false,UnknownCard,UnknownCard,false];
  237. }
  238. };
  239. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  240. [$succ,$_card_type,$_org_type,$_isTransfer] = $tianyan_cardtyper($card_no);
  241. if($succ) {
  242. $card_type = $_card_type;
  243. $org_type = $_org_type;
  244. $isTransfer = $_isTransfer;
  245. }
  246. else {
  247. $isTransfer = false;
  248. $card_type = card_type($card_no,$region_no);
  249. }
  250. $status = 6;
  251. $ret = $tianyan($card_no, $region_no, $status);
  252. if ($ret) {
  253. return [true, $card_type, $region_no, $isTransfer, $status];
  254. }
  255. $ret = $ali($card_no, $region_no, $status);
  256. if ($ret) {
  257. return [true, $card_type, $region_no, $isTransfer, $status];
  258. }
  259. return [false, $card_type, -1, false, $status];
  260. }
  261. //[$validate,$card_type,$region_no,$isTransfer,$status,$black]
  262. public function validate($card_no)
  263. {
  264. $time_checker = function ($info)
  265. {
  266. $update_time = $info['update_time'];
  267. $delta = time() - intval($update_time);
  268. $status = intval($info['card_state']);
  269. if(in_array($status,[0,5]) && $delta > 86400 * 15) {
  270. return false;
  271. }
  272. else {
  273. return $delta < 360 * 86400 && $delta >= 0;
  274. }
  275. };
  276. $pinfo_getter = function ($card_no) use ($time_checker)
  277. {
  278. $mod_card = Model('card_info');
  279. $info = $mod_card->getCardInfo($card_no);
  280. if(empty($info)) {
  281. return [false,[]];
  282. }
  283. elseif($time_checker($info)) {
  284. $mod_card->where(['card_no' => $card_no])->update(['using_times' => $info['using_times'] + 1]);
  285. $card_type = intval($info['card_type']);
  286. $region_no = intval($info['regin']);
  287. $isTransfer = !(intval($info['transfer']) == 0);
  288. $status = intval($info['card_state']);
  289. $black = intval($info['black']);
  290. $validate = $this->validate_checker($status);
  291. $result = [$validate,$card_type,$region_no,$isTransfer,$status,$black];
  292. return [true,$result];
  293. }
  294. else {
  295. return [false,[]];
  296. }
  297. };
  298. $pinfo_updator = function ($card_no,$card_type,$org_type,$region_no,$isTransfer,$status)
  299. {
  300. $mod_card = Model('card_info');
  301. $transfer = $isTransfer == true ? 1 : 0;
  302. $mod_card->replace_card($card_no,$card_type,$org_type,$region_no,$transfer,$status);
  303. };
  304. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  305. $validate = true;
  306. $black = 0;
  307. $org_type = card_type($card_no,$region_no);
  308. $region_no = -1;
  309. if($org_type === UnknownCard) {
  310. return [false, $org_type, $region_no, false, 1, $black];
  311. }
  312. if($org_type == PetroChinaCard || $org_type == SinopecCard) {
  313. return [$validate, $org_type, $region_no, false, 1, $black];
  314. }
  315. [$succ,$result] = $pinfo_getter($card_no);
  316. if($succ) return $result;
  317. [$succ, $card_type, $region_no, $isTransfer, $status] = $this->tencent_query($card_no);
  318. if ($succ === false)
  319. {
  320. [$succ, $card_type, $region_no, $isTransfer, $status] = $this->tianyan_query($card_no);
  321. if ($succ === false) {
  322. return [true, $org_type, -1, false, 1, $black];
  323. }
  324. }
  325. $validate = $this->validate_checker($status);
  326. $pinfo_updator($card_no,$card_type,$org_type,$region_no,$isTransfer,$status);
  327. return [$validate,$card_type,$region_no,$isTransfer,$status,$black];
  328. }
  329. }