Times.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Module\Ulogic;
  3. use App\Module\Ulogic\Model\UserTime;
  4. /**
  5. * 用户计数器
  6. *
  7. */
  8. class Times
  9. {
  10. /**
  11. * 增加计数
  12. * @param $user_id
  13. * @param $stype
  14. * @param $sid
  15. * @param $times
  16. * @return int
  17. */
  18. static public function add($user_id, $stype, $sid, $times = 1)
  19. {
  20. $old = UserTime::query()->where([
  21. 'user_id' => $user_id,
  22. 'stype' => $stype,
  23. 'sid' => $sid
  24. ])->first();
  25. if (!$old) {
  26. $old = new UserTime();
  27. $old->user_id = $user_id;
  28. $old->stype = $stype;
  29. $old->sid = $sid;
  30. }
  31. $old->number += $times;
  32. $old->save();
  33. // dump($old->stype,$stype);
  34. return $old->number;
  35. }
  36. /**
  37. * 检查是否超过限制
  38. * @param $user_id
  39. * @param $stype
  40. * @param $sid
  41. * @param $times
  42. * @return bool
  43. */
  44. static public function check($user_id, $stype, $sid, $times)
  45. {
  46. $old = UserTime::query()->where([
  47. 'user_id' => $user_id,
  48. 'stype' => $stype,
  49. 'sid' => $sid
  50. ])->first();
  51. if (!$old) {
  52. return true;
  53. }
  54. if ($old->number >= $times) {
  55. return false;
  56. }
  57. return true;
  58. }
  59. }