update
This commit is contained in:
24
admin/application/common/model/ApiKey.php
Normal file
24
admin/application/common/model/ApiKey.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class ApiKey extends Model
|
||||
{
|
||||
protected $name = 'api_keys';
|
||||
|
||||
// 自动时间戳
|
||||
//protected $autoWriteTimestamp = true;
|
||||
//protected $updateTime = false;
|
||||
|
||||
// 读取器处理JSON字段
|
||||
public function getPermissionsAttr($value)
|
||||
{
|
||||
return json_decode($value, true);
|
||||
}
|
||||
|
||||
public function getIpWhitelistAttr($value)
|
||||
{
|
||||
return json_decode($value, true);
|
||||
}
|
||||
}
|
||||
47
admin/application/common/service/AuthService.php
Normal file
47
admin/application/common/service/AuthService.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace app\common\service;
|
||||
|
||||
use think\Db;
|
||||
use app\common\model\ApiKey;
|
||||
|
||||
class AuthService
|
||||
{
|
||||
protected $keyInfo;
|
||||
public function __construct($apiKey)
|
||||
{
|
||||
$this->keyInfo = ApiKey::where('api_key', $apiKey)
|
||||
->cache("api_key_{$apiKey}", 300) // 缓存5分钟
|
||||
->find();
|
||||
}
|
||||
public function verifyApiKey()
|
||||
{
|
||||
// 查询数据库(带缓存)
|
||||
$keyInfo = $this->keyInfo;
|
||||
|
||||
if (!$keyInfo) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查密钥状态
|
||||
if (!$keyInfo->is_active || $keyInfo->expires_at < time()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 记录最后使用时间
|
||||
Db::name('api_keys')
|
||||
->where('id', $keyInfo->id)
|
||||
->update(['last_used_at' => time()]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getDeveloperInfo()
|
||||
{
|
||||
// 根据业务需求返回开发者信息
|
||||
return [
|
||||
'developer_id' => $this->keyInfo->app_name,
|
||||
'app_id' => $this->keyInfo->id,
|
||||
//'permissions' => json_decode($this->keyInfo->permissions, true)
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user