82 lines
2.6 KiB
PHP
82 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Model;
|
|
|
|
use App\Utils\Ip;
|
|
use Hyperf\HttpMessage\Server\Response;
|
|
use Hyperf\HttpServer\Contract\RequestInterface;
|
|
|
|
/**
|
|
* @property int $log_id
|
|
* @property int $account_type
|
|
* @property int $belong_id
|
|
* @property int $account_id
|
|
* @property string $username
|
|
* @property string $title
|
|
* @property string $method
|
|
* @property string $flag
|
|
* @property int $code
|
|
* @property string $request
|
|
* @property string $response
|
|
* @property string $ip
|
|
* @property string $ua
|
|
* @property string $create_time
|
|
*/
|
|
class AccountLog extends Model
|
|
{
|
|
/**
|
|
* The table associated with the model.
|
|
*/
|
|
protected ?string $table = 'account_log';
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*/
|
|
protected array $fillable = [];
|
|
|
|
/**
|
|
* The attributes that should be cast to native types.
|
|
*/
|
|
protected array $casts = ['log_id' => 'integer', 'account_type' => 'integer', 'belong_id' => 'integer', 'account_id' => 'integer', 'code' => 'integer'];
|
|
|
|
public static function recordLog(RequestInterface $request, mixed $admin, string $flag, mixed $response): bool
|
|
{
|
|
$code = 0;
|
|
$content = "";
|
|
if ($response instanceof Response) {
|
|
$content = $response->getBody()->getContents();
|
|
if ($body = json_decode($content, true)) {
|
|
$code = $body['code'] ?? 200;
|
|
}
|
|
}
|
|
return self::insert([
|
|
'account_type' => $admin['account_type'],
|
|
'belong_id' => $admin['belong_id'],
|
|
'account_id' => $admin['account_id'],
|
|
'username' => $admin['username'],
|
|
'create_time' => date("Y-m-d H:i:s"),
|
|
'ua' => Ip::ua(),
|
|
'ip' => Ip::ip(),
|
|
'flag' => $flag,
|
|
'title' => Menu::getTitleByCache(strtolower($request->getMethod()), $flag, $admin['account_type']),
|
|
'method' => strtolower($request->getMethod()),
|
|
'code' => $code,
|
|
'request' => json_encode($request->all(), JSON_UNESCAPED_UNICODE),
|
|
'response' => $content
|
|
]);
|
|
}
|
|
|
|
public static function list(array $param)
|
|
{
|
|
return self::when(isset($param['account_id']) && $param['account_id'], function ($query) use ($param) {
|
|
$query->where('account_id', $param['account_id']);
|
|
})->when(isset($param['flag']) && $param['flag'], function ($query) use ($param) {
|
|
$query->where('flag', $param['flag']);
|
|
})->select(['log_id', 'username', 'title', 'method', 'flag', 'code', 'ip', 'ua', 'create_time'])
|
|
->orderByDesc("log_id")
|
|
->paginate((int)$param['limit']);
|
|
}
|
|
}
|