love_php/app/Http/Controllers/Server/H5/VideoController.php

308 lines
15 KiB
PHP
Raw Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Http\Controllers\Server\H5;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\AnchorVideo;
use App\Models\Live\Anchor;
use App\Models\MerchantUsers;
use App\Models\Server\MerchantInformation;
use App\Models\Server\MerchantUser;
use App\Models\Server\SaasNotice;
use App\Models\User;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
class VideoController extends Controller
{
public function shortVideoListV2(Request $request)
{
try {
$merchant_id = $request->merchant_id;
$merchant_user = MerchantUser::where('id', $request->merchant_user_id)->first();
$video_id = $request->video_id;
$video_ids = AnchorVideo::where('m_id', $merchant_id)->where('id', '<', $video_id)->limit(5)->orderBy('id', 'desc')->pluck('id')->toArray();
$videos = AnchorVideo::where('m_id', $merchant_id)->where('status', 1)->whereIn('id', $video_ids)->select('id', 'title', 'anchor_id', 'm_id', 'cover', 'status', 'video_url', 'created_at')->orderBy('id', 'desc')->paginate(5);
foreach ($videos as $key => $video) {
$video->pv = Redis::zscore('short_videopv', $video->id) ?? 0;
$video->liker_num = $video->likers()->count();
$video->comment_num = $video->totalCommentsCount();
$video->liker_status = $merchant_user->hasLiked($video);
}
return $this->success('ok', $videos);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
public function shortVideoListV3(Request $request)
{
try {
$merchant_id = $request->merchant_id;
$merchant_user = MerchantUser::where('id', $request->merchant_user_id)->first();
$video_id = $request->video_id;
$video_ids = AnchorVideo::where('m_id', $merchant_id)->where('id', '>', $video_id)->limit(5)->orderBy('id', 'desc')->pluck('id')->toArray();
$videos = AnchorVideo::where('m_id', $merchant_id)->where('status', 1)->whereIn('id', $video_ids)->select('id', 'title', 'anchor_id', 'm_id', 'cover', 'status', 'video_url', 'created_at')->orderBy('id', 'desc')->paginate(5);
foreach ($videos as $key => $video) {
$video->pv = Redis::zscore('short_videopv', $video->id) ?? 0;
$video->liker_num = $video->likers()->count();
$video->comment_num = $video->totalCommentsCount();
$video->liker_status = $merchant_user->hasLiked($video);
}
return $this->success('ok', $videos);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 短视频详情
* @param Request $request
* @param $video_id
* @return JsonResponse|string
*/
public function shortVideoDetail(Request $request, $video_id)
{
try {
$video = AnchorVideo::withTrashed()->where('id', $video_id)->select('id', 'title', 'anchor_id', 'm_id', 'cover', 'status', 'video_url', 'created_at', 'deleted_at')->first();
$video->pv = Redis::zincrby('short_videopv', 1, $video_id);
//点赞数
$video->liker_num = $video->likers()->count();
//评论数
$video->comment_num = $video->totalCommentsCount();
// 是否已经点赞
$merchant_user = MerchantUser::where('id', $request->merchant_user_id)->first();
$video->liker_status = $merchant_user->hasLiked($video);
//分享二维码
$jump_url = urlencode(env('APP_URL') . '/pu/#/videoDetails/' . $video_id);
$url = env('APP_URL') . '/api/official/live/wechat/FamilyAuth?merchant_id=' . $video->m_id . '&anchor_openid=&url=' . $jump_url;
$qr_code = Redis::get('VideoDetail_S' . $video_id);
if (!$qr_code) {
$qr_code = $this->getPreviewQrcode($url);
Redis::setex('VideoDetail_S' . $video_id, 60 * 60 * 24 * 30, $qr_code);
$qr_code = Redis::get('VideoDetail_S' . $video_id);
}
$video->share_qr_code = $qr_code;
$video->merchant_id = $video->m_id;
$pre_video_id = 0;//上一个视频id
$next_video_id = 0;//下一个视频id
$video_ids = AnchorVideo::where('m_id', $video->m_id)->where('status', 1)->orderBy('is_top', 'desc')->orderBy('top_time', 'desc')->orderBy('created_at', 'desc')->orderBy('id', 'desc')->pluck('id')->toArray();
// dd($video_ids);
$video_count = count($video_ids);//100
foreach ($video_ids as $key => $value) {//0~99
if ($video_id != $value) continue;
if ($value == $video_id && $key == 0 && $video_count == 1) {//第一个视频 且 就一个视频
$pre_video_id = 0;
$next_video_id = 0;
} elseif ($value == $video_id && $key == 0 && $video_count > 1) {//第一个视频 且 不止一个视频
$pre_video_id = 0;
$next_video_id = $video_ids[$key + 1];
} elseif ($value == $video_id && $key > 0 && $key == $video_count - 1) {//不是第一个视频 是最后一个视频
$pre_video_id = $video_ids[$key - 1];
$next_video_id = 0;
} else {
$pre_video_id = $video_ids[$key - 1];
$next_video_id = $video_ids[$key + 1];
}
}
$video->pre_video_id = $pre_video_id;
$video->next_video_id = $next_video_id;
//下架或删除状态 0下架 1正常上架 2已删除
$publish_state = 0;
if ($video->deleted_at) {
$publish_state = 2;
} elseif ($video->status) {
$publish_state = 1;
} else {
$publish_state = 0;
}
$video->publish_state = $publish_state;
unset($video->m_id);
return $this->success('ok', $video);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 发表短视频评论
* @param Request $request
* @param $video_id
* @return JsonResponse|string
*/
public function submitVideoComment(Request $request, $video_id)
{
try {
$content = $request->input('content');
if (!$content) return $this->failure('评论内容不能为空');
$reply_id = $request->reply_id ?? 0;
$video = AnchorVideo::where('id', $video_id)->first();
if (!$video) return $this->failure('该记录不存在或已删除');
$merchant_user = MerchantUsers::where('id', $request->merchant_user_id)->first();
$result = $merchant_user->comment($video, $content);
$result->reply_id = $reply_id;
$result->save();
$content = $merchant_user->nickname . '在您的视频【' . $video->title . '】发表了评论';
SaasNotice::addRecord($video->merchant_id, $request->merchant_user_id, 'comment', $result->id, $content, 0);
return $this->success('ok', $result);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 评论列表
* @param $video_id
* @return JsonResponse|string
*/
public function videoCommentList($video_id)
{
try {
$video = AnchorVideo::where('id', $video_id)->select('id', 'anchor_id', 'm_id', 'cover', 'video_url', 'created_at')->first();
$comments = $video->comments()->select('id', 'commentable_id', 'commented_id', 'reply_id', 'comment', 'created_at')->where('reply_id', 0)->orderBy('id', 'desc')->paginate();
foreach ($comments as $key => $value) {
# code...
$merchant_user = MerchantUsers::select('nickname', 'pic')->where('id', $value->commented_id)->first();
$value->nickname = $merchant_user->nickname ?? '匿名用户';
$value->pic = $merchant_user->pic ?? User::DefaultAvatar;
$reply = $video->comments()->select('id', 'commentable_id', 'commented_id', 'reply_id', 'comment', 'created_at')->where('reply_id', $value->id)->orderBy('id', 'desc')->get();
foreach ($reply as $k => $v) {
$merchant_user = MerchantUsers::select('nickname', 'pic')->where('id', $v->commented_id)->first();
$v->nickname = $merchant_user->nickname ?? '匿名用户';
$v->pic = $merchant_user->pic ?? User::DefaultAvatar;
}
$value->reply = $reply;
}
return $this->success('ok', $comments);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 点赞
* @param Request $request
* @param $video_id
* @return JsonResponse|string
*/
public function likeVideo(Request $request, $video_id)
{
try {
$merchant_user = MerchantUser::where('id', $request->merchant_user_id)->first();
$video = AnchorVideo::where('id', $video_id)->first();
if ($merchant_user->hasLiked($video)) {
$merchant_user->dislike($video, 'like');
} else {
$result = $merchant_user->like($video, 'like');
$content = $merchant_user->nickname . '点赞了您的视频【' . $video->title . '】';
SaasNotice::addRecord($video->merchant_id, $merchant_user->id, 'like', $result->id, $content, 0);
}
return $this->success('ok');
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 点赞列表
* @param Request $request
* @param $video_id
* @return JsonResponse|string
*/
public function videoLikes(Request $request, $video_id)
{
try {
$video = AnchorVideo::select('id', 'title', 'cover')->where('id', $video_id)->first();
$comments = $video->likers()->select('id', 'likedable_id', 'created_at')->orderBy('id', 'desc')->paginate();
foreach ($comments as $key => $value) {
$user = MerchantUser::select('id', 'nickname', 'pic')->where('id', $value->likedable_id)->first();
$value->nickname = $user->nickname ?? '匿名用户';
$value->pic = $user->pic ?? User::DefaultAvatar;
}
return $this->success('ok', $comments);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 短视频和文章列表
* @param Request $request
* @return JsonResponse|string
*/
public function inforsAndVideos(Request $request)
{
try {
$information_arr = [];
$video_arr = [];
$page = $request->page;
$cache_key = 'infor_and_video_list_page_' . $page . $request->merchant_id;
//咨询文章
if (Cache::has($cache_key)) {
$result = Cache::get($cache_key);
return $this->success('ok', $result);
} else {
$infors = MerchantInformation::with('tag')->where('merchant_id', $request->merchant_id)->where('status', 1)->get();
foreach ($infors as $key => $info) {
$time = date("Y-m-d H:i:s", strtotime($info->created_at));
$pv = Redis::zscore('information', $info->id) ? Redis::zscore('information', $info->id) : 0;
$information_arr[$key] = ['id' => $info->id, 'type' => 'information', 'pv' => $pv, 'title' => $info->title, 'pic' => $info->pic, 'created_at' => $time, 'is_top' => $info->is_top, 'top_time' => $info->top_time, 'tag' => $info->tag];
}
$videos = AnchorVideo::where('m_id', $request->merchant_id)->where('status', 1)->get();
foreach ($videos as $key => $video) {
$time = date("Y-m-d H:i:s", strtotime($video->publish_at));
$pv = Redis::zscore('short_videopv', $video->id) ? Redis::zscore('short_videopv', $video->id) : 0;
$video_arr[$key] = ['id' => $video->id, 'type' => 'video', 'pv' => $pv, 'title' => $video->title, 'pic' => $video->cover, 'created_at' => $time, 'is_top' => $video->is_top, 'top_time' => $video->top_time];
}
$data = array_merge($video_arr, $information_arr);
}
if (empty($data)) {
$current_page = 1;
$data = [];
$first_page_url = env('APP_URL') . '/api/s/h5/video/infor/list?page=1&merchant_id=' . $request->merchant_id;
$from = null;
$last_page = 1;
$last_page_url = env('APP_URL') . '/api/s/h5/video/infor/list?page=1&merchant_id=' . $request->merchant_id;
$next_page_url = null;
$path = env('APP_URL') . '/api/s/h5/video/infor/list';
$per_page = 15;
$prev_page_url = null;
$to = null;
$total = 0;
return $this->success('ok', compact('current_page', 'data', 'first_page_url', 'from', 'last_page', 'last_page_url', 'next_page_url', 'path', 'per_page', 'prev_page_url', 'to', 'total'));
}
foreach ($data as $key => $raw) {
$is_top[$key] = $raw['is_top'];
$top_time[$key] = $raw['top_time'];
$created_at[$key] = $raw['created_at'];
$ids[$key] = $raw['id'];
}
// $time_key = array_column($data,'created_at');
// array_multisort($time_key,SORT_DESC,$data);
array_multisort($is_top, SORT_DESC, $top_time, SORT_DESC, $created_at, SORT_DESC, $ids, SORT_DESC, $data);
//手动分页
$page = $request->page ?? 1;
$perPage = 15;
$offset = ($page * $perPage) - $perPage;
$result = new LengthAwarePaginator(array_slice($data, $offset, $perPage), count($data), $perPage, $page, ['path' => $request->url(), 'query' => $request->query()]);
Cache::forever($cache_key, $result);
return $this->success('ok', $result);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
}