time()], ''); return [true, 0]; } else { $latest = current($result); $cur = time(); $success = ($cur - $latest) > 120; if ($success) { wcache("card_expired", [$card_no => time()], ''); } return [$success, $latest + 120 - $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); } $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($method, $params) { try { QueueClient::async_push("AysncAddDispatcher", ['method' => $method, 'params' => $params], 10); 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_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_commit($chname, $card_type, $spec, $quality, $fsuccess = true) { $ins = Cache::getInstance('cacheredis'); $name = 'channel_monitor_commit'; $sec = time(); $min = $sec - $sec % 60; if ($fsuccess) { $key_sec = "succ-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}"; $key_min = "succm-{$chname}-{$quality}-{$card_type}-{$spec}-{$min}"; } else { $key_sec = "fail-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}"; $key_min = "failm-{$chname}-{$quality}-{$card_type}-{$spec}-{$min}"; } $ins->hIncrBy($name, $key_sec, 1); $ins->hIncrBy($name, $key_min, 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 hget_commit_min($chname, $card_type, $spec, $quality, $time_stamp, $fsuccess = true) { $ins = Cache::getInstance('cacheredis'); $name = 'channel_monitor_commit'; $min = $time_stamp - $time_stamp % 60; if ($fsuccess) { $key_sec = "succm-{$chname}-{$quality}-{$card_type}-{$spec}-{$min}"; } else { $key_sec = "failm-{$chname}-{$quality}-{$card_type}-{$spec}-{$min}"; } $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(); $min = $sec - $sec % 60; if ($fsuccess) { $key_sec = "succ-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}"; $key_min = "succm-{$chname}-{$quality}-{$card_type}-{$spec}-{$min}"; } else { $key_sec = "fail-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}"; $key_min = "failm-{$chname}-{$quality}-{$card_type}-{$spec}-{$min}"; } $ins->hIncrBy($name, $key_sec, 1); $ins->hIncrBy($name, $key_min, 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 hget_notify_min($chname, $card_type, $spec, $quality, $time_stamp, $fsuccess = true) { $ins = Cache::getInstance('cacheredis'); $name = 'channel_monitor_notify'; $min = $time_stamp - $time_stamp % 60; if ($fsuccess) { $key_sec = "succm-{$chname}-{$quality}-{$card_type}-{$spec}-{$min}"; } else { $key_sec = "failm-{$chname}-{$quality}-{$card_type}-{$spec}-{$min}"; } $value = $ins->hget($name, '', $key_sec); return intval($value); } }