mirror of
https://gitee.com/apiadmin/ApiAdmin.git
synced 2025-04-06 03:58:00 +08:00
modified 完成规则消费方缓存支持
This commit is contained in:
parent
f9c6a0613e
commit
551bc0b009
@ -11,6 +11,7 @@ namespace app\api\behavior;
|
||||
use app\model\AdminList;
|
||||
use app\util\ApiLog;
|
||||
use app\util\ReturnCode;
|
||||
use think\Cache;
|
||||
use think\Request;
|
||||
|
||||
class ApiAuth {
|
||||
@ -33,12 +34,20 @@ class ApiAuth {
|
||||
$hash = $this->request->routeInfo();
|
||||
if (isset($hash['rule'][1])) {
|
||||
$hash = $hash['rule'][1];
|
||||
$this->apiInfo = AdminList::get(['hash' => $hash]);
|
||||
if ($this->apiInfo) {
|
||||
$this->apiInfo = $this->apiInfo->toArray();
|
||||
|
||||
$cached = Cache::has('ApiInfo:' . $hash);
|
||||
if ($cached) {
|
||||
$this->apiInfo = Cache::get('ApiInfo:' . $hash);
|
||||
} else {
|
||||
return json(['code' => ReturnCode::DB_READ_ERROR, 'msg' => '获取接口配置数据失败', 'data' => []]);
|
||||
$apiInfo = AdminList::get(['hash' => $hash]);
|
||||
if ($apiInfo) {
|
||||
$this->apiInfo = $apiInfo->toArray();
|
||||
Cache::set('ApiInfo:' . $hash, $this->apiInfo);
|
||||
} else {
|
||||
return json(['code' => ReturnCode::DB_READ_ERROR, 'msg' => '获取接口配置数据失1败', 'data' => []]);
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->apiInfo['accessToken'] && !$this->apiInfo['isTest']) {
|
||||
$accessRes = $this->checkAccessToken();
|
||||
if ($accessRes) {
|
||||
@ -68,7 +77,7 @@ class ApiAuth {
|
||||
if (!isset($access_token) || !$access_token) {
|
||||
return json(['code' => ReturnCode::ACCESS_TOKEN_TIMEOUT, 'msg' => '缺少参数access-token', 'data' => []]);
|
||||
} else {
|
||||
$appInfo = cache($access_token);
|
||||
$appInfo = cache('AccessToken:' . $access_token);
|
||||
if (!$appInfo) {
|
||||
return json(['code' => ReturnCode::ACCESS_TOKEN_TIMEOUT, 'msg' => 'access-token已过期', 'data' => []]);
|
||||
}
|
||||
@ -91,6 +100,7 @@ class ApiAuth {
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO::需要根据实际情况另外改写
|
||||
* 检测用户登录情况 检测通过请赋予USER_INFO值
|
||||
*/
|
||||
private function checkLogin() {
|
||||
|
@ -30,7 +30,7 @@ class ApiPermission {
|
||||
$hash = $hash['rule'][1];
|
||||
$access_token = $this->request->header('access-token');
|
||||
if ($access_token) {
|
||||
$appInfo = cache($access_token);
|
||||
$appInfo = cache('AccessToken:' . $access_token);
|
||||
$allRules = explode(',', $appInfo['app_api']);
|
||||
if (!in_array($hash, $allRules)) {
|
||||
$data = ['code' => ReturnCode::INVALID, 'msg' => '非常抱歉,您没有权限怎么做!', 'data' => []];
|
||||
|
@ -11,6 +11,7 @@ namespace app\api\behavior;
|
||||
use app\model\AdminFields;
|
||||
use app\util\ApiLog;
|
||||
use app\util\DataType;
|
||||
use think\Cache;
|
||||
use think\Request;
|
||||
|
||||
class BuildResponse {
|
||||
@ -27,7 +28,15 @@ class BuildResponse {
|
||||
$hash = $request->routeInfo();
|
||||
if (isset($hash['rule'][1])) {
|
||||
$hash = $hash['rule'][1];
|
||||
$rule = AdminFields::all(['hash' => $hash, 'type' => 1]);
|
||||
|
||||
$has = Cache::has('ResponseFieldsRule:' . $hash);
|
||||
if ($has) {
|
||||
$rule = cache('ResponseFieldsRule:' . $hash);
|
||||
} else {
|
||||
$rule = AdminFields::all(['hash' => $hash, 'type' => 1]);
|
||||
cache('ResponseFieldsRule:' . $hash, $rule);
|
||||
}
|
||||
|
||||
if ($rule) {
|
||||
$rule = json_decode(json_encode($rule), true);
|
||||
$newRule = array_column($rule, 'dataType', 'showName');
|
||||
|
@ -1,7 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* 输入参数过滤行为
|
||||
* TODO::Route异常捕获、规则缓存
|
||||
* @since 2017-07-25
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
@ -13,6 +12,7 @@ use app\model\AdminFields;
|
||||
use app\util\ApiLog;
|
||||
use app\util\ReturnCode;
|
||||
use app\util\DataType;
|
||||
use think\Cache;
|
||||
use think\Request;
|
||||
use think\Validate;
|
||||
|
||||
@ -47,8 +47,17 @@ class RequestFilter {
|
||||
$hash = $request->routeInfo();
|
||||
if (isset($hash['rule'][1])) {
|
||||
$hash = $hash['rule'][1];
|
||||
$rule = AdminFields::all(['hash' => $hash, 'type' => 0]);
|
||||
$newRule = $this->buildValidateRule($rule);
|
||||
|
||||
$has = Cache::has('RequestFields:NewRule:' . $hash);
|
||||
if ($has) {
|
||||
$newRule = cache('RequestFields:NewRule:' . $hash);
|
||||
$rule = cache('RequestFields:Rule:' . $hash);
|
||||
} else {
|
||||
$rule = AdminFields::all(['hash' => $hash, 'type' => 0]);
|
||||
$newRule = $this->buildValidateRule($rule);
|
||||
cache('RequestFields:NewRule:' . $hash, $newRule);
|
||||
cache('RequestFields:Rule:' . $hash, $rule);
|
||||
}
|
||||
|
||||
if ($newRule) {
|
||||
$validate = new Validate($newRule);
|
||||
|
@ -41,16 +41,16 @@ class BuildToken extends Base {
|
||||
return $this->buildFailed(ReturnCode::INVALID, '身份令牌验证失败');
|
||||
}
|
||||
$expires = config('apiAdmin.ACCESS_TOKEN_TIME_OUT');
|
||||
$accessToken = cache($param['device_id']);
|
||||
$accessToken = cache('AccessToken:' . $param['device_id']);
|
||||
if ($accessToken) {
|
||||
cache($accessToken, null);
|
||||
cache($param['device_id'], null);
|
||||
cache('AccessToken:' . $accessToken, null);
|
||||
cache('AccessToken:' . $param['device_id'], null);
|
||||
}
|
||||
$accessToken = $this->buildAccessToken($appInfo['app_id'], $appInfo['app_secret']);
|
||||
$appInfo['device_id'] = $param['device_id'];
|
||||
ApiLog::setAppInfo($appInfo);
|
||||
cache($accessToken, $appInfo, $expires);
|
||||
cache($param['device_id'], $accessToken, $expires);
|
||||
cache('AccessToken:' . $accessToken, $appInfo, $expires);
|
||||
cache('AccessToken:' . $param['device_id'], $accessToken, $expires);
|
||||
$return['access_token'] = $accessToken;
|
||||
$return['expires_in'] = $expires;
|
||||
|
||||
@ -71,13 +71,14 @@ class BuildToken extends Base {
|
||||
* @param $data
|
||||
* @return string
|
||||
*/
|
||||
private function getAuthToken( $appSecret, $data ){
|
||||
if(empty($data)){
|
||||
private function getAuthToken($appSecret, $data) {
|
||||
if (empty($data)) {
|
||||
return '';
|
||||
}else{
|
||||
} else {
|
||||
$preArr = array_merge($data, ['app_secret' => $appSecret]);
|
||||
ksort($preArr);
|
||||
$preStr = http_build_query($preArr);
|
||||
|
||||
return md5($preStr);
|
||||
}
|
||||
}
|
||||
@ -88,8 +89,9 @@ class BuildToken extends Base {
|
||||
* @param $appSecret
|
||||
* @return string
|
||||
*/
|
||||
private function buildAccessToken( $appId, $appSecret ){
|
||||
$preStr = $appSecret.$appId.time().Strs::keyGen();
|
||||
private function buildAccessToken($appId, $appSecret) {
|
||||
$preStr = $appSecret . $appId . time() . Strs::keyGen();
|
||||
|
||||
return md5($preStr);
|
||||
}
|
||||
|
||||
|
170
composer.lock
generated
Normal file
170
composer.lock
generated
Normal file
@ -0,0 +1,170 @@
|
||||
{
|
||||
"_readme": [
|
||||
"This file locks the dependencies of your project to a known state",
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"hash": "6d6e9a23ce221d217fca0657ab1b54a2",
|
||||
"content-hash": "de8523ca5915d4eb4e43fe8d2a1a4d27",
|
||||
"packages": [
|
||||
{
|
||||
"name": "php-curl-class/php-curl-class",
|
||||
"version": "8.0.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/php-curl-class/php-curl-class.git",
|
||||
"reference": "bdfe1fcca0c32562050c84e01e3a4cbdc31e22fd"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://files.phpcomposer.com/files/php-curl-class/php-curl-class/bdfe1fcca0c32562050c84e01e3a4cbdc31e22fd.zip",
|
||||
"reference": "bdfe1fcca0c32562050c84e01e3a4cbdc31e22fd",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-curl": "*",
|
||||
"php": ">=5.3"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "*",
|
||||
"squizlabs/php_codesniffer": "*"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Curl\\": "src/Curl/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"Unlicense"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Zach Borboa"
|
||||
}
|
||||
],
|
||||
"description": "PHP Curl Class makes it easy to send HTTP requests and integrate with web APIs.",
|
||||
"homepage": "https://github.com/php-curl-class/php-curl-class",
|
||||
"keywords": [
|
||||
"api",
|
||||
"class",
|
||||
"client",
|
||||
"curl",
|
||||
"framework",
|
||||
"http",
|
||||
"http client",
|
||||
"json",
|
||||
"php",
|
||||
"requests",
|
||||
"rest",
|
||||
"restful",
|
||||
"web service",
|
||||
"xml"
|
||||
],
|
||||
"time": "2018-01-27 15:40:39"
|
||||
},
|
||||
{
|
||||
"name": "topthink/framework",
|
||||
"version": "v5.0.15",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/top-think/framework.git",
|
||||
"reference": "7c1375791fe8772e33282ee8611ea465dc215fca"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://files.phpcomposer.com/files/top-think/framework/7c1375791fe8772e33282ee8611ea465dc215fca.zip",
|
||||
"reference": "7c1375791fe8772e33282ee8611ea465dc215fca",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.4.0",
|
||||
"topthink/think-installer": "~1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"johnkary/phpunit-speedtrap": "^1.0",
|
||||
"mikey179/vfsstream": "~1.6",
|
||||
"phpdocumentor/reflection-docblock": "^2.0",
|
||||
"phploc/phploc": "2.*",
|
||||
"phpunit/phpunit": "4.8.*",
|
||||
"sebastian/phpcpd": "2.*"
|
||||
},
|
||||
"type": "think-framework",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"think\\": "library/think"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"Apache-2.0"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "liu21st",
|
||||
"email": "liu21st@gmail.com"
|
||||
}
|
||||
],
|
||||
"description": "the new thinkphp framework",
|
||||
"homepage": "http://thinkphp.cn/",
|
||||
"keywords": [
|
||||
"framework",
|
||||
"orm",
|
||||
"thinkphp"
|
||||
],
|
||||
"time": "2018-01-31 08:40:10"
|
||||
},
|
||||
{
|
||||
"name": "topthink/think-installer",
|
||||
"version": "v1.0.12",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/top-think/think-installer.git",
|
||||
"reference": "1be326e68f63de4e95977ed50f46ae75f017556d"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://files.phpcomposer.com/files/top-think/think-installer/1be326e68f63de4e95977ed50f46ae75f017556d.zip",
|
||||
"reference": "1be326e68f63de4e95977ed50f46ae75f017556d",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"composer-plugin-api": "^1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"composer/composer": "1.0.*@dev"
|
||||
},
|
||||
"type": "composer-plugin",
|
||||
"extra": {
|
||||
"class": "think\\composer\\Plugin"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"think\\composer\\": "src"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"Apache-2.0"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "yunwuxin",
|
||||
"email": "448901948@qq.com"
|
||||
}
|
||||
],
|
||||
"time": "2017-05-27 06:58:09"
|
||||
}
|
||||
],
|
||||
"packages-dev": [],
|
||||
"aliases": [],
|
||||
"minimum-stability": "stable",
|
||||
"stability-flags": [],
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": {
|
||||
"php": ">=5.6.0"
|
||||
},
|
||||
"platform-dev": []
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user