1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace refill;
- //监控,手机卡号在指定的多个通道中,保持充值中唯一性
- //如果订单成功,在指定时间段里面,不能继续充值
- class sending_monitor
- {
- private static $stChannelNames = [];//beirui_nation
- private const SENDING = 1;
- private const SUCC = 2;
- private const SUCC_INTERVAL_SECS = 900;
- public function can_add($card_no, $ch_name)
- {
- if (!in_array($ch_name, sending_monitor::$stChannelNames)) {
- return true;
- }
- $ret = $this->read($card_no);
- if ($ret === false) {
- return true;
- }
- [$ch_name, $time, $state] = $ret;
- if (!in_array($ch_name, sending_monitor::$stChannelNames)) {
- //如果限制的通道发生变化了,过去的数据无效处理。
- dcache('unique_sending_monitor', 'refill-', $card_no);
- return true;
- }
- if ($state === sending_monitor::SUCC)
- {
- if (time() - $time >= sending_monitor::SUCC_INTERVAL_SECS) {
- return true;
- } else {
- return false;
- }
- }
- elseif ($state === sending_monitor::SENDING) {
- return false;
- }
- else {
- return true;
- }
- }
- public function add($card_no, $chname)
- {
- if (!in_array($chname, sending_monitor::$stChannelNames)) {
- return;
- }
- $this->write($card_no, $chname, sending_monitor::SENDING);
- }
- public function success($card_no, $chname)
- {
- if (!in_array($chname, sending_monitor::$stChannelNames)) {
- return;
- }
- $this->write($card_no, $chname, sending_monitor::SENDING);
- }
- public function fail($card_no, $chname)
- {
- if (!in_array($chname, sending_monitor::$stChannelNames)) {
- return;
- }
- $name = 'unique_sending_monitor';
- dcache($name, 'refill-', $card_no);
- }
- private function write($card_no, $chname, $state)
- {
- $name = 'unique_sending_monitor';
- $data = [$card_no => json_encode([$chname, time(), $state])];
- wcache($name, $data, 'refill-');
- }
- private function read($card_no)
- {
- $name = 'unique_sending_monitor';
- $ret = rcache($name, 'refill-', $card_no);
- if (empty($ret)) {
- return false;
- }
- $values = $ret[$card_no];
- [$chname, $time, $state] = json_decode($values, true);
- return [$chname, $time, $state];
- }
- }
|