mtopcard.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. #卡的状态
  13. const CardNormal = 1;
  14. const CardDeled = 2;
  15. #限制参数
  16. const UserMaxCards = 10;
  17. const CardMaxUsers = 5;
  18. #card_key参数设置
  19. const UnusedCard = 0;
  20. const ReserveCard = 1;
  21. const AssignedCard = 2;
  22. const FreezedCard = 3;
  23. #充值卡类型
  24. const OilCardPaper = 1;
  25. const PhoneCardPaper = 2;
  26. #省份列表
  27. const ProvinceList = [
  28. 1 => '北京',
  29. 2 => '天津',
  30. 3 => '河北',
  31. 4 => '山西',
  32. 5 => '内蒙古',
  33. 6 => '辽宁',
  34. 7 => '吉林',
  35. 8 => '黑龙江',
  36. 9 => '上海',
  37. 10 => '江苏',
  38. 11 => '浙江',
  39. 12 => '安徽',
  40. 13 => '福建',
  41. 14 => '江西',
  42. 15 => '山东',
  43. 16 => '河南',
  44. 17 => '湖北',
  45. 18 => '湖南',
  46. 19 => '广东',
  47. 20 => '广西',
  48. 21 => '海南',
  49. 22 => '重庆',
  50. 23 => '四川',
  51. 24 => '贵州',
  52. 25 => '云南',
  53. 26 => '西藏',
  54. 27 => '陕西',
  55. 28 => '甘肃',
  56. 29 => '青海',
  57. 30 => '宁夏',
  58. 31 => '新疆'
  59. ];
  60. function month_stamp($time=null) : int {
  61. $date = getdate($time);
  62. $stamp = $date['year'] * 100 + $date['mon'];
  63. return $stamp;
  64. }
  65. function topcard_type($str_type)
  66. {
  67. if(empty($str_type)) {
  68. return UnknownCard;
  69. }
  70. $str_type = trim(strtolower($str_type));
  71. if($str_type == 'petrochina') { //中石油
  72. return PetroChinaCard;
  73. }
  74. elseif ($str_type == 'sinopec') { //中石化
  75. return SinopecCard;
  76. }
  77. elseif ($str_type == 'chinamobile') { //中石化
  78. return ChinaMobileCard;
  79. }
  80. elseif($str_type == 'chinaunicom') { //手机卡
  81. return ChinaUnicomCard;
  82. }
  83. elseif($str_type == 'chinatelecom') { //手机卡
  84. return ChinaTelecomCard;
  85. }
  86. elseif($str_type == 'phone') { //手机卡
  87. return PhoneCard;
  88. }
  89. else {
  90. return UnknownCard;
  91. }
  92. }
  93. function scard_type($card_type)
  94. {
  95. if($card_type == PetroChinaCard) { //中石油
  96. return 'petrochina';
  97. }
  98. elseif ($card_type == SinopecCard) { //中石化
  99. return 'sinopec';
  100. }
  101. elseif ($card_type == ChinaMobileCard) { //中石化
  102. return 'chinamobile';
  103. }
  104. elseif($card_type == ChinaUnicomCard ) { //手机卡
  105. return 'chinaunicom';
  106. }
  107. elseif($card_type == ChinaTelecomCard) { //手机卡
  108. return 'chinatelecom';
  109. }
  110. elseif($card_type == PhoneCard) { //手机卡
  111. return 'phone';
  112. }
  113. else {
  114. return 'unknown';
  115. }
  116. }
  117. require_once(BASE_HELPER_PATH . '/mtopcard/topcard.php');
  118. require_once(BASE_HELPER_PATH . '/mtopcard/user_topcards.php');
  119. require_once(BASE_HELPER_PATH . '/mtopcard/CardPaper.php');
  120. require_once(BASE_HELPER_PATH . '/mtopcard/cards_helper.php');
  121. function priority_cards($member_id, $page_type = '')
  122. {
  123. if($page_type == 'oil') {
  124. $types = [PetroChinaCard,SinopecCard];
  125. }
  126. elseif($page_type == 'phone') {
  127. $types = [PhoneCard];
  128. }
  129. else {
  130. $types = [PetroChinaCard,SinopecCard,PhoneCard];
  131. }
  132. $user_cards = new user_topcards($member_id);
  133. return $user_cards->priority_cards($types);
  134. }
  135. function topcard_format($card_list)
  136. {
  137. $ret = [];
  138. foreach ($card_list as $item) {
  139. $card = new topcard($item);
  140. $item = $card->format();
  141. $item['card_type'] = scard_type($item['card_type']);
  142. $ret[] = $item;
  143. }
  144. return $ret;
  145. }
  146. function simple_card_type($cardno)
  147. {
  148. if (preg_match('/^1[0-9]{18}$/', $cardno, $matches)) {
  149. return SinopecCard;
  150. } elseif (preg_match('/^9[0-9]{15}$/', $cardno, $matches)) {
  151. return PetroChinaCard;
  152. } 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)) {
  153. return ChinaMobileCard;
  154. } elseif (preg_match('/^(?:13[0-2]|145|15[56]|166|17[156]|18[56]|196)\d{8}$/', $cardno, $matches)) {
  155. return ChinaUnicomCard;
  156. } elseif (preg_match('/^(?:133|149|153|177|173|18[019]|19[0139])\d{8}$/', $cardno, $matches)) {
  157. return ChinaTelecomCard;
  158. } elseif (preg_match('/^1\d{10}$/', $cardno, $matches)) {
  159. return PhoneCard;
  160. } else {
  161. return UnknownCard;
  162. }
  163. }
  164. function card_type($cardno,&$regin_no)
  165. {
  166. $regin_no = -1;
  167. if(preg_match( '/^1[0-9]{18}$/',$cardno,$matches)) {
  168. return SinopecCard;
  169. }
  170. elseif(preg_match( '/^9[0-9]{15}$/',$cardno,$matches)) {
  171. return PetroChinaCard;
  172. }
  173. elseif(preg_match('/^1\d{10}$/',$cardno,$matches))
  174. {
  175. $checker = function ($phone,&$region)
  176. {
  177. if (empty($phone)) return false; //手机号不能为空
  178. $url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel={$phone}";
  179. $resp = http_request($url); //获取API返回 的数据
  180. if(empty($resp)) return false;
  181. $resp = mb_convert_encoding($resp, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5'); //解决中文乱码
  182. $datas = explode('=', $resp);
  183. if(count($datas) == 2)
  184. {
  185. $body = trim($datas[1]);
  186. if(preg_match_all("/(\w+):'([^']+)/", $body, $m)) {
  187. $res = array_combine($m[1], $m[2]);
  188. $province = formatProvince($res['province']);
  189. $region = array_search($province,ProvinceList);
  190. return $res;
  191. }
  192. }
  193. return false;
  194. };
  195. $ret = $checker($cardno,$region);
  196. if (empty($ret))
  197. {
  198. $regin_no = -1;
  199. Log::record("淘宝接口没法使用了,用传统办法识别卡类型",Log::DEBUG);
  200. 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)) {
  201. return ChinaMobileCard;
  202. } elseif (preg_match('/^(?:13[0-2]|145|15[56]|166|17[156]|18[56]|196)\d{8}$/', $cardno, $matches)) {
  203. return ChinaUnicomCard;
  204. } elseif (preg_match('/^(?:133|149|153|177|173|18[019]|19[0139])\d{8}$/', $cardno, $matches)) {
  205. return ChinaTelecomCard;
  206. } else {
  207. return UnknownCard;
  208. }
  209. }
  210. elseif($ret['catName'] == '中国联通') {
  211. $regin_no = $region;
  212. return ChinaUnicomCard;
  213. }
  214. elseif($ret['catName'] == '中国电信') {
  215. $regin_no = $region;
  216. return ChinaTelecomCard;
  217. }
  218. elseif($ret['catName'] == '中国移动') {
  219. $regin_no = $region;
  220. return ChinaMobileCard;
  221. }
  222. else {
  223. return UnknownCard;
  224. }
  225. }
  226. else {
  227. return UnknownCard;
  228. }
  229. }
  230. function oil_type($cardno)
  231. {
  232. if(preg_match( '/^1[0-9]{18}$/',$cardno,$matches)) {
  233. return SinopecCard;
  234. }
  235. elseif(preg_match( '/^9[0-9]{15}$/',$cardno,$matches)) {
  236. return PetroChinaCard;
  237. }
  238. else {
  239. return UnknownCard;
  240. }
  241. }
  242. function valid_phone($phone) : bool
  243. {
  244. $url = 'https://mobileempty.shumaidata.com/mobileempty';
  245. $params['mobile'] = $phone;
  246. $headers = ["Authorization:APPCODE " . '8f92d951293f4d2ea48ff86d5a70c537'];
  247. $resp = http_request($url, $params, 'GET', false, $headers);
  248. if ($resp == false) return true;
  249. $resp = json_decode($resp, true);
  250. if ($resp == false) return true;
  251. if ($resp['success'] == true && $resp['code'] == 200)
  252. {
  253. // "status": 1 //状态 0:空号;1:实号;2:停机;3:库无;4:沉默号;5:风险号
  254. $status = intval($resp['data']['status']);
  255. if (in_array($status, [0])) {
  256. Log::record("valid_phone phone:{$phone}, status : {$status}", Log::DEBUG);
  257. return false;
  258. } else {
  259. return true;
  260. }
  261. } else {
  262. Log::record("valid_phone phone:{$phone} return msg:{$resp['msg']}", Log::DEBUG);
  263. return true;
  264. }
  265. }
  266. function formatProvince(string $province) : string
  267. {
  268. if(empty($province)) {
  269. return '';
  270. }
  271. $checkArr = ["省","市","自治区","特别行政区"];
  272. for($i = 0; $i < count($checkArr); $i++) {
  273. if(strpos($province, $checkArr[$i]) === false) {
  274. continue;
  275. } else {
  276. $province = mb_strcut($province, 0, strrpos($province, $checkArr[$i]));
  277. }
  278. }
  279. return $province;
  280. }