改进 暂停逻辑
This commit is contained in:
parent
3b1e919d4b
commit
7ef301ceff
@ -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);
|
||||
}
|
||||
|
@ -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());
|
||||
|
@ -27,7 +27,9 @@ class Host extends Model
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'configuration' => 'array'
|
||||
// 'configuration' => 'array',
|
||||
'suspended_at' => 'datetime',
|
||||
|
||||
];
|
||||
|
||||
|
||||
@ -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'));
|
||||
// });
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('hosts', function (Blueprint $table) {
|
||||
//
|
||||
$table->timestamp('suspended_at')->nullable()->index()->after('status');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('hosts', function (Blueprint $table) {
|
||||
//
|
||||
});
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user