modified 处理全部接口请求流程

This commit is contained in:
zhaoxiang 2019-07-01 14:42:38 +08:00
parent 6a92e77c5d
commit c5ee12f931
5 changed files with 48 additions and 43 deletions

View File

@ -8,7 +8,6 @@
namespace app\api\controller;
use app\util\ApiLog;
use app\util\ReturnCode;
use think\Controller;
@ -18,7 +17,7 @@ class Base extends Controller {
protected $userInfo = [];
public function _initialize() {
$this->userInfo = ApiLog::getUserInfo();
// $this->userInfo = ''; 这部分初始化用户信息可以参考admin模块下的Base去自行处理
}
public function buildSuccess($data, $msg = '操作成功', $code = ReturnCode::SUCCESS) {
@ -53,4 +52,4 @@ class Base extends Controller {
}
}
}
}

View File

@ -9,7 +9,6 @@ namespace app\api\controller;
use app\model\AdminApp;
use app\util\ApiLog;
use app\util\ReturnCode;
use app\util\Strs;
@ -48,7 +47,6 @@ class BuildToken extends Base {
}
$accessToken = $this->buildAccessToken($appInfo['app_id'], $appInfo['app_secret']);
$appInfo['device_id'] = $param['device_id'];
ApiLog::setAppInfo($appInfo);
cache('AccessToken:' . $accessToken, $appInfo, $expires);
cache('AccessToken:' . $param['device_id'], $accessToken, $expires);
$return['access_token'] = $accessToken;

View File

@ -2,10 +2,26 @@
namespace app\http\middleware;
class ApiLog
{
public function handle($request, \Closure $next)
{
return $next($request);
use app\util\ApiLogTool;
class ApiLog {
/**
* @param \think\facade\Request $request
* @param \Closure $next
* @return mixed|\think\response\Json
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public function handle($request, \Closure $next) {
$response = $next($request);
ApiLogTool::setApiInfo($request->API_CONF_DETAIL);
ApiLogTool::setAppInfo($request->APP_CONF_DETAIL);
ApiLogTool::setRequest($request->param());
ApiLogTool::setResponse($response->getData());
ApiLogTool::setHeader($request->header());
ApiLogTool::save();
return $response;
}
}

View File

@ -2,10 +2,10 @@
namespace app\http\middleware;
class ApiResponse
{
public function handle($request, \Closure $next)
{
return $next($request);
use think\facade\Config;
class ApiResponse {
public function handle($request, \Closure $next) {
return $next($request)->header(Config::get('apiadmin.CROSS_DOMAIN'));
}
}

View File

@ -7,44 +7,47 @@
namespace app\util;
class ApiLog {
use think\facade\Env;
class ApiLogTool {
private static $appInfo = 'null';
private static $apiInfo = 'null';
private static $request = 'null';
private static $requestAfterFilter = 'null';
private static $response = 'null';
private static $header = 'null';
private static $userInfo = 'null';
private static $separator = '###';
private static $separator = ' | ';
public static function setAppInfo($data) {
self::$appInfo =
(isset($data['app_id']) ? $data['app_id'] : '') . self::$separator .
(isset($data['app_name']) ? $data['app_name'] : '') . self::$separator .
(isset($data['device_id']) ? $data['device_id'] : '');
(isset($data['app_id']) ? $data['app_id'] : 'null') . self::$separator .
(isset($data['app_name']) ? $data['app_name'] : 'null') . self::$separator .
(isset($data['device_id']) ? $data['device_id'] : 'null');
}
public static function setHeader($data) {
$userToken = (isset($data['user-token']) && !empty($data['user-token'])) ? $data['user-token'] : 'null';
$accessToken = (isset($data['access-token']) && !empty($data['access-token'])) ? $data['access-token'] : 'null';
$version = (isset($data['version']) && !empty($data['version'])) ? $data['version'] : 'null';
self::$header = $accessToken . self::$separator . $userToken . self::$separator . $version;
self::$header = $accessToken . self::$separator . $version;
}
public static function setApiInfo($data) {
self::$apiInfo = isset($data['apiClass']) ? $data['apiClass'] : '' . self::$separator . isset($data['hash']) ? $data['hash'] : '';
self::$apiInfo =
(isset($data['hash']) ? $data['hash'] : 'null') . self::$separator .
(isset($data['api_class']) ? $data['api_class'] : 'null');
}
/**
* 这部分的日志其实很关键但是由于不再强制检测UserToken所以这部分日志暂时不生效请大家各自适配
* @param $data
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public static function setUserInfo($data) {
if (is_array($data) || is_object($data)) {
$data = json_encode($data);
self::$userInfo = $data;
}
self::$userInfo = $data;
}
public static function getUserInfo() {
return json_decode(self::$userInfo, true);
}
public static function setRequest($data) {
@ -54,13 +57,6 @@ class ApiLog {
self::$request = $data;
}
public static function setRequestAfterFilter($data) {
if (is_array($data) || is_object($data)) {
$data = json_encode($data);
}
self::$requestAfterFilter = $data;
}
public static function setResponse($data, $code = '') {
if (is_array($data) || is_object($data)) {
$data = json_encode($data);
@ -69,17 +65,13 @@ class ApiLog {
}
public static function save() {
$logPath = RUNTIME_PATH . 'ApiLog' . DS;
if (self::$appInfo == 'null') {
self::$appInfo = 'null' . self::$separator . 'null' . self::$separator . 'null';
}
$logPath = Env::get('runtime_path') . 'ApiLog' . DIRECTORY_SEPARATOR;
$logStr = implode(self::$separator, array(
'[' . date('Y-m-d H:i:s') . '] ',
self::$apiInfo,
date('Y-m-d H:i:s'),
self::$request,
self::$header,
self::$response,
self::$requestAfterFilter,
self::$appInfo,
self::$userInfo
));
@ -97,7 +89,7 @@ class ApiLog {
*/
public static function writeLog($log, $type = 'sql', $filePath = '') {
if(!$filePath) {
$filePath = '.' . DS . 'runtime' . DS;
$filePath = '.' . DIRECTORY_SEPARATOR . 'runtime' . DIRECTORY_SEPARATOR;
}
$filename = $filePath . date("Ymd") . '_' . $type . ".log";
@$handle = fopen($filename, "a+");