增加部分api接口功能
This commit is contained in:
54
app/http/middleware/Api.php
Normal file
54
app/http/middleware/Api.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\http\middleware;
|
||||
use app\model\Config;
|
||||
use app\model\Department;
|
||||
use app\model\Dictionary;
|
||||
use app\model\Firm;
|
||||
use think\facade\Cache;
|
||||
|
||||
class Api {
|
||||
|
||||
protected $data = [];
|
||||
|
||||
public function handle($request, \Closure $next) {
|
||||
$request->pageConfig = array(
|
||||
'list_rows' => $request->param('limit', 30),
|
||||
'page' => $request->param('page', 1),
|
||||
);
|
||||
$this->cacheData($request); //缓存基础数据
|
||||
$response = $next($request);
|
||||
|
||||
if (is_array($response->getData())) {
|
||||
$this->data = array_merge($this->data, $response->getData());
|
||||
} else {
|
||||
$this->data = $response->getData();
|
||||
}
|
||||
|
||||
if ($request->isAjax()) {
|
||||
return json($this->data);
|
||||
} else {
|
||||
if (\is_string($this->data) && $this->data != '') {
|
||||
return $response;
|
||||
} else {
|
||||
return json($this->data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function cacheData($request) {
|
||||
//缓存配置信息
|
||||
$config = Cache::get('config');
|
||||
if (!$config) {
|
||||
Cache::set('config', Config::getConfigList($request));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
46
app/http/middleware/ApiAuth.php
Normal file
46
app/http/middleware/ApiAuth.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\http\middleware;
|
||||
|
||||
use app\model\MemberLog;
|
||||
use app\model\RoleAccess;
|
||||
use sent\jwt\exception\JWTException;
|
||||
use sent\jwt\exception\TokenExpiredException;
|
||||
use sent\jwt\JWTAuth as Auth;
|
||||
|
||||
class ApiAuth {
|
||||
|
||||
public $data = ['code' => 0];
|
||||
|
||||
public function __construct(Auth $auth) {
|
||||
$this->auth = $auth;
|
||||
}
|
||||
|
||||
public function handle($request, \Closure $next) {
|
||||
try {
|
||||
$auth = $this->auth->auth();
|
||||
$user = (array) $auth['data']->getValue();
|
||||
|
||||
$user['role'] = RoleAccess::getRoleByUid($user['uid']);
|
||||
$request->user = $user;
|
||||
//记录用户操作记录
|
||||
MemberLog::record($request);
|
||||
} catch (TokenExpiredException $e) {
|
||||
$this->data['msg'] = $e->getMessage();
|
||||
$this->data['code'] = 2001;
|
||||
return json($this->data)->code($this->data['code']);
|
||||
} catch (JWTException $e) {
|
||||
$this->data['code'] = 2000;
|
||||
$this->data['msg'] = $e->getMessage();
|
||||
return json($this->data)->code($this->data['code']);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -21,9 +21,10 @@ class Validate {
|
||||
//获取当前参数
|
||||
$params = $request->param();
|
||||
//获取访问控制器
|
||||
$controller = strtr(strtolower($request->controller()), '.', '\\');
|
||||
$controller = strtr($request->controller(), '.', '\\');
|
||||
|
||||
//获取操作名,用于验证场景scene
|
||||
$scene = $request->action();
|
||||
$scene = $request->action();
|
||||
$validate = "app\\http\\validate\\" . $controller;
|
||||
//仅当验证器存在时 进行校验
|
||||
if (class_exists($validate) && $request->isPost()) {
|
||||
@@ -35,8 +36,8 @@ class Validate {
|
||||
if (!$v->check($params)) {
|
||||
//校验不通过则直接返回错误信息
|
||||
$data = array(
|
||||
'msg' => $v->getError(),
|
||||
'code' => 1,
|
||||
'msg' => $v->getError(),
|
||||
'code' => 0,
|
||||
'data' => '',
|
||||
'time' => time(),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user