增加 基本 Event
This commit is contained in:
parent
4f5073e6e1
commit
9ccc08c2b7
29
app/Events/ServerEvent.php
Normal file
29
app/Events/ServerEvent.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use Illuminate\Broadcasting\Channel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class ServerEvent extends Event implements ShouldBroadcast
|
||||
{
|
||||
|
||||
public array $servers;
|
||||
|
||||
public string $type = 'servers.updated';
|
||||
|
||||
public function __construct($servers)
|
||||
{
|
||||
$this->servers = $servers;
|
||||
}
|
||||
|
||||
public function broadcastOn()
|
||||
{
|
||||
return new Channel('servers');
|
||||
}
|
||||
|
||||
public function broadcastAs()
|
||||
{
|
||||
return 'servers';
|
||||
}
|
||||
}
|
@ -2,34 +2,52 @@
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Broadcasting\Channel;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class UserEvent extends Event implements ShouldBroadcastNow
|
||||
{
|
||||
use SerializesModels;
|
||||
|
||||
public User $user;
|
||||
public $user_id;
|
||||
public string $type = 'ping';
|
||||
public $message;
|
||||
public $module;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(User $user)
|
||||
public function __construct($user_id, $type, $message)
|
||||
{
|
||||
//
|
||||
$this->user = $user;
|
||||
$this->user_id = $user_id;
|
||||
|
||||
$this->type = $type;
|
||||
|
||||
// if message is model
|
||||
if (is_object($message)) {
|
||||
$this->message = $message->toArray();
|
||||
} else {
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
// if (Auth::check()) {
|
||||
|
||||
if (Auth::guard('remote')->check()) {
|
||||
$this->module = Auth::guard('remote')->user();
|
||||
} else {
|
||||
$this->module = null;
|
||||
}
|
||||
}
|
||||
|
||||
public function broadcastOn()
|
||||
{
|
||||
return new PrivateChannel('users.' . $this->user->id);
|
||||
return new PrivateChannel('users.' . $this->user_id);
|
||||
}
|
||||
|
||||
public function broadcastAs()
|
||||
|
@ -2,12 +2,14 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Events\UserEvent;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class ServerController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request) {
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
|
||||
$servers = Cache::get('servers', []);
|
||||
//
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Jobs\Remote;
|
||||
|
||||
use App\Events\ServerEvent;
|
||||
use App\Models\Module\Module;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
@ -12,8 +13,6 @@
|
||||
use GuzzleHttp\Exception\ConnectException;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
// use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||
|
||||
class FetchModule implements ShouldQueue
|
||||
{
|
||||
use InteractsWithQueue, Queueable, SerializesModels;
|
||||
@ -77,6 +76,8 @@ public function handle()
|
||||
]
|
||||
];
|
||||
}, $json['data']['servers']));
|
||||
|
||||
broadcast(new ServerEvent($servers));
|
||||
}
|
||||
// $module->update([
|
||||
// 'data' => $response->json()
|
||||
|
@ -2,15 +2,16 @@
|
||||
|
||||
namespace App\Jobs\Remote\WorkOrder;
|
||||
|
||||
use Log;
|
||||
use App\Events\UserEvent;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
// use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use App\Models\WorkOrder\Reply as WorkOrderReply;
|
||||
use Log;
|
||||
|
||||
class Reply implements ShouldQueue
|
||||
{
|
||||
@ -47,6 +48,9 @@ public function handle()
|
||||
|
||||
if ($response->successful()) {
|
||||
$this->reply->is_pending = false;
|
||||
|
||||
broadcast(new UserEvent($this->reply->workOrder->user_id, 'work-order.replied', $this->reply));
|
||||
|
||||
} else {
|
||||
$this->reply->is_pending = true;
|
||||
}
|
||||
|
@ -2,12 +2,13 @@
|
||||
|
||||
namespace App\Jobs\Remote\WorkOrder;
|
||||
|
||||
use App\Events\UserEvent;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
// use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use App\Models\WorkOrder\WorkOrder as WorkOrderWorkOrder;
|
||||
|
||||
class WorkOrder implements ShouldQueue
|
||||
@ -46,6 +47,8 @@ public function handle()
|
||||
|
||||
if (!$response->successful()) {
|
||||
$this->workOrder->status = 'error';
|
||||
} else {
|
||||
broadcast(new UserEvent($this->workOrder->user_id, 'work-order.updated', $this->workOrder));
|
||||
}
|
||||
|
||||
$this->workOrder->save();
|
||||
|
@ -2,10 +2,11 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Events\UserEvent;
|
||||
use App\Models\Transaction;
|
||||
use App\Models\Module\Module;
|
||||
use App\Models\WorkOrder\WorkOrder;
|
||||
// use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use App\Models\WorkOrder\WorkOrder;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Exceptions\User\BalanceNotEnoughException;
|
||||
@ -139,41 +140,18 @@ public function cost($price = null, $auto = true)
|
||||
|
||||
$transaction->reduceDrops($this->user_id, $this->id, $this->module_id, $auto, $this->price);
|
||||
|
||||
broadcast(new UserEvent($this->user_id, 'balances.drops.reduced', $this->user));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建主机
|
||||
*
|
||||
* 在此之后,所有的主机都将由 module 创建,并且主机的数据仅被用作计费。
|
||||
*
|
||||
* 废弃
|
||||
* @deprecated
|
||||
*/
|
||||
// on create
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
// static::creating(function ($model) {
|
||||
// // if sanctum
|
||||
// // if (auth('api')->check()) {
|
||||
// // $model->user_id = auth('api')->id();
|
||||
// // } else {
|
||||
// // // if user_id is null
|
||||
// // // check user_id is exists
|
||||
// // throw_if(!User::find($model->user_id), CommonException::class, 'user is not exists');
|
||||
// // }
|
||||
|
||||
// // // set price to 0
|
||||
// // $model->price = 0;
|
||||
|
||||
// // $model->load('module');
|
||||
// // $model->module->load(['provider', 'module']);
|
||||
|
||||
// // add to queue
|
||||
|
||||
// });
|
||||
static::created(function ($model) {
|
||||
broadcast(new UserEvent($model->user_id, 'hosts.created', $model));
|
||||
});
|
||||
|
||||
static::updating(function ($model) {
|
||||
if ($model->status == 'suspended') {
|
||||
@ -181,6 +159,8 @@ protected static function boot()
|
||||
} else if ($model->status == 'running') {
|
||||
$model->suspended_at = null;
|
||||
}
|
||||
|
||||
broadcast(new UserEvent($model->user_id, 'hosts.updating', $model));
|
||||
});
|
||||
|
||||
// when Updated
|
||||
@ -188,16 +168,18 @@ protected static function boot()
|
||||
dispatch(new \App\Jobs\Remote\Host($model, 'patch'));
|
||||
|
||||
Cache::forget('user_hosts_' . $model->user_id);
|
||||
|
||||
broadcast(new UserEvent($model->user_id, 'hosts.updated', $model));
|
||||
});
|
||||
|
||||
// // when delete
|
||||
//
|
||||
// static::deleting(function ($model) {
|
||||
// // return false;
|
||||
|
||||
// // dispatch(new \App\Jobs\Remote\Host($model, 'delete'));
|
||||
// broadcast(new UserEvent($model->user_id, 'hosts.deleting', $model));
|
||||
// });
|
||||
|
||||
static::deleted(function ($model) {
|
||||
broadcast(new UserEvent($model->user_id, 'hosts.deleted', $model));
|
||||
|
||||
Cache::forget('user_hosts_' . $model->user_id);
|
||||
});
|
||||
}
|
||||
|
@ -1,38 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Server;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Status extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'server_status';
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'ip',
|
||||
'status',
|
||||
'module_id',
|
||||
];
|
||||
|
||||
// scope
|
||||
public function scopeModule($query)
|
||||
{
|
||||
return $query->where('module_id', auth('remote')->id());
|
||||
}
|
||||
|
||||
|
||||
// when update, check owner
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
static::updating(function ($model) {
|
||||
if ($model->module_id !== auth('remote')->id()) {
|
||||
abort(403, 'Unauthorized action.');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -3,11 +3,12 @@
|
||||
namespace App\Models\User;
|
||||
|
||||
use App\Models\Host;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use App\Events\UserEvent;
|
||||
use App\Exceptions\CommonException;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
class Task extends Model
|
||||
{
|
||||
@ -86,6 +87,12 @@ protected static function boot()
|
||||
}
|
||||
});
|
||||
|
||||
// created
|
||||
static::created(function ($model) {
|
||||
$model->load('host');
|
||||
broadcast(new UserEvent($model->user_id, 'tasks.created', $model));
|
||||
});
|
||||
|
||||
// updateing
|
||||
static::updating(function ($model) {
|
||||
if ($model->progress == 100) {
|
||||
@ -96,11 +103,16 @@ protected static function boot()
|
||||
// updated and delete
|
||||
static::updated(function ($model) {
|
||||
Cache::forget('user_tasks_' . $model->user_id);
|
||||
|
||||
$model->load('host');
|
||||
broadcast(new UserEvent($model->user_id, 'tasks.updated', $model));
|
||||
});
|
||||
|
||||
|
||||
static::deleted(function ($model) {
|
||||
Cache::forget('user_tasks_' . $model->user_id);
|
||||
|
||||
broadcast(new UserEvent($model->user_id, 'tasks.deleted', $model));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,9 @@
|
||||
|
||||
namespace App\Models\WorkOrder;
|
||||
|
||||
use App\Exceptions\CommonException;
|
||||
use App\Models\User;
|
||||
use App\Events\UserEvent;
|
||||
use App\Exceptions\CommonException;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
|
||||
@ -60,6 +61,8 @@ protected static function boot()
|
||||
if (auth('remote')->check()) {
|
||||
$model->user_id = null;
|
||||
$model->workOrder->status = 'replied';
|
||||
|
||||
broadcast(new UserEvent($model->user_id, 'work-order.replied', $model->workOrder));
|
||||
}
|
||||
|
||||
$model->workOrder->save();
|
||||
|
@ -106,6 +106,4 @@
|
||||
$router->delete('/{route:.*}/', $controller);
|
||||
});
|
||||
|
||||
|
||||
$router->get('broadcasting/auth', ['uses' => 'BroadcastController@authenticate']);
|
||||
$router->post('broadcasting/auth', ['uses' => 'BroadcastController@authenticate']);
|
||||
|
@ -5,3 +5,7 @@
|
||||
Broadcast::channel('users.{userId}', function ($user, $userId) {
|
||||
return (int) $user->id === (int) $userId;
|
||||
});
|
||||
|
||||
Broadcast::channel('servers', function () {
|
||||
return true;
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user