123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- <?php
- namespace refill;
- require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
- require_once(BASE_HELPER_PATH . '/queue/rdispatcher.php');
- require_once(BASE_HELPER_PATH . '/refill/policy/rlock.php');
- use queue;
- use mtopcard;
- use Log;
- use Exception;
- use Cache;
- use QueueClient;
- use refill;
- class util
- {
- const ThirdRefillAmount = 100;
- static function make_mobile()
- {
- static $prefix = ["139", "138", "137", "136", "135", "134", "159", "158", "157", "150", "151", "152",
- "188", "187", "182", "183", "184", "178", "130", "131", "132", "156", "155", "186", "185",
- "176", "133", "153", "189", "180", "181", "177"];
- $pos = mt_rand(0, count($prefix) - 1);
- $no = "{$prefix[$pos]}" . mt_rand(10000000, 99999999);
- return $no;
- }
- public static function can_refill($card_no, $card_type)
- {
- if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
- $result = rcache('card_expired', '', "{$card_no}");
- if (empty($result)) {
- wcache("card_expired", [$card_no => time()], '');
- return [true, 0];
- } else {
- $latest = current($result);
- $cur = time();
- $success = ($cur - $latest) > 2;
- if ($success) {
- wcache("card_expired", [$card_no => time()], '');
- }
- return [$success, $latest + 2 - $cur];
- }
- } else {
- return [true, 0];
- }
- }
- public static function can_commit($card_no, $card_type)
- {
- if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard)
- {
- $result = rcache('card_expired', '', "{$card_no}");
- if (empty($result)) {
- wcache("card_expired", [$card_no => time()], '');
- return [true, 0];
- }
- else
- {
- $latest = current($result);
- $cur = time();
- $lowest = 300;
- if ($cur > $latest && ($cur - $latest) >= $lowest) {
- wcache("card_expired", [$card_no => time()], '');
- return [true, 0];
- } else {
- wcache("card_expired", [$card_no => $latest + $lowest], '');
- return [false, $latest + $lowest - $cur];
- }
- }
- }
- else {
- return [true, 0];
- }
- }
- static function read_card($card_no, $card_type = 0)
- {
- if (empty($card_no)) return false;
- $data = rcache($card_no, 'cardrefill-');
- if (empty($data)) {
- $mod_topcard = Model('topcard');
- $ret = $mod_topcard->get_card($card_no);
- if (empty($ret)) {
- if ($card_type === 0) {
- $card_type = mtopcard\card_type($card_no,$regin_no);
- }
- $bind_phone = util::make_mobile();
- $mod_topcard->add($card_no, $card_type, time(), $bind_phone);
- $data['bind_phone'] = $bind_phone;
- $data['refill_time'] = time();
- $data['times'] = 0;
- $data['black_card'] = 0;
- wcache($card_no, $data, 'cardrefill-');
- } else {
- $val = $ret[0];
- $data['bind_phone'] = $val['bind_phone'];
- $data['black_card'] = $val['black_card'];
- $data['refill_time'] = time();
- $data['times'] = 0;
- }
- }
- //之前没加black_card处理,这个字段不存在.
- if (!array_key_exists('black_card', $data)) {
- $data['black_card'] = 0;
- }
- return $data;
- }
- static function inc_card($card_no, $card_info)
- {
- $card_info['times'] += 1;
- $card_info['refill_time'] = time();
- wcache($card_no, $card_info, 'cardrefill-');
- }
- public static function del_card($card_no)
- {
- dcache($card_no, 'cardrefill-');
- }
- public static function set_black($card_no)
- {
- if (empty($card_no)) return false;
- $card_info = util::read_card($card_no);
- if (!empty($card_info)) {
- $card_info['black_card'] = 1;
- $mod_topcard = Model('topcard');
- $mod_topcard->table('topcard')->where(['card_no' => $card_no])->update(['black_card' => 1]);
- wcache($card_no, $card_info, 'cardrefill-');
- return true;
- } else {
- return false;
- }
- }
- private static function black_order($order_sn, $msg)
- {
- static $errMsgs = ["只能给主卡且卡状态正常的加油卡充值", "加油卡卡号错误或不支持"];
- if (empty($msg)) return false;
- if (in_array($msg, $errMsgs)) {
- $refill = Model('refill_order');
- $order = $refill->getOrderInfo(['order_sn' => $order_sn]);
- if (empty($order)) return false;
- $card_no = $order['card_no'];
- return util::set_black($card_no);
- }
- }
- public static function black_from_log($file_name)
- {
- $fn = fopen($file_name, "r");
- if (empty($fn)) {
- Log::record("Open File {$file_name} error.", Log::ERR);
- return false;
- } else {
- Log::record("{$file_name} start woring", Log::DEBUG);
- }
- $errs = [];
- while (!feof($fn)) {
- $line = trim(fgets($fn));
- $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);
- if ($ret) {
- $order_sn = $matches['order_sn'];
- $message = $matches['message'];
- self::black_order($order_sn, $message);
- $errs[$message] = empty($errs[$message]) ? 1 : $errs[$message] + 1;
- }
- }
- foreach ($errs as $msg => $count) {
- Log::record("msg:{$msg} count:{$count}", Log::DEBUG);
- }
- fclose($fn);
- return true;
- }
- public static function async_add($params, $period = 10)
- {
- try {
- QueueClient::async_push("AysncAddDispatcher", ['method' => 'add', 'params' => $params], $period);
- return true;
- } catch (Exception $ex) {
- return false;
- }
- }
- public static function async_notify($chname,$data, $period)
- {
- try {
- QueueClient::async_push("AysncAddDispatcher", ['method' => 'notify', 'params' => ['channel' => $chname, 'params' => $data]], $period);
- return true;
- } catch (Exception $ex) {
- return false;
- }
- }
- public static function push_add($params)
- {
- try {
- queue\DispatcherClient::instance()->push('add', $params);
- return true;
- } catch (Exception $ex) {
- return false;
- }
- }
- public static function push_addthird($params)
- {
- try {
- queue\DispatcherClient::instance()->push('addthird', $params);
- return true;
- } catch (Exception $ex) {
- return false;
- }
- }
- public static function push_notify($chname, $params)
- {
- try {
- queue\DispatcherClient::instance()->push('notify', ['channel' => $chname, 'params' => $params]);
- return true;
- } catch (Exception $ex) {
- return false;
- }
- }
- public static function push_notify_merchant($order_id, $manual)
- {
- try {
- queue\DispatcherClient::instance()->push('notify_mechant', ['order_id' => $order_id, 'manual' => $manual]);
- return true;
- } catch (Exception $ex) {
- return false;
- }
- }
- public static function push_query($order_id)
- {
- try {
- queue\DispatcherClient::instance()->push('query', ['order_id' => $order_id]);
- return true;
- } catch (Exception $ex) {
- return false;
- }
- }
- public static function manual_success($order_id)
- {
- try {
- queue\DispatcherClient::instance()->push('manual_success', ['order_id' => $order_id]);
- return true;
- } catch (Exception $ex) {
- return false;
- }
- }
- public static function manual_cancel($order_id)
- {
- try {
- queue\DispatcherClient::instance()->push('manual_cancel', ['order_id' => $order_id]);
- return true;
- } catch (Exception $ex) {
- return false;
- }
- }
- //统计提交订单数据
- public static function incr_commit_pre($chname, $card_type, $spec, $quality)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'channel_monitor_commit';
- $sec = time();
- $key_sec = "pre-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
- $ins->hIncrBy($name, $key_sec, 1);
- }
- public static function hget_commit_pre_sec($chname, $card_type, $spec, $quality, $time_stamp)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'channel_monitor_commit';
- $key_sec = "pre-{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
- $value = $ins->hget($name, '', $key_sec);
- return intval($value);
- }
- //统计用户提交数据
- public static function incr_user_commit($mchid,$card_type, $spec,$quality)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'user_monitor_commit';
- $sec = time();
- $key_sec = "{$mchid}-{$quality}-{$card_type}-{$spec}-{$sec}";
- $ins->hIncrBy($name, $key_sec, 1);
- }
- public static function incr_user_success($mchid,$card_type, $spec,$quality)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'user_monitor_success';
- $sec = time();
- $key_sec = "{$mchid}-{$quality}-{$card_type}-{$spec}-{$sec}";
- $ins->hIncrBy($name, $key_sec, 1);
- }
- public static function incr_commit($chname, $card_type, $spec, $quality, $fsuccess = true)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'channel_monitor_commit';
- $sec = time();
- if ($fsuccess) {
- $key_sec = "succ-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
- } else {
- $key_sec = "fail-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
- }
- $ins->hIncrBy($name, $key_sec, 1);
- }
- public static function hget_commit_sec($chname, $card_type, $spec, $quality, $time_stamp, $fsuccess = true)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'channel_monitor_commit';
- if ($fsuccess) {
- $key_sec = "succ-{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
- } else {
- $key_sec = "fail-{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
- }
- $value = $ins->hget($name, '', $key_sec);
- return intval($value);
- }
- //统计回调通知数据
- public static function incr_notify($chname, $card_type, $spec, $quality, $fsuccess = true)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'channel_monitor_notify';
- $sec = time();
- if ($fsuccess) {
- $key_sec = "succ-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
- } else {
- $key_sec = "fail-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
- }
- $ins->hIncrBy($name, $key_sec, 1);
- }
- public static function hget_notify_sec($chname, $card_type, $spec, $quality, $time_stamp, $fsuccess = true)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'channel_monitor_notify';
- if ($fsuccess) {
- $key_sec = "succ-{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
- } else {
- $key_sec = "fail-{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
- }
- $value = $ins->hget($name, '', $key_sec);
- return intval($value);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public static function incr_amount_lock($mchid, $card_type, $spec)
- {
- if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
- refill\rlock::incr_sys_storage($card_type,$spec,1);
- refill\rlock::incr_mch_total_storage($mchid,$card_type,$spec);
- refill\rlock::incr_mch_storage($mchid,$card_type,$spec,1);
- }
- }
- public static function decr_amount_lock($mchid, $card_type, $spec)
- {
- if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
- refill\rlock::decr_sys_storage($card_type,$spec,1);
- refill\rlock::decr_mch_total_storage($mchid,$card_type,$spec);
- refill\rlock::decr_mch_storage($mchid,$card_type,$spec,1);
- }
- }
- public static function add_exclude_channel($mchid,$mchorder,$card_type,$chname)
- {
- if($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'oil_exclude_channels';
- $key = "{$mchid}-{$mchorder}";
- $chnames = $ins->hget($name, '', $key);
- $chnames = unserialize($chnames);
- if(empty($chnames)) {
- $chnames = [];
- }
- if(!in_array($chname,$chnames)) {
- $chnames[] = $chname;
- $ins->hset($name,'',[$key => serialize($chnames)]);
- }
- }
- }
- public static function get_exclude_channel($mchid,$mchorder,$card_type)
- {
- if($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'oil_exclude_channels';
- $key = "{$mchid}-{$mchorder}";
- $chnames = $ins->hget($name, '', $key);
- $chnames = unserialize($chnames);
- if(is_array($chnames)) {
- return $chnames;
- } else {
- return [];
- }
- }
- else {
- return [];
- }
- }
- public static function del_exclude_channel($mchid,$mchorder,$card_type)
- {
- if($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'oil_exclude_channels';
- $key = "{$mchid}-{$mchorder}";
- $ins->hdel($name, '', $key);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public static function push_queue($mchid,$mch_order,$val)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'merchant_order_query';
- $key = "{$mchid}-{$mch_order}";
- $ins->hset($name, '', [$key => $val]);
- }
- public static function pop_queue($mchid,$mch_order)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'merchant_order_query';
- $key = "{$mchid}-{$mch_order}";
- $ins->hdel($name, '', $key);
- }
- public static function query_queue($mchid,$mch_order)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'merchant_order_query';
- $key = "{$mchid}-{$mch_order}";
- $value = $ins->hget($name,'',$key);
- return $value;
- }
- }
|