public function store(Request $request) { $article = Article::find($request->article_id); if (empty($article)) { return response()->json(['status' => 404, 'msg' => '文章不存在']); } if (!$article->comment_status) { return response()->json(['status' => 403, 'msg' => '文章已禁用评论']); } $rules = array('body' => 'required'); $validation = Validator::make($commentData = $request->all(), $rules); if ($validation->fails()) { return response()->json(['status' => 0, 'msg' => '评论内容不能为空']); } $user = Auth::user(); $commentData['user_id'] = $user->id; $parsedComment = parseAt($commentData['body']); $atUidArr = $parsedComment['uidArr']; $commentData['body'] = Markdown::parse($parsedComment['comments']); $comment = Comment::create($commentData); $html = view('articles._comment', compact('comment'))->render(); $comment->article->increment('comment_count'); $user->histories()->create(['type' => 'comment', 'content' => '评论文章《<a href="/article/' . $article->id . '#comment-' . $comment->id . '" target="_blank">' . $article->title . '</a>》']); Notify::notify([$article->user_id], '<a href="/user/' . $user->id . '" target="_blank">' . $user->name . '</a> 评论了您的文章 <a href="/article/' . $article->id . '#comment-' . $comment->id . '" target="_blank">' . $article->title . '</a>', 'comment'); if ($atUidArr) { Notify::notify($atUidArr, '<a href="/user/' . $user->id . '" target="_blank">' . $user->name . '</a> 在文章 <a href="/article/' . $article->id . '#comment-' . $comment->id . '" target="_blank">' . $article->title . '</a> 的评论中提到了您', 'comment'); } return response()->json(['status' => 200, 'msg' => '评论成功', 'html' => $html]); }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(NotifyRequest $request) { $data = $request->all(); $data['is_system'] = 1; if (!$data['to_all'] && !$data['user_id']) { return redirect()->back()->withErrors(['请指定用户ID或发送给所有用户'])->withInput(); } Notify::notify([$request->user_id], $request->body, $request->type, $request->to_all, 1); flash()->message('发送成功!'); return redirect('/admin/notifications/index'); dd($request->all()); }
public function follow($u) { $user = Auth::user(); if ($user->id == $u->id) { return 401; } if (User::isFollowing($user, $u)) { $user->follows()->detach([$u->id]); $user->decrement('follows_count'); $u->decrement('fans_count'); $user->histories()->create(['type' => 'unfollow', 'content' => '取消关注:<strong><a href="/user/' . $u->id . '" target="_blank">' . $u->name . '</a></strong>']); return -1; } else { $user->follows()->attach([$u->id]); $user->increment('follows_count'); $u->increment('fans_count'); $user->histories()->create(['type' => 'follow', 'content' => '关注用户:<strong><a href="/user/' . $u->id . '" target="_blank">' . $u->name . '</a></strong>']); Notify::notify([$u->id], '<a href="/user/' . $user->id . '" target="_blank">' . $user->name . '</a> 关注了您', 'follow'); return 1; } }
public function collect(Request $request) { $article = Article::find($request->id, ['id', 'user_id', 'title']); if (empty($article)) { return response()->json(['status' => 404]); } $user = Auth::user(); if ($collect = Collect::isCollect($user, $article)) { $user->collects()->detach($article->id); $article->decrement('collect_count'); return response()->json(['status' => 200, 'action' => 'down']); } else { $user->collects()->attach($article->id); $article->increment('collect_count'); $user->histories()->create(['type' => 'collect', 'content' => '收藏文章《<a href="/article/' . $article->id . '" target="_blank">' . $article->title . '</a>》']); Notify::notify([$article->user_id], '<a href="/user/' . $user->id . '" target="_blank">' . $user->name . '</a> 收藏了您的文章 <a href="/article/' . $article->id . '" target="_blank">' . $article->title . '</a>', 'collect'); return response()->json(['status' => 200, 'action' => 'up']); } }