From 551bc0b009786be9b7ac8c077ca26ab6619822fc Mon Sep 17 00:00:00 2001 From: zhaoxiang <756958008@qq.com> Date: Wed, 14 Mar 2018 01:19:34 +0800 Subject: [PATCH] =?UTF-8?q?modified=20=E5=AE=8C=E6=88=90=E8=A7=84=E5=88=99?= =?UTF-8?q?=E6=B6=88=E8=B4=B9=E6=96=B9=E7=BC=93=E5=AD=98=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/api/behavior/ApiAuth.php | 20 ++- application/api/behavior/ApiPermission.php | 2 +- application/api/behavior/BuildResponse.php | 11 +- application/api/behavior/RequestFilter.php | 15 +- application/api/controller/BuildToken.php | 22 +-- composer.lock | 170 +++++++++++++++++++++ 6 files changed, 220 insertions(+), 20 deletions(-) create mode 100644 composer.lock diff --git a/application/api/behavior/ApiAuth.php b/application/api/behavior/ApiAuth.php index 8b4ef5a..d4f926d 100644 --- a/application/api/behavior/ApiAuth.php +++ b/application/api/behavior/ApiAuth.php @@ -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() { diff --git a/application/api/behavior/ApiPermission.php b/application/api/behavior/ApiPermission.php index da15119..1aa227f 100644 --- a/application/api/behavior/ApiPermission.php +++ b/application/api/behavior/ApiPermission.php @@ -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' => []]; diff --git a/application/api/behavior/BuildResponse.php b/application/api/behavior/BuildResponse.php index 84aec08..94b8345 100644 --- a/application/api/behavior/BuildResponse.php +++ b/application/api/behavior/BuildResponse.php @@ -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'); diff --git a/application/api/behavior/RequestFilter.php b/application/api/behavior/RequestFilter.php index 1259eb0..7995a13 100644 --- a/application/api/behavior/RequestFilter.php +++ b/application/api/behavior/RequestFilter.php @@ -1,7 +1,6 @@ */ @@ -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); diff --git a/application/api/controller/BuildToken.php b/application/api/controller/BuildToken.php index 285709b..b9e53a8 100644 --- a/application/api/controller/BuildToken.php +++ b/application/api/controller/BuildToken.php @@ -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); } diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..5f7877d --- /dev/null +++ b/composer.lock @@ -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": [] +}