Skip to content

Commit c17432a

Browse files
committed
消息通知列表
1 parent bf0ec22 commit c17432a

File tree

9 files changed

+131
-7
lines changed

9 files changed

+131
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Support\Facades\Auth;
7+
8+
class NotificationsController extends Controller
9+
{
10+
public function __construct()
11+
{
12+
$this->middleware('auth');
13+
}
14+
15+
public function index()
16+
{
17+
// 获取登录用户的所有通知
18+
$notifications = Auth::user()->notifications()->paginate(20);
19+
// 标记为已读,未读数量清零
20+
Auth::user()->markAsRead();
21+
return view('notifications.index', compact('notifications'));
22+
}
23+
}

app/Models/User.php

+7
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,11 @@ public function replies()
6464
{
6565
return $this->hasMany(Reply::class);
6666
}
67+
68+
public function markAsRead()
69+
{
70+
$this->notification_count = 0;
71+
$this->save();
72+
$this->unreadNotifications->markAsRead();
73+
}
6774
}

public/css/app.css

+12
Original file line numberDiff line numberDiff line change
@@ -20907,3 +20907,15 @@ body {
2090720907
font-size: 0.9em;
2090820908
color: #b3b3b3;
2090920909
}
20910+
20911+
/* 消息通知 */
20912+
.notification-badge .badge {
20913+
font-size: 12px;
20914+
margin-top: 14px;
20915+
}
20916+
.notification-badge .badge-secondary {
20917+
background-color: #EBE8E8 !important;
20918+
}
20919+
.notification-badge .badge-hint {
20920+
background-color: #d15b47 !important;
20921+
}

public/mix-manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"/js/app.js": "/js/app.js?id=50991537ade597bb0c683d960e912178",
3-
"/css/app.css": "/css/app.css?id=ebad5a98a33c69df5cf3638b95cc6817",
3+
"/css/app.css": "/css/app.css?id=e15b6631a842d3dc5ba74f3e8b8ba30f",
44
"/js/hotkeys.js": "/js/hotkeys.js?id=60b95061e721b635da02b5cc7792204b",
55
"/js/module.js": "/js/module.js?id=1f0206730b87684b7ad654a458319014",
66
"/js/simditor.js": "/js/simditor.js?id=e19ffe3d664de3d4228a9bd3573b4a6b",

resources/sass/app.scss

+16-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ body {
137137
}
138138
}
139139

140-
141140
/* 回复列表 */
142141
.topic-reply {
143142
a {
@@ -149,3 +148,19 @@ body {
149148
color: #b3b3b3;
150149
}
151150
}
151+
152+
/* 消息通知 */
153+
.notification-badge {
154+
.badge {
155+
font-size: 12px;
156+
margin-top: 14px;
157+
}
158+
159+
.badge-secondary {
160+
background-color: #EBE8E8!important;
161+
}
162+
163+
.badge-hint {
164+
background-color: #d15b47 !important;
165+
}
166+
}

resources/views/layouts/_header.blade.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,17 @@
2626
<li class="nav-item"><a class="nav-link" href="{{ route('login') }}">登录</a></li>
2727
<li class="nav-item"><a class="nav-link" href="{{ route('register') }}">注册</a></li>
2828
@else
29-
<li class="nav-item">
30-
<a class="nav-link mt-1 mr-3" href="{{ route('topics.create') }}">
31-
<i class="fa-solid fa-plus"></i>
32-
</a>
33-
</li>
29+
<li class="nav-item">
30+
<a class="nav-link mt-1 mr-3" href="{{ route('topics.create') }}">
31+
<i class="fa-solid fa-plus"></i>
32+
</a>
33+
</li>
34+
35+
<li class="nav-item notification-badge">
36+
<a class="nav-link ms-3 me-3 badge bg-secondary rounded-pill badge-{{ Auth::user()->notification_count > 0 ? 'hint' : 'secondary' }} text-white" href="{{ route('notifications.index') }}">
37+
{{ Auth::user()->notification_count }}
38+
</a>
39+
</li>
3440

3541
<li class="nav-item dropdown">
3642
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@extends('layouts.app')
2+
3+
@section('title', '我的通知')
4+
5+
@section('content')
6+
<div class="container">
7+
<div class="col-md-10 offset-md-1">
8+
<div class="card ">
9+
10+
<div class="card-body">
11+
12+
<h3 class="text-xs-center">
13+
<i class="far fa-bell" aria-hidden="true"></i> 我的通知
14+
</h3>
15+
<hr>
16+
17+
@if ($notifications->count())
18+
19+
<div class="list-unstyled notification-list">
20+
@foreach ($notifications as $notification)
21+
@include('notifications.types._' . Str::snake(class_basename($notification->type)))
22+
@endforeach
23+
24+
{!! $notifications->render() !!}
25+
</div>
26+
27+
@else
28+
<div class="empty-block">没有消息通知!</div>
29+
@endif
30+
31+
</div>
32+
</div>
33+
</div>
34+
</div>
35+
@stop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<li class="d-flex @if ( ! $loop->last) border-bottom @endif">
2+
<div>
3+
<a href="{{ route('users.show', $notification->data['user_id']) }}">
4+
<img class="img-thumbnail mr-3" alt="{{ $notification->data['user_name'] }}" src="{{ $notification->data['user_avatar'] }}" style="width:48px;height:48px;" />
5+
</a>
6+
</div>
7+
8+
<div class="flex-grow-1 ms-2">
9+
<div class="mt-0 mb-1 text-secondary">
10+
<a class="text-decoration-none" href="{{ route('users.show', $notification->data['user_id']) }}">{{ $notification->data['user_name'] }}</a>
11+
评论了
12+
<a class="text-decoration-none" href="{{ $notification->data['topic_link'] }}">{{ $notification->data['topic_title'] }}</a>
13+
14+
{{-- 回复删除按钮 --}}
15+
<span class="meta float-end" title="{{ $notification->created_at }}">
16+
<i class="far fa-clock"></i>
17+
{{ $notification->created_at->diffForHumans() }}
18+
</span>
19+
</div>
20+
<div class="reply-content">
21+
{!! $notification->data['reply_content'] !!}
22+
</div>
23+
</div>
24+
</li>

routes/web.php

+2
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@
3939
Route::get('topics/{topic}/{slug?}', 'TopicsController@show')->name('topics.show');
4040

4141
Route::resource('replies', 'RepliesController', ['only' => ['store', 'destroy']]);
42+
43+
Route::resource('notifications', 'NotificationsController', ['only' => ['index']]);

0 commit comments

Comments
 (0)