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

330 lines
11 KiB
PHP

<?php
namespace Yunshop\VideoShare\frontend;
use app\common\components\ApiController;
use app\common\exceptions\AppException;
use app\common\facades\Setting;
use app\common\models\Member;
use app\common\services\MiniFileLimitService;
use Illuminate\Support\Facades\DB;
use Yunshop\VideoShare\common\model\Comment;
use Yunshop\VideoShare\common\model\Support;
use Yunshop\VideoShare\common\model\VideoShareGoods;
class CommentController extends ApiController
{
public function preAction()
{
parent::preAction();
$set = Setting::get('plugin.video-share');
if ($set['is_open_comment'] != 1) {
throw new AppException('评论功能已关闭');
};
}
/**
* @return \Illuminate\Http\JsonResponse
* 文章评论列表
*/
public function commentList()
{
$video_id = \YunShop::request()->video_id;
$num = Comment::uniacid()
->where(['video_id' => $video_id, 'comment_id' => 0])
->count();
$finder = VideoShareGoods::uniacid()->find($video_id);
if(!$finder)
{
throw new AppException('不存在的视频');
}
$model = Comment::uniacid()
->with(['hasManyReply', 'hasManySupport' => function ($q) {
$q->where(['member_id' => \YunShop::app()->getMemberId(), 'status' => 1]);
}])
->where(['video_id' => $video_id, 'comment_id' => 0])
->orderBy('created_at', 'desc')
->orderBy('id', 'desc')
->paginate('', ['*'], '', (int)request()->page ?: 1);
$model = $model->toArray();
$model['data'] = collect($model['data'])->map(function ($item) use ($finder){
$num = count($item['has_many_reply']);
unset($item['has_many_reply']);
$is_support = 0;
if($item['has_many_support'])
{
$is_support = 1;
}
$can_del = 0;
if($finder->uid == \YunShop::app()->getMemberId() || $item['uid'] == \YunShop::app()->getMemberId()){
$can_del = 1;
}
$collection = collect($item)
->put('reply_num', $num)
->put('append_arr',[])
->put('can_del',$can_del)
->put('is_support',$is_support);
return $collection;
});
$member = Member::where('uid', \YunShop::app()->getMemberId())
->select(['uid', 'avatar', 'nickname'])->first();
return $this->successJson('ok', ['list' => $model, 'member' => $member, 'num' => $num]);
}
public function getVideoType()
{
$video_type = Setting::get('plugin.video-share')['video_type'] ?? 2;
return $this->successJson('ok', $video_type);
}
/**
* @return \Illuminate\Http\JsonResponse
* 查看回复
*/
public function getReplys()
{
$comment_id = \YunShop::request()->comment_id;
$comment = Comment::uniacid()->find($comment_id);
if (!$comment || $comment->comment_id != 0) {
return $this->errorJson('未检测到数据!');
}
$finder = VideoShareGoods::uniacid()->find($comment->video_id);
if(!$finder)
{
throw new AppException('不存在的视频');
}
$model = Comment::uniacid()
->where('comment_id', $comment_id)
->with(['hasManySupport' => function ($q) {
$q->where(['member_id' => \YunShop::app()->getMemberId(), 'status' => 1]);
}])
->orderBy('created_at', 'desc')
->orderBy('id', 'desc')
->paginate(5, ['*'], '', (int)request()->page ?: 1);
$model = $model->toArray();
$model['data'] = collect($model['data'])->map(function ($item) use ($finder){
$is_support = 0;
if($item['has_many_support'])
{
$is_support = 1;
}
$can_del = 0;
if($finder->uid == \YunShop::app()->getMemberId() || $item['uid'] == \YunShop::app()->getMemberId()){
$can_del = 1;
}
$collection = collect($item)
->put('can_del',$can_del)
->put('is_support',$is_support);
return $collection;
});
return $this->successJson('ok', $model);
}
public function createComment()
{
$commentModel = new Comment();
$ingress = request()->ingress;
$member = Member::getUserInfos(\YunShop::app()->getMemberId())->first();
if (!$member) {
return $this->errorJson('评论失败!未检测到会员数据!');
}
$commentStatus = '1';
$comment = [
'video_id' => \YunShop::request()->video_id,
'content' => \YunShop::request()->content,
];
if ($ingress && !empty($comment['content'])) {
$check_result = (new MiniFileLimitService())->checkMsg($comment['content']);
if ($check_result['errcode'] != 0) {
return $this->errorJson('输入信息含有违法违规内容');
}
}
if (!$comment['video_id']) {
return $this->errorJson('评论失败!未检测到文章ID!');
}
if (!$comment['content']) {
return $this->errorJson('评论失败!未检测到评论内容!');
}
if (\YunShop::request()->images) {
$comment['images'] = json_decode(\YunShop::request()->images);
if (is_array($comment['images'])) {
if (count($comment['images']) > 5) {
return $this->errorJson('追加评论失败!评论图片不能多于5张!');
}
$comment['images'] = serialize($comment['images']);
} else {
return $this->errorJson('追加评论失败!评论图片数据不正确!');
}
} else {
$comment['images'] = serialize([]);
}
$commentModel->setRawAttributes($comment);
$commentModel->uniacid = \YunShop::app()->uniacid;
$commentModel->nick_name = $member->nickname;
$commentModel->uid = $member->uid;
$commentModel->head_img_url = $member->avatar;
$commentModel->type = '1';
$res = $this->insertComment($commentModel, $commentStatus);
return $res;
}
public function replyComment()
{
$commentModel = new Comment();
$member = Member::getUserInfos(\YunShop::app()->getMemberId())->first();
if (!$member) {
return $this->errorJson('回复评论失败!未检测到会员数据!');
}
$id = \YunShop::request()->comment_id;
$reply = $commentModel::uniacid()->find($id);
if (!$reply) {
return $this->errorJson('回复评论失败!未检测到评论数据!');
}
$comment = [
'video_id' => $reply->video_id,
'content' => \YunShop::request()->content,
'comment_id' => $reply->comment_id ? $reply->comment_id : $reply->id,
];
if (!$comment['content']) {
return $this->errorJson('回复评论失败!未检测到评论内容!');
}
if (\YunShop::request()->images) {
$comment['images'] = json_decode(\YunShop::request()->images);
if (is_array($comment['images'])) {
if (count($comment['images']) > 5) {
return $this->errorJson('追加评论失败!评论图片不能多于5张!');
}
$comment['images'] = serialize($comment['images']);
} else {
return $this->errorJson('追加评论失败!评论图片数据不正确!');
}
} else {
$comment['images'] = serialize([]);
}
$commentModel->setRawAttributes($comment);
$commentModel->uniacid = \YunShop::app()->uniacid;
$commentModel->uid = $member->uid;
$commentModel->nick_name = $member->nickname;
$commentModel->head_img_url = $member->avatar;
if ($reply->comment_id) {
$commentModel->reply_id = $reply->uid;
$commentModel->reply_name = $reply->nick_name;
} else {
$commentModel->reply_id = 0;
$commentModel->reply_name = null;
}
$commentModel->reply_comment_id = $id;
$commentModel->type = '2';
return $this->insertComment($commentModel);
}
public function del()
{
//主评论不关联删除子评论。没人有权删除别人的评论,一直存在
$id = \YunShop::request()->comment_id;
$comment = Comment::uniacid()->find($id);
$video = VideoShareGoods::uniacid()->find($comment->video_id);
if (!$comment || !$video) {
return $this->errorJson('评论不存在');
}
if($comment->uid != \YunShop::app()->getMemberId() && $video->uid != \YunShop::app()->getMemberId())
{
return $this->errorJson('无删除权限');
}
$comment->delete();
return $this->successJson('ok', '删除成功');
}
public function support()
{
$comment_id = \YunShop::request()->comment_id;
$ReliableModel = Support::uniacid()->where(['comment_id' => $comment_id, 'member_id' => \YunShop::app()->getMemberId()])->first();
DB::beginTransaction();
try {
if ($ReliableModel) {
if ($ReliableModel->status == Support::STATUS_SUCCESS) {
$ReliableModel->status = Support::STATUS_FAIL;
$ReliableModel->save();
$this->changeSupport($comment_id, -1);
} elseif ($ReliableModel->status == Support::STATUS_FAIL) {
$ReliableModel->status = Support::STATUS_SUCCESS;
$ReliableModel->save();
$this->changeSupport($comment_id, 1);
}
} else {
$arr = [
'uniacid' => \YunShop::app()->uniacid,
'comment_id' => $comment_id,
'member_id' => \YunShop::app()->getMemberId(),
'status' => Support::STATUS_SUCCESS
];
$model = new Support;
$model->fill($arr);
$model->save();
$this->changeSupport($comment_id, 1);
}
DB::commit();
return $this->successJson('ok', '成功');
} catch (\Exception $e) {
DB::rollBack();
return $this->errorJson($e->getMessage());
}
}
private function insertComment($commentModel, $commentStatus = '')
{
$validator = $commentModel->validator($commentModel->getAttributes());
if ($validator->fails()) {
//检测失败
return $this->errorJson($validator->messages());
} else {
//数据保存
if ($commentModel->save()) {
return $this->successJson('评论成功!', $commentModel);
} else {
return $this->errorJson('评论失败!');
}
}
}
private function changeSupport($comment_id, $num)
{
$comment = Comment::uniacid()->find($comment_id);
if ($comment) {
$res = bcadd($comment->support_num, $num);
if($res < 0)
{
return;
}
$comment->support_num = $res;
$comment->save();
}
}
}