util.php 12 KB

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