mtopcard.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. #天眼空号检测账号
  62. const TIAN_YAN_APP_Id = '2Xfa6IFIPv0sVUjy';
  63. const TIAN_YAN_APP_Security = '2Xfa6IFIPv0sVUjynOddsfh6KXfbyJ84';
  64. function month_stamp($time=null) : int {
  65. $date = getdate($time);
  66. $stamp = $date['year'] * 100 + $date['mon'];
  67. return $stamp;
  68. }
  69. function topcard_type($str_type)
  70. {
  71. if(empty($str_type)) {
  72. return UnknownCard;
  73. }
  74. $str_type = trim(strtolower($str_type));
  75. if($str_type == 'petrochina') { //中石油
  76. return PetroChinaCard;
  77. }
  78. elseif ($str_type == 'sinopec') { //中石化
  79. return SinopecCard;
  80. }
  81. elseif ($str_type == 'chinamobile') { //中石化
  82. return ChinaMobileCard;
  83. }
  84. elseif($str_type == 'chinaunicom') { //手机卡
  85. return ChinaUnicomCard;
  86. }
  87. elseif($str_type == 'chinatelecom') { //手机卡
  88. return ChinaTelecomCard;
  89. }
  90. elseif($str_type == 'phone') { //手机卡
  91. return PhoneCard;
  92. }
  93. elseif($str_type == 'third') {
  94. return ThirdRefillCard;
  95. }
  96. else {
  97. return UnknownCard;
  98. }
  99. }
  100. function scard_type($card_type)
  101. {
  102. if($card_type == PetroChinaCard) { //中石油
  103. return 'petrochina';
  104. }
  105. elseif ($card_type == SinopecCard) { //中石化
  106. return 'sinopec';
  107. }
  108. elseif ($card_type == ChinaMobileCard) { //中石化
  109. return 'chinamobile';
  110. }
  111. elseif($card_type == ChinaUnicomCard ) { //手机卡
  112. return 'chinaunicom';
  113. }
  114. elseif($card_type == ChinaTelecomCard) { //手机卡
  115. return 'chinatelecom';
  116. }
  117. elseif($card_type == PhoneCard) { //手机卡
  118. return 'phone';
  119. }
  120. elseif($card_type == ThirdRefillCard) {
  121. return 'third';
  122. }
  123. else {
  124. return 'unknown';
  125. }
  126. }
  127. require_once(BASE_HELPER_PATH . '/mtopcard/topcard.php');
  128. require_once(BASE_HELPER_PATH . '/mtopcard/user_topcards.php');
  129. require_once(BASE_HELPER_PATH . '/mtopcard/CardPaper.php');
  130. require_once(BASE_HELPER_PATH . '/mtopcard/cards_helper.php');
  131. function priority_cards($member_id, $page_type = '')
  132. {
  133. if($page_type == 'oil') {
  134. $types = [PetroChinaCard,SinopecCard];
  135. }
  136. elseif($page_type == 'phone') {
  137. $types = [PhoneCard];
  138. }
  139. else {
  140. $types = [PetroChinaCard,SinopecCard,PhoneCard];
  141. }
  142. $user_cards = new user_topcards($member_id);
  143. return $user_cards->priority_cards($types);
  144. }
  145. function topcard_format($card_list)
  146. {
  147. $ret = [];
  148. foreach ($card_list as $item) {
  149. $card = new topcard($item);
  150. $item = $card->format();
  151. $item['card_type'] = scard_type($item['card_type']);
  152. $ret[] = $item;
  153. }
  154. return $ret;
  155. }
  156. function simple_card_type($cardno)
  157. {
  158. if (preg_match('/^1[0-9]{18}$/', $cardno, $matches)) {
  159. return SinopecCard;
  160. } elseif (preg_match('/^9[0-9]{15}$/', $cardno, $matches)) {
  161. return PetroChinaCard;
  162. } 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)) {
  163. return ChinaMobileCard;
  164. } elseif (preg_match('/^(?:13[0-2]|145|15[56]|166|17[156]|18[56]|196)\d{8}$/', $cardno, $matches)) {
  165. return ChinaUnicomCard;
  166. } elseif (preg_match('/^(?:133|149|153|177|173|18[019]|19[0139])\d{8}$/', $cardno, $matches)) {
  167. return ChinaTelecomCard;
  168. } elseif (preg_match('/^1\d{10}$/', $cardno, $matches)) {
  169. return PhoneCard;
  170. } else {
  171. return UnknownCard;
  172. }
  173. }
  174. function card_type($cardno,&$regin_no,$fCoroutine = false)
  175. {
  176. $regin_no = -1;
  177. if(preg_match( '/^1[0-9]{18}$/',$cardno,$matches)) {
  178. return SinopecCard;
  179. }
  180. elseif(preg_match( '/^9[0-9]{15}$/',$cardno,$matches)) {
  181. return PetroChinaCard;
  182. }
  183. elseif(preg_match('/^1\d{10}$/',$cardno,$matches))
  184. {
  185. $checker = function ($phone,&$region)
  186. {
  187. if (empty($phone)) return false; //手机号不能为空
  188. $url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel={$phone}";
  189. $resp = http_request($url); //获取API返回 的数据
  190. if(empty($resp)) return false;
  191. $resp = mb_convert_encoding($resp, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5'); //解决中文乱码
  192. $datas = explode('=', $resp);
  193. if(count($datas) == 2)
  194. {
  195. $body = trim($datas[1]);
  196. if(preg_match_all("/(\w+):'([^']+)/", $body, $m)) {
  197. $res = array_combine($m[1], $m[2]);
  198. $province = formatProvince($res['province']);
  199. $region = array_search($province,ProvinceList);
  200. return $res;
  201. }
  202. }
  203. return false;
  204. };
  205. $i = 0;
  206. do {
  207. $ret = $checker($cardno, $region);
  208. $i++;
  209. } while (empty($ret) && $i < 3);
  210. if (empty($ret))
  211. {
  212. $regin_no = -1;
  213. Log::record("淘宝接口没法使用了,用传统办法识别卡类型",Log::ERR);
  214. 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)) {
  215. return ChinaMobileCard;
  216. } elseif (preg_match('/^(?:13[0-2]|145|15[56]|166|17[156]|18[56]|196)\d{8}$/', $cardno, $matches)) {
  217. return ChinaUnicomCard;
  218. } elseif (preg_match('/^(?:133|149|153|177|173|18[019]|19[0139])\d{8}$/', $cardno, $matches)) {
  219. return ChinaTelecomCard;
  220. } else {
  221. return UnknownCard;
  222. }
  223. }
  224. elseif($ret['catName'] == '中国联通') {
  225. $regin_no = $region;
  226. return ChinaUnicomCard;
  227. }
  228. elseif($ret['catName'] == '中国电信') {
  229. $regin_no = $region;
  230. return ChinaTelecomCard;
  231. }
  232. elseif($ret['catName'] == '中国移动') {
  233. $regin_no = $region;
  234. return ChinaMobileCard;
  235. }
  236. else {
  237. return UnknownCard;
  238. }
  239. }
  240. else {
  241. return UnknownCard;
  242. }
  243. }
  244. function oil_type($cardno)
  245. {
  246. if(preg_match( '/^1[0-9]{18}$/',$cardno,$matches)) {
  247. return SinopecCard;
  248. }
  249. elseif(preg_match( '/^9[0-9]{15}$/',$cardno,$matches)) {
  250. return PetroChinaCard;
  251. }
  252. else {
  253. return UnknownCard;
  254. }
  255. }
  256. function valid_phone($phone) : bool
  257. {
  258. $url = 'https://mobileempty.shumaidata.com/mobileempty';
  259. $params['mobile'] = $phone;
  260. $headers = ["Authorization:APPCODE " . '8f92d951293f4d2ea48ff86d5a70c537'];
  261. $resp = http_request($url, $params, 'GET', false, $headers);
  262. if ($resp == false) return true;
  263. $resp = json_decode($resp, true);
  264. if ($resp == false) return true;
  265. if ($resp['success'] == true && $resp['code'] == 200)
  266. {
  267. // "status": 1 //状态 0:空号;1:实号;2:停机;3:库无;4:沉默号;5:风险号
  268. $status = intval($resp['data']['status']);
  269. if (in_array($status, [0,5])) {
  270. Log::record("valid_phone phone:{$phone}, status : {$status}", Log::DEBUG);
  271. return false;
  272. } else {
  273. return true;
  274. }
  275. } else {
  276. Log::record("valid_phone phone:{$phone} return msg:{$resp['msg']}", Log::DEBUG);
  277. return true;
  278. }
  279. }
  280. function tian_yan_valid_phone($phone) : bool
  281. {
  282. $url = 'https://api.shumaidata.com/v4/mobile_empty/check';
  283. $appid = TIAN_YAN_APP_Id;
  284. $appSecurity = TIAN_YAN_APP_Security;
  285. $cur = microtime (true);
  286. $data['appid'] = $appid;
  287. $data['timestamp'] = intval($cur * 1000);
  288. $content = "{$appid}&{$data['timestamp']}&{$appSecurity}";
  289. $data['sign'] = md5($content);
  290. $data['mobile'] = $phone;
  291. $resp = http_request($url, $data, 'GET');
  292. if ($resp == false) return true;
  293. $resp = json_decode($resp, true);
  294. if ($resp == false) return true;
  295. if ($resp['success'] == true && $resp['code'] == 200)
  296. {
  297. // "status": 1 //状态 0:空号;1:实号;2:停机;3:库无;4:沉默号;5:风险号
  298. $status = intval($resp['data']['status']);
  299. if (in_array($status, [0,5])) {
  300. Log::record("tianyan_valid_phone phone:{$phone}, status : {$status}", Log::DEBUG);
  301. return false;
  302. } else {
  303. return true;
  304. }
  305. } else {
  306. Log::record("tianyan_valid_phone phone:{$phone} return msg:{$resp['msg']}", Log::DEBUG);
  307. return true;
  308. }
  309. }
  310. function formatProvince(string $province) : string
  311. {
  312. if(empty($province)) {
  313. return '';
  314. }
  315. $checkArr = ["省","市","自治区","特别行政区"];
  316. for($i = 0; $i < count($checkArr); $i++) {
  317. if(strpos($province, $checkArr[$i]) === false) {
  318. continue;
  319. } else {
  320. $province = mb_strcut($province, 0, strrpos($province, $checkArr[$i]));
  321. }
  322. }
  323. return $province;
  324. }