ContinuousTimes.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace App\Module\System\Services;
  3. use App\Module\Ulogic\Model\UserDaytime;
  4. /**
  5. * 连续次数处理
  6. *
  7. */
  8. class ContinuousTimes
  9. {
  10. /**
  11. * 增加计数
  12. *
  13. * @param $user_id
  14. * @param $stype
  15. * @param $sid
  16. * @param $times
  17. * @param $diff
  18. * @return int|mixed
  19. */
  20. static public function add($user_id, $stype, $sid, $times = 1, $diff = 120)
  21. {
  22. $old = \App\Module\System\Models\ContinuousTimes::class::query()->where([
  23. 'user_id' => $user_id,
  24. 'stype' => $stype,
  25. 'sid' => $sid,
  26. ])->first();
  27. if (!$old) {
  28. $old = new \App\Module\System\Models\ContinuousTimes();
  29. $old->user_id = $user_id;
  30. $old->stype = $stype;
  31. $old->sid = $sid;
  32. $old->last_time = 0;
  33. $old->number = 0;
  34. }
  35. $old->diff = $diff;
  36. if (($old->last_time + $diff) < time()) {
  37. // 不连续了
  38. $old->number = 0;
  39. }
  40. $old->last_time = time();
  41. $old->number += $times;
  42. $old->save();
  43. // dump($old->stype,$stype);
  44. return $old->number;
  45. }
  46. /**
  47. * 检查是否超过限制
  48. * @param $user_id
  49. * @param $stype
  50. * @param $sid
  51. * @param $times
  52. * @return bool
  53. */
  54. static public function check($user_id, $stype, $sid, $times = 1)
  55. {
  56. /**
  57. * @var \App\Module\System\Models\ContinuousTimes $old
  58. */
  59. $old = \App\Module\System\Models\ContinuousTimes::query()->where([
  60. 'user_id' => $user_id,
  61. 'stype' => $stype,
  62. 'sid' => $sid,
  63. ])->first();
  64. if (!$old) {
  65. return true;
  66. }
  67. $diff = $old->diff;
  68. if (($old->last_time + $diff) < time()) {
  69. // 不连续了
  70. $old->number = 0;
  71. }
  72. if ($old->number >= $times) {
  73. return false;
  74. }
  75. return true;
  76. }
  77. }