Skip to content

Commit

Permalink
feat: 完成仿简书项目的学习,还有几个 bug 留待修复
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangbao05 committed Nov 4, 2019
1 parent c1966c4 commit b3309df
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 0 deletions.
30 changes: 30 additions & 0 deletions app/Admin/Controllers/NoticeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Admin\Controllers;

class NoticeController extends Controller
{
public function index()
{
$notices = \App\Notice::all();
return view('admin/notice/index', compact('notices'));
}

public function create()
{
return view('admin/notice/create');
}

public function store()
{
$this->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");
}
}
19 changes: 19 additions & 0 deletions app/Http/Controllers/NoticeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Controllers;

use App\Topic;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;

class NoticeController extends Controller
{
public function index()
{
// 获取当前用户
$user = \Auth::user();
$notices = $user->notices;

return view('notice/index', compact('notices'));
}
}
41 changes: 41 additions & 0 deletions app/Jobs/SendMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class SendMessage implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

private $notice;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct(\App\Notice $notice)
{
//
$this->notice = $notice;
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// 通知每个用户系统消息
$users = \App\User::all();
foreach ($users as $user) {
$user->addNotice($this->notice);
}
}
}
10 changes: 10 additions & 0 deletions app/Notice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App;

use App\Model;

class Notice extends Model
{
//
}
13 changes: 13 additions & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,17 @@ public function hasStar($uid)
{
return $this->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);
}
}
42 changes: 42 additions & 0 deletions database/migrations/2019_11_04_110156_create_notice_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateNoticeTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
Schema::create('notices', function (Blueprint $table) {
$table->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');
}
}
38 changes: 38 additions & 0 deletions database/migrations/2019_11_04_140746_create_jobs_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateJobsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('jobs', function (Blueprint $table) {
$table->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');
}
}
42 changes: 42 additions & 0 deletions resources/views/admin/notice/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@extends("admin.layout.main")

@section("content")
<!-- Main content -->
<section class="content">
<!-- Small boxes (Stat box) -->
<div class="row">
<div class="col-lg-10 col-xs-6">
<div class="box">

<!-- /.box-header -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">增加通知</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form role="form" action="/admin/notices" method="POST">
{{csrf_field()}}
<div class="box-body">
<div class="form-group">
<label for="exampleInputEmail1">标题</label>
<input type="text" class="form-control" name="title">
</div>
</div>
<div class="box-body">
<div class="form-group">
<label for="exampleInputEmail1">内容</label>
<textarea class="form-control" name="content"></textarea>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary">提交</button>
</div>
</form>
</div>
</div>
</div>
</div>
</section>
@endsection
39 changes: 39 additions & 0 deletions resources/views/admin/notice/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@extends('admin.layout.main')
@section("content")

<!-- Main content -->
<section class="content">
<!-- Small boxes (Stat box) -->
<div class="row">
<div class="col-lg-10 col-xs-6">
<div class="box">

<div class="box-header with-border">
<h3 class="box-title">通知列表</h3>
</div>
<a type="button" class="btn " href="/admin/notices/create">增加通知</a>
<!-- /.box-header -->
<div class="box-body">
<table class="table table-bordered">
<tbody>
<tr>
<th style="width: 10px">#</th>
<th>通知名称</th>
<th>操作</th>
</tr>
@foreach($notices as $notice)
<tr>
<td>{{$notice->id}}</td>
<td>{{$notice->title}}</td>
<td></td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</section>
<!-- /.content -->
@endsection
16 changes: 16 additions & 0 deletions resources/views/notice/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@extends("layout.main")

@section("content")

<div class="col-sm-8 blog-main">
@foreach($notices as $notice)
<div class="blog-post">
<p class="blog-post-meta">{{$notice->title}}</p>

<p>{{$notice->content}}</p>
</div>
@endforeach
</div><!-- /.blog-main -->


@endsection
5 changes: 5 additions & 0 deletions routes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']]);
});
});
});
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');


0 comments on commit b3309df

Please sign in to comment.