sending_monitor.php 3.0 KB

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