From 7ef301ceffc498bfa09030980d3a65188a879184 Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Mon, 29 Aug 2022 18:59:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=20=E6=9A=82=E5=81=9C?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Remote/Host/HostController.php | 9 ++- app/Jobs/Remote/Host.php | 4 +- app/Models/Host.php | 72 +++++++++++-------- ...094841_add_suspended_at_to_hosts_table.php | 33 +++++++++ 4 files changed, 83 insertions(+), 35 deletions(-) create mode 100644 database/migrations/2022_08_29_094841_add_suspended_at_to_hosts_table.php diff --git a/app/Http/Controllers/Remote/Host/HostController.php b/app/Http/Controllers/Remote/Host/HostController.php index f64e51c..297cea8 100644 --- a/app/Http/Controllers/Remote/Host/HostController.php +++ b/app/Http/Controllers/Remote/Host/HostController.php @@ -85,7 +85,7 @@ public function update(Request $request, Host $host) // 'managed_price' => 'sometimes|numeric|nullable', // 如果是立即扣费 - 'cost_once' => 'sometimes|boolean|nullable', + 'cost_once' => 'sometimes|numeric|nullable', ]); // if has cost_once @@ -95,7 +95,12 @@ public function update(Request $request, Host $host) return $this->updated($request->cost_once); } - $host->update($request->all()); + $update = $request->all(); + // module_id 不能被更新 + unset($update['module_id']); + unset($update['user_id']); + + $host->update($update); return $this->updated($host); } diff --git a/app/Jobs/Remote/Host.php b/app/Jobs/Remote/Host.php index 05c0b1e..ffe2070 100644 --- a/app/Jobs/Remote/Host.php +++ b/app/Jobs/Remote/Host.php @@ -42,8 +42,8 @@ public function handle() $http = Http::remote($this->host->module->api_token, $this->host->module->url); switch ($this->type) { - case 'put': - $response = $http->put('hosts/' . $this->host->id, $this->host->toArray()); + case 'patch': + $response = $http->patch('hosts/' . $this->host->id, $this->host->toArray()); break; case 'post': $response = $http->post('hosts', $this->host->toArray()); diff --git a/app/Models/Host.php b/app/Models/Host.php index 2b6cb18..eb53586 100644 --- a/app/Models/Host.php +++ b/app/Models/Host.php @@ -27,7 +27,9 @@ class Host extends Model ]; protected $casts = [ - 'configuration' => 'array' + // 'configuration' => 'array', + 'suspended_at' => 'datetime', + ]; @@ -89,7 +91,7 @@ public function cost($price = null) } else { $user = Cache::put($cache_key, $this->user, now()->addDay()); } - + // Log::debug($user); @@ -102,7 +104,7 @@ public function cost($price = null) } - $user->drops -= $this->price; + $user->drops -= (int) $this->price; // update cache Cache::put($cache_key, $user, now()->addDay()); @@ -119,40 +121,48 @@ public function cost($price = null) * @deprecated */ // on create - // protected static function boot() - // { - // parent::boot(); + protected static function boot() + { + parent::boot(); - // // static::creating(function ($model) { - // // // if sanctum - // // // if (auth('sanctum')->check()) { - // // // $model->user_id = auth('sanctum')->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'); - // // // } + // static::creating(function ($model) { + // // if sanctum + // // if (auth('sanctum')->check()) { + // // $model->user_id = auth('sanctum')->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; + // // // set price to 0 + // // $model->price = 0; - // // // $model->load('module'); - // // // $model->module->load(['provider', 'module']); + // // $model->load('module'); + // // $model->module->load(['provider', 'module']); - // // // add to queue + // // add to queue - // // }); + // }); - // // when Updated - // static::updated(function ($model) { - // dispatch(new \App\Jobs\Remote\Host($model, 'put')); - // }); + static::updating(function ($model) { + if ($model->status == 'suspended') { + $model->suspended_at = now(); + } else if ($model->status == 'running') { + $model->suspended_at = null; + } + }); - // // when delete - // static::deleting(function ($model) { - // // return false; + // when Updated + static::updated(function ($model) { + dispatch(new \App\Jobs\Remote\Host($model, 'patch')); + }); - // dispatch(new \App\Jobs\Remote\Host($model, 'delete')); - // }); - // } + // // when delete + // static::deleting(function ($model) { + // // return false; + + // // dispatch(new \App\Jobs\Remote\Host($model, 'delete')); + // }); + } } diff --git a/database/migrations/2022_08_29_094841_add_suspended_at_to_hosts_table.php b/database/migrations/2022_08_29_094841_add_suspended_at_to_hosts_table.php new file mode 100644 index 0000000..dbce206 --- /dev/null +++ b/database/migrations/2022_08_29_094841_add_suspended_at_to_hosts_table.php @@ -0,0 +1,33 @@ +timestamp('suspended_at')->nullable()->index()->after('status'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('hosts', function (Blueprint $table) { + // + }); + } +};