Skip to content
This repository was archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
feat: 新增 slack 通知
Browse files Browse the repository at this point in the history
  • Loading branch information
flamelin committed Apr 17, 2017
1 parent 7907844 commit 803e636
Show file tree
Hide file tree
Showing 9 changed files with 602 additions and 283 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

# notify
SLACK_WEBHOOK_URL=
154 changes: 83 additions & 71 deletions app/Http/Controllers/LogsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,88 +4,100 @@

use App\Http\Requests\LogFormRequest;
use App\Log;
use App\Notifications\LogCreated;
use Illuminate\Http\Request;
use Auth;

// use App\Http\Requests;

class LogsController extends Controller {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
$logs = Log::whereUserId(auth::user()->id)->orderBy('id', 'desc')->paginate(20);
class LogsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$logs = Log::whereUserId(auth::user()->id)->orderBy('id', 'desc')->paginate(20);
return view('logs.index', compact('logs'));
}
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{

return view('logs.create');
}
return view('logs.create');
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(LogFormRequest $request) {
$log = new Log([
'done' => $request->done,
'future' => $request->future,
'date' => $request->date,
'user_id' => Auth::user()->id
]);
$log->save();
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(LogFormRequest $request)
{
$log = new Log([
'done' => $request->done,
'future' => $request->future,
'date' => $request->date,
'user_id' => auth()->user()->id,
]);
$log->save();

return redirect('/log/create')->with('status', '工作日誌新增成功');
}
if (!empty(env('SLACK_WEBHOOK_URL'))) {
auth()->user()->notify(new LogCreated($log));
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id) {
//
}
return redirect('/log/create')->with('status', '工作日誌新增成功');
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id) {
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id) {
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
5 changes: 3 additions & 2 deletions app/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class Log extends Model
{
protected $fillable = ['user_id', 'date', 'done', 'future'];

public function user() {
return $this->belongsTo('App\User');
public function user()
{
return $this->belongsTo('App\User');
}
}
52 changes: 52 additions & 0 deletions app/Notifications/LogCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Notifications;

use App\Log;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;

class LogCreated extends Notification
{
use Queueable;
protected $log = null;

/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Log $log)
{
$this->log = $log;
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['slack'];
}

public function toSlack($notifable)
{
$log = $this->log;

return (new SlackMessage)
->success()
->content(sprintf('%s 的工作日誌', $log->user->name))
->attachment(function ($attachment) use ($log) {
$attachment
->title($log->created_at->format('Y-m-d'))
->fields([
'今天完成事項' => $log->done,
'明日預計事項' => $log->future,
]);
});
}
}
49 changes: 29 additions & 20 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,39 @@
namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Zizaco\Entrust\Traits\EntrustUserTrait;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
use EntrustUserTrait;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword;
use EntrustUserTrait;
use Notifiable;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function routeNotificationForSlack()
{
return env('SLACK_WEBHOOK_URL');
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.3.*",
"zizaco/entrust": "5.2.x-dev"
"zizaco/entrust": "5.2.x-dev",
"guzzlehttp/guzzle": "^6.2"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
Expand Down
Loading

0 comments on commit 803e636

Please sign in to comment.