love_php/app/Http/Controllers/App/MomentController.php
2026-04-02 09:20:51 +08:00

418 lines
16 KiB
PHP

<?php
namespace App\Http\Controllers\App;
use Actuallymab\LaravelComment\Contracts\Commentable;
use App\Models\App\Comment;
use App\Models\App\Vote;
use App\Models\App\UserVote;
use App\Models\App\VoteOption;
use App\Models\User;
use App\Models\Moment;
use App\Models\MomentTopic;
use App\Models\UnlikeMomentLog;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Contracts\MomentContract;
use App\Models\RankHistory;
use Illuminate\Support\Facades\Log;
class MomentController extends Controller
{
use ResponseJson;
protected $momentCon;
public function __construct(MomentContract $momentCon)
{
$this->momentCon = $momentCon;
}
/**
* 动态列表
* @param Request $request [description]
* @return [type] [description]
*/
public function momentList(Request $request)
{
try {
$moments = $this->momentCon->moments();
if (empty($moments)) throw new \Exception("动态列表失败", 1);
return $this->success('ok', $moments);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('获取动态列表失败,请稍后再试');
}
}
public function moment(Request $request, $moment_id)
{
try {
$moment = $this->momentCon->moment($moment_id);
if(is_array($moment) && $moment['code'] == 1)
return $this->failure($moment['msg']);
if($moment && $moment->user) unset($moment->user->mobile);
if(!empty($moment->topic)){
$moment->topic->quote_num = Moment::where('topic_id', $moment->topic_id)->where(function($sql){
$sql->where('is_audited', 1)
->orWhere(function($query){
$query->where('user_id',auth()->id())->where('is_audited',0);
});
})->count();
}
if (empty($moment)) throw new \Exception("动态详情失败", 1);
if (is_array($moment) && $moment['code']) return $this->failure($moment['msg']);
$comments = $this->momentCon->momentComments($moment->id, $limit=0, $nopage=0);
return $this->success('ok', compact('moment', 'comments'));
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('获取动态详情失败,请稍后再试');
}
}
public function addMoment(Request $request)
{
$user = auth()->user();
if(empty($user)){
return $this->fail('profile', 2, '', '未登录', '你的账号还没登录');
}
try {
$res = $this->momentCon->addMomentV2($user, 'APP');
if (empty($res)) throw new \Exception("发布动态失败", 1);
if (is_array($res) && $res['code']) return $this->failure($res['msg']);
$res['data']['topic_give'] = ['state'=>0,'msg'=>''];
// $moment['topic_give'] = $res['topic_give'];
// if($request->topic_id == 25 && time()< 1622476800){
// $superCount = $user->getSuperCount($user->id,'520_topic_give');
// if($superCount == 0){
// $user->addSuperRank(0, 1,'520_topic_give');
// $superCount = $user->getSuperCount($user->id,'520_topic_give');
// if($superCount > 0){
// $moment['topic_give'] = ['state'=>1,'msg'=>'领取成功'];
// }
// }
// }
return $this->success('ok', $res['data']);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('发布动态详情失败,请稍后再试');
}
}
public function removeMoment(Request $request, $moment_id)
{
try {
$moment = $this->momentCon->deleteMoment($moment_id);
if (empty($moment)) throw new \Exception("删除动态失败", 1);
if ($moment && is_array($moment) && $moment['code']) return $this->failure($moment['msg']);
return $this->success('ok', $moment);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('删除动态失败,请稍后再试');
}
}
/**
* 话题列表
* @param Request $request [description]
* @return [type] [description]
*/
public function topics(Request $request)
{
try {
$topics = $this->momentCon->topics($request);
$user = auth()->user();
foreach ($topics as $topic){
$topic->quote_num = $topic->moment_count;
$topic->is_participated = 0;
if($user){
if($topic->moment_topics_activities){
$topic->is_participated = 0;
$other_type = RankHistory::TOPICACTIVITIESPARTGIVE.'_'.$topic->id;
$superCount = $user->getSuperCount($user->id,RankHistory::TOPICACTIVITIESPARTGIVE,$other_type);
if($superCount>=1){
$topic->is_participated = 1;
}
}
}
}
if (empty($topics)) throw new \Exception("获取话题列表失败", 1);
return $this->success('ok', $topics);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('获取话题列表失败,请稍后再试');
}
}
public function topic(Request $request, $topic_id)
{
try {
$user = auth()->user();
//不喜欢的动态id
$unlike_moment_id = UnlikeMomentLog::where('user_id', $user->id)->pluck('moment_id')->toArray();
//没有用户的动态id
$has_no_user_moment_id = Moment::doesntHave('user')->pluck('id')->toArray();
$topic = MomentTopic::where('id', $topic_id)
->with(['momentTopicsActivities'=>function($q){
$date = date('Y-m-d H:i:s');
$q->where('start_time','<=',$date);
$q->where('end_time','>=',$date);
}])
->withCount(['moment'=>function($query) use ($unlike_moment_id, $has_no_user_moment_id){
$query->where(function ($sql) use($unlike_moment_id, $has_no_user_moment_id){
$sql->where('is_audited', 1)->whereNotIn('id', $unlike_moment_id)->whereNotIn('id', $has_no_user_moment_id);
})
->orWhere(function($sql){
$sql->where('user_id',auth()->id())->where('is_audited',0);
});
}])->first();
$topic->is_participated = 0;
if($user){
$topic->is_participated = 0;
$other_type = RankHistory::TOPICACTIVITIESPARTGIVE.'_'.$topic_id;
$superCount = $user->getSuperCount($user->id,RankHistory::TOPICACTIVITIESPARTGIVE,$other_type);
if($superCount>=1){
$topic->is_participated = 1;
}
}
$topic->quote_num = $topic->moment_count;
return $this->success('ok', $topic);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('获取话题列表失败,请稍后再试');
}
}
/**
* 举报动态
* @param Request $request [description]
* @param [type] $moment_id [description]
* @return [type] [description]
*/
public function complaintMoment(Request $request, $moment_id)
{
try {
$result = $this->momentCon->complaintMoment($moment_id);
if (empty($result)) throw new \Exception("举报动态失败", 1);
if ($result && is_array($result) && $result['code']) return $this->failure($result['msg']);
return $this->success('ok');
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('举报动态失败,请稍后再试');
}
}
public function momentLikers(Request $request, $moment_id)
{
try {
$users = $this->momentCon->momentLikers($moment_id);
if (empty($users)) throw new \Exception("点赞列表失败", 1);
if (is_array($users) && $users['code']) return $this->failure($users['msg']);
return $this->success('ok', $users);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure("获取点赞列表失败,请稍后再试");
}
}
public function commentMoment(Request $request, $moment_id)
{
try {
$is_set = Moment::where('id', $moment_id)->count();
if(!$is_set) return $this->failure('动态已被删除');
if(empty($request->comment)){
return $this->failure('评论不能为空');
}
$result = \CommonUtilsService::checkoutTextArray([$request->comment]);
if($result['code'] == 1){
return $this->failure($result['cause']);
}
$result = $this->momentCon->commentMoment($moment_id);
if(is_array($result) && array_key_exists('code',$result)){
return $this->failure($result['msg']);
}
if($request->reply_id){
$result->reply_id = $request->reply_id;
$result->save();
}
$result->user = User::select('id', 'nickname', 'app_avatar', 'photo','sex')->where('id', $result->commented_id)->first();
$result->replyer = User::select('id', 'nickname', 'app_avatar', 'photo','sex')->where('id', $result->reply_id)->first();
if (empty($result)) throw new \Exception("评论失败", 1);
return $this->success('ok', $result);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('评论动态失败,请稍后再试');
}
}
public function likeMoment(Request $request, $moment_id)
{
try {
$result = $this->momentCon->likeMoment($moment_id);
if (empty($result)) throw new \Exception("点赞失败", 1);
if (is_array($result) && $result['code']) return $this->failure($result['msg']);
$isLkerMoment = $result['isLkerMoment'];
return $this->success('ok', compact('isLkerMoment'));
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('点赞动态失败,请稍后再试');
}
}
public function unlikeMoment(Request $request, $moment_id)
{
try {
$result = $this->momentCon->unlikeMoment($moment_id);
if (empty($result)) throw new \Exception("不感兴趣失败", 1);
return $this->success('ok');
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('操作失败,请稍后再试');
}
}
//评论列表
public function comments(Request $request){
try {
$result = $this->momentCon->comments($request);
return $this->success('ok', $result);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('获取评论失败,请稍后再试');
}
}
//带投票的动态
public function momentVotes(Request $request){
try {
$votes = $this->momentCon->momentVotes($request);
return $this->success('ok', $votes);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('获取动态列表失败,请稍后再试');
}
}
//投票详情
public function vote(Request $request, $vote_id){
try {
$is_set = Vote::where('id', $vote_id)->count();
if(empty($is_set)){
return $this->failure('投票不存在');
}
$vote = $this->momentCon->vote($vote_id);
return $this->success('ok', $vote);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('获取投票详情失败,请稍后再试');
}
}
//投票
public function voteOption(Request $request, $option_id){
try {
$vote_id = VoteOption::where('id', $option_id)->value('vote_id');
$has_vote = UserVote::where('user_id', auth()->id())->where('vote_id', $vote_id)->count();
if($has_vote){
return $this->failure('您已投票');
}
$user_id = auth()->id();
$user_vote = New UserVote();
$user_vote->user_id = $user_id;
$user_vote->vote_id = $vote_id;
$user_vote->option_id = $option_id;
$user_vote->save();
$vote = $this->momentCon->vote($vote_id);
return $this->success('ok', $vote);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('投票失败');
}
}
//点赞投票
public function likeVote(Request $request, $vote_id){
try {
$vote = Vote::find($vote_id);
if(empty($vote)){
return $this->failure('投票不存在');
}
$result = $this->momentCon->likeModel($vote);
if (empty($result)) throw new \Exception("点赞失败", 1);
if (is_array($result) && $result['code']) return $this->failure($result['msg']);
$isLker = $result['isLker'];
return $this->success('ok', compact('isLker'));
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('点赞动态失败,请稍后再试');
}
}
public function commentVote(Request $request, $vote_id)
{
try {
if(empty($request->comment)){
return $this->failure('评论不能为空');
}
\DB::beginTransaction();
$vote = Vote::find($vote_id);
if(empty($vote)){
return $this->failure('投票不存在');
}
$result = $this->momentCon->commentModel($vote);
if($request->reply_id){
$result->reply_id = $request->reply_id;
$result->save();
}
//同步到动态
if($request->sync_to_moment){
$request->merge(['content' => $request->comment, 'vote_id' => $vote_id]);
//发布动态
$moment = $this->momentCon->addMoment(auth()->id());
}
$result->user = User::select('id', 'nickname', 'app_avatar', 'photo','sex')->where('id', $result->commented_id)->first();
$result->replyer = User::select('id', 'nickname', 'app_avatar', 'photo','sex')->where('id', $result->reply_id)->first();
if (empty($result)) throw new \Exception("评论失败", 1);
\DB::commit();
return $this->success('ok', $result);
} catch (\Exception $e) {
\DB::rollback();
$this->getError($e);
return $this->failure('评论动态失败,请稍后再试');
}
}
//点赞评论
public function likeComment(Request $request, $comment_id){
try {
$comment = Comment::find($comment_id);
if(empty($comment)){
return $this->failure('评论不存在');
}
$result = $this->momentCon->likeModel($comment);
if (empty($result)) throw new \Exception("点赞失败", 1);
if (is_array($result) && $result['code']) return $this->failure($result['msg']);
$isLker = $result['isLker'];
return $this->success('ok', compact('isLker'));
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('点赞评论失败,请稍后再试');
}
}
//某个表点赞过的人
public function liker(Request $request){
try {
$result = $this->momentCon->liker($request);
return $this->success('ok', $result);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('获取点赞列表失败,请稍后再试');
}
}
}