bztang-admin/plugins/video-share/src/frontend/VideoController.php

584 lines
20 KiB
PHP

<?php
namespace Yunshop\VideoShare\frontend;
use app\common\components\ApiController;
use app\common\facades\Setting;
use app\common\services\MiniFileLimitService;
use app\common\services\Utils;
use app\framework\Http\Request;
use app\frontend\models\Member;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;
use Yunshop\BusinessCard\common\models\BusinessCardModel;
use Yunshop\VideoShare\common\events\VideoWatchEvent;
use Yunshop\VideoShare\common\model\CategoryModel;
use Yunshop\VideoShare\common\model\Comment;
use Yunshop\VideoShare\common\model\Follow;
use Yunshop\VideoShare\common\model\LikeLog;
use Yunshop\VideoShare\common\model\VideoShareGoods;
use Yunshop\VideoShare\common\model\VideoShareWatchRecord;
use Yunshop\VideoShare\common\events\VideoPublishEvent;
class VideoController extends ApiController
{
// 发现 列表
public function getList()
{
$uid = \YunShop::app()->getMemberId();
$search = request()->search;
//兼容APP请求
if (!is_array($search)) {
$search =[$search => 1];
}
$list = VideoShareGoods::getList($search)
->where('status', 1)
->orderBy('id', 'desc')
->paginate();
$businessCard = app('plugins')->isEnabled('business-card');
$list->map(function ($item) use ($businessCard, $uid) {
// 图片
$item->goods->thumb = yz_tomedia($item->goods->thumb);
if ($item->goods->has_option) {
$item->goods->price = $item->goods->hasManyOptions->min('product_price');
}
$item->member->avatar = yz_tomedia($item->member->avatar);
$item->cover = yz_tomedia($item->cover);
$item->video = yz_tomedia($item->video);
// 会员名片
$item->member->business_card = 0;
if ($businessCard) {
$cardModel = BusinessCardModel::select()
->where('member_id', $item->member->uid)
->first();
if ($cardModel) {
$item->member->business_card = $cardModel->id;
}
}
// 会员点赞 默认可以点赞
$item->member_like = 1;
$likeLog = LikeLog::select()
->where('uid', $uid)
->where('vid', $item->id)
->first();
if ($likeLog) {
$item->member_like = 0;
}
$item->room_id = 0;
//判断是否关注
$follow = Follow::uniacid()->where("member_id",$item->uid)->where("follow_member_id",$uid)->count();
$item->is_follow = ($follow>0) ? 1 : 0;
//评论
$comment_num = Comment::uniacid()
->where(['video_id' => $item->id, 'comment_id' => 0])
->count();
$item->comment_num = $comment_num;
//判断当前会员是否在直播
$item ->is_live = 0;
if (app('plugins')->isEnabled('room')) {
$anchor = \Yunshop\Room\models\Anchor::uniacid()->where("member_id",$item->uid)->first();
if ($anchor) {
$item->is_live = $anchor['status'];
}
$item->room_id = \Yunshop\Room\models\Room::uniacid()->where("status",3)->value("id");
}
//投放广告视频插件-观看赚/投放广告按钮
$item->ad_serving_button_status = [];
if (app('plugins')->isEnabled('ad-serving')) {
$item->ad_serving_button_status = \Yunshop\AdServing\services\PrivilegeValidateService::checkButton($item->id,2);
}
// 统计会员持有视频总数
$item->member_video_num = count(VideoShareGoods::where('uid', $item->uid)->whereNotNull('video')->get());
});
$video_redpack=['is_open'=>0,'data'=>[]];
if (app('plugins')->isEnabled('video-redpack')){
$video_redpack['is_open'] = 1;
$video_redpack['data']=\Yunshop\VideoRedpack\services\ActivityService::getActiviyForFrontend();
}
$set = \Setting::get('plugin.video-share');
return $this->successJson('成功', [
'list' => $list,
'is_plugins' => app('plugins')->isEnabled('answer-reward'),
'points_plugin' => app('plugins')->isEnabled('video-share-points'),
'video_redpack'=>$video_redpack,
'comment_open' => $set['is_open_comment']?:0
]);
}
//获取基础设置
public function getBasicSet()
{
$set = \Setting::get('plugin.video-share');
$setting['is_show_cate'] = $set['is_show_cate'];
$setting['is_show_own_follow'] = $set['is_show_own_follow'];
$setting['is_show_recommend'] = $set['is_show_recommend'];
$setting['is_show_live'] = $set['is_show_live'];
$setting['topLike'] = $set['topLike'] ?: "";
$setting['topSmileLike'] = $set['topSmileLike'];
$setting['customIcon'] = empty($set['customIcon']) ? "" : yz_tomedia($set['customIcon']);
$setting['icon_name'] = $set['icon_name'];
$setting['iconLink'] = $set['iconLink'] ?: "";
$setting['iconSmileLink'] = $set['iconSmileLink'];
$setting['is_show_name'] = $set['is_show_name']?:0;
$setting['is_member_enter'] = isset($set['is_member_enter']) ? $set['is_member_enter']: 1;
$setting['is_show_video_progress'] = $set['is_show_video_progress']?1:0;
return $this->successJson("",['set' => $setting]);
}
//获取分类
public function getCategory()
{
$set = \Setting::get('plugin.video-share');
$data = [];
if ($set['is_show_cate'] != 1) {
return $this->successJson("",$data);
}
$is_select = request()->is_select;
if ($set['is_show_hide'] == 1 && $is_select == 1) {
$category = CategoryModel::uniacid()->select("id","name")->orderBy('sort','desc')->get();
} else {
$category = CategoryModel::uniacid()->select("id","name")->where("is_show",1)->orderBy('sort','desc')->get();
}
$category = empty($category) ? [] : $category->toArray();
return $this->successJson("",$category);
}
//关注/取消关注
public function editFollow()
{
$uid = request()->get("member_id");
$type = Request()->get("follow_type");
$member_id = \YunShop::app()->getMemberId();
if ($type == 1) {
//关注
$exist = Follow::uniacid()->where("member_id",$uid)->where("follow_member_id",$member_id)->count();
if ($exist>0) {
return $this->errorJson("已关注");
}
$follow_data = [
"uniacid" => \YunShop::app()->uniacid,
"member_id" => $uid,
"follow_member_id" => $member_id,
"created_at" => time(),
"updated_at" => time()
];
if (Follow::insert($follow_data)) {
return $this->successJson("成功");
}
return $this->errorJson("失败");
} elseif ($type == 2) {
//取消关注
//关注
$follow = Follow::uniacid()->where("member_id",$uid)->where("follow_member_id",$member_id)->first();
if (!$follow) {
return $this->errorJson("数据错误");
}
$follow->deleted_at = time();
if($follow->save()) {
return $this->successJson("成功");
}
return $this->errorJson("失败");
} else {
return $this->errorJson("数据错误");
}
}
//添加观看记录
public function addRecord(){
$uid = \YunShop::app()->getMemberId();
$videoId = request()->video_id;
$result = VideoShareWatchRecord::create(['member_id' => $uid, 'video_id' => $videoId]);
$this->addBrowseFootprintRedis($videoId);
return $this->successJson('成功', $result);
}
protected static function addBrowseFootprintRedis($videoId)
{
if (app('plugins')->isEnabled('browse-footprint')){
$page_type = '/videoDetail';
$fullUrl = yzAppFullUrl('/videoDetail',['vid' => $videoId]);
if (request()->type == 2) {
$page_type = 'packageC/video_goods/VideoDetail/VideoDetail';
$fullUrl = 'packageC/video_goods/VideoDetail/VideoDetail?vid' . $videoId;
}
$temporary_data = [
'member_id' => \YunShop::app()->getMemberId(),//会员id
'page_type' => $page_type,//页面类型
'page_type_id' => $videoId,//页面类型ID
'port_type' => request()->type,//端口类型
'pc' => request()->input('pc') ?: 0,//参数为1 & type=5 为PC端
'ip' => Utils::getClientIp() ?: 0,
'ip_name' => '未知',//TODO 需要引库
'i' => \YunShop::app()->uniacid,
'cookie' => substr(md5(request()->header()['cookie'][0]), 8, 16),
'full-url' => $fullUrl ?: '',//完整URL链接
'mid' => (int)request()->mid ?: 0,//完整URL链接
'created_at' => time()
];
$json = json_encode($temporary_data);
Redis::lpush('plugin-browse_footprint:middleware', $json);
}
}
// 发布 plugin.video-share.frontend.video.sub
public function sub()
{
$uid = \YunShop::app()->getMemberId();
$goodsId = intval(request()->goods_id);
$title = request()->title;
$video = request()->video;
$file_name = request()->file_name;
$cover = request()->cover;
$ingress = request()->ingress;
$category_id = request()->category_id;
$set = \Setting::get('plugin.video-share');
if($set['default_title']) $title = $set['default_title'];
if($set['default_cate']) $category_id = $set['default_cate'];
if (!$title) {
return $this->errorJson('标题参数错误');
}
if (!$video && $set['must_video'] == 1) {
return $this->errorJson('视频参数错误');
}
if (!$cover) {
return $this->errorJson('封面参数错误');
}
if(!isset($set['select_goods']) || $set['select_goods']){
//需要选择商品(todo 未设置信息的时候默认需要选择商品)
if (!$goodsId) {
return $this->errorJson('请选择商品');
}
}else{//已关闭选择商品
$goodsId = 0;
}
if (!$set['is_review']){//判断是否开启审核
$status = 1;
}else{
$status = 0;
}
if ($ingress) {
$check_result = (new MiniFileLimitService())->checkMsg($title);
if ($check_result['errcode'] != 0) {
return $this->errorJson('输入信息含有违法违规内容');
}
}
$data = [
'goods_id' => $goodsId,
'uniacid' => \YunShop::app()->uniacid,
'uid' => $uid,
'title' => $title,
'video' => $video?:'',
'file_name' => $file_name?:'',
'cover' => $cover,
'share_num' => 0,
'like_num' => 0,
'order_price' => 0,
'amount_total' => 0,
'status' => $status,
'category_id'=>empty($category_id) ? 0 : $category_id
];
if (request()->is_crm) {
$data['is_crm'] = 1;
}
$video = VideoShareGoods::create($data);
event(new VideoPublishEvent($video));
return $this->successJson('成功', [
'style_type' => $set['list_style'] == 2 ? '2' : '1' //前端列表样式
]);
}
// 保存视频 暂时不用
public function upload()
{
$file = request()->file('file');
if (!$file) {
return $this->errorJson('请传入正确参数.');
}
if ($file->isValid()) {
// 获取文件相关信息
$originalName = $file->getClientOriginalName(); // 文件原名
$realPath = $file->getRealPath(); //临时文件的绝对路径
$ext = $file->getClientOriginalExtension();
$newOriginalName = md5($originalName . str_random(6)) . '.' . $ext;
\Storage::disk('image')->put($newOriginalName, file_get_contents($realPath));
return $this->successJson('上传成功', [
'img' => \Storage::disk('image')->url($newOriginalName),
]);
}
}
// 查看
public function getVideoGoods()
{
$uid = \YunShop::app()->getMemberId();
$videoGoodsId = intval(request()->video_goods_id);
if (!$videoGoodsId) {
return $this->errorJson('参数错误[video_goods_id=null]');
}
$videoGoods = VideoShareGoods::with([
'member' => function ($member) {
$member->select(['avatar', 'uid', 'nickname']);
},
'goods' => function ($goods) {
$goods->select(['id', 'title', 'price', 'market_price', 'thumb','has_option']);
}
])->where('status', 1)
->find($videoGoodsId);
if (!$videoGoods) {
return $this->errorJson('未找到信息');
}
$businessCard = app('plugins')->isEnabled('business-card');
$videoGoods->member->business_card = 0;
if ($businessCard) {
$cardModel = BusinessCardModel::select()
->where('member_id', $videoGoods->member->uid)
->first();
if ($cardModel) {
$videoGoods->member->business_card = $cardModel->id;
}
}
$videoGoods->goods->thumb = yz_tomedia($videoGoods->goods->thumb);
$videoGoods->cover = yz_tomedia($videoGoods->cover);
$videoGoods->video = yz_tomedia($videoGoods->video);
$videoGoods->member_video_num = count(VideoShareGoods::where('uid', $videoGoods->member->uid)->whereNotNull('video')->get());
$videoGoods->comment_num = Comment::where('video_id',$videoGoodsId)->count();
// 会员点赞 默认可以点赞
$videoGoods->member_like = 1;
$likeLog = LikeLog::select()
->where('uid', $uid)
->where('vid', $videoGoodsId)
->first();
if ($likeLog) {
$videoGoods->member_like = 0;
}
//投放广告视频插件-观看赚/投放广告按钮
$videoGoods->ad_serving_button_status = [];
if (app('plugins')->isEnabled('ad-serving')) {
$videoGoods->ad_serving_button_status = \Yunshop\AdServing\services\PrivilegeValidateService::checkButton(request()->video_goods_id,2);
}
//判断当前会员是否在直播
$is_live = 0;
if (app('plugins')->isEnabled('room')) {
$member_id = \YunShop::app()->getMemberId();
$anchor = \Yunshop\Room\models\Anchor::uniacid()->where("member_id",$member_id)->first();
if ($anchor) {
$is_live = $anchor['status'];
}
}
//判断是否关注
$follow = Follow::uniacid()->where("member_id",$videoGoods->uid)->where("follow_member_id",$uid)->count();
return $this->successJson('成功', [
'video_goods' => $videoGoods,
'is_live' => $is_live,
'is_follow' => ($follow>0) ? 1 : 0
]);
}
// 增加分享次数 plugin.video-share.frontend.video.share
public function share()
{
$videoGoodsId = intval(request()->video_goods_id);
if (!$videoGoodsId) {
return $this->errorJson('参数错误[video_goods_id=null]');
}
$videoGoods = VideoShareGoods::find($videoGoodsId);
if (!$videoGoods) {
return $this->errorJson('未找到信息');
}
$videoGoods->share_num += 1;
$videoGoods->save();
return $this->successJson('分享次数增加成功',$videoGoods->share_num);
}
// 增加点赞次数
public function like()
{
$uid = \YunShop::app()->getMemberId();
$videoGoodsId = intval(request()->video_goods_id);
if (!$videoGoodsId) {
return $this->errorJson('参数错误[video_goods_id=null]');
}
$videoGoods = VideoShareGoods::find($videoGoodsId);
if (!$videoGoods) {
return $this->errorJson('未找到信息');
}
$likeLog = LikeLog::select()
->where('uid', $uid)
->where('vid', $videoGoodsId)
->first();
if ($likeLog) {
$videoGoods->like_num -= 1;
$likeLog->delete();
$msg = '点赞取消成功';
} else {
$videoGoods->like_num += 1;
LikeLog::create([
'uniacid' => \YunShop::app()->uniacid,
'uid' => $uid,
'vid' => $videoGoodsId
]);
$msg = '点赞成功';
}
$videoGoods->save();
return $this->successJson($msg, [
]);
}
/**
* 会员发布的视频列表
*/
public function myVideo()
{
$status = request()->status;
$uid = request()->v_uid ?: \Yunshop::app()->getMemberId();
// $set = \Setting::get('plugin.video-share');
$videos = VideoShareGoods::myVideo($uid)->where(['status' => $status])
->orderBy('id', 'desc')
->paginate(20);
foreach ($videos as &$item){
$item['goods']['thumb'] = yz_tomedia($item['goods']['thumb']);
$item['cover'] = yz_tomedia($item['cover']);
$item['video'] = yz_tomedia($item['video']);
// 会员点赞 默认可以点赞
$item->member_like = 1;
$likeLog = LikeLog::select()
->where('uid', $uid)
->where('vid', $item->id)
->first();
if ($likeLog) {
$item->member_like = 0;
}
//视频数量
$item->member_video_num = count(VideoShareGoods::where('uid', $uid)->whereNotNull('video')->get());
//评论数量
$item->comment_num = Comment::uniacid()->where(['video_id' => $item->id, 'comment_id' => 0])->count();
}
$release = VideoShareGoods::myVideo($uid)->where('status',1)->count();//发布总数
$review = VideoShareGoods::myVideo($uid)->where('status',0)->count();//审核总数
$member = Member::uniacid()->select('uid', 'avatar', 'nickname')->where('uid',$uid)->first();
return $this->successJson('成功', [
'video' => $videos,
'member' => $member,
'release' => $release,
'review' => $review,
'is_open' => \Setting::get('plugin.video-share')['is_review'] ?: 0
]);
}
public function del()
{
$id = request()->id;
$member_id = \Yunshop::app()->getMemberId();
//进行文件删除
$videoGoods = VideoShareGoods::where('id', $id)->where('uid', $member_id)->first();
if (empty($videoGoods)) {
return $this->errorJson('视频不存在或已删除');
}
Storage::disk('audios')->delete($videoGoods->file_name);
$result = VideoShareGoods::where('id',$id)->delete();
if ($result) {
return $this->successJson('删除成功');
} else {
return $this->errorJson('数据异常,删除失败');
}
}
public function watchVideo()
{
$video_id = request()->video_id;
$video = VideoShareGoods::uniacid()->where('id', $video_id)->first();
//触发观看视频事件
//event((new VideoWatchEvent($video_id, \Yunshop::app()->getMemberId())));
if (!$video || !app('plugins')->isEnabled('video-share-points')) {
return $this->successJson('ok', ['is_show' => false]);
}
//观看奖励
$res = (new \Yunshop\VideoSharePoints\common\services\VideoWatchService)->handle(\Yunshop::app()->getMemberId(), $video_id);
if ($res['judge'] && $res['points'] > 0) {
return $this->successJson('ok', ['is_show' => true, 'points' => $res['points'], 'point_name' => $res['name'], 'days' => $res['days'] ?: 0]);
} else {
return $this->successJson('ok', ['is_show' => false]);
}
}
}