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() {
$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);
}
}

View File

@ -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 <zhaoxiang051405@gmail.com>
*/
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);
}
}
}

View File

@ -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 {
}
}
}
}

View File

@ -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格式返回的处理方法

View File

@ -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'
],
];