util.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace refill;
  3. require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
  4. use mtopcard;
  5. use Log;
  6. class util
  7. {
  8. static function make_mobile()
  9. {
  10. static $prefix = ["139","138","137","136","135","134","159","158","157","150","151","152",
  11. "188","187","182","183","184","178","130","131","132","156","155","186","185",
  12. "176","133","153","189","180","181","177"];
  13. $pos = mt_rand(0,count($prefix) - 1);
  14. $no = "{$prefix[$pos]}" . mt_rand(10000000, 99999999);
  15. return $no;
  16. }
  17. static function read_card($card_no,$card_type = 0)
  18. {
  19. if(empty($card_no)) return false;
  20. $data = rcache($card_no, 'cardrefill-');
  21. if(empty($data))
  22. {
  23. $mod_topcard = Model('topcard');
  24. $ret = $mod_topcard->get_card($card_no);
  25. if(empty($ret))
  26. {
  27. if($card_type === 0) {
  28. $card_type = mtopcard\card_type($card_no);
  29. }
  30. $bind_phone = util::make_mobile();
  31. $mod_topcard->add($card_no,$card_type,time(),$bind_phone);
  32. $data['bind_phone'] = $bind_phone;
  33. $data['refill_time'] = time();
  34. $data['times'] = 0;
  35. $data['black_card'] = 0;
  36. wcache($card_no,$data,'cardrefill-');
  37. }
  38. else {
  39. $val = $ret[0];
  40. $data['bind_phone'] = $val['bind_phone'];
  41. $data['black_card'] = $val['black_card'];
  42. $data['refill_time'] = time();
  43. $data['times'] = 0;
  44. }
  45. }
  46. //之前没加black_card处理,这个字段不存在.
  47. if(!array_key_exists('black_card',$data)) {
  48. $data['black_card'] = 0;
  49. }
  50. return $data;
  51. }
  52. static function inc_card($card_no, $card_info)
  53. {
  54. $card_info['times'] += 1;
  55. $card_info['refill_time'] = time();
  56. wcache($card_no,$card_info,'cardrefill-');
  57. }
  58. static function del_card($card_no)
  59. {
  60. dcache($card_no,'cardrefill-');
  61. }
  62. static function set_black($card_no)
  63. {
  64. if(empty($card_no)) return false;
  65. $card_info = util::read_card($card_no);
  66. if(!empty($card_info)) {
  67. $card_info['black_card'] = 1;
  68. $mod_topcard = Model('topcard');
  69. $mod_topcard->where(['card_no' => $card_no])->update(['black_card' => 1]);
  70. wcache($card_no,$card_info,'cardrefill-');
  71. return true;
  72. }
  73. else {
  74. return false;
  75. }
  76. }
  77. private static function black_order($order_sn,$msg)
  78. {
  79. static $errMsgs = ["只能给绑定正确手机号的油卡充值或只能给主卡充值","只能给主卡且卡状态正常的加油卡充值","加油卡卡号错误或不支持"];
  80. if(empty($msg)) return false;
  81. if(in_array($msg,$errMsgs))
  82. {
  83. $refill = Model('refill_order');
  84. $order = $refill->getOrderInfo(['order_sn' => $order_sn]);
  85. if(empty($order)) return false;
  86. $card_no = $order['card_no'];
  87. return util::set_black($card_no);
  88. }
  89. }
  90. static function black_from_log($file_name)
  91. {
  92. $fn = fopen($file_name,"r");
  93. if(empty($fn)) {
  94. Log::record("Open File {$file_name} error.",Log::ERR);
  95. return false;
  96. }
  97. else {
  98. Log::record("{$file_name} start woring",Log::DEBUG);
  99. }
  100. $errs = [];
  101. while(! feof($fn)) {
  102. $line = trim(fgets($fn));
  103. $ret = preg_match('/[\w\W]+"channelOrderNumber":"(?P<order_sn>[^"]+)"[\w\W]+"message":"(?P<message>[\x{4e00}-\x{9fa5}]+)"[\w\W]+"status":109/u',$line,$matches);
  104. if($ret) {
  105. $order_sn = $matches['order_sn'];
  106. $message = $matches['message'];
  107. self::black_order($order_sn,$message);
  108. $errs[$message] = empty($errs[$message]) ? 1 : $errs[$message] + 1;
  109. }
  110. }
  111. foreach ($errs as $msg => $count) {
  112. Log::record("msg:{$msg} count:{$count}",Log::DEBUG);
  113. }
  114. fclose($fn);
  115. return true;
  116. }
  117. }