SetAtRead.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace app\common\task;
  3. use yunwuxin\cron\Task;
  4. use think\Exception;
  5. use think\facade\Cache;
  6. use app\manage\model\{Config};
  7. use app\enterprise\model\Message;
  8. // 自动清理消息定时任务
  9. class SetAtRead extends Task
  10. {
  11. // 定时任务日志内容
  12. protected $content='';
  13. protected $path='';
  14. protected $daytime=86400;
  15. public function configure()
  16. {
  17. //设置每天8点执行
  18. $this->everyMinute();
  19. }
  20. /**
  21. * 执行任务
  22. * @return mixed
  23. */
  24. protected function execute()
  25. {
  26. try {
  27. $atListQueue=Cache::get('atListQueue');
  28. if($atListQueue){
  29. foreach ($atListQueue as $key=>$val){
  30. $message=Message::where('msg_id',$key)->value('at');
  31. $atList=($message ?? null) ? explode(',',$message): [];
  32. // 两个数组取差集
  33. $uniqueArr=array_unique($val);
  34. $newAtList = array_filter($atList, function ($value) use ($uniqueArr) {
  35. return !in_array($value, $uniqueArr);
  36. });
  37. Message::where('msg_id',$key)->update(['at'=>implode(',',$newAtList)]);
  38. }
  39. Cache::delete('atListQueue');
  40. }
  41. print "****************设置已读成功******************\n";
  42. } catch (Exception $e) {
  43. print '设置已读失败:'.$e->getMessage()."\n";
  44. }
  45. }
  46. }