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 = 30; 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[^"]+)"[\w\W]+"message":"(?P[\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 { $ret = self::push_queue('add', $params); return $ret !== false; } catch (Exception $ex) { return false; } } public static function push_add_zero($params) { try { $ret = self::push_queue('add_zero', $params); return $ret !== false; } catch (Exception $ex) { return false; } } public static function push_addthird($params) { try { $ret = self::push_queue('addthird', $params); return $ret !== false; } catch (Exception $ex) { return false; } } public static function push_notify($chname, $params) { try { $ret = self::push_queue('notify', ['channel' => $chname, 'params' => $params]); return $ret !== false; } catch (Exception $ex) { return false; } } public static function push_notify_merchant($order_id, $manual) { try { $ret = self::push_queue('notify_mechant', ['order_id' => $order_id, 'manual' => $manual]); return $ret !== false; } catch (Exception $ex) { return false; } } public static function push_query($order_id) { try { $ret = self::push_queue('query', ['order_id' => $order_id]); return $ret !== false; } catch (Exception $ex) { return false; } } public static function push_query_net($order_id) { try { $ret = self::push_queue('query_net', ['order_id' => $order_id]); return $ret !== false; } catch (Exception $ex) { return false; } } public static function manual_success($order_id) { try { $ret = self::push_queue('manual_success', ['order_id' => $order_id]); return $ret !== false; } catch (Exception $ex) { return false; } } public static function manual_cancel($order_id) { try { $ret = self::push_queue('manual_cancel', ['order_id' => $order_id]); return $ret !== false; } catch (Exception $ex) { return false; } } public static function push_queue($method, $value) { if (defined('USE_COROUTINE') && USE_COROUTINE && defined('COROUTINE_HOOK_TCP') && COROUTINE_HOOK_TCP) { $queue_name = 'QUEUE_DISPATCHER_CO'; $ins = Cache::getInstance('cacheredis'); return $ins->lpush($queue_name, serialize([$method => $value])); } else { return queue\DispatcherClient::instance()->push($method,$value); } } public static function dispatcher_queue_length() { $ins = Cache::getInstance('cacheredis'); return $ins->lLen('QUEUE_DISPATCHER_CO'); } //统计提交订单数据 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 set_order_channels($mchid,$mchorder,$datas) { $ins = Cache::getInstance('cacheredis'); $name = 'order_channels_v2'; $key = "{$mchid}-{$mchorder}"; $ins->hset($name, '', [$key => serialize($datas)]); } public static function get_order_channels($mchid, $mchorder) { //old-name oil_exclude_channels $ins = Cache::getInstance('cacheredis'); $name = 'order_channels_v2'; $key = "{$mchid}-{$mchorder}"; $chnames = $ins->hget($name, '', $key); $chnames = unserialize($chnames); if(is_array($chnames)) { return $chnames; } else { return []; } } public static function del_order_channels($mchid, $mchorder) { $ins = Cache::getInstance('cacheredis'); $name = 'order_channels_v2'; $key = "{$mchid}-{$mchorder}"; $ins->hdel($name, '', $key); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static function push_queue_order($mchid,$mch_order,$order_state) { if(empty($mch_order)) return; $ins = Cache::getInstance('cacheredis'); $name = 'merchant_order_query'; $key = "{$mchid}-{$mch_order}"; $ins->hset($name, '', [$key => $order_state]); } public static function pop_queue_order($mchid,$mch_order) { util::del_order_channels($mchid,$mch_order); Model('refill_order')->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_HANDLED]); $ins = Cache::getInstance('cacheredis'); $name = 'merchant_order_query'; $key = "{$mchid}-{$mch_order}"; $ret = $ins->hdel($name, '', $key); } public static function query_queue_order($mchid,$mch_order) { $ins = Cache::getInstance('cacheredis'); $name = 'merchant_order_query'; $key = "{$mchid}-{$mch_order}"; $value = $ins->hget($name,'',$key); return $value; } public static function need_check($net_errno) { [$type,$code] = explode('-',$net_errno); $code = intval($code); if($type == "CURL") { static $errors = [CURLE_GOT_NOTHING,CURLE_RECV_ERROR]; return in_array($code,$errors); } elseif($type == "HTTP") { static $excludes = [404]; return !in_array($code,$excludes); } else { return false; } } }