2022-08-13 06:04:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models\Module;
|
|
|
|
|
2022-08-14 13:57:56 +00:00
|
|
|
use Illuminate\Support\Str;
|
2022-10-29 05:53:32 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2022-09-08 16:12:02 +00:00
|
|
|
use Illuminate\Auth\Authenticatable;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
use Laravel\Lumen\Auth\Authorizable;
|
2022-09-24 12:01:15 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2022-08-13 06:04:47 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-10-29 05:53:32 +00:00
|
|
|
use GuzzleHttp\Exception\ConnectException;
|
2022-08-14 13:57:56 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2022-09-08 16:12:02 +00:00
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
|
2022-08-13 06:04:47 +00:00
|
|
|
|
2022-09-08 16:12:02 +00:00
|
|
|
class Module extends Model implements AuthenticatableContract, AuthorizableContract
|
2022-08-13 06:04:47 +00:00
|
|
|
{
|
2022-09-08 16:12:02 +00:00
|
|
|
use Authenticatable, Authorizable, HasFactory;
|
2022-08-13 06:04:47 +00:00
|
|
|
|
|
|
|
protected $table = 'modules';
|
|
|
|
|
2022-08-14 13:57:56 +00:00
|
|
|
// primary key
|
|
|
|
public $incrementing = false;
|
|
|
|
protected $keyType = 'string';
|
|
|
|
public $timestamps = false;
|
|
|
|
|
2022-08-13 06:04:47 +00:00
|
|
|
protected $fillable = [
|
2022-08-14 13:57:56 +00:00
|
|
|
'id',
|
2022-08-14 14:00:22 +00:00
|
|
|
'name',
|
2022-08-14 13:57:56 +00:00
|
|
|
'api_token'
|
2022-08-13 06:04:47 +00:00
|
|
|
];
|
2022-08-14 13:57:56 +00:00
|
|
|
|
2022-09-13 02:04:10 +00:00
|
|
|
protected $hidden = [
|
|
|
|
'api_token',
|
2022-10-31 11:03:38 +00:00
|
|
|
'url',
|
|
|
|
'wecom_key'
|
2022-09-13 02:04:10 +00:00
|
|
|
];
|
|
|
|
|
2022-08-19 10:14:23 +00:00
|
|
|
|
2022-08-19 10:31:24 +00:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2022-08-19 10:14:23 +00:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2022-08-29 09:31:08 +00:00
|
|
|
|
|
|
|
// post, get, patch, delete 等请求
|
2022-09-08 17:12:18 +00:00
|
|
|
public function remoteRequest($method, $path, $requests)
|
2022-08-29 09:31:08 +00:00
|
|
|
{
|
2022-09-19 13:39:12 +00:00
|
|
|
$user = auth('api')->user();
|
|
|
|
|
2022-08-29 09:31:08 +00:00
|
|
|
$http = Http::remote($this->api_token, $this->url)
|
2022-09-08 17:12:18 +00:00
|
|
|
->accept('application/json');
|
2022-08-29 09:31:08 +00:00
|
|
|
|
2022-09-19 13:39:12 +00:00
|
|
|
// add Headers
|
|
|
|
$http->withHeaders([
|
|
|
|
'X-User' => $user->id
|
|
|
|
]);
|
|
|
|
|
|
|
|
$requests['user_id'] = $user->id;
|
2022-08-29 09:31:08 +00:00
|
|
|
|
2022-08-29 17:11:19 +00:00
|
|
|
|
|
|
|
if ($method == 'post') {
|
|
|
|
// add user to requests
|
|
|
|
$requests['user'] = $user;
|
|
|
|
}
|
|
|
|
|
2022-09-08 17:12:18 +00:00
|
|
|
$response = $http->{$method}("functions/{$path}", $requests);
|
2022-08-29 09:31:08 +00:00
|
|
|
|
|
|
|
$json = $response->json();
|
|
|
|
|
|
|
|
$status = $response->status();
|
|
|
|
|
|
|
|
return [
|
|
|
|
'body' => $response->body(),
|
|
|
|
'json' => $json,
|
|
|
|
'status' => $status
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-09-19 13:24:14 +00:00
|
|
|
public function moduleRequest($method, $path, $requests)
|
|
|
|
{
|
2022-09-19 13:39:12 +00:00
|
|
|
$module_id = auth('remote')->id();
|
|
|
|
|
2022-09-19 13:24:14 +00:00
|
|
|
$http = Http::remote($this->api_token, $this->url)
|
|
|
|
->accept('application/json');
|
|
|
|
|
2022-09-19 13:39:12 +00:00
|
|
|
$http->withHeaders([
|
|
|
|
'X-Module' => $module_id
|
|
|
|
]);
|
2022-09-19 13:24:14 +00:00
|
|
|
|
2022-09-19 13:39:12 +00:00
|
|
|
$requests['module_id'] = $module_id;
|
2022-09-19 13:24:14 +00:00
|
|
|
|
|
|
|
$response = $http->{$method}("exports/{$path}", $requests);
|
|
|
|
|
|
|
|
$json = $response->json();
|
|
|
|
|
|
|
|
$status = $response->status();
|
|
|
|
return [
|
|
|
|
'body' => $response->body(),
|
|
|
|
'json' => $json,
|
|
|
|
'status' => $status
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-08-29 09:31:08 +00:00
|
|
|
|
|
|
|
|
2022-08-26 14:37:20 +00:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2022-10-29 05:53:32 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 12:01:15 +00:00
|
|
|
|
|
|
|
// get cached modules
|
|
|
|
public static function cached_modules()
|
|
|
|
{
|
|
|
|
return Cache::remember('modules', 600, function () {
|
|
|
|
return Module::all();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-14 13:57:56 +00:00
|
|
|
protected static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
static::creating(function ($model) {
|
|
|
|
// if local
|
|
|
|
if (!app()->environment('local')) {
|
|
|
|
$model->api_token = Str::random(60);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-08-13 06:04:47 +00:00
|
|
|
}
|