123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- namespace refill;
- use Log;
- //监控,手机卡号在指定的多个通道中,保持充值中唯一性
- //如果订单成功,在指定时间段里面,不能继续充值
- class sending_monitor
- {
- private const cache_name = 'unique_sending_monitor';
- private static $stChannelNames = [];//beirui_nation
- private const SENDING = 1;
- private const SUCC = 2;
- private const SUCC_INTERVAL_SECS = 900;
- public function __construct()
- {
- }
- public function can_commit($card_no, $ch_name)
- {
- Log::record("sending_monitor can_commit $card_no $ch_name",Log::DEBUG);
- return true;
- }
- private 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(sending_monitor::cache_name, '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 commit($card_no, $ch_name)
- {
- Log::record("sending_monitor commit $card_no $ch_name",Log::DEBUG);
- }
- public function notify($card_no, $ch_name, $fsucc)
- {
- Log::record("sending_monitor commit $card_no $ch_name succ=$fsucc",Log::DEBUG);
- }
- private function add($card_no, $chname)
- {
- if (!in_array($chname, sending_monitor::$stChannelNames)) {
- return;
- }
- $this->write($card_no, $chname, sending_monitor::SENDING);
- }
- private function success($card_no, $chname)
- {
- if (!in_array($chname, sending_monitor::$stChannelNames)) {
- return;
- }
- $this->write($card_no, $chname, sending_monitor::SENDING);
- }
- private function fail($card_no, $chname)
- {
- if (!in_array($chname, sending_monitor::$stChannelNames)) {
- return;
- }
- dcache(sending_monitor::cache_name, 'refill-', $card_no);
- }
- private function write($card_no, $chname, $state)
- {
- $data = [$card_no => json_encode([$chname, time(), $state])];
- wcache(sending_monitor::cache_name, $data, 'refill-');
- }
- private function read($card_no)
- {
- $ret = rcache(sending_monitor::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];
- }
- }
|