diff --git a/application/admin/behavior/ApiAuth.php b/application/admin/behavior/ApiAuth.php index bcde6f7..7d9f9e6 100644 --- a/application/admin/behavior/ApiAuth.php +++ b/application/admin/behavior/ApiAuth.php @@ -22,15 +22,18 @@ class ApiAuth { */ public function run() { $request = Request::instance(); + $header = config('apiAdmin.CROSS_DOMAIN'); $userToken = $request->header('Authorization', ''); if ($userToken) { $userInfo = cache($userToken); $userInfo = json_decode($userInfo, true); 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 { - return json(['code' => ReturnCode::AUTH_ERROR, 'msg' => '缺少Authorization', 'data' => []]); + $data = ['code' => ReturnCode::AUTH_ERROR, 'msg' => '缺少Authorization', 'data' => []]; + return json($data, 200, $header); } } diff --git a/application/admin/behavior/BuildResponse.php b/application/admin/behavior/BuildResponse.php index d1416fb..5e76c2a 100644 --- a/application/admin/behavior/BuildResponse.php +++ b/application/admin/behavior/BuildResponse.php @@ -8,6 +8,9 @@ namespace app\admin\behavior; +use think\Config; +use think\Response; + class BuildResponse { /** @@ -15,12 +18,9 @@ class BuildResponse { * @param $response * @author zhaoxiang */ - public function run($response) { - $header['Access-Control-Allow-Origin'] = '*'; - $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'; + public function run(Response $response) { + $header = Config::get('apiAdmin.CROSS_DOMAIN'); $response->header($header); } -} \ No newline at end of file +} diff --git a/application/admin/controller/Base.php b/application/admin/controller/Base.php index f5ad502..18893ab 100644 --- a/application/admin/controller/Base.php +++ b/application/admin/controller/Base.php @@ -27,7 +27,7 @@ class Base extends Controller { $return['debug'] = $this->debug; } - return json($return); + return $return; } public function buildFailed($code, $msg, $data = []) { @@ -40,7 +40,7 @@ class Base extends Controller { $return['debug'] = $this->debug; } - return json($return); + return $return; } protected function debug($data) { @@ -49,4 +49,4 @@ class Base extends Controller { } } -} \ No newline at end of file +} diff --git a/application/config.php b/application/config.php index 45eabd5..ba55cb6 100755 --- a/application/config.php +++ b/application/config.php @@ -31,7 +31,7 @@ return [ // 扩展函数文件 'extra_file_list' => [THINK_PATH . 'helper' . EXT], // 默认输出类型 - 'default_return_type' => 'html', + 'default_return_type' => 'json', // 默认AJAX 数据返回格式,可选json xml ... 'default_ajax_return' => 'json', // 默认JSONP格式返回的处理方法 diff --git a/application/extra/apiAdmin.php b/application/extra/apiAdmin.php index 446857f..3965be8 100644 --- a/application/extra/apiAdmin.php +++ b/application/extra/apiAdmin.php @@ -18,4 +18,12 @@ return [ 'ONLINE_TIME' => 7200, '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' + ], ];