util.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <?php
  2. namespace refill;
  3. require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
  4. require_once(BASE_HELPER_PATH . '/queue/rdispatcher.php');
  5. require_once(BASE_HELPER_PATH . '/refill/policy/rlock.php');
  6. use queue;
  7. use mtopcard;
  8. use Log;
  9. use Exception;
  10. use Cache;
  11. use QueueClient;
  12. use refill;
  13. class util
  14. {
  15. const ThirdRefillAmount = 100;
  16. static function make_mobile()
  17. {
  18. static $prefix = ["139", "138", "137", "136", "135", "134", "159", "158", "157", "150", "151", "152",
  19. "188", "187", "182", "183", "184", "178", "130", "131", "132", "156", "155", "186", "185",
  20. "176", "133", "153", "189", "180", "181", "177"];
  21. $pos = mt_rand(0, count($prefix) - 1);
  22. $no = "{$prefix[$pos]}" . mt_rand(10000000, 99999999);
  23. return $no;
  24. }
  25. public static function can_refill($card_no, $card_type)
  26. {
  27. if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
  28. $result = rcache('card_expired', '', "{$card_no}");
  29. if (empty($result)) {
  30. wcache("card_expired", [$card_no => time()], '');
  31. return [true, 0];
  32. } else {
  33. $latest = current($result);
  34. $cur = time();
  35. $success = ($cur - $latest) > 2;
  36. if ($success) {
  37. wcache("card_expired", [$card_no => time()], '');
  38. }
  39. return [$success, $latest + 2 - $cur];
  40. }
  41. } else {
  42. return [true, 0];
  43. }
  44. }
  45. public static function can_commit($card_no, $card_type)
  46. {
  47. if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard)
  48. {
  49. $result = rcache('card_expired', '', "{$card_no}");
  50. if (empty($result)) {
  51. wcache("card_expired", [$card_no => time()], '');
  52. return [true, 0];
  53. }
  54. else
  55. {
  56. $latest = current($result);
  57. $cur = time();
  58. $lowest = 300;
  59. if ($cur > $latest && ($cur - $latest) >= $lowest) {
  60. wcache("card_expired", [$card_no => time()], '');
  61. return [true, 0];
  62. } else {
  63. wcache("card_expired", [$card_no => $latest + $lowest], '');
  64. return [false, $latest + $lowest - $cur];
  65. }
  66. }
  67. }
  68. else {
  69. return [true, 0];
  70. }
  71. }
  72. static function read_card($card_no, $card_type = 0)
  73. {
  74. if (empty($card_no)) return false;
  75. $data = rcache($card_no, 'cardrefill-');
  76. if (empty($data)) {
  77. $mod_topcard = Model('topcard');
  78. $ret = $mod_topcard->get_card($card_no);
  79. if (empty($ret)) {
  80. if ($card_type === 0) {
  81. $card_type = mtopcard\card_type($card_no,$regin_no);
  82. }
  83. $bind_phone = util::make_mobile();
  84. $mod_topcard->add($card_no, $card_type, time(), $bind_phone);
  85. $data['bind_phone'] = $bind_phone;
  86. $data['refill_time'] = time();
  87. $data['times'] = 0;
  88. $data['black_card'] = 0;
  89. wcache($card_no, $data, 'cardrefill-');
  90. } else {
  91. $val = $ret[0];
  92. $data['bind_phone'] = $val['bind_phone'];
  93. $data['black_card'] = $val['black_card'];
  94. $data['refill_time'] = time();
  95. $data['times'] = 0;
  96. }
  97. }
  98. //之前没加black_card处理,这个字段不存在.
  99. if (!array_key_exists('black_card', $data)) {
  100. $data['black_card'] = 0;
  101. }
  102. return $data;
  103. }
  104. static function inc_card($card_no, $card_info)
  105. {
  106. $card_info['times'] += 1;
  107. $card_info['refill_time'] = time();
  108. wcache($card_no, $card_info, 'cardrefill-');
  109. }
  110. public static function del_card($card_no)
  111. {
  112. dcache($card_no, 'cardrefill-');
  113. }
  114. public static function set_black($card_no)
  115. {
  116. if (empty($card_no)) return false;
  117. $card_info = util::read_card($card_no);
  118. if (!empty($card_info)) {
  119. $card_info['black_card'] = 1;
  120. $mod_topcard = Model('topcard');
  121. $mod_topcard->table('topcard')->where(['card_no' => $card_no])->update(['black_card' => 1]);
  122. wcache($card_no, $card_info, 'cardrefill-');
  123. return true;
  124. } else {
  125. return false;
  126. }
  127. }
  128. private static function black_order($order_sn, $msg)
  129. {
  130. static $errMsgs = ["只能给主卡且卡状态正常的加油卡充值", "加油卡卡号错误或不支持"];
  131. if (empty($msg)) return false;
  132. if (in_array($msg, $errMsgs)) {
  133. $refill = Model('refill_order');
  134. $order = $refill->getOrderInfo(['order_sn' => $order_sn]);
  135. if (empty($order)) return false;
  136. $card_no = $order['card_no'];
  137. return util::set_black($card_no);
  138. }
  139. }
  140. public static function black_from_log($file_name)
  141. {
  142. $fn = fopen($file_name, "r");
  143. if (empty($fn)) {
  144. Log::record("Open File {$file_name} error.", Log::ERR);
  145. return false;
  146. } else {
  147. Log::record("{$file_name} start woring", Log::DEBUG);
  148. }
  149. $errs = [];
  150. while (!feof($fn)) {
  151. $line = trim(fgets($fn));
  152. $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);
  153. if ($ret) {
  154. $order_sn = $matches['order_sn'];
  155. $message = $matches['message'];
  156. self::black_order($order_sn, $message);
  157. $errs[$message] = empty($errs[$message]) ? 1 : $errs[$message] + 1;
  158. }
  159. }
  160. foreach ($errs as $msg => $count) {
  161. Log::record("msg:{$msg} count:{$count}", Log::DEBUG);
  162. }
  163. fclose($fn);
  164. return true;
  165. }
  166. public static function async_add($params, $period = 10)
  167. {
  168. try {
  169. QueueClient::async_push("AysncAddDispatcher", ['method' => 'add', 'params' => $params], $period);
  170. return true;
  171. } catch (Exception $ex) {
  172. return false;
  173. }
  174. }
  175. public static function async_notify($chname,$data, $period)
  176. {
  177. try {
  178. QueueClient::async_push("AysncAddDispatcher", ['method' => 'notify', 'params' => ['channel' => $chname, 'params' => $data]], $period);
  179. return true;
  180. } catch (Exception $ex) {
  181. return false;
  182. }
  183. }
  184. public static function push_add($params)
  185. {
  186. try {
  187. queue\DispatcherClient::instance()->push('add', $params);
  188. return true;
  189. } catch (Exception $ex) {
  190. return false;
  191. }
  192. }
  193. public static function push_addthird($params)
  194. {
  195. try {
  196. queue\DispatcherClient::instance()->push('addthird', $params);
  197. return true;
  198. } catch (Exception $ex) {
  199. return false;
  200. }
  201. }
  202. public static function push_notify($chname, $params)
  203. {
  204. try {
  205. queue\DispatcherClient::instance()->push('notify', ['channel' => $chname, 'params' => $params]);
  206. return true;
  207. } catch (Exception $ex) {
  208. return false;
  209. }
  210. }
  211. public static function push_notify_merchant($order_id, $manual)
  212. {
  213. try {
  214. queue\DispatcherClient::instance()->push('notify_mechant', ['order_id' => $order_id, 'manual' => $manual]);
  215. return true;
  216. } catch (Exception $ex) {
  217. return false;
  218. }
  219. }
  220. public static function push_query($order_id)
  221. {
  222. try {
  223. queue\DispatcherClient::instance()->push('query', ['order_id' => $order_id]);
  224. return true;
  225. } catch (Exception $ex) {
  226. return false;
  227. }
  228. }
  229. public static function manual_success($order_id)
  230. {
  231. try {
  232. queue\DispatcherClient::instance()->push('manual_success', ['order_id' => $order_id]);
  233. return true;
  234. } catch (Exception $ex) {
  235. return false;
  236. }
  237. }
  238. public static function manual_cancel($order_id)
  239. {
  240. try {
  241. queue\DispatcherClient::instance()->push('manual_cancel', ['order_id' => $order_id]);
  242. return true;
  243. } catch (Exception $ex) {
  244. return false;
  245. }
  246. }
  247. //统计提交订单数据
  248. public static function incr_commit_pre($chname, $card_type, $spec, $quality)
  249. {
  250. $ins = Cache::getInstance('cacheredis');
  251. $name = 'channel_monitor_commit';
  252. $sec = time();
  253. $key_sec = "pre-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
  254. $ins->hIncrBy($name, $key_sec, 1);
  255. }
  256. public static function hget_commit_pre_sec($chname, $card_type, $spec, $quality, $time_stamp)
  257. {
  258. $ins = Cache::getInstance('cacheredis');
  259. $name = 'channel_monitor_commit';
  260. $key_sec = "pre-{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
  261. $value = $ins->hget($name, '', $key_sec);
  262. return intval($value);
  263. }
  264. //统计用户提交数据
  265. public static function incr_user_commit($mchid,$card_type, $spec,$quality)
  266. {
  267. $ins = Cache::getInstance('cacheredis');
  268. $name = 'user_monitor_commit';
  269. $sec = time();
  270. $key_sec = "{$mchid}-{$quality}-{$card_type}-{$spec}-{$sec}";
  271. $ins->hIncrBy($name, $key_sec, 1);
  272. }
  273. public static function incr_user_success($mchid,$card_type, $spec,$quality)
  274. {
  275. $ins = Cache::getInstance('cacheredis');
  276. $name = 'user_monitor_success';
  277. $sec = time();
  278. $key_sec = "{$mchid}-{$quality}-{$card_type}-{$spec}-{$sec}";
  279. $ins->hIncrBy($name, $key_sec, 1);
  280. }
  281. public static function incr_commit($chname, $card_type, $spec, $quality, $fsuccess = true)
  282. {
  283. $ins = Cache::getInstance('cacheredis');
  284. $name = 'channel_monitor_commit';
  285. $sec = time();
  286. if ($fsuccess) {
  287. $key_sec = "succ-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
  288. } else {
  289. $key_sec = "fail-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
  290. }
  291. $ins->hIncrBy($name, $key_sec, 1);
  292. }
  293. public static function hget_commit_sec($chname, $card_type, $spec, $quality, $time_stamp, $fsuccess = true)
  294. {
  295. $ins = Cache::getInstance('cacheredis');
  296. $name = 'channel_monitor_commit';
  297. if ($fsuccess) {
  298. $key_sec = "succ-{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
  299. } else {
  300. $key_sec = "fail-{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
  301. }
  302. $value = $ins->hget($name, '', $key_sec);
  303. return intval($value);
  304. }
  305. //统计回调通知数据
  306. public static function incr_notify($chname, $card_type, $spec, $quality, $fsuccess = true)
  307. {
  308. $ins = Cache::getInstance('cacheredis');
  309. $name = 'channel_monitor_notify';
  310. $sec = time();
  311. if ($fsuccess) {
  312. $key_sec = "succ-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
  313. } else {
  314. $key_sec = "fail-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
  315. }
  316. $ins->hIncrBy($name, $key_sec, 1);
  317. }
  318. public static function hget_notify_sec($chname, $card_type, $spec, $quality, $time_stamp, $fsuccess = true)
  319. {
  320. $ins = Cache::getInstance('cacheredis');
  321. $name = 'channel_monitor_notify';
  322. if ($fsuccess) {
  323. $key_sec = "succ-{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
  324. } else {
  325. $key_sec = "fail-{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
  326. }
  327. $value = $ins->hget($name, '', $key_sec);
  328. return intval($value);
  329. }
  330. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  331. public static function incr_amount_lock($mchid, $card_type, $spec)
  332. {
  333. if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
  334. refill\rlock::incr_sys_storage($card_type,$spec,1);
  335. refill\rlock::incr_mch_total_storage($mchid,$card_type,$spec);
  336. refill\rlock::incr_mch_storage($mchid,$card_type,$spec,1);
  337. }
  338. }
  339. public static function decr_amount_lock($mchid, $card_type, $spec)
  340. {
  341. if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
  342. refill\rlock::decr_sys_storage($card_type,$spec,1);
  343. refill\rlock::decr_mch_total_storage($mchid,$card_type,$spec);
  344. refill\rlock::decr_mch_storage($mchid,$card_type,$spec,1);
  345. }
  346. }
  347. public static function add_exclude_channel($mchid,$mchorder,$card_type,$chname)
  348. {
  349. if($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard)
  350. {
  351. $ins = Cache::getInstance('cacheredis');
  352. $name = 'oil_exclude_channels';
  353. $key = "{$mchid}-{$mchorder}";
  354. $chnames = $ins->hget($name, '', $key);
  355. $chnames = unserialize($chnames);
  356. if(empty($chnames)) {
  357. $chnames = [];
  358. }
  359. if(!in_array($chname,$chnames)) {
  360. $chnames[] = $chname;
  361. $ins->hset($name,'',[$key => serialize($chnames)]);
  362. }
  363. }
  364. }
  365. public static function get_exclude_channel($mchid,$mchorder,$card_type)
  366. {
  367. if($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard)
  368. {
  369. $ins = Cache::getInstance('cacheredis');
  370. $name = 'oil_exclude_channels';
  371. $key = "{$mchid}-{$mchorder}";
  372. $chnames = $ins->hget($name, '', $key);
  373. $chnames = unserialize($chnames);
  374. if(is_array($chnames)) {
  375. return $chnames;
  376. } else {
  377. return [];
  378. }
  379. }
  380. else {
  381. return [];
  382. }
  383. }
  384. public static function del_exclude_channel($mchid,$mchorder,$card_type)
  385. {
  386. if($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard)
  387. {
  388. $ins = Cache::getInstance('cacheredis');
  389. $name = 'oil_exclude_channels';
  390. $key = "{$mchid}-{$mchorder}";
  391. $ins->hdel($name, '', $key);
  392. }
  393. }
  394. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  395. public static function push_queue($mchid,$mch_order,$val)
  396. {
  397. $ins = Cache::getInstance('cacheredis');
  398. $name = 'merchant_order_query';
  399. $key = "{$mchid}-{$mch_order}";
  400. $ins->hset($name, '', [$key => $val]);
  401. }
  402. public static function pop_queue($mchid,$mch_order)
  403. {
  404. $ins = Cache::getInstance('cacheredis');
  405. $name = 'merchant_order_query';
  406. $key = "{$mchid}-{$mch_order}";
  407. $ins->hdel($name, '', $key);
  408. }
  409. public static function query_queue($mchid,$mch_order)
  410. {
  411. $ins = Cache::getInstance('cacheredis');
  412. $name = 'merchant_order_query';
  413. $key = "{$mchid}-{$mch_order}";
  414. $value = $ins->hget($name,'',$key);
  415. return $value;
  416. }
  417. }