util.php 13 KB

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