VideoAutoCommentJob.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Jobs;
  3. use App\Model\CommentModel;
  4. use App\Model\UserModel;
  5. use App\Model\VideoModel;
  6. use App\Repositories\CommentRepository;
  7. use Carbon\Carbon;
  8. use Hyperf\AsyncQueue\Job;
  9. class VideoAutoCommentJob extends Job
  10. {
  11. public $params;
  12. public function __construct($params = [])
  13. {
  14. // 这里最好是普通数据,不要使用携带 IO 的对象,比如 PDO 对象
  15. /*$this->params = [
  16. 'video_id' => 12131,
  17. 'type' => 1,
  18. 'status' => 1,
  19. 'check' => 1,
  20. ];*/
  21. $this->params = $params;
  22. }
  23. public function handle()
  24. {
  25. if (!empty($this->params['video_id']) && $this->params['type'] == 1 && $this->params['status'] == 1 && $this->params['check'] == 1) {
  26. // 开始随机写入评论.
  27. $commentRepo = make(CommentRepository::class);
  28. $count = CommentModel::query()->where(['module' => 'video', 'svalue' => $this->params['video_id']])->count('id');
  29. if (empty($count)) {
  30. // 视频没有评论,才进行自动追加评论
  31. $commentPath = self::getCommentPath();
  32. if (file_exists($commentPath)) {
  33. $content = file_get_contents($commentPath);
  34. $arrComments = explode("\n", $content);
  35. $users = UserModel::where(['ip' => '113.238.142.208'])->where('gender', '!=', 3)->inRandomOrder()->take(100)->get();
  36. $arrUsers = $users->pluck('code')->toArray();
  37. $oldCommentContent = '_1_';
  38. $commentContent = '';
  39. $obj = VideoModel::query()->find($this->params['video_id']);
  40. if (empty($obj)) {
  41. return true;
  42. }
  43. $videoCreatedAt = $obj->created_at;
  44. $maxCommentNumber = rand(1, 12);
  45. $nowDate = Carbon::now()->toDateTimeString();
  46. for ($i = 0; $i < $maxCommentNumber; $i++) {
  47. if (!empty($commentContent)) {
  48. $oldCommentContent = $commentContent;
  49. }
  50. $userKey = array_rand($arrUsers);
  51. $userCode = $arrUsers[$userKey];
  52. $commentKey = array_rand($arrComments);
  53. $commentContent = $arrComments[$commentKey];
  54. if ($oldCommentContent == $commentContent) {
  55. // 避免连续重复,再随机一次.
  56. $commentKey = array_rand($arrComments);
  57. $commentContent = $arrComments[$commentKey];
  58. }
  59. $setarr = [
  60. 'user_id' => $userCode,
  61. 'module' => 'video',
  62. 'svalue' => $obj->id,
  63. 'subject' => mb_substr($obj->title, 0, 120),
  64. 'content' => $commentContent,
  65. 'parent_id' => 0,
  66. 'total_support' => rand(0, 200),
  67. 'ip' => '113.238.142.208',
  68. ];
  69. $commentObj = $commentRepo->createComment($setarr);
  70. $commentObj->created_at = Carbon::parse($videoCreatedAt)->addMinutes(rand(1, 6))->toDateTimeString();
  71. $commentObj->save();
  72. }
  73. // 观看数,点赞数伪造
  74. $obj->watch = rand(20, 300); // 观看次数.
  75. $obj->like = rand(1, 100); // 点赞数
  76. $obj->save();
  77. }
  78. }
  79. }
  80. }
  81. /**
  82. * 获取评论路径.
  83. * @return string
  84. */
  85. private function getCommentPath()
  86. {
  87. return base_path(). '/storage/txt/comment.txt';
  88. }
  89. }