DayTimes.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Module\Ulogic;
  3. use App\Module\Ulogic\Models\UserDaytime;
  4. /**
  5. * 用户 日计数器
  6. *
  7. */
  8. class DayTimes
  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,$day=null, $times = 1)
  19. {
  20. if ($day === null) {
  21. $day = date('Ymd');
  22. }
  23. $old = UserDaytime::query()->where([
  24. 'user_id' => $user_id,
  25. 'stype' => $stype,
  26. 'sid' => $sid,
  27. 'day' => $day
  28. ])->first();
  29. if (!$old) {
  30. $old = new UserDaytime();
  31. $old->user_id = $user_id;
  32. $old->stype = $stype;
  33. $old->sid = $sid;
  34. $old->day = $day;
  35. }
  36. $old->number += $times;
  37. $old->save();
  38. // dump($old->stype,$stype);
  39. return $old->number;
  40. }
  41. /**
  42. * 检查是否超过限制
  43. * @param $user_id
  44. * @param $stype
  45. * @param $sid
  46. * @param $times
  47. * @return bool
  48. */
  49. static public function check($user_id, $stype, $sid, $day = null,$times =0)
  50. {
  51. if ($day === null) {
  52. $day = date('Ymd');
  53. }
  54. /**
  55. * @var UserDaytime $old
  56. */
  57. $old = UserDaytime::query()->where([
  58. 'user_id' => $user_id,
  59. 'stype' => $stype,
  60. 'sid' => $sid,
  61. 'day' => $day
  62. ])->first();
  63. if (!$old) {
  64. return true;
  65. }
  66. if ($old->number >= $times) {
  67. return false;
  68. }
  69. return true;
  70. }
  71. }