server/app/Model/AccountLog.php

82 lines
2.3 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'];
/**
* 登录记录日志
* Author: cfn <cfn@leapy.cn>
* @param array $data
* @return bool
*/
public static function loginRecord(array $data): bool
{
return self::insert($data);
}
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
]);
}
}