util.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. use function PHPUnit\Framework\returnArgument;
  12. class util
  13. {
  14. static function make_mobile()
  15. {
  16. static $prefix = ["139", "138", "137", "136", "135", "134", "159", "158", "157", "150", "151", "152",
  17. "188", "187", "182", "183", "184", "178", "130", "131", "132", "156", "155", "186", "185",
  18. "176", "133", "153", "189", "180", "181", "177"];
  19. $pos = mt_rand(0, count($prefix) - 1);
  20. $no = "{$prefix[$pos]}" . mt_rand(10000000, 99999999);
  21. return $no;
  22. }
  23. public static function can_refill($card_no, $card_type)
  24. {
  25. if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
  26. $result = rcache('card_expired', '', "{$card_no}");
  27. if (empty($result)) {
  28. wcache("card_expired", [$card_no => time()], '');
  29. return [true, 0];
  30. } else {
  31. $latest = current($result);
  32. $cur = time();
  33. $success = ($cur - $latest) > 120;
  34. if ($success) {
  35. wcache("card_expired", [$card_no => time()], '');
  36. }
  37. return [$success, $latest + 120 - $cur];
  38. }
  39. } else {
  40. return [true, 0];
  41. }
  42. }
  43. static function read_card($card_no, $card_type = 0)
  44. {
  45. if (empty($card_no)) return false;
  46. $data = rcache($card_no, 'cardrefill-');
  47. if (empty($data)) {
  48. $mod_topcard = Model('topcard');
  49. $ret = $mod_topcard->get_card($card_no);
  50. if (empty($ret)) {
  51. if ($card_type === 0) {
  52. $card_type = mtopcard\card_type($card_no);
  53. }
  54. $bind_phone = util::make_mobile();
  55. $mod_topcard->add($card_no, $card_type, time(), $bind_phone);
  56. $data['bind_phone'] = $bind_phone;
  57. $data['refill_time'] = time();
  58. $data['times'] = 0;
  59. $data['black_card'] = 0;
  60. wcache($card_no, $data, 'cardrefill-');
  61. } else {
  62. $val = $ret[0];
  63. $data['bind_phone'] = $val['bind_phone'];
  64. $data['black_card'] = $val['black_card'];
  65. $data['refill_time'] = time();
  66. $data['times'] = 0;
  67. }
  68. }
  69. //之前没加black_card处理,这个字段不存在.
  70. if (!array_key_exists('black_card', $data)) {
  71. $data['black_card'] = 0;
  72. }
  73. return $data;
  74. }
  75. static function inc_card($card_no, $card_info)
  76. {
  77. $card_info['times'] += 1;
  78. $card_info['refill_time'] = time();
  79. wcache($card_no, $card_info, 'cardrefill-');
  80. }
  81. public static function del_card($card_no)
  82. {
  83. dcache($card_no, 'cardrefill-');
  84. }
  85. public static function set_black($card_no)
  86. {
  87. if (empty($card_no)) return false;
  88. $card_info = util::read_card($card_no);
  89. if (!empty($card_info)) {
  90. $card_info['black_card'] = 1;
  91. $mod_topcard = Model('topcard');
  92. $mod_topcard->table('topcard')->where(['card_no' => $card_no])->update(['black_card' => 1]);
  93. wcache($card_no, $card_info, 'cardrefill-');
  94. return true;
  95. } else {
  96. return false;
  97. }
  98. }
  99. private static function black_order($order_sn, $msg)
  100. {
  101. static $errMsgs = ["只能给主卡且卡状态正常的加油卡充值", "加油卡卡号错误或不支持"];
  102. if (empty($msg)) return false;
  103. if (in_array($msg, $errMsgs)) {
  104. $refill = Model('refill_order');
  105. $order = $refill->getOrderInfo(['order_sn' => $order_sn]);
  106. if (empty($order)) return false;
  107. $card_no = $order['card_no'];
  108. return util::set_black($card_no);
  109. }
  110. }
  111. public static function black_from_log($file_name)
  112. {
  113. $fn = fopen($file_name, "r");
  114. if (empty($fn)) {
  115. Log::record("Open File {$file_name} error.", Log::ERR);
  116. return false;
  117. } else {
  118. Log::record("{$file_name} start woring", Log::DEBUG);
  119. }
  120. $errs = [];
  121. while (!feof($fn)) {
  122. $line = trim(fgets($fn));
  123. $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);
  124. if ($ret) {
  125. $order_sn = $matches['order_sn'];
  126. $message = $matches['message'];
  127. self::black_order($order_sn, $message);
  128. $errs[$message] = empty($errs[$message]) ? 1 : $errs[$message] + 1;
  129. }
  130. }
  131. foreach ($errs as $msg => $count) {
  132. Log::record("msg:{$msg} count:{$count}", Log::DEBUG);
  133. }
  134. fclose($fn);
  135. return true;
  136. }
  137. public static function async_add($method, $params)
  138. {
  139. try {
  140. QueueClient::async_push("AysncAddDispatcher", ['method' => $method, 'params' => $params], 10);
  141. return true;
  142. } catch (Exception $ex) {
  143. return false;
  144. }
  145. }
  146. public static function push_add($params)
  147. {
  148. try {
  149. queue\DispatcherClient::instance()->push('add', $params);
  150. return true;
  151. } catch (Exception $ex) {
  152. return false;
  153. }
  154. }
  155. public static function push_notify($chname, $params)
  156. {
  157. try {
  158. queue\DispatcherClient::instance()->push('notify', ['channel' => $chname, 'params' => $params]);
  159. return true;
  160. } catch (Exception $ex) {
  161. return false;
  162. }
  163. }
  164. public static function push_notify_merchant($order_id, $manual)
  165. {
  166. try {
  167. queue\DispatcherClient::instance()->push('notify_mechant', ['order_id' => $order_id, 'manual' => $manual]);
  168. return true;
  169. } catch (Exception $ex) {
  170. return false;
  171. }
  172. }
  173. public static function push_query($order_id)
  174. {
  175. try {
  176. queue\DispatcherClient::instance()->push('query', ['order_id' => $order_id]);
  177. return true;
  178. } catch (Exception $ex) {
  179. return false;
  180. }
  181. }
  182. public static function manual_success($order_id)
  183. {
  184. try {
  185. queue\DispatcherClient::instance()->push('manual_success', ['order_id' => $order_id]);
  186. return true;
  187. } catch (Exception $ex) {
  188. return false;
  189. }
  190. }
  191. public static function manual_cancel($order_id)
  192. {
  193. try {
  194. queue\DispatcherClient::instance()->push('manual_cancel', ['order_id' => $order_id]);
  195. return true;
  196. } catch (Exception $ex) {
  197. return false;
  198. }
  199. }
  200. //统计提交订单数据
  201. public static function incr_commit_pre($chname, $card_type, $spec, $quality)
  202. {
  203. $ins = Cache::getInstance('cacheredis');
  204. $name = 'channel_monitor_commit';
  205. $sec = time();
  206. $key_sec = "pre-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
  207. $ins->hIncrBy($name, $key_sec, 1);
  208. }
  209. public static function hget_commit_pre_sec($chname, $card_type, $spec, $quality, $time_stamp)
  210. {
  211. $ins = Cache::getInstance('cacheredis');
  212. $name = 'channel_monitor_commit';
  213. $key_sec = "pre-{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
  214. $value = $ins->hget($name, '', $key_sec);
  215. return intval($value);
  216. }
  217. public static function incr_commit($chname, $card_type, $spec, $quality, $fsuccess = true)
  218. {
  219. $ins = Cache::getInstance('cacheredis');
  220. $name = 'channel_monitor_commit';
  221. $sec = time();
  222. $min = $sec - $sec % 60;
  223. if ($fsuccess) {
  224. $key_sec = "succ-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
  225. $key_min = "succm-{$chname}-{$quality}-{$card_type}-{$spec}-{$min}";
  226. } else {
  227. $key_sec = "fail-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
  228. $key_min = "failm-{$chname}-{$quality}-{$card_type}-{$spec}-{$min}";
  229. }
  230. $ins->hIncrBy($name, $key_sec, 1);
  231. $ins->hIncrBy($name, $key_min, 1);
  232. }
  233. public static function hget_commit_sec($chname, $card_type, $spec, $quality, $time_stamp, $fsuccess = true)
  234. {
  235. $ins = Cache::getInstance('cacheredis');
  236. $name = 'channel_monitor_commit';
  237. if ($fsuccess) {
  238. $key_sec = "succ-{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
  239. } else {
  240. $key_sec = "fail-{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
  241. }
  242. $value = $ins->hget($name, '', $key_sec);
  243. return intval($value);
  244. }
  245. public static function hget_commit_min($chname, $card_type, $spec, $quality, $time_stamp, $fsuccess = true)
  246. {
  247. $ins = Cache::getInstance('cacheredis');
  248. $name = 'channel_monitor_commit';
  249. $min = $time_stamp - $time_stamp % 60;
  250. if ($fsuccess) {
  251. $key_sec = "succm-{$chname}-{$quality}-{$card_type}-{$spec}-{$min}";
  252. } else {
  253. $key_sec = "failm-{$chname}-{$quality}-{$card_type}-{$spec}-{$min}";
  254. }
  255. $value = $ins->hget($name, '', $key_sec);
  256. return intval($value);
  257. }
  258. //统计回调通知数据
  259. public static function incr_notify($chname, $card_type, $spec, $quality, $fsuccess = true)
  260. {
  261. $ins = Cache::getInstance('cacheredis');
  262. $name = 'channel_monitor_notify';
  263. $sec = time();
  264. $min = $sec - $sec % 60;
  265. if ($fsuccess) {
  266. $key_sec = "succ-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
  267. $key_min = "succm-{$chname}-{$quality}-{$card_type}-{$spec}-{$min}";
  268. } else {
  269. $key_sec = "fail-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
  270. $key_min = "failm-{$chname}-{$quality}-{$card_type}-{$spec}-{$min}";
  271. }
  272. $ins->hIncrBy($name, $key_sec, 1);
  273. $ins->hIncrBy($name, $key_min, 1);
  274. }
  275. public static function hget_notify_sec($chname, $card_type, $spec, $quality, $time_stamp, $fsuccess = true)
  276. {
  277. $ins = Cache::getInstance('cacheredis');
  278. $name = 'channel_monitor_notify';
  279. if ($fsuccess) {
  280. $key_sec = "succ-{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
  281. } else {
  282. $key_sec = "fail-{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
  283. }
  284. $value = $ins->hget($name, '', $key_sec);
  285. return intval($value);
  286. }
  287. public static function hget_notify_min($chname, $card_type, $spec, $quality, $time_stamp, $fsuccess = true)
  288. {
  289. $ins = Cache::getInstance('cacheredis');
  290. $name = 'channel_monitor_notify';
  291. $min = $time_stamp - $time_stamp % 60;
  292. if ($fsuccess) {
  293. $key_sec = "succm-{$chname}-{$quality}-{$card_type}-{$spec}-{$min}";
  294. } else {
  295. $key_sec = "failm-{$chname}-{$quality}-{$card_type}-{$spec}-{$min}";
  296. }
  297. $value = $ins->hget($name, '', $key_sec);
  298. return intval($value);
  299. }
  300. }