util.php 13 KB

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