|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace App\Controller\Laboratory\Bilibili; |
| 5 | + |
| 6 | +use App\Constants\StatusCode; |
| 7 | +use App\Controller\AbstractController; |
| 8 | +use App\Foundation\Annotation\Explanation; |
| 9 | +use App\Foundation\Utils\Queue; |
| 10 | +use App\Job\Bilibili\UpUserInfoRecordJob; |
| 11 | +use App\Model\Laboratory\Bilibili\UpUser; |
| 12 | +use Hyperf\Di\Annotation\Inject; |
| 13 | +use Hyperf\HttpServer\Annotation\Controller; |
| 14 | +use Hyperf\HttpServer\Annotation\Middleware; |
| 15 | +use Hyperf\HttpServer\Annotation\Middlewares; |
| 16 | +use Hyperf\HttpServer\Annotation\RequestMapping; |
| 17 | +use App\Middleware\RequestMiddleware; |
| 18 | +use App\Middleware\PermissionMiddleware; |
| 19 | + |
| 20 | +/** |
| 21 | + * Up主 |
| 22 | + * Class UpUserController |
| 23 | + * @Controller(prefix="laboratory/bilibili_module/up_user") |
| 24 | + */ |
| 25 | +class UpUserController extends AbstractController |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @Inject() |
| 29 | + * @var UpUser |
| 30 | + */ |
| 31 | + private $upUser; |
| 32 | + |
| 33 | + /** |
| 34 | + * @Inject() |
| 35 | + * @var Queue |
| 36 | + */ |
| 37 | + private $queue; |
| 38 | + |
| 39 | + /** |
| 40 | + * @Explanation(content="录入Up主") |
| 41 | + * @RequestMapping(path="up_user_add", methods="post") |
| 42 | + * @Middlewares({ |
| 43 | + * @Middleware(RequestMiddleware::class), |
| 44 | + * @Middleware(PermissionMiddleware::class) |
| 45 | + * }) |
| 46 | + * @throws \Exception |
| 47 | + * @return \Psr\Http\Message\ResponseInterface |
| 48 | + */ |
| 49 | + public function upUserAdd() |
| 50 | + { |
| 51 | + $upUserInfo = $this->request->all()['up_user_info'] ?? []; |
| 52 | + if (empty($upUserInfo)) $this->throwExp(StatusCode::ERR_VALIDATION, 'Up主信息不能为空'); |
| 53 | + |
| 54 | + //是否存在空的upUserUrl |
| 55 | + $isExistEmptyUrl = false; |
| 56 | + $addMidArr = []; |
| 57 | + foreach ($upUserInfo as $upUser) { |
| 58 | + $upUserUrl = $upUser['up_user_url'] ?? ''; |
| 59 | + $timedStatus = $upUser['timed_status'] ?? ''; |
| 60 | + if (empty($upUserUrl)) { |
| 61 | + $isExistEmptyUrl = true; |
| 62 | + continue; |
| 63 | + } |
| 64 | + $lastString = basename($upUserUrl); |
| 65 | + $mid = explode('?', $lastString)[0] ?? ''; |
| 66 | + if (empty($mid)) continue; |
| 67 | + |
| 68 | + $upUser = new UpUser(); |
| 69 | + $upUser->mid = $mid; |
| 70 | + $upUser->timed_status = $timedStatus; |
| 71 | + $upUser->save(); |
| 72 | + $addMidArr[] = $mid; |
| 73 | + } |
| 74 | + //推送一个队列,异步获取Up主信息 |
| 75 | + $this->queue->push(new UpUserInfoRecordJob([ |
| 76 | + 'up_user_mid' => $addMidArr, |
| 77 | + ])); |
| 78 | + |
| 79 | + if ($isExistEmptyUrl) return $this->successByMessage('录入Up主成功,部分Url条目为空录入失败'); |
| 80 | + |
| 81 | + return $this->successByMessage('录入Up主成功'); |
| 82 | + } |
| 83 | +} |
0 commit comments