diff --git a/app/Admin/Controllers/NoticeController.php b/app/Admin/Controllers/NoticeController.php new file mode 100644 index 0000000..646a0e9 --- /dev/null +++ b/app/Admin/Controllers/NoticeController.php @@ -0,0 +1,30 @@ +validate(request(), [ + 'title' => 'required|string', + 'content' => 'required|string' + ]); + + $notice = \App\Notice::create(request(['title', 'content'])); + dispatch(new \App\Jobs\SendMessage($notice)); + + return redirect("/admin/notices"); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/NoticeController.php b/app/Http/Controllers/NoticeController.php new file mode 100644 index 0000000..8fc8bea --- /dev/null +++ b/app/Http/Controllers/NoticeController.php @@ -0,0 +1,19 @@ +notices; + + return view('notice/index', compact('notices')); + } +} diff --git a/app/Jobs/SendMessage.php b/app/Jobs/SendMessage.php new file mode 100644 index 0000000..71d9d44 --- /dev/null +++ b/app/Jobs/SendMessage.php @@ -0,0 +1,41 @@ +notice = $notice; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + // 通知每个用户系统消息 + $users = \App\User::all(); + foreach ($users as $user) { + $user->addNotice($this->notice); + } + } +} diff --git a/app/Notice.php b/app/Notice.php new file mode 100644 index 0000000..2689360 --- /dev/null +++ b/app/Notice.php @@ -0,0 +1,10 @@ +stars()->where('star_id', $uid)->count(); } + + // 用户收到的通知 + public function notices() + { + return $this->belongsToMany(\App\Notice::class, 'user_notice', 'user_id' + , 'notice_id')->withPivot(['user_id', 'notice_id']); + } + + // 给用户增加通知 + public function addNotice($notice) + { + return $this->notices()->save($notice); + } } \ No newline at end of file diff --git a/database/migrations/2019_11_04_110156_create_notice_table.php b/database/migrations/2019_11_04_110156_create_notice_table.php new file mode 100644 index 0000000..b0bd9c3 --- /dev/null +++ b/database/migrations/2019_11_04_110156_create_notice_table.php @@ -0,0 +1,42 @@ +increments('id'); + $table->string('title', 50)->default(""); + $table->string('content', 1000)->default(""); + $table->timestamps(); + }); + + Schema::create('user_notice', function (Blueprint $table) { + $table->increments('id'); + $table->integer('user_id')->default(0); + $table->integer('notice_id')->default(0); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + Schema::dropIfExists('notices'); + Schema::dropIfExists('user_notice'); + } +} diff --git a/database/migrations/2019_11_04_140746_create_jobs_table.php b/database/migrations/2019_11_04_140746_create_jobs_table.php new file mode 100644 index 0000000..acd2597 --- /dev/null +++ b/database/migrations/2019_11_04_140746_create_jobs_table.php @@ -0,0 +1,38 @@ +bigIncrements('id'); + $table->string('queue'); + $table->longText('payload'); + $table->unsignedTinyInteger('attempts'); + $table->unsignedInteger('reserved_at')->nullable(); + $table->unsignedInteger('available_at'); + $table->unsignedInteger('created_at'); + + $table->index(['queue', 'reserved_at']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('jobs'); + } +} diff --git a/resources/views/admin/notice/create.blade.php b/resources/views/admin/notice/create.blade.php new file mode 100644 index 0000000..89d88cf --- /dev/null +++ b/resources/views/admin/notice/create.blade.php @@ -0,0 +1,42 @@ +@extends("admin.layout.main") + +@section("content") + +
+ +
+
+
+ + +
+
+

增加通知

+
+ + +
+ {{csrf_field()}} +
+
+ + +
+
+
+
+ + +
+
+ + +
+
+
+
+
+
+@endsection \ No newline at end of file diff --git a/resources/views/admin/notice/index.blade.php b/resources/views/admin/notice/index.blade.php new file mode 100644 index 0000000..f29ccf6 --- /dev/null +++ b/resources/views/admin/notice/index.blade.php @@ -0,0 +1,39 @@ +@extends('admin.layout.main') +@section("content") + + +
+ +
+
+
+ +
+

通知列表

+
+ 增加通知 + +
+ + + + + + + + @foreach($notices as $notice) + + + + + + @endforeach + +
#通知名称操作
{{$notice->id}}{{$notice->title}}
+
+
+
+
+
+ +@endsection \ No newline at end of file diff --git a/resources/views/notice/index.blade.php b/resources/views/notice/index.blade.php new file mode 100644 index 0000000..f3f9c66 --- /dev/null +++ b/resources/views/notice/index.blade.php @@ -0,0 +1,16 @@ +@extends("layout.main") + +@section("content") + +
+ @foreach($notices as $notice) +
+ + +

{{$notice->content}}

+
+ @endforeach +
+ + +@endsection \ No newline at end of file diff --git a/routes/admin.php b/routes/admin.php index 9906074..7718a19 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -47,5 +47,10 @@ Route::resource('topics', '\App\Admin\Controllers\TopicController', ['only' => ['index', 'create' , 'store', 'destroy']]); }); + + Route::group(['middleware' => 'can:notice'], function () { + Route::resource('notices', '\App\Admin\Controllers\NoticeController', ['only' => ['index', 'create' + , 'store']]); + }); }); }); \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 040f553..737d7e3 100644 --- a/routes/web.php +++ b/routes/web.php @@ -60,6 +60,9 @@ // 投稿 Route::post('/topic/{topic}/submit', '\App\Http\Controllers\TopicController@submit'); +// 通知 +Route::get('/notices', '\App\Http\Controllers\NoticeController@index'); + include_once('admin.php');