util.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace refill;
  3. require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
  4. require_once(BASE_HELPER_PATH . '/queue/rdispatcher.php');
  5. use queue;
  6. use mtopcard;
  7. use Log;
  8. use Exception;
  9. use Cache;
  10. use QueueClient;
  11. class util
  12. {
  13. static function make_mobile()
  14. {
  15. static $prefix = ["139","138","137","136","135","134","159","158","157","150","151","152",
  16. "188","187","182","183","184","178","130","131","132","156","155","186","185",
  17. "176","133","153","189","180","181","177"];
  18. $pos = mt_rand(0,count($prefix) - 1);
  19. $no = "{$prefix[$pos]}" . mt_rand(10000000, 99999999);
  20. return $no;
  21. }
  22. static function read_card($card_no,$card_type = 0)
  23. {
  24. if(empty($card_no)) return false;
  25. $data = rcache($card_no, 'cardrefill-');
  26. if(empty($data))
  27. {
  28. $mod_topcard = Model('topcard');
  29. $ret = $mod_topcard->get_card($card_no);
  30. if(empty($ret))
  31. {
  32. if($card_type === 0) {
  33. $card_type = mtopcard\card_type($card_no);
  34. }
  35. $bind_phone = util::make_mobile();
  36. $mod_topcard->add($card_no,$card_type,time(),$bind_phone);
  37. $data['bind_phone'] = $bind_phone;
  38. $data['refill_time'] = time();
  39. $data['times'] = 0;
  40. $data['black_card'] = 0;
  41. wcache($card_no,$data,'cardrefill-');
  42. }
  43. else {
  44. $val = $ret[0];
  45. $data['bind_phone'] = $val['bind_phone'];
  46. $data['black_card'] = $val['black_card'];
  47. $data['refill_time'] = time();
  48. $data['times'] = 0;
  49. }
  50. }
  51. //之前没加black_card处理,这个字段不存在.
  52. if(!array_key_exists('black_card',$data)) {
  53. $data['black_card'] = 0;
  54. }
  55. return $data;
  56. }
  57. static function inc_card($card_no, $card_info)
  58. {
  59. $card_info['times'] += 1;
  60. $card_info['refill_time'] = time();
  61. wcache($card_no,$card_info,'cardrefill-');
  62. }
  63. public static function del_card($card_no)
  64. {
  65. dcache($card_no,'cardrefill-');
  66. }
  67. public static function set_black($card_no)
  68. {
  69. if(empty($card_no)) return false;
  70. $card_info = util::read_card($card_no);
  71. if(!empty($card_info)) {
  72. $card_info['black_card'] = 1;
  73. $mod_topcard = Model('topcard');
  74. $mod_topcard->table('topcard')->where(['card_no' => $card_no])->update(['black_card' => 1]);
  75. wcache($card_no,$card_info,'cardrefill-');
  76. return true;
  77. }
  78. else {
  79. return false;
  80. }
  81. }
  82. private static function black_order($order_sn,$msg)
  83. {
  84. static $errMsgs = ["只能给绑定正确手机号的油卡充值或只能给主卡充值","只能给主卡且卡状态正常的加油卡充值","加油卡卡号错误或不支持"];
  85. if(empty($msg)) return false;
  86. if(in_array($msg,$errMsgs))
  87. {
  88. $refill = Model('refill_order');
  89. $order = $refill->getOrderInfo(['order_sn' => $order_sn]);
  90. if(empty($order)) return false;
  91. $card_no = $order['card_no'];
  92. return util::set_black($card_no);
  93. }
  94. }
  95. public static function black_from_log($file_name)
  96. {
  97. $fn = fopen($file_name,"r");
  98. if(empty($fn)) {
  99. Log::record("Open File {$file_name} error.",Log::ERR);
  100. return false;
  101. }
  102. else {
  103. Log::record("{$file_name} start woring",Log::DEBUG);
  104. }
  105. $errs = [];
  106. while(! feof($fn)) {
  107. $line = trim(fgets($fn));
  108. $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);
  109. if($ret) {
  110. $order_sn = $matches['order_sn'];
  111. $message = $matches['message'];
  112. self::black_order($order_sn,$message);
  113. $errs[$message] = empty($errs[$message]) ? 1 : $errs[$message] + 1;
  114. }
  115. }
  116. foreach ($errs as $msg => $count) {
  117. Log::record("msg:{$msg} count:{$count}",Log::DEBUG);
  118. }
  119. fclose($fn);
  120. return true;
  121. }
  122. public static function async_add($method,$params)
  123. {
  124. try {
  125. QueueClient::async_push("AysncAddDispatcher",['method' => $method,'params'=> $params],10);
  126. return true;
  127. }
  128. catch (Exception $ex) {
  129. return false;
  130. }
  131. }
  132. public static function push_add($params)
  133. {
  134. try {
  135. queue\DispatcherClient::instance()->push('add',$params);
  136. return true;
  137. }
  138. catch (Exception $ex) {
  139. return false;
  140. }
  141. }
  142. public static function push_notify($chname,$params)
  143. {
  144. try {
  145. queue\DispatcherClient::instance()->push('notify',['channel' => $chname,'params' => $params]);
  146. return true;
  147. }
  148. catch (Exception $ex) {
  149. return false;
  150. }
  151. }
  152. public static function push_notify_merchant($order_id,$manual)
  153. {
  154. try {
  155. queue\DispatcherClient::instance()->push('notify_mechant',['order_id' => $order_id,'manual' => $manual]);
  156. return true;
  157. }
  158. catch (Exception $ex) {
  159. return false;
  160. }
  161. }
  162. public static function push_query($order_id)
  163. {
  164. try {
  165. queue\DispatcherClient::instance()->push('query',['order_id' => $order_id]);
  166. return true;
  167. }
  168. catch (Exception $ex) {
  169. return false;
  170. }
  171. }
  172. public static function incr_order($chname,$card_type,$amout,$quality)
  173. {
  174. $ins = Cache::getInstance('cacheredis');
  175. $sec = time();
  176. $min = $sec - $sec % 60;
  177. $key = "storge-push-{$chname}-{$quality}-{$card_type}-{$amout}-{$min}";
  178. $ins->incr($key);
  179. $key = "speed-{$chname}-{$quality}-{$card_type}-{$amout}-{$sec}";
  180. $ins->incr($key);
  181. }
  182. public static function decr_order($chname,$card_type,$amout,$quality)
  183. {
  184. $ins = Cache::getInstance('cacheredis');
  185. $sec = time();
  186. $min = $sec - $sec % 60;
  187. $key = "storge-pop-{$chname}-{$quality}-{$card_type}-{$amout}-{$min}";
  188. $ins->decr($key);
  189. }
  190. }