mtopcard.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. elseif($str_type == 'third') {
  91. return ThirdRefillCard;
  92. }
  93. else {
  94. return UnknownCard;
  95. }
  96. }
  97. function scard_type($card_type)
  98. {
  99. if($card_type == PetroChinaCard) { //中石油
  100. return 'petrochina';
  101. }
  102. elseif ($card_type == SinopecCard) { //中石化
  103. return 'sinopec';
  104. }
  105. elseif ($card_type == ChinaMobileCard) { //中石化
  106. return 'chinamobile';
  107. }
  108. elseif($card_type == ChinaUnicomCard ) { //手机卡
  109. return 'chinaunicom';
  110. }
  111. elseif($card_type == ChinaTelecomCard) { //手机卡
  112. return 'chinatelecom';
  113. }
  114. elseif($card_type == PhoneCard) { //手机卡
  115. return 'phone';
  116. }
  117. elseif($card_type == ThirdRefillCard) {
  118. return 'third';
  119. }
  120. else {
  121. return 'unknown';
  122. }
  123. }
  124. require_once(BASE_HELPER_PATH . '/mtopcard/topcard.php');
  125. require_once(BASE_HELPER_PATH . '/mtopcard/user_topcards.php');
  126. require_once(BASE_HELPER_PATH . '/mtopcard/CardPaper.php');
  127. require_once(BASE_HELPER_PATH . '/mtopcard/cards_helper.php');
  128. function priority_cards($member_id, $page_type = '')
  129. {
  130. if($page_type == 'oil') {
  131. $types = [PetroChinaCard,SinopecCard];
  132. }
  133. elseif($page_type == 'phone') {
  134. $types = [PhoneCard];
  135. }
  136. else {
  137. $types = [PetroChinaCard,SinopecCard,PhoneCard];
  138. }
  139. $user_cards = new user_topcards($member_id);
  140. return $user_cards->priority_cards($types);
  141. }
  142. function topcard_format($card_list)
  143. {
  144. $ret = [];
  145. foreach ($card_list as $item) {
  146. $card = new topcard($item);
  147. $item = $card->format();
  148. $item['card_type'] = scard_type($item['card_type']);
  149. $ret[] = $item;
  150. }
  151. return $ret;
  152. }
  153. function simple_card_type($cardno)
  154. {
  155. if (preg_match('/^1[0-9]{18}$/', $cardno, $matches)) {
  156. return SinopecCard;
  157. } elseif (preg_match('/^9[0-9]{15}$/', $cardno, $matches)) {
  158. return PetroChinaCard;
  159. } 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)) {
  160. return ChinaMobileCard;
  161. } elseif (preg_match('/^(?:13[0-2]|145|15[56]|166|17[156]|18[56]|196)\d{8}$/', $cardno, $matches)) {
  162. return ChinaUnicomCard;
  163. } elseif (preg_match('/^(?:133|149|153|177|173|18[019]|19[0139])\d{8}$/', $cardno, $matches)) {
  164. return ChinaTelecomCard;
  165. } elseif (preg_match('/^1\d{10}$/', $cardno, $matches)) {
  166. return PhoneCard;
  167. } else {
  168. return UnknownCard;
  169. }
  170. }
  171. function card_type($cardno,&$regin_no)
  172. {
  173. $regin_no = -1;
  174. if(preg_match( '/^1[0-9]{18}$/',$cardno,$matches)) {
  175. return SinopecCard;
  176. }
  177. elseif(preg_match( '/^9[0-9]{15}$/',$cardno,$matches)) {
  178. return PetroChinaCard;
  179. }
  180. elseif(preg_match('/^1\d{10}$/',$cardno,$matches))
  181. {
  182. $regin_no = -1;
  183. 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)) {
  184. return ChinaMobileCard;
  185. } elseif (preg_match('/^(?:13[0-2]|145|15[56]|166|17[156]|18[56]|196)\d{8}$/', $cardno, $matches)) {
  186. return ChinaUnicomCard;
  187. } elseif (preg_match('/^(?:133|149|153|177|173|18[019]|19[0139])\d{8}$/', $cardno, $matches)) {
  188. return ChinaTelecomCard;
  189. } else {
  190. return UnknownCard;
  191. }
  192. }
  193. else {
  194. return UnknownCard;
  195. }
  196. }
  197. function oil_type($cardno)
  198. {
  199. if(preg_match( '/^1[0-9]{18}$/',$cardno,$matches)) {
  200. return SinopecCard;
  201. }
  202. elseif(preg_match( '/^9[0-9]{15}$/',$cardno,$matches)) {
  203. return PetroChinaCard;
  204. }
  205. else {
  206. return UnknownCard;
  207. }
  208. }
  209. function valid_phone($card_no)
  210. {
  211. $type_checker = function ($channel)
  212. {
  213. if($channel == '中国联通') {
  214. return ChinaUnicomCard;
  215. }
  216. elseif($channel == '中国电信') {
  217. return ChinaTelecomCard;
  218. }
  219. elseif($channel == '中国移动') {
  220. return ChinaMobileCard;
  221. }
  222. else {
  223. return UnknownCard;
  224. }
  225. };
  226. $validate_checker = function ($status)
  227. {
  228. // "status": 1 //状态 0:空号;1:实号;2:停机;3:库无;4:沉默号;5:风险号
  229. if (in_array($status, [0,5])) {
  230. return false;
  231. } else {
  232. return true;
  233. }
  234. };
  235. $regin_checker = function ($area)
  236. {
  237. $areas = explode('-',$area);
  238. if(count($areas) > 0)
  239. {
  240. $province = $areas[0];
  241. $endtxts= ["省","市","自治区","特别行政区"];
  242. foreach ($endtxts as $endtxt)
  243. {
  244. if(strpos($province, $endtxt) === false) {
  245. continue;
  246. } else {
  247. $province = mb_strcut($province, 0, strrpos($province, $endtxt));
  248. }
  249. }
  250. $region = array_search($province, ProvinceList);
  251. return $region;
  252. }
  253. else {
  254. return -1;
  255. }
  256. };
  257. $ali = function ($card_no, &$validate, &$card_type, &$region_no) use ($validate_checker, $type_checker, $regin_checker): bool
  258. {
  259. $url = 'https://mobileempty.shumaidata.com/mobileempty';
  260. $params['mobile'] = $card_no;
  261. $headers = ["Authorization:APPCODE " . '8f92d951293f4d2ea48ff86d5a70c537'];
  262. $net_err = 0;
  263. $resp = http_request($url, $params, 'GET', false, $headers,$net_err);
  264. if ($resp == false) return false;
  265. $resp = json_decode($resp, true);
  266. if ($resp == false) return false;
  267. if ($resp['code'] == 200)
  268. {
  269. $data = $resp['data'];
  270. $validate = $validate_checker(intval($data['status']));
  271. $card_type = $type_checker($data['channel']);
  272. $region_no = $regin_checker($data['area']);
  273. return true;
  274. } else {
  275. Log::record("ali_valid phone:{$card_no} return msg:{$resp['msg']}", Log::DEBUG);
  276. return false;
  277. }
  278. };
  279. $tianyan = function ($card_no, &$validate, &$card_type, &$region_no) use ($validate_checker, $type_checker, $regin_checker): bool
  280. {
  281. $url = 'https://api.shumaidata.com/v4/mobile_empty/check';
  282. $appid = '2Xfa6IFIPv0sVUjy';
  283. $appSecurity = '2Xfa6IFIPv0sVUjynOddsfh6KXfbyJ84';
  284. $cur = microtime (true);
  285. $data['appid'] = $appid;
  286. $data['timestamp'] = intval($cur * 1000);
  287. $content = "{$appid}&{$data['timestamp']}&{$appSecurity}";
  288. $data['sign'] = md5($content);
  289. $data['mobile'] = $card_no;
  290. $net_err = 0;
  291. $resp = http_request($url, $data, 'GET',false, [],$net_err);
  292. if ($resp == false) return false;
  293. $resp = json_decode($resp, true);
  294. if ($resp == false) return false;
  295. if ($resp['code'] == 200)
  296. {
  297. $data = $resp['data'];
  298. $validate = $validate_checker(intval($data['status']));
  299. $card_type = $type_checker($data['channel']);
  300. $region_no = $regin_checker($data['area']);
  301. return true;
  302. } else {
  303. Log::record("tianyan_valid phone:{$card_no} return msg:{$resp['msg']}", Log::DEBUG);
  304. return false;
  305. }
  306. };
  307. $tianyan_transfer = function ($card_no)
  308. {
  309. $url = 'https://api.shumaidata.com/v4/mobile-transfer/query';
  310. $appid = '2Xfa6IFIPv0sVUjy';
  311. $appSecurity = '2Xfa6IFIPv0sVUjynOddsfh6KXfbyJ84';
  312. $cur = microtime (true);
  313. $data['appid'] = $appid;
  314. $data['timestamp'] = intval($cur * 1000);
  315. $content = "{$appid}&{$data['timestamp']}&{$appSecurity}";
  316. $data['sign'] = md5($content);
  317. $data['mobile'] = $card_no;
  318. $net_err = 0;
  319. $resp = http_request($url, $data, 'GET',false, [],$net_err);
  320. if ($resp == false) return false;
  321. $resp = json_decode($resp, true);
  322. if ($resp == false) return false;
  323. if ($resp['code'] == 200)
  324. {
  325. $data = $resp['data'];
  326. $ispType = $data['ispType'];
  327. $newIspType = $data['newIspType'];
  328. Log::record("tianyan_transfer phone:{$card_no} ispType:{$ispType} newIspType:{$newIspType}", Log::DEBUG);
  329. return $newIspType;
  330. } else {
  331. Log::record("tianyan_transfer phone:{$card_no} return msg:{$resp['msg']}", Log::DEBUG);
  332. return false;
  333. }
  334. };
  335. $validate = true;
  336. $card_type = card_type($card_no,$region_no);
  337. $region_no = -1;
  338. if($card_type == PetroChinaCard || $card_type == SinopecCard) {
  339. return [$validate,$card_type,$region_no];
  340. }
  341. $ret = $tianyan($card_no,$validate,$card_type,$region_no);
  342. if($ret) {
  343. $newIspType = $tianyan_transfer($card_no);
  344. if(!empty($newIspType)) {
  345. $card_type = $type_checker("中国{$newIspType}");
  346. }
  347. return [$validate,$card_type,$region_no];
  348. }
  349. $ret = $ali($card_no,$validate,$card_type,$region_no);
  350. if($ret) {
  351. return [$validate,$card_type,$region_no];
  352. }
  353. $card_type = card_type($card_no,$region_no);
  354. return [$validate,$card_type,$region_no];
  355. }