321 lines
13 KiB
PHP
321 lines
13 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Server\Admin;
|
||
|
|
|
||
|
|
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\MerchantInfo;
|
||
|
|
use App\Models\MerchantUsers;
|
||
|
|
use App\Models\Server\MerchantAccount;
|
||
|
|
use App\Models\Server\MerchantUser;
|
||
|
|
use App\Models\User;
|
||
|
|
use Illuminate\Support\Facades\Cache;
|
||
|
|
use Illuminate\Support\Facades\Redis;
|
||
|
|
|
||
|
|
class VideoController extends Controller
|
||
|
|
{
|
||
|
|
//短视频控制器
|
||
|
|
/**
|
||
|
|
* 上传短视频
|
||
|
|
* @param Request $request
|
||
|
|
* @return JsonResponse|string
|
||
|
|
* @throws \Exception
|
||
|
|
*/
|
||
|
|
public function uploadShortVideo(Request $request){
|
||
|
|
try {
|
||
|
|
$merchant = MerchantAccount::where('id', $request->account_id)->first();
|
||
|
|
$anchor = Anchor::where('m_id', $merchant->id)->first();
|
||
|
|
$video = new AnchorVideo();
|
||
|
|
if (!$request->aliyun_video_id) return $this->failure('请选择要上传的短视频');
|
||
|
|
if (!$request->cover) return $this->failure('请上传课程封面');
|
||
|
|
if (!$request->title) return $this->failure('请输入视频介绍');
|
||
|
|
if ($request->aliyun_video_id) {
|
||
|
|
$result = \AliyunService::getPlayInfo($request->aliyun_video_id);
|
||
|
|
if (empty($result)) throw new \Exception("获取播放地址失败", 1);
|
||
|
|
$video->video_url = $result['PlayInfoList']['PlayInfo'][0]['PlayURL'];
|
||
|
|
}
|
||
|
|
$video->m_id = $merchant->id;
|
||
|
|
$video->anchor_id = $anchor->id ?? 0;
|
||
|
|
$video->cover = $request->cover;
|
||
|
|
$video->title = $request->title;
|
||
|
|
$video->status = $request->status ?? 0;
|
||
|
|
$video->channel = 'admin';
|
||
|
|
$video->save();
|
||
|
|
$key = 'infor_and_video_list_page_1' . $request->account_id;
|
||
|
|
Cache::forget($key);
|
||
|
|
return $this->success('ok', ['id' => $video->id]);
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
$this->getError($e);
|
||
|
|
return $this->failure('服务器休息中,请稍后再试');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 编辑短视频
|
||
|
|
* @param Request $request
|
||
|
|
* @param $video_id
|
||
|
|
* @return JsonResponse|string
|
||
|
|
* @throws \Exception
|
||
|
|
*/
|
||
|
|
public function updateShortVideo(Request $request,$video_id){
|
||
|
|
try {
|
||
|
|
$video = AnchorVideo::where('id', $video_id)->first();
|
||
|
|
if ($request->has('cover') && $request->cover != $video->cover) {
|
||
|
|
$video->cover = $request->cover;
|
||
|
|
}
|
||
|
|
if ($request->has('title') && $request->title != $video->title) {
|
||
|
|
$video->title = $request->title;
|
||
|
|
}
|
||
|
|
if ($request->has('aliyun_video_id') && !empty($request->aliyun_video_id)) {
|
||
|
|
$result = \AliyunService::getPlayInfo($request->aliyun_video_id);
|
||
|
|
if (empty($result)) throw new \Exception("获取播放地址失败", 1);
|
||
|
|
$video->video_url = $result['PlayInfoList']['PlayInfo'][0]['PlayURL'];
|
||
|
|
}
|
||
|
|
if ($request->has('status') && $video->status != $request->status) {
|
||
|
|
$video->status = $request->status;
|
||
|
|
}
|
||
|
|
if ($request->has('is_top')) {
|
||
|
|
$video->is_top = $request->is_top;
|
||
|
|
$video->top_time = $request->is_top ? date('Y-m-d H:i:s') : null;
|
||
|
|
}
|
||
|
|
$video->save();
|
||
|
|
$key = 'infor_and_video_list_page_1' . $request->account_id;
|
||
|
|
Cache::forget($key);
|
||
|
|
return $this->success('ok');
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
$this->getError($e);
|
||
|
|
return $this->failure('服务器休息中,请稍后再试');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 短视频列表
|
||
|
|
* @param Request $request
|
||
|
|
* @return JsonResponse|string
|
||
|
|
*/
|
||
|
|
public function videoList(Request $request){
|
||
|
|
try {
|
||
|
|
$merchant_id = $request->account_id;
|
||
|
|
$keyword = $request->keyword;
|
||
|
|
$status = $request->status ?? 2;
|
||
|
|
$channel = $request->channel ?? 'all';
|
||
|
|
$videos = AnchorVideo::where('m_id', $merchant_id)->select('id', 'title', 'anchor_id', 'm_id', 'cover', 'status', 'video_url', 'is_top', 'top_time', 'created_at');
|
||
|
|
if ($status != 2) {
|
||
|
|
$videos = $videos->where('status', $status);
|
||
|
|
}
|
||
|
|
if ($channel != 'all') {
|
||
|
|
$videos = $videos->where('channel', $channel);
|
||
|
|
}
|
||
|
|
if ($keyword) {
|
||
|
|
$keyword = trim($keyword);
|
||
|
|
$videos = $videos->where('title', 'like', '%' . $keyword . '%');
|
||
|
|
}
|
||
|
|
$videos = $videos->orderBy('is_top', 'desc')->orderBy('top_time', 'desc')->orderBy('id', 'desc')->paginate();
|
||
|
|
foreach ($videos as $key => $video) {
|
||
|
|
$video->pv = Redis::zscore('short_videopv', $video->id) ?: 0;
|
||
|
|
$video->type = '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 videoDetail(Request $request,$video_id){
|
||
|
|
try {
|
||
|
|
$serve_tab = $request->serve_tab;
|
||
|
|
$video = AnchorVideo::withTrashed()->where('id', $video_id)->select('id', 'title', 'anchor_id', 'm_id', 'cover', 'status', 'video_url', 'created_at', 'deleted_at')->first();
|
||
|
|
if ($video->m_id != $request->account_id) {
|
||
|
|
return $this->failure('课程不属于档登陆用户');
|
||
|
|
}
|
||
|
|
$video->pv = Redis::zincrby('short_videopv', 1, $video_id) ?? 0;
|
||
|
|
//点赞数
|
||
|
|
$video->liker_num = $video->likers()->count();
|
||
|
|
//评论数
|
||
|
|
$video->comment_num = $video->totalCommentsCount();
|
||
|
|
// 分享二维码
|
||
|
|
$jump_url = urlencode(env('APP_URL') . '/pu/#/videoDetails/' . $video_id);
|
||
|
|
$url = env('APP_URL') . '/api/official/live/wechat/FamilyAuth?merchant_id=' . $video->m_id . '&serve_tab=' . $serve_tab . '&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->share_url = $url;
|
||
|
|
//下架或删除状态 0下架 1正常上架 2已删除
|
||
|
|
$publish_state = 0;
|
||
|
|
if ($video->deleted_at) {
|
||
|
|
$publish_state = 2;
|
||
|
|
} elseif ($video->status == 1) {
|
||
|
|
$publish_state = 1;
|
||
|
|
} else {
|
||
|
|
$publish_state = 0;
|
||
|
|
}
|
||
|
|
$video->publish_state = $publish_state;
|
||
|
|
return $this->success('ok', $video);
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
$this->getError($e);
|
||
|
|
return $this->failure('服务器休息中,请稍后再试');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 删除短视频
|
||
|
|
* @param $video_id
|
||
|
|
* @return JsonResponse|string
|
||
|
|
*/
|
||
|
|
public function removeShortVideo($video_id){
|
||
|
|
try {
|
||
|
|
$video = AnchorVideo::where('id', $video_id)->first();
|
||
|
|
if (empty($video)) return $this->failure('要删除的视频不存在');
|
||
|
|
$video->delete();
|
||
|
|
return $this->success('ok');
|
||
|
|
} 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
|
||
|
|
* @return JsonResponse|string
|
||
|
|
*/
|
||
|
|
public function delVideoComment(Request $request){
|
||
|
|
try {
|
||
|
|
$comment_id = $request->comment_id;
|
||
|
|
$video_id = $request->video_id;
|
||
|
|
$video = AnchorVideo::select('id', 'title', 'm_id')->where('id', $video_id)->first();
|
||
|
|
$video->comments()->where(function ($sql) use ($comment_id) {
|
||
|
|
$sql->where('id', $comment_id)
|
||
|
|
->orWhere('reply_id', $comment_id);
|
||
|
|
})->delete();
|
||
|
|
return $this->success('ok');
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
$this->getError($e);
|
||
|
|
return $this->failure('服务器休息中,请稍后再试');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 点赞列表
|
||
|
|
* @param $video_id
|
||
|
|
* @return JsonResponse|string
|
||
|
|
*/
|
||
|
|
public function videoLikes($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 synVideo(Request $request){
|
||
|
|
try {
|
||
|
|
$m_id = $request->account_id;
|
||
|
|
$links = $request->links;//数组
|
||
|
|
$result = [];
|
||
|
|
if (empty($links)) return $this->failure('请输入要同步的链接');
|
||
|
|
foreach ($links as $k => $link) {
|
||
|
|
if (empty($link)) continue;
|
||
|
|
$json = json_encode(['m_id' => $m_id, 'value' => $link]);
|
||
|
|
switch ($k) {
|
||
|
|
case 0:
|
||
|
|
$value = ['account_link' => $link];
|
||
|
|
$key = 'merchant:weixin';
|
||
|
|
break;
|
||
|
|
case 1:
|
||
|
|
$value = ['douyin_link' => $link];
|
||
|
|
$key = 'merchant:douyin';
|
||
|
|
break;
|
||
|
|
case 2:
|
||
|
|
$value = ['kuaishou_link' => $link];
|
||
|
|
$key = 'merchant:kuaishou';
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
# code...
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
MerchantInfo::updateOrCreate(['m_id' => $m_id], $value);
|
||
|
|
Redis::lpush($key, $json);
|
||
|
|
}
|
||
|
|
return $this->success('ok');
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
$this->getError($e);
|
||
|
|
return $this->failure('服务器休息中,请稍后再试');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 回填
|
||
|
|
* @param Request $request
|
||
|
|
* @return JsonResponse|string
|
||
|
|
*/
|
||
|
|
public function backUrl(Request $request){
|
||
|
|
try {
|
||
|
|
$result = MerchantInfo::where('m_id', $request->account_id)->select('account_link', 'douyin_link', 'kuaishou_link')->first();
|
||
|
|
return $this->success('ok', $result);
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
$this->getError($e);
|
||
|
|
return $this->failure('服务器休息中,请稍后再试');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|