47 lines
1.1 KiB
PHP
47 lines
1.1 KiB
PHP
<?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)
|
|
];
|
|
}
|
|
} |