178 lines
4.2 KiB
PHP
178 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Module;
|
|
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Auth\Authenticatable;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Laravel\Lumen\Auth\Authorizable;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use GuzzleHttp\Exception\ConnectException;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
|
|
|
|
class Module extends Model implements AuthenticatableContract, AuthorizableContract
|
|
{
|
|
use Authenticatable, Authorizable, HasFactory;
|
|
|
|
protected $table = 'modules';
|
|
|
|
// primary key
|
|
public $incrementing = false;
|
|
protected $keyType = 'string';
|
|
public $timestamps = false;
|
|
|
|
protected $fillable = [
|
|
'id',
|
|
'name',
|
|
'api_token'
|
|
];
|
|
|
|
protected $hidden = [
|
|
'api_token',
|
|
'url'
|
|
];
|
|
|
|
|
|
public function remoteHost($host_id, $func, $requests)
|
|
{
|
|
$http = Http::remote($this->api_token, $this->url);
|
|
$response = $http->post("hosts/{$host_id}/functions/" . $func, $requests);
|
|
|
|
$json = $response->json();
|
|
$status = $response->status();
|
|
|
|
return [$json, $status];
|
|
}
|
|
|
|
public function remote($func, $requests)
|
|
{
|
|
$http = Http::remote($this->api_token, $this->url);
|
|
$response = $http->post('functions/' . $func, $requests);
|
|
|
|
$json = $response->json();
|
|
$status = $response->status();
|
|
|
|
return [$json, $status];
|
|
}
|
|
|
|
|
|
// post, get, patch, delete 等请求
|
|
public function remoteRequest($method, $path, $requests)
|
|
{
|
|
$user = auth('api')->user();
|
|
|
|
$http = Http::remote($this->api_token, $this->url)
|
|
->accept('application/json');
|
|
|
|
// add Headers
|
|
$http->withHeaders([
|
|
'X-User' => $user->id
|
|
]);
|
|
|
|
$requests['user_id'] = $user->id;
|
|
|
|
|
|
if ($method == 'post') {
|
|
// add user to requests
|
|
$requests['user'] = $user;
|
|
}
|
|
|
|
$response = $http->{$method}("functions/{$path}", $requests);
|
|
|
|
$json = $response->json();
|
|
|
|
$status = $response->status();
|
|
|
|
return [
|
|
'body' => $response->body(),
|
|
'json' => $json,
|
|
'status' => $status
|
|
];
|
|
}
|
|
|
|
public function moduleRequest($method, $path, $requests)
|
|
{
|
|
$module_id = auth('remote')->id();
|
|
|
|
$http = Http::remote($this->api_token, $this->url)
|
|
->accept('application/json');
|
|
|
|
$http->withHeaders([
|
|
'X-Module' => $module_id
|
|
]);
|
|
|
|
$requests['module_id'] = $module_id;
|
|
|
|
$response = $http->{$method}("exports/{$path}", $requests);
|
|
|
|
$json = $response->json();
|
|
|
|
$status = $response->status();
|
|
return [
|
|
'body' => $response->body(),
|
|
'json' => $json,
|
|
'status' => $status
|
|
];
|
|
}
|
|
|
|
|
|
|
|
public function remotePost($path = '', $data = [])
|
|
{
|
|
$http = Http::remote($this->api_token, $this->url);
|
|
$response = $http->post($path, $data);
|
|
|
|
$json = $response->json();
|
|
$status = $response->status();
|
|
|
|
return [$json, $status];
|
|
}
|
|
|
|
public function check($module_id = null)
|
|
{
|
|
if ($module_id) {
|
|
$module = Module::find($module_id);
|
|
} else {
|
|
$module = $this;
|
|
}
|
|
|
|
try {
|
|
$http = Http::remote($module->api_token, $module->url);
|
|
// dd($module->url);
|
|
$response = $http->get('remote');
|
|
} catch (ConnectException $e) {
|
|
Log::error($e->getMessage());
|
|
}
|
|
|
|
if ($response->status() == 200) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
// get cached modules
|
|
public static function cached_modules()
|
|
{
|
|
return Cache::remember('modules', 600, function () {
|
|
return Module::all();
|
|
});
|
|
}
|
|
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
static::creating(function ($model) {
|
|
// if local
|
|
if (!app()->environment('local')) {
|
|
$model->api_token = Str::random(60);
|
|
}
|
|
});
|
|
}
|
|
}
|