| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <?php
- /**
- * 关系api.
- *
- * @author ningque
- * @date 2020-02-20 02:12:58
- */
- declare (strict_types=1);
- namespace Modules\Api\Controllers;
- use App\Constants\CommonConstant;
- use App\Model\Mongo\UserRelationModel;
- use App\Model\Mongo\VideoLikeModel;
- use App\Model\UserModel;
- use App\Model\VideoModel;
- use App\Repositories\RelationRepository;
- use App\Repositories\Service\CommonService;
- use App\Repositories\Service\QueueService;
- use App\Repositories\UserLevelRelationRepository;
- use App\Repositories\UserRepository;
- use App\Repositories\Utils\M3u8CacheRepository;
- use Carbon\Carbon;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- use Modules\Api\Services\VideoService;
- class RelationController extends BaseController
- {
- use \Hyperf\Di\Aop\ProxyTrait;
- use \Hyperf\Di\Aop\PropertyHandlerTrait;
- function __construct()
- {
- if (method_exists(parent::class, '__construct')) {
- parent::__construct(...func_get_args());
- }
- $this->__handlePropertyHandler(__CLASS__);
- }
- /**
- * 关注/取消关注
- * @return mixed
- */
- public function doRelation(ValidatorFactoryInterface $validatorFactory)
- {
- $userId = $this->getUserId();
- $inputData = $this->getJsonData();
- $rules = ['to_user_id' => 'required|string'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $toUserId = strval(data_get($inputData, 'to_user_id'));
- if ($userId == $toUserId) {
- return json_fail('自己不能关注自己');
- }
- $toUserInfo = make(UserRepository::class)->getUserInfoFromCache($toUserId);
- if (!$toUserInfo) {
- return json_fail('用户关态异常');
- }
- $favoriteKey = md5('favorite_' . $userId . '_' . $toUserId);
- $favoriteData = ['_id' => $favoriteKey];
- $favorite = make(UserRelationModel::class)->db()->countDocuments($favoriteData);
- $focusResult = false;
- if ($favorite) {
- //若已关注,则取消
- make(UserRelationModel::class)->db()->deleteOne($favoriteData);
- } else {
- $focusResult = true;
- //若未关注,则关注
- $favoriteData['from_user_id'] = $userId;
- $favoriteData['to_user_id'] = $toUserId;
- $favoriteData['date'] = Carbon::now()->toDateTimeString();
- make(UserRelationModel::class)->db()->insertOne($favoriteData);
- }
- make(UserRepository::class)->delUserCache($userId);
- make(UserRepository::class)->delUserCache($toUserId);
- return json_success($focusResult);
- }
- /**
- * 关注列表(含关注和被关注)
- * @return mixed
- */
- public function list(ValidatorFactoryInterface $validatorFactory)
- {
- $userId = $this->getUserId();
- $inputData = $this->getJsonData();
- $rules = ['type' => 'required|in:favorite,fans', 'page' => 'nullable|numeric', 'page_size' => 'nullable|numeric'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $type = data_get($inputData, 'type');
- $page = intval(data_get($inputData, 'page', 1));
- $pageSize = intval(data_get($inputData, 'page_size', 10));
- $skip = ($page - 1) * $pageSize;
- if ($type == 'favorite') {
- $data = ['from_user_id' => $userId];
- } else {
- $data = ['to_user_id' => $userId];
- }
- $options = ['skip' => $skip, 'limit' => $pageSize, 'sort' => ['date' => -1]];
- $tmp = make(UserRelationModel::class)->db()->find($data, $options);
- $result['list'] = [];
- if ($tmp) {
- $ids = [];
- $needUserId = $type == 'favorite' ? 'to_user_id' : 'from_user_id';
- foreach ($tmp->toArray() as $t) {
- $ids[] = $t[$needUserId];
- }
- $result['list'] = UserModel::query()->select(['code', 'nick', 'avatar'])->whereIn('code', $ids)->get();
- if (!empty($result['list'])) {
- foreach ($result['list'] as $k => $v) {
- $result['list'][$k]['avatar'] = M3u8CacheRepository::getImageUrl($result['list'][$k]['avatar']);
- }
- }
- }
- return json_success($result);
- }
- /**
- * 我点赞过的视频分页列表.
- * @return mixed
- */
- public function likeVideoList(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'is_long' => 'nullable|int',
- // 目前只有短视频,无视这个参数.
- 'page' => 'nullable|numeric',
- 'page_size' => 'nullable|numeric',
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $page = intval(data_get($inputData, 'page', 1));
- $pageSize = intval(data_get($inputData, 'page_size', 10));
- $skip = ($page - 1) * $pageSize;
- $relationRepo = make(RelationRepository::class);
- $userCode = $this->getUserId();
- $filters = ['uid' => $userCode];
- $amountVideo = make(VideoLikeModel::class)->db()->countDocuments($filters);
- $maxPages = ceil($amountVideo / $pageSize);
- $options = ['skip' => $skip, 'limit' => $pageSize, 'sort' => ['date' => -1]];
- $video_like = make(VideoLikeModel::class)->db()->find(['uid' => $userCode], $options)->toArray();
- $result = ['current_page' => $page, 'data' => [], 'first_page_url' => '', 'from' => null, 'last_page' => $maxPages, 'last_page_url' => '', 'next_page_url' => null, 'path' => '', 'per_page' => $pageSize, 'prev_page_url' => '', 'to' => null, 'total' => $amountVideo];
- $columns = ['id', 'title', 'cover', 'like', 'uid', 'coins', 'pay_type', 'mu', 'comment', 'watch', 'resolution', 'duration'];
- if (!empty($video_like)) {
- $arrIds = [];
- foreach ($video_like as $v) {
- $arrIds[] = $v['vid'];
- }
- $arrIds = array_slice($arrIds, 0, 100);
- // 最大100
- $id_str = implode(',', $arrIds);
- $result2 = VideoModel::query()->with(['category:type.id as type_id,type.title', 'user'])->whereIn('id', $arrIds)->where(['status' => 1])->orderByRaw('field(id,' . $id_str . ')')->get($columns);
- if (!empty($result2->isNotEmpty())) {
- $result['data'] = $result2->toArray();
- foreach ($result['data'] as $k => &$v) {
- $v['mu'] = '';
- // 列表干掉m3u8地址.
- // $v['mu'] = M3u8CacheRepository::getVideoInnerUrl($v['mu'], $userCode, $v['id'], 'video');
- $v['cover'] = M3u8CacheRepository::getImageUrl($v['cover']);
- if (!empty($v['user'])) {
- $v['user']['avatar'] = M3u8CacheRepository::getImageUrl($v['user']['avatar']);
- // $v['user']['is_focus'] = $relationRepo->userIsFocusOtherUser($userCode, $v['user']['code']);
- }
- // $v['is_my_like'] = $relationRepo->userIsLikeVideo($userCode, $v['id']);
- if (!empty($v['category'])) {
- foreach ($v['category'] as $ck => $t) {
- unset($v['category'][$ck]['pivot']);
- }
- }
- }
- }
- }
- return json_success($result);
- }
- /**
- * 视频点赞/取消点赞
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- */
- public function likeVideo(ValidatorFactoryInterface $validatorFactory)
- {
- $user_code = $this->getUserId();
- $inputData = $this->getJsonData();
- $rules = ['video_id' => 'required|int'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $videoId = intval(data_get($inputData, 'video_id', 0));
- $key = $user_code . '_' . $videoId;
- $_id = substr(md5($key), 8, 16);
- $data = ['_id' => $_id];
- $count = make(VideoLikeModel::class)->db()->countDocuments($data);
- if ($count) {
- //若已关注,则取消
- make(VideoLikeModel::class)->db()->deleteOne($data);
- make(QueueService::class)->addVideoLikeJob(['video_id' => $videoId, 'amount' => -1]);
- $result = 0;
- } else {
- $videoInfo = VideoModel::query()->find($videoId, ['uid'])->toArray();
- //若未关注,则关注
- $data['uid'] = $user_code;
- $data['vid'] = $videoId;
- $data['date'] = Carbon::now()->toDateTimeString();
- $data['video_user_uid'] = (string) $videoInfo['uid'];
- // 记录作者UID
- make(VideoLikeModel::class)->db()->insertOne($data);
- make(QueueService::class)->addVideoLikeJob(['video_id' => $videoId, 'amount' => 1]);
- $result = 1;
- }
- make(UserRepository::class)->delUserCache($user_code);
- return json_success($result);
- }
- public function myInvitePages(ValidatorFactoryInterface $validatorFactory)
- {
- $userCode = $this->getUserId();
- $inputData = $this->getJsonData();
- $rules = ['page' => 'nullable|numeric', 'page_size' => 'nullable|numeric'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $page = intval(data_get($inputData, 'page', 1));
- $pageSize = intval(data_get($inputData, 'page_size', 10));
- $lists = make(UserLevelRelationRepository::class)->userInvitePages($userCode, $page, $pageSize);
- return json_success($lists);
- }
- public function followUserPages(ValidatorFactoryInterface $validatorFactory)
- {
- $userCode = $this->getUserId();
- $inputData = $this->getJsonData();
- $rules = ['user_code' => 'required|max:16', 'page' => 'nullable|numeric', 'page_size' => 'nullable|numeric'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $type = 'favorite';
- $page = intval(data_get($inputData, 'page', 1));
- $pageSize = intval(data_get($inputData, 'page_size', 10));
- $page = format_page($page);
- if ($pageSize > 50) {
- $pageSize = CommonConstant::PAGE_SIZE;
- }
- $skip = ($page - 1) * $pageSize;
- $data = ['from_user_id' => $inputData['user_code']];
- $options = ['skip' => $skip, 'limit' => $pageSize, 'sort' => ['date' => -1]];
- $relationRepo = make(UserRelationModel::class);
- $amount = $relationRepo->db()->countDocuments($data);
- $lists = $relationRepo->db()->find($data, $options);
- $arrData = ['current_page' => $page, 'per_page' => $pageSize, 'total' => $amount, 'data' => []];
- if (!empty($lists)) {
- $arrIds = [];
- foreach ($lists->toArray() as $v) {
- $arrIds[] = $v['to_user_id'];
- }
- $columns = ['id', 'code', 'nick', 'avatar'];
- $users = UserModel::query()->whereIn('code', $arrIds)->get($columns);
- if ($users->count()) {
- $users = $users->toArray();
- foreach ($users as $v) {
- $videoCount = VideoModel::query()->where(['uid' => $v['code'], 'status' => 1])->count();
- $arrData['data'][] = ['user_code' => $v['code'], 'nick' => $v['nick'], 'avatar' => M3u8CacheRepository::getImageUrl($v['avatar']), 'video_count' => $videoCount];
- }
- }
- }
- return json_success($arrData);
- }
- }
|