// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\ActionLogModel; use App\Models\ApiKeyModel; use App\Models\MemberModel; use App\Services\BaseService; use App\Services\RedisService; /** * 接口账号管理-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services\Common */ class ApiKeyService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 */ public function __construct() { $this->model = new ApiKeyModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 获取接口配置 * @param int $userId * @return array|false|mixed */ public function getConfig($userId=0) { $cacheKey = "caches:apiKeys:user_{$userId}"; $config = RedisService::get($cacheKey); if(empty($config)){ $keyId = 0; if($userId){ $keyId = MemberModel::where(['id'=>$userId])->value('api_key_id'); } $config = $this->model->where(['mark'=>1]) ->where(function($query) use($keyId){ if($keyId){ $query->where(['status'=>1,'id'=> $keyId]); }else{ $query->where(['status'=>1]); } }) ->select(['id','api_key','api_secret','passphrase','status']) ->first(); $config = $config? $config->toArray() : []; if($config){ RedisService::set($cacheKey, $config, rand(300, 600)); } } return $config; } /** * 添加会编辑 * @return array * @since 2020/11/11 * @author laravel开发员 */ public function edit() { // 请求参数 $data = request()->all(); ActionLogModel::setTitle("修改OK接口账号信息"); ActionLogModel::record(); return parent::edit($data); // TODO: Change the autogenerated stub } }