| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace App\Jobs;
- use App\Model\CommentModel;
- use App\Model\UserModel;
- use App\Model\VideoModel;
- use App\Repositories\CommentRepository;
- use Carbon\Carbon;
- use Hyperf\AsyncQueue\Job;
- class VideoAutoCommentJob extends Job
- {
- public $params;
- public function __construct($params = [])
- {
- // 这里最好是普通数据,不要使用携带 IO 的对象,比如 PDO 对象
- /*$this->params = [
- 'video_id' => 12131,
- 'type' => 1,
- 'status' => 1,
- 'check' => 1,
- ];*/
- $this->params = $params;
- }
- public function handle()
- {
- if (!empty($this->params['video_id']) && $this->params['type'] == 1 && $this->params['status'] == 1 && $this->params['check'] == 1) {
- // 开始随机写入评论.
- $commentRepo = make(CommentRepository::class);
- $count = CommentModel::query()->where(['module' => 'video', 'svalue' => $this->params['video_id']])->count('id');
- if (empty($count)) {
- // 视频没有评论,才进行自动追加评论
- $commentPath = self::getCommentPath();
- if (file_exists($commentPath)) {
- $content = file_get_contents($commentPath);
- $arrComments = explode("\n", $content);
- $users = UserModel::where(['ip' => '113.238.142.208'])->where('gender', '!=', 3)->inRandomOrder()->take(100)->get();
- $arrUsers = $users->pluck('code')->toArray();
- $oldCommentContent = '_1_';
- $commentContent = '';
- $obj = VideoModel::query()->find($this->params['video_id']);
- if (empty($obj)) {
- return true;
- }
- $videoCreatedAt = $obj->created_at;
- $maxCommentNumber = rand(1, 12);
- $nowDate = Carbon::now()->toDateTimeString();
- for ($i = 0; $i < $maxCommentNumber; $i++) {
- if (!empty($commentContent)) {
- $oldCommentContent = $commentContent;
- }
- $userKey = array_rand($arrUsers);
- $userCode = $arrUsers[$userKey];
- $commentKey = array_rand($arrComments);
- $commentContent = $arrComments[$commentKey];
- if ($oldCommentContent == $commentContent) {
- // 避免连续重复,再随机一次.
- $commentKey = array_rand($arrComments);
- $commentContent = $arrComments[$commentKey];
- }
- $setarr = [
- 'user_id' => $userCode,
- 'module' => 'video',
- 'svalue' => $obj->id,
- 'subject' => mb_substr($obj->title, 0, 120),
- 'content' => $commentContent,
- 'parent_id' => 0,
- 'total_support' => rand(0, 200),
- 'ip' => '113.238.142.208',
- ];
- $commentObj = $commentRepo->createComment($setarr);
- $commentObj->created_at = Carbon::parse($videoCreatedAt)->addMinutes(rand(1, 6))->toDateTimeString();
- $commentObj->save();
- }
- // 观看数,点赞数伪造
- $obj->watch = rand(20, 300); // 观看次数.
- $obj->like = rand(1, 100); // 点赞数
- $obj->save();
- }
- }
- }
- }
- /**
- * 获取评论路径.
- * @return string
- */
- private function getCommentPath()
- {
- return base_path(). '/storage/txt/comment.txt';
- }
- }
|