ChatExpireCleanJob.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Jobs;
  3. use App\Lib\Uploads\UploadHandler;
  4. use App\Models\WxAttachment;
  5. use App\Models\WxChat;
  6. use App\Models\WxNotice;
  7. use App\Wen\Utils\FileUtils;
  8. use App\Wen\Utils\Settings;
  9. use Illuminate\Bus\Queueable;
  10. use Illuminate\Contracts\Queue\ShouldBeUnique;
  11. use Illuminate\Contracts\Queue\ShouldQueue;
  12. use Illuminate\Foundation\Bus\Dispatchable;
  13. use Illuminate\Queue\InteractsWithQueue;
  14. use Illuminate\Queue\SerializesModels;
  15. use Illuminate\Support\Carbon;
  16. class ChatExpireCleanJob implements ShouldQueue
  17. {
  18. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  19. /**
  20. * Create a new job instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. //
  27. }
  28. /**
  29. * Execute the job.
  30. *
  31. * @return void
  32. */
  33. public function handle()
  34. {
  35. $rows = WxNotice::where('created_at', '<', Carbon::now()->subDays(60))->forceDelete();
  36. _logger_(__file__, __line__, 'ChatExpireCleanJob任务:共删除'.$rows.'条通知记录');
  37. //
  38. $after_days = Settings::get('chat_expire_days', 90);
  39. $chats = WxChat::where('created_at', '<', Carbon::now()->subDays($after_days))->where('expand_type','!=',9)->get();
  40. $clean_chats = 0;
  41. $clean_images = 0;
  42. if($chats){
  43. foreach ($chats as $chat){
  44. // 删除附件
  45. if($chat->chat_image){
  46. $attachment_part = FileUtils::get_attachment_part_from_url($chat->chat_image);
  47. if ($attachment_part) {
  48. $the_attachment = WxAttachment::where([
  49. ['domain', '=', $attachment_part['domain']],['path', '=', $attachment_part['path']]
  50. ])->first();
  51. if($the_attachment){
  52. if(UploadHandler::del($the_attachment)){
  53. $clean_images += 1;
  54. }else{
  55. _logger_(__file__, __line__, '清理图片失败');
  56. }
  57. }else{
  58. _logger_(__file__, __line__, '不存在附件表的url地址');
  59. }
  60. }
  61. }
  62. $chat->forceDelete();
  63. $clean_chats += 1;
  64. }
  65. }
  66. _logger_(__file__, __line__, 'ChatExpireCleanJob任务:共删除'.$clean_chats.'条聊天记录,并清理掉 ' . $clean_images . '个附件');
  67. }
  68. }