modified 优化跨域支持

This commit is contained in:
zhaoxiang 2018-02-03 16:53:15 +08:00
parent d1788b9fe0
commit ca028dac85
5 changed files with 23 additions and 12 deletions

View File

@ -22,15 +22,18 @@ class ApiAuth {
*/ */
public function run() { public function run() {
$request = Request::instance(); $request = Request::instance();
$header = config('apiAdmin.CROSS_DOMAIN');
$userToken = $request->header('Authorization', ''); $userToken = $request->header('Authorization', '');
if ($userToken) { if ($userToken) {
$userInfo = cache($userToken); $userInfo = cache($userToken);
$userInfo = json_decode($userInfo, true); $userInfo = json_decode($userInfo, true);
if (!$userInfo || !isset($userInfo['id'])) { if (!$userInfo || !isset($userInfo['id'])) {
return json(['code' => ReturnCode::AUTH_ERROR, 'msg' => 'Authorization不匹配', 'data' => []]); $data = ['code' => ReturnCode::AUTH_ERROR, 'msg' => 'Authorization不匹配', 'data' => []];
return json($data, 200, $header);
} }
} else { } else {
return json(['code' => ReturnCode::AUTH_ERROR, 'msg' => '缺少Authorization', 'data' => []]); $data = ['code' => ReturnCode::AUTH_ERROR, 'msg' => '缺少Authorization', 'data' => []];
return json($data, 200, $header);
} }
} }

View File

@ -8,6 +8,9 @@
namespace app\admin\behavior; namespace app\admin\behavior;
use think\Config;
use think\Response;
class BuildResponse { class BuildResponse {
/** /**
@ -15,11 +18,8 @@ class BuildResponse {
* @param $response * @param $response
* @author zhaoxiang <zhaoxiang051405@gmail.com> * @author zhaoxiang <zhaoxiang051405@gmail.com>
*/ */
public function run($response) { public function run(Response $response) {
$header['Access-Control-Allow-Origin'] = '*'; $header = Config::get('apiAdmin.CROSS_DOMAIN');
$header['Access-Control-Allow-Methods'] = 'POST,PUT,GET,DELETE';
$header['Access-Control-Allow-Headers'] = 'Authorization, User-Agent, Keep-Alive, Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With';
$header['Access-Control-Allow-Credentials'] = 'true';
$response->header($header); $response->header($header);
} }

View File

@ -27,7 +27,7 @@ class Base extends Controller {
$return['debug'] = $this->debug; $return['debug'] = $this->debug;
} }
return json($return); return $return;
} }
public function buildFailed($code, $msg, $data = []) { public function buildFailed($code, $msg, $data = []) {
@ -40,7 +40,7 @@ class Base extends Controller {
$return['debug'] = $this->debug; $return['debug'] = $this->debug;
} }
return json($return); return $return;
} }
protected function debug($data) { protected function debug($data) {

View File

@ -31,7 +31,7 @@ return [
// 扩展函数文件 // 扩展函数文件
'extra_file_list' => [THINK_PATH . 'helper' . EXT], 'extra_file_list' => [THINK_PATH . 'helper' . EXT],
// 默认输出类型 // 默认输出类型
'default_return_type' => 'html', 'default_return_type' => 'json',
// 默认AJAX 数据返回格式,可选json xml ... // 默认AJAX 数据返回格式,可选json xml ...
'default_ajax_return' => 'json', 'default_ajax_return' => 'json',
// 默认JSONP格式返回的处理方法 // 默认JSONP格式返回的处理方法

View File

@ -18,4 +18,12 @@ return [
'ONLINE_TIME' => 7200, 'ONLINE_TIME' => 7200,
'COMPANY_NAME' => 'ApiAdmin开发维护团队', 'COMPANY_NAME' => 'ApiAdmin开发维护团队',
//跨域配置
'CROSS_DOMAIN' => [
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'POST,PUT,GET,DELETE',
'Access-Control-Allow-Headers' => 'Authorization, User-Agent, Keep-Alive, Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With',
'Access-Control-Allow-Credentials' => 'true'
],
]; ];