sending_monitor.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace refill;
  3. //监控,手机卡号在指定的多个通道中,保持充值中唯一性
  4. //如果订单成功,在指定时间段里面,不能继续充值
  5. class sending_monitor
  6. {
  7. private static $stChannelNames = [];//beirui_nation
  8. private const SENDING = 1;
  9. private const SUCC = 2;
  10. private const SUCC_INTERVAL_SECS = 900;
  11. public function can_add($card_no, $ch_name)
  12. {
  13. if (!in_array($ch_name, sending_monitor::$stChannelNames)) {
  14. return true;
  15. }
  16. $ret = $this->read($card_no);
  17. if ($ret === false) {
  18. return true;
  19. }
  20. [$ch_name, $time, $state] = $ret;
  21. if (!in_array($ch_name, sending_monitor::$stChannelNames)) {
  22. //如果限制的通道发生变化了,过去的数据无效处理。
  23. dcache('unique_sending_monitor', 'refill-', $card_no);
  24. return true;
  25. }
  26. if ($state === sending_monitor::SUCC)
  27. {
  28. if (time() - $time >= sending_monitor::SUCC_INTERVAL_SECS) {
  29. return true;
  30. } else {
  31. return false;
  32. }
  33. }
  34. elseif ($state === sending_monitor::SENDING) {
  35. return false;
  36. }
  37. else {
  38. return true;
  39. }
  40. }
  41. public function add($card_no, $chname)
  42. {
  43. if (!in_array($chname, sending_monitor::$stChannelNames)) {
  44. return;
  45. }
  46. $this->write($card_no, $chname, sending_monitor::SENDING);
  47. }
  48. public function success($card_no, $chname)
  49. {
  50. if (!in_array($chname, sending_monitor::$stChannelNames)) {
  51. return;
  52. }
  53. $this->write($card_no, $chname, sending_monitor::SENDING);
  54. }
  55. public function fail($card_no, $chname)
  56. {
  57. if (!in_array($chname, sending_monitor::$stChannelNames)) {
  58. return;
  59. }
  60. $name = 'unique_sending_monitor';
  61. dcache($name, 'refill-', $card_no);
  62. }
  63. private function write($card_no, $chname, $state)
  64. {
  65. $name = 'unique_sending_monitor';
  66. $data = [$card_no => json_encode([$chname, time(), $state])];
  67. wcache($name, $data, 'refill-');
  68. }
  69. private function read($card_no)
  70. {
  71. $name = 'unique_sending_monitor';
  72. $ret = rcache($name, 'refill-', $card_no);
  73. if (empty($ret)) {
  74. return false;
  75. }
  76. $values = $ret[$card_no];
  77. [$chname, $time, $state] = json_decode($values, true);
  78. return [$chname, $time, $state];
  79. }
  80. }