AutoClearIntegral.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\listener\user;
  12. use app\services\user\UserIntegralServices;
  13. use crmeb\utils\Cron;
  14. use crmeb\interfaces\ListenerInterface;
  15. use think\facade\Log;
  16. /**
  17. * 定时清空用户积分
  18. * Class AutoClearIntegral
  19. * @package app\listener\user
  20. */
  21. class AutoClearIntegral extends Cron implements ListenerInterface
  22. {
  23. /**
  24. * @param $event
  25. */
  26. public function handle($event): void
  27. {
  28. //定时清空用户积分
  29. $this->tick(1000 * 60 * 60, function () {
  30. //清空积分
  31. try {
  32. /** @var UserIntegralServices $userIntegralServices */
  33. $userIntegralServices = app()->make(UserIntegralServices::class);
  34. [$clear_time, $start_time, $end_time] = $userIntegralServices->getTime();
  35. //到清空积分的最后一天
  36. if ($clear_time == strtotime(date('Y-m-d', time()))) {
  37. return $userIntegralServices->clearExpireIntegral();
  38. }
  39. return true;
  40. } catch (\Throwable $e) {
  41. Log::error('清空积分,失败原因:[' . class_basename($this) . ']' . $e->getMessage());
  42. }
  43. });
  44. }
  45. }