Exemplo n.º 1
0
 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]);
 }
Exemplo n.º 2
0
 public static function notify($idArr = array(), $body, $type, $to_all = 0, $is_system = 0)
 {
     $currentId = auth()->id();
     if (!$currentId) {
         return;
     }
     $data = $notifiedUidArr = [];
     $now = \Carbon\Carbon::now();
     if ($to_all) {
         $data = ['user_id' => 0, 'body' => $body, 'type' => $type, 'to_all' => $to_all, 'is_system' => $is_system, 'created_at' => $now, 'updated_at' => $now];
     } elseif (!empty($idArr)) {
         $idArr = array_unique($idArr);
         foreach ($idArr as $id) {
             if ($id == $currentId) {
                 return;
             }
             $data[] = ['user_id' => $id, 'body' => $body, 'type' => $type, 'to_all' => $to_all, 'is_system' => $is_system, 'created_at' => $now, 'updated_at' => $now];
             $notifiedUidArr[] = $id;
         }
     }
     if (!empty($data)) {
         Notify::insert($data);
         if ($to_all) {
             \DB::table('users')->increment('notice_count');
         } elseif ($notifiedUidArr) {
             User::whereIn('id', $notifiedUidArr)->increment('notice_count');
         }
     }
 }
Exemplo n.º 3
0
 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;
     }
 }
Exemplo n.º 4
0
 public function getTemplate($event_id, $channel_id)
 {
     $notify = Notify::where('event_id', '=', $event_id)->where('channel_id', $channel_id)->where('status', '=', 1)->firstOrFail();
     if (!empty($notify)) {
         $result = NotifyTemplate::findOrFail($notify['template_id']);
         $template = $result->path;
         return $template;
     }
     return false;
 }
Exemplo n.º 5
0
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($id)
 {
     Notify::whereId($id)->delete();
     return response()->json(200);
 }
Exemplo n.º 6
0
 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']);
     }
 }