Skip to content

Commit f30fa65

Browse files
author
zhaoxiang
committed
modified 完善前端接口跨域问题 特别鸣谢:@HUSEH @初柒
1 parent 990e869 commit f30fa65

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

application/api/behavior/ApiAuth.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class ApiAuth {
2121
*/
2222
private $request;
2323
private $apiInfo;
24+
private $header;
2425

2526
/**
2627
* 默认行为函数
@@ -32,6 +33,7 @@ class ApiAuth {
3233
public function run() {
3334
$this->request = Request::instance();
3435
$hash = $this->request->routeInfo();
36+
$this->header = config('apiAdmin.CROSS_DOMAIN');
3537
if (isset($hash['rule'][1])) {
3638
$hash = $hash['rule'][1];
3739

@@ -44,7 +46,7 @@ public function run() {
4446
$this->apiInfo = $apiInfo->toArray();
4547
Cache::set('ApiInfo:' . $hash, $this->apiInfo);
4648
} else {
47-
return json(['code' => ReturnCode::DB_READ_ERROR, 'msg' => '获取接口配置数据失败', 'data' => []]);
49+
return json(['code' => ReturnCode::DB_READ_ERROR, 'msg' => '获取接口配置数据失败', 'data' => []], 200, $this->header);
4850
}
4951
}
5052

@@ -75,11 +77,11 @@ public function run() {
7577
private function checkAccessToken() {
7678
$access_token = $this->request->header('access-token');
7779
if (!isset($access_token) || !$access_token) {
78-
return json(['code' => ReturnCode::ACCESS_TOKEN_TIMEOUT, 'msg' => '缺少参数access-token', 'data' => []]);
80+
return json(['code' => ReturnCode::ACCESS_TOKEN_TIMEOUT, 'msg' => '缺少参数access-token', 'data' => []], 200, $this->header);
7981
} else {
8082
$appInfo = cache('AccessToken:' . $access_token);
8183
if (!$appInfo) {
82-
return json(['code' => ReturnCode::ACCESS_TOKEN_TIMEOUT, 'msg' => 'access-token已过期', 'data' => []]);
84+
return json(['code' => ReturnCode::ACCESS_TOKEN_TIMEOUT, 'msg' => 'access-token已过期', 'data' => []], 200, $this->header);
8385
}
8486
ApiLog::setAppInfo($appInfo);
8587
}
@@ -91,10 +93,10 @@ private function checkAccessToken() {
9193
private function checkVersion() {
9294
$version = $this->request->header('version');
9395
if (!isset($version) || !$version) {
94-
return json(['code' => ReturnCode::EMPTY_PARAMS, 'msg' => '缺少参数version', 'data' => []]);
96+
return json(['code' => ReturnCode::EMPTY_PARAMS, 'msg' => '缺少参数version', 'data' => []], 200, $this->header);
9597
} else {
9698
if ($version != config('apiAdmin.APP_VERSION')) {
97-
return json(['code' => ReturnCode::VERSION_INVALID, 'msg' => 'API版本不匹配', 'data' => []]);
99+
return json(['code' => ReturnCode::VERSION_INVALID, 'msg' => 'API版本不匹配', 'data' => []], 200, $this->header);
98100
}
99101
}
100102
}
@@ -107,13 +109,13 @@ private function checkLogin() {
107109
$userToken = $this->request->header('user-token', '');
108110
if ($this->apiInfo['needLogin']) {
109111
if (!$userToken) {
110-
return json(['code' => ReturnCode::AUTH_ERROR, 'msg' => '缺少user-token', 'data' => []]);
112+
return json(['code' => ReturnCode::AUTH_ERROR, 'msg' => '缺少user-token', 'data' => []], 200, $this->header);
111113
}
112114
}
113115
if ($userToken) {
114116
$userInfo = cache('wx:openId:' . $userToken);
115117
if (!is_array($userInfo) || !isset($userInfo['openId'])) {
116-
return json(['code' => ReturnCode::AUTH_ERROR, 'msg' => 'user-token不匹配', 'data' => []]);
118+
return json(['code' => ReturnCode::AUTH_ERROR, 'msg' => 'user-token不匹配', 'data' => []], 200, $this->header);
117119
}
118120
ApiLog::setUserInfo($userInfo);
119121
}

application/api/behavior/ApiPermission.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ApiPermission {
2525
*/
2626
public function run() {
2727
$this->request = Request::instance();
28+
$header = config('apiAdmin.CROSS_DOMAIN');
2829
$hash = $this->request->routeInfo();
2930
if (isset($hash['rule'][1])) {
3031
$hash = $hash['rule'][1];
@@ -33,9 +34,7 @@ public function run() {
3334
$appInfo = cache('AccessToken:' . $access_token);
3435
$allRules = explode(',', $appInfo['app_api']);
3536
if (!in_array($hash, $allRules)) {
36-
$data = ['code' => ReturnCode::INVALID, 'msg' => '非常抱歉,您没有权限这么做!', 'data' => []];
37-
38-
return json($data);
37+
return json(['code' => ReturnCode::INVALID, 'msg' => '非常抱歉,您没有权限这么做!', 'data' => []], 200, $header);
3938
}
4039
}
4140
}

application/api/behavior/RequestFilter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public function run() {
6363
if ($newRule) {
6464
$validate = new Validate($newRule);
6565
if (!$validate->check($data)) {
66-
return json(['code' => ReturnCode::PARAM_INVALID, 'msg' => $validate->getError(), 'data' => []]);
66+
$header = config('apiAdmin.CROSS_DOMAIN');
67+
return json(['code' => ReturnCode::PARAM_INVALID, 'msg' => $validate->getError(), 'data' => []], 200, $header);
6768
}
6869
}
6970

0 commit comments

Comments
 (0)