Lae/app/Models/Module.php

217 lines
6.7 KiB
PHP
Raw Normal View History

2022-08-13 06:04:47 +00:00
<?php
2022-11-06 11:28:22 +00:00
namespace App\Models;
2022-08-13 06:04:47 +00:00
2022-11-06 14:57:01 +00:00
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
2022-11-08 13:01:43 +00:00
use GuzzleHttp\Exception\ConnectException;
2022-11-06 11:28:22 +00:00
use Illuminate\Foundation\Auth\User as Authenticatable;
2022-11-19 03:08:16 +00:00
use Illuminate\Support\Facades\Cache;
2022-11-08 13:01:43 +00:00
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
2022-11-19 03:08:16 +00:00
use JetBrains\PhpStorm\ArrayShape;
2022-08-13 06:04:47 +00:00
2022-11-20 03:40:20 +00:00
/**
* App\Models\Module
*
2022-11-20 12:32:49 +00:00
* @property string $id
* @property string $name
2022-11-20 03:40:20 +00:00
* @property string|null $api_token
* @property string|null $url
* @property string|null $wecom_key 企业微信机器人 key
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module all($columns = [])
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module avg($column)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module cache(array $tags = [])
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module cachedValue(array $arguments, string $cacheKey)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module count($columns = '*')
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module disableCache()
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module disableModelCaching()
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module exists()
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module flushCache(array $tags = [])
2022-11-20 12:32:49 +00:00
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module
* getModelCacheCooldown(\Illuminate\Database\Eloquent\Model $instance)
2022-11-20 03:40:20 +00:00
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module inRandomOrder($seed = '')
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module insert(array $values)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module isCachable()
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module max($column)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module min($column)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module newModelQuery()
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module newQuery()
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module query()
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module sum($column)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module truncate()
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module whereApiToken($value)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module whereId($value)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module whereName($value)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module whereUrl($value)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module whereWecomKey($value)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Module withCacheCooldownSeconds(?int $seconds = null)
* @mixin \Eloquent
*/
2022-11-06 11:28:22 +00:00
class Module extends Authenticatable
2022-08-13 06:04:47 +00:00
{
2022-11-06 14:57:01 +00:00
use Cachable;
public $incrementing = false;
2022-08-13 06:04:47 +00:00
2022-08-14 13:57:56 +00:00
// primary key
public $timestamps = false;
protected $table = 'modules';
protected $keyType = 'string';
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',
'url',
'wecom_key'
2022-09-13 02:04:10 +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-19 10:14:23 +00:00
2022-08-19 10:31:24 +00:00
public function remoteHost($host_id, $func, $requests)
{
2022-11-16 02:29:50 +00:00
$http = Http::module($this->api_token, $this->url);
2022-08-19 10:31:24 +00:00
$response = $http->post("hosts/{$host_id}/functions/" . $func, $requests);
$json = $response->json();
$status = $response->status();
return [$json, $status];
}
// post, get, patch, delete 等请求
2022-08-19 10:14:23 +00:00
public function remote($func, $requests)
{
2022-11-16 02:29:50 +00:00
$http = Http::module($this->api_token, $this->url);
2022-08-19 10:14:23 +00:00
$response = $http->post('functions/' . $func, $requests);
$json = $response->json();
$status = $response->status();
return [$json, $status];
}
2022-09-08 17:12:18 +00:00
public function remoteRequest($method, $path, $requests)
2022-08-29 09:31:08 +00:00
{
2022-11-06 11:28:22 +00:00
$user = auth()->user();
2022-09-19 13:39:12 +00:00
2022-11-19 03:08:16 +00:00
$http = Http::module($this->api_token, $this->url);
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
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
];
}
public function moduleRequest($method, $path, $requests)
{
2022-11-06 14:57:01 +00:00
$module_id = auth('module')->id();
2022-09-19 13:39:12 +00:00
2022-11-16 02:29:50 +00:00
$http = Http::module($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:39:12 +00:00
$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
];
}
2022-08-26 14:37:20 +00:00
public function remotePost($path = '', $data = [])
{
2022-11-16 02:29:50 +00:00
$http = Http::module($this->api_token, $this->url);
2022-08-26 14:37:20 +00:00
$response = $http->post($path, $data);
$json = $response->json();
$status = $response->status();
return [$json, $status];
}
// // get cached modules
// public static function cached_modules()
// {
// return Cache::remember('modules', 600, function () {
// return Module::all();
// });
// }
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 {
2022-11-16 02:29:50 +00:00
$http = Http::module($module->api_token, $module->url);
2022-10-29 05:53:32 +00:00
// dd($module->url);
$response = $http->get('remote');
} catch (ConnectException $e) {
Log::error($e->getMessage());
}
if ($response->status() == 200) {
return true;
} else {
return false;
}
}
2022-11-19 03:08:16 +00:00
#[ArrayShape(['transactions' => "array"])]
public function calculate(): array
{
2022-11-19 04:37:18 +00:00
$cache_key = 'module_earning_' . $this->id;
2022-11-19 03:08:16 +00:00
2022-11-19 04:37:18 +00:00
return Cache::get($cache_key, []);
2022-11-19 03:08:16 +00:00
}
2022-08-13 06:04:47 +00:00
}