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-30 07:57:31 +00:00
|
|
|
use Illuminate\Http\Client\PendingRequest;
|
2022-11-27 06:40:45 +00:00
|
|
|
use Illuminate\Http\Client\Response;
|
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;
|
|
|
|
|
2022-11-16 05:16:56 +00:00
|
|
|
public $incrementing = false;
|
2022-08-13 06:04:47 +00:00
|
|
|
|
2022-08-14 13:57:56 +00:00
|
|
|
// primary key
|
|
|
|
public $timestamps = false;
|
2022-11-16 05:16:56 +00:00
|
|
|
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',
|
2022-10-31 11:03:38 +00:00
|
|
|
'url',
|
|
|
|
'wecom_key'
|
2022-09-13 02:04:10 +00:00
|
|
|
];
|
|
|
|
|
2022-11-16 05:16: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-19 10:14:23 +00:00
|
|
|
|
2022-11-20 14:16:04 +00:00
|
|
|
// public function moduleHostFunctions($host_id, $func, $requests): array
|
|
|
|
// {
|
|
|
|
// $http = Http::module($this->api_token, $this->url);
|
|
|
|
// $response = $http->post("hosts/{$host_id}/functions/" . $func, $requests);
|
|
|
|
//
|
|
|
|
// return $this->getResponse($response);
|
|
|
|
// }
|
2022-08-19 10:31:24 +00:00
|
|
|
|
2022-11-16 05:16:56 +00:00
|
|
|
|
|
|
|
// post, get, patch, delete 等请求
|
2022-11-20 14:16:04 +00:00
|
|
|
public function remote($func, $requests): array
|
2022-08-19 10:14:23 +00:00
|
|
|
{
|
2022-11-30 07:57:31 +00:00
|
|
|
$response = $this->http()->post('functions/' . $func, $requests);
|
2022-08-19 10:14:23 +00:00
|
|
|
|
2022-11-20 14:16:04 +00:00
|
|
|
return $this->getResponse($response);
|
2022-08-19 10:14:23 +00:00
|
|
|
}
|
|
|
|
|
2022-11-20 14:16:04 +00:00
|
|
|
public function request($method, $path, $requests): array
|
2022-08-29 09:31:08 +00:00
|
|
|
{
|
2022-11-27 06:40:45 +00:00
|
|
|
return $this->baseRequest($method, "functions/{$path}", $requests);
|
|
|
|
}
|
2022-08-29 09:31:08 +00:00
|
|
|
|
2022-11-27 06:40:45 +00:00
|
|
|
public function baseRequest($method, $path, $requests): array
|
|
|
|
{
|
|
|
|
$user = auth('sanctum')->user();
|
2022-11-23 08:47:34 +00:00
|
|
|
|
2022-11-27 06:40:45 +00:00
|
|
|
if ($user) {
|
2022-11-30 07:57:31 +00:00
|
|
|
$this->http()->withHeaders([
|
2022-11-27 06:40:45 +00:00
|
|
|
'X-User-id' => $user->id,
|
|
|
|
]);
|
|
|
|
$requests['user_id'] = $user->id;
|
|
|
|
if ($method == 'post') {
|
|
|
|
// add user to requests
|
|
|
|
$requests['user'] = $user;
|
|
|
|
}
|
2022-08-29 17:11:19 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 07:57:31 +00:00
|
|
|
$response = $this->http()->{$method}($path, $requests);
|
2022-08-29 09:31:08 +00:00
|
|
|
|
2022-11-20 14:16:04 +00:00
|
|
|
return $this->getResponse($response);
|
2022-08-29 09:31:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-20 14:16:04 +00:00
|
|
|
public function moduleRequest($method, $path, $requests): array
|
2022-09-19 13:24:14 +00:00
|
|
|
{
|
2022-11-06 14:57:01 +00:00
|
|
|
$module_id = auth('module')->id();
|
2022-09-19 13:39:12 +00:00
|
|
|
|
2022-11-30 07:57:31 +00:00
|
|
|
$this->http()->withHeaders([
|
2022-09-19 13:39:12 +00:00
|
|
|
'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
|
|
|
|
2022-11-30 07:57:31 +00:00
|
|
|
$response = $this->http()->{$method}("exports/{$path}", $requests);
|
2022-09-19 13:24:14 +00:00
|
|
|
|
2022-11-20 14:16:04 +00:00
|
|
|
return $this->getResponse($response);
|
|
|
|
}
|
2022-09-19 13:24:14 +00:00
|
|
|
|
2022-11-20 14:16:04 +00:00
|
|
|
private function getResponse(Response $response): array
|
|
|
|
{
|
|
|
|
$json = $response->json();
|
2022-09-19 13:24:14 +00:00
|
|
|
$status = $response->status();
|
2022-11-20 14:16:04 +00:00
|
|
|
|
2022-09-19 13:24:14 +00:00
|
|
|
return [
|
|
|
|
'body' => $response->body(),
|
|
|
|
'json' => $json,
|
|
|
|
'status' => $status
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-11-20 14:16:04 +00:00
|
|
|
public function check($module_id = null): bool
|
2022-10-29 05:53:32 +00:00
|
|
|
{
|
|
|
|
if ($module_id) {
|
|
|
|
$module = Module::find($module_id);
|
|
|
|
} else {
|
|
|
|
$module = $this;
|
|
|
|
}
|
|
|
|
|
2022-11-20 14:16:04 +00:00
|
|
|
$success = 0;
|
|
|
|
|
2022-10-29 05:53:32 +00:00
|
|
|
try {
|
2022-11-30 07:57:31 +00:00
|
|
|
$response = $this->http()->get('remote');
|
2022-11-20 14:16:04 +00:00
|
|
|
|
|
|
|
if ($response->status() == 200) {
|
|
|
|
$success = 1;
|
|
|
|
}
|
2022-10-29 05:53:32 +00:00
|
|
|
} catch (ConnectException $e) {
|
|
|
|
Log::error($e->getMessage());
|
|
|
|
}
|
|
|
|
|
2022-11-20 14:16:04 +00:00
|
|
|
return $success;
|
2022-10-29 05:53:32 +00:00
|
|
|
}
|
2022-11-19 03:08:16 +00:00
|
|
|
|
2022-11-30 07:57:31 +00:00
|
|
|
public function http(): PendingRequest
|
|
|
|
{
|
2022-12-05 04:47:48 +00:00
|
|
|
return Http::module($this->api_token, $this->url)->acceptJson();
|
2022-11-30 07:57:31 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
return Cache::get($cache_key, []);
|
2022-11-19 03:08:16 +00:00
|
|
|
}
|
2022-08-13 06:04:47 +00:00
|
|
|
}
|