|
@@ -3,23 +3,78 @@
|
|
|
namespace refill\event;
|
|
|
|
|
|
use Log;
|
|
|
+use refill\order;
|
|
|
|
|
|
# 规避相同手机卡号撞单问题解
|
|
|
-class card_crash
|
|
|
+class card_crash implements IEverntRefill
|
|
|
{
|
|
|
private const cache_name = 'card_crash';
|
|
|
- private static $stChannelNames = [];//beirui_nation
|
|
|
-
|
|
|
- private const SENDING = 1;
|
|
|
- private const SUCC = 2;
|
|
|
- private const SUCC_INTERVAL_SECS = 900;
|
|
|
+ private $mChannels = [];
|
|
|
+ private $mSuccInterval = 900;
|
|
|
|
|
|
public function __construct()
|
|
|
{
|
|
|
}
|
|
|
public function load($cfgs)
|
|
|
{
|
|
|
+ $channels = $cfgs['channels'] ?? '';
|
|
|
+ $channels = explode(',',$channels);
|
|
|
+
|
|
|
+ $this->mChannels = [];
|
|
|
+ foreach ($channels as $item) {
|
|
|
+ $this->mChannels[] = trim($item);
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->mSuccInterval = intval($cfgs['succ_interval'] ?? 900);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function onBeforeSubmit(order $order)
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ public function onSubmit(order $order)
|
|
|
+ {
|
|
|
+ $card_no = $order->card_no();
|
|
|
+ $oid = $order->unique_id();
|
|
|
+ $org_val = [
|
|
|
+ 'sc' => '', //succ ch_name
|
|
|
+ 'st' => 0, //succ time
|
|
|
+ 'cc' => [] //正在提交的通道
|
|
|
+ ];
|
|
|
+
|
|
|
+ $val = $this->read($card_no) ?? $org_val;
|
|
|
+ $val['cc'][$oid] = '';
|
|
|
+ $this->write($card_no,$val);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public function onBeforeCommit(order $order, $ch_name): bool
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ public function onCommit(order $order, $ch_name)
|
|
|
+ {
|
|
|
+ }
|
|
|
|
|
|
+ public function onNeterror(order $order, $ch_name)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ public function onNotify($refill_info, $order_info, $success)
|
|
|
+ {
|
|
|
+ $uid = "{$refill_info['mchid']}-{$refill_info['mch_order']}";
|
|
|
+ $ch_name = $refill_info['channel_name'];
|
|
|
+
|
|
|
+ Log::record("onEvent notify uid=$uid channel=$ch_name success=$success", Log::DEBUG);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function onComplete($refill_info, $order_info, $success)
|
|
|
+ {
|
|
|
+ $uid = "{$refill_info['mchid']}-{$refill_info['mch_order']}";
|
|
|
+ $ch_name = $refill_info['channel_name'];
|
|
|
+
|
|
|
+ Log::record("onEvent complete uid=$uid channel=$ch_name success=$success", Log::DEBUG);
|
|
|
}
|
|
|
|
|
|
public function can_commit($card_no, $ch_name)
|
|
@@ -73,48 +128,32 @@ class card_crash
|
|
|
|
|
|
private function add($card_no, $chname)
|
|
|
{
|
|
|
- if (!in_array($chname, card_crash::$stChannelNames)) {
|
|
|
- return;
|
|
|
- }
|
|
|
|
|
|
- $this->write($card_no, $chname, card_crash::SENDING);
|
|
|
}
|
|
|
|
|
|
private function success($card_no, $chname)
|
|
|
{
|
|
|
- if (!in_array($chname, card_crash::$stChannelNames)) {
|
|
|
- return;
|
|
|
- }
|
|
|
|
|
|
- $this->write($card_no, $chname, card_crash::SENDING);
|
|
|
}
|
|
|
|
|
|
private function fail($card_no, $chname)
|
|
|
{
|
|
|
- if (!in_array($chname, card_crash::$stChannelNames)) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- dcache(card_crash::cache_name, 'refill-', $card_no);
|
|
|
}
|
|
|
|
|
|
- private function write($card_no, $chname, $state)
|
|
|
+ private function write($card_no, $val)
|
|
|
{
|
|
|
- $data = [$card_no => json_encode([$chname, time(), $state])];
|
|
|
+ $data = [$card_no => json_encode($val)];
|
|
|
wcache(card_crash::cache_name, $data, 'refill-');
|
|
|
}
|
|
|
|
|
|
private function read($card_no)
|
|
|
{
|
|
|
$ret = rcache(card_crash::cache_name, 'refill-', $card_no);
|
|
|
-
|
|
|
if (empty($ret)) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
$values = $ret[$card_no];
|
|
|
- [$chname, $time, $state] = json_decode($values, true);
|
|
|
-
|
|
|
- return [$chname, $time, $state];
|
|
|
+ return json_decode($values, true);
|
|
|
}
|
|
|
}
|