stanley-king 1 tahun lalu
induk
melakukan
0695265bc7

+ 46 - 9
helper/refill/EventManager.php

@@ -3,6 +3,7 @@
 namespace refill;
 
 use Log;
+use refill\event\card_crash;
 
 class EventManager
 {
@@ -16,44 +17,80 @@ class EventManager
         return self::$stInstance;
     }
 
+    private $mStubs = [];
+
     public function load()
     {
+        $cfgs = [
+            'open_crash' => true,
+            'cfgs_crash' => [
+                'channels' => [],
+                'succ_interval' => 900
+            ]
+        ];
 
+        $open_crash = $cfgs['open_crash'] ?? false;
+        if($open_crash) {
+            $crash = new card_crash();
+            $crash->load($cfgs['cfgs_crash'] ?? []);
+            $this->mStubs[] = $crash;
+        }
     }
 
-    public static function onBeforeSubmit(order $order)
+    public function onBeforeSubmit(order $order)
     {
+        foreach ($this->mStubs as $stub)
+        {
+            if($stub->onBeforeSubmit($order) === false) {
+                return false;
+            }
+        }
+
         return true;
     }
     public function onSubmit(order $order)
     {
+        foreach ($this->mStubs as $stub) {
+            $stub->onSubmit($order);
+        }
     }
 
     public function onBeforeCommit(order $order, $ch_name): bool
     {
+        foreach ($this->mStubs as $stub)
+        {
+            if ($stub->onBeforeCommit($order, $ch_name) === false) {
+                return false;
+            }
+        }
+
         return true;
     }
     public function onCommit(order $order, $ch_name)
     {
+        foreach ($this->mStubs as $stub) {
+            $stub->onCommit($order, $ch_name);
+        }
     }
 
     public function onNeterror(order $order, $ch_name)
     {
+        foreach ($this->mStubs as $stub) {
+            $stub->onNeterror($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);
+        foreach ($this->mStubs as $stub) {
+            $stub->onNotify($$refill_info, $order_info, $success);
+        }
     }
 
     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);
+        foreach ($this->mStubs as $stub) {
+            $stub->onComplete($$refill_info, $order_info, $success);
+        }
     }
 }

+ 19 - 0
helper/refill/event/IEverntRefill.php

@@ -0,0 +1,19 @@
+<?php
+
+namespace refill\event;
+
+use refill\order;
+
+interface IEverntRefill
+{
+    public function load(array $cfgs);
+    public static function onBeforeSubmit(order $order);
+    public function onSubmit(order $order);
+    public function onBeforeCommit(order $order, $ch_name): bool;
+    public function onCommit(order $order, $ch_name);
+    public function onNeterror(order $order, $ch_name);
+
+    public function onNotify($refill_info, $order_info, $success);
+
+    public function onComplete($refill_info, $order_info, $success);
+}

+ 64 - 25
helper/refill/event/card_crash.php

@@ -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);
     }
 }

+ 1 - 0
test/TestRefillUtil.php

@@ -11,6 +11,7 @@ require_once(BASE_CORE_PATH . '/lrlz.php');
 require_once(BASE_ROOT_PATH . '/fooder.php');
 require_once(BASE_HELPER_PATH . '/refill/util.php');
 require_once(BASE_HELPER_PATH . '/refill/event/card_crash.php');
+require_once(BASE_HELPER_PATH . '/refill/event/IEventRefill.php');