modified 完成规则消费方缓存支持

This commit is contained in:
zhaoxiang 2018-03-14 01:19:34 +08:00
parent f9c6a0613e
commit 551bc0b009
6 changed files with 220 additions and 20 deletions

View File

@ -11,6 +11,7 @@ namespace app\api\behavior;
use app\model\AdminList; use app\model\AdminList;
use app\util\ApiLog; use app\util\ApiLog;
use app\util\ReturnCode; use app\util\ReturnCode;
use think\Cache;
use think\Request; use think\Request;
class ApiAuth { class ApiAuth {
@ -33,12 +34,20 @@ class ApiAuth {
$hash = $this->request->routeInfo(); $hash = $this->request->routeInfo();
if (isset($hash['rule'][1])) { if (isset($hash['rule'][1])) {
$hash = $hash['rule'][1]; $hash = $hash['rule'][1];
$this->apiInfo = AdminList::get(['hash' => $hash]);
if ($this->apiInfo) { $cached = Cache::has('ApiInfo:' . $hash);
$this->apiInfo = $this->apiInfo->toArray(); if ($cached) {
$this->apiInfo = Cache::get('ApiInfo:' . $hash);
} else { } 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']) { if ($this->apiInfo['accessToken'] && !$this->apiInfo['isTest']) {
$accessRes = $this->checkAccessToken(); $accessRes = $this->checkAccessToken();
if ($accessRes) { if ($accessRes) {
@ -68,7 +77,7 @@ class ApiAuth {
if (!isset($access_token) || !$access_token) { if (!isset($access_token) || !$access_token) {
return json(['code' => ReturnCode::ACCESS_TOKEN_TIMEOUT, 'msg' => '缺少参数access-token', 'data' => []]); return json(['code' => ReturnCode::ACCESS_TOKEN_TIMEOUT, 'msg' => '缺少参数access-token', 'data' => []]);
} else { } else {
$appInfo = cache($access_token); $appInfo = cache('AccessToken:' . $access_token);
if (!$appInfo) { if (!$appInfo) {
return json(['code' => ReturnCode::ACCESS_TOKEN_TIMEOUT, 'msg' => 'access-token已过期', 'data' => []]); return json(['code' => ReturnCode::ACCESS_TOKEN_TIMEOUT, 'msg' => 'access-token已过期', 'data' => []]);
} }
@ -91,6 +100,7 @@ class ApiAuth {
} }
/** /**
* TODO::需要根据实际情况另外改写
* 检测用户登录情况 检测通过请赋予USER_INFO值 * 检测用户登录情况 检测通过请赋予USER_INFO值
*/ */
private function checkLogin() { private function checkLogin() {

View File

@ -30,7 +30,7 @@ class ApiPermission {
$hash = $hash['rule'][1]; $hash = $hash['rule'][1];
$access_token = $this->request->header('access-token'); $access_token = $this->request->header('access-token');
if ($access_token) { if ($access_token) {
$appInfo = cache($access_token); $appInfo = cache('AccessToken:' . $access_token);
$allRules = explode(',', $appInfo['app_api']); $allRules = explode(',', $appInfo['app_api']);
if (!in_array($hash, $allRules)) { if (!in_array($hash, $allRules)) {
$data = ['code' => ReturnCode::INVALID, 'msg' => '非常抱歉,您没有权限怎么做!', 'data' => []]; $data = ['code' => ReturnCode::INVALID, 'msg' => '非常抱歉,您没有权限怎么做!', 'data' => []];

View File

@ -11,6 +11,7 @@ namespace app\api\behavior;
use app\model\AdminFields; use app\model\AdminFields;
use app\util\ApiLog; use app\util\ApiLog;
use app\util\DataType; use app\util\DataType;
use think\Cache;
use think\Request; use think\Request;
class BuildResponse { class BuildResponse {
@ -27,7 +28,15 @@ class BuildResponse {
$hash = $request->routeInfo(); $hash = $request->routeInfo();
if (isset($hash['rule'][1])) { if (isset($hash['rule'][1])) {
$hash = $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) { if ($rule) {
$rule = json_decode(json_encode($rule), true); $rule = json_decode(json_encode($rule), true);
$newRule = array_column($rule, 'dataType', 'showName'); $newRule = array_column($rule, 'dataType', 'showName');

View File

@ -1,7 +1,6 @@
<?php <?php
/** /**
* 输入参数过滤行为 * 输入参数过滤行为
* TODO::Route异常捕获、规则缓存
* @since 2017-07-25 * @since 2017-07-25
* @author zhaoxiang <zhaoxiang051405@gmail.com> * @author zhaoxiang <zhaoxiang051405@gmail.com>
*/ */
@ -13,6 +12,7 @@ use app\model\AdminFields;
use app\util\ApiLog; use app\util\ApiLog;
use app\util\ReturnCode; use app\util\ReturnCode;
use app\util\DataType; use app\util\DataType;
use think\Cache;
use think\Request; use think\Request;
use think\Validate; use think\Validate;
@ -47,8 +47,17 @@ class RequestFilter {
$hash = $request->routeInfo(); $hash = $request->routeInfo();
if (isset($hash['rule'][1])) { if (isset($hash['rule'][1])) {
$hash = $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) { if ($newRule) {
$validate = new Validate($newRule); $validate = new Validate($newRule);

View File

@ -41,16 +41,16 @@ class BuildToken extends Base {
return $this->buildFailed(ReturnCode::INVALID, '身份令牌验证失败'); return $this->buildFailed(ReturnCode::INVALID, '身份令牌验证失败');
} }
$expires = config('apiAdmin.ACCESS_TOKEN_TIME_OUT'); $expires = config('apiAdmin.ACCESS_TOKEN_TIME_OUT');
$accessToken = cache($param['device_id']); $accessToken = cache('AccessToken:' . $param['device_id']);
if ($accessToken) { if ($accessToken) {
cache($accessToken, null); cache('AccessToken:' . $accessToken, null);
cache($param['device_id'], null); cache('AccessToken:' . $param['device_id'], null);
} }
$accessToken = $this->buildAccessToken($appInfo['app_id'], $appInfo['app_secret']); $accessToken = $this->buildAccessToken($appInfo['app_id'], $appInfo['app_secret']);
$appInfo['device_id'] = $param['device_id']; $appInfo['device_id'] = $param['device_id'];
ApiLog::setAppInfo($appInfo); ApiLog::setAppInfo($appInfo);
cache($accessToken, $appInfo, $expires); cache('AccessToken:' . $accessToken, $appInfo, $expires);
cache($param['device_id'], $accessToken, $expires); cache('AccessToken:' . $param['device_id'], $accessToken, $expires);
$return['access_token'] = $accessToken; $return['access_token'] = $accessToken;
$return['expires_in'] = $expires; $return['expires_in'] = $expires;
@ -71,13 +71,14 @@ class BuildToken extends Base {
* @param $data * @param $data
* @return string * @return string
*/ */
private function getAuthToken( $appSecret, $data ){ private function getAuthToken($appSecret, $data) {
if(empty($data)){ if (empty($data)) {
return ''; return '';
}else{ } else {
$preArr = array_merge($data, ['app_secret' => $appSecret]); $preArr = array_merge($data, ['app_secret' => $appSecret]);
ksort($preArr); ksort($preArr);
$preStr = http_build_query($preArr); $preStr = http_build_query($preArr);
return md5($preStr); return md5($preStr);
} }
} }
@ -88,8 +89,9 @@ class BuildToken extends Base {
* @param $appSecret * @param $appSecret
* @return string * @return string
*/ */
private function buildAccessToken( $appId, $appSecret ){ private function buildAccessToken($appId, $appSecret) {
$preStr = $appSecret.$appId.time().Strs::keyGen(); $preStr = $appSecret . $appId . time() . Strs::keyGen();
return md5($preStr); return md5($preStr);
} }

170
composer.lock generated Normal file
View 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": []
}