diff --git a/application/admin/controller/App.php b/application/admin/controller/App.php index 10e8bef..ff148d0 100644 --- a/application/admin/controller/App.php +++ b/application/admin/controller/App.php @@ -90,7 +90,7 @@ class App extends Base { if ($id) { $res = AdminApp::update($data, ['id' => $id]); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } } @@ -124,9 +124,9 @@ class App extends Base { } $res = AdminApp::create($data); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -144,11 +144,15 @@ class App extends Base { 'id' => $id ]); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { $appInfo = AdminApp::get($id); cache('AccessToken:' . $appInfo['app_secret'], null); - return $this->buildSuccess([]); + if($oldWiki = cache('WikiLogin:' . $id)) { + cache('WikiLogin:' . $oldWiki, null); + } + + return $this->buildSuccess(); } } @@ -176,11 +180,15 @@ class App extends Base { } $res = AdminApp::update($data, ['id' => $postData['id']]); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { $appInfo = AdminApp::get($postData['id']); cache('AccessToken:' . $appInfo['app_secret'], null); - return $this->buildSuccess([]); + if($oldWiki = cache('WikiLogin:' . $postData['id'])) { + cache('WikiLogin:' . $oldWiki, null); + } + + return $this->buildSuccess(); } } @@ -198,7 +206,10 @@ class App extends Base { cache('AccessToken:' . $appInfo['app_secret'], null); AdminApp::destroy($id); + if($oldWiki = cache('WikiLogin:' . $id)) { + cache('WikiLogin:' . $oldWiki, null); + } - return $this->buildSuccess([]); + return $this->buildSuccess(); } } diff --git a/application/admin/controller/AppGroup.php b/application/admin/controller/AppGroup.php index 3f39382..c8f6a7e 100644 --- a/application/admin/controller/AppGroup.php +++ b/application/admin/controller/AppGroup.php @@ -80,9 +80,9 @@ class AppGroup extends Base { 'id' => $id ]); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -95,9 +95,9 @@ class AppGroup extends Base { $postData = $this->request->post(); $res = AdminAppGroup::create($postData); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -110,9 +110,9 @@ class AppGroup extends Base { $postData = $this->request->post(); $res = AdminAppGroup::update($postData); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -134,6 +134,6 @@ class AppGroup extends Base { AdminAppGroup::destroy(['hash' => $hash]); - return $this->buildSuccess([]); + return $this->buildSuccess(); } } diff --git a/application/admin/controller/Auth.php b/application/admin/controller/Auth.php index 708802f..cce7462 100644 --- a/application/admin/controller/Auth.php +++ b/application/admin/controller/Auth.php @@ -109,7 +109,7 @@ class Auth extends Base { unset($postData['rules']); $res = AdminAuthGroup::create($postData); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { if ($rules) { $insertData = []; @@ -124,7 +124,7 @@ class Auth extends Base { (new AdminAuthRule())->saveAll($insertData); } - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -141,9 +141,9 @@ class Auth extends Base { 'status' => $status ]); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -164,9 +164,9 @@ class Auth extends Base { unset($postData['rules']); $res = AdminAuthGroup::update($postData); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -199,7 +199,7 @@ class Auth extends Base { AdminAuthGroup::destroy($id); AdminAuthRule::destroy(['group_id' => $id]); - return $this->buildSuccess([]); + return $this->buildSuccess(); } /** @@ -226,9 +226,9 @@ class Auth extends Base { 'uid' => $uid ]); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { - return $this->buildSuccess([]); + return $this->buildSuccess(); } } diff --git a/application/admin/controller/Base.php b/application/admin/controller/Base.php index ea9d552..ee8996d 100644 --- a/application/admin/controller/Base.php +++ b/application/admin/controller/Base.php @@ -22,13 +22,13 @@ class Base extends Controller { $this->userInfo = $this->request->API_ADMIN_USER_INFO; } - public function buildSuccess($data, $msg = '操作成功', $code = ReturnCode::SUCCESS) { + public function buildSuccess($data = [], $msg = '操作成功', $code = ReturnCode::SUCCESS) { $return = [ 'code' => $code, 'msg' => $msg, 'data' => $data ]; - if ($this->debug) { + if (config('app.app_debug') && $this->debug) { $return['debug'] = $this->debug; } @@ -56,13 +56,13 @@ class Base extends Controller { cache('Login:' . $apiAuth, json_encode($this->userInfo), config('apiadmin.ONLINE_TIME')); } - public function buildFailed($code, $msg, $data = []) { + public function buildFailed($code, $msg = '操作失败', $data = []) { $return = [ 'code' => $code, 'msg' => $msg, 'data' => $data ]; - if ($this->debug) { + if (config('app.app_debug') && $this->debug) { $return['debug'] = $this->debug; } diff --git a/application/admin/controller/Fields.php b/application/admin/controller/Fields.php index 70c22cb..fe51246 100644 --- a/application/admin/controller/Fields.php +++ b/application/admin/controller/Fields.php @@ -104,9 +104,9 @@ class Fields extends Base { cache('ResponseFieldsRule:' . $postData['hash'], null); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { - return $this->buildSuccess('操作成功'); + return $this->buildSuccess(); } } @@ -127,9 +127,9 @@ class Fields extends Base { cache('ResponseFieldsRule:' . $postData['hash'], null); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -152,7 +152,7 @@ class Fields extends Base { AdminFields::destroy($id); - return $this->buildSuccess([]); + return $this->buildSuccess(); } /** @@ -200,7 +200,7 @@ class Fields extends Base { cache('RequestFields:Rule:' . $hash, null); cache('ResponseFieldsRule:' . $hash, null); - return $this->buildSuccess([]); + return $this->buildSuccess(); } private function handle($data, &$dataArr, $prefix = 'data', $index = 'data') { diff --git a/application/admin/controller/InterfaceGroup.php b/application/admin/controller/InterfaceGroup.php index bc14a08..9bbf797 100644 --- a/application/admin/controller/InterfaceGroup.php +++ b/application/admin/controller/InterfaceGroup.php @@ -79,9 +79,9 @@ class InterfaceGroup extends Base { 'id' => $id ]); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -94,9 +94,9 @@ class InterfaceGroup extends Base { $postData = $this->request->post(); $res = AdminGroup::create($postData); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -109,9 +109,9 @@ class InterfaceGroup extends Base { $postData = $this->request->post(); $res = AdminGroup::update($postData); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -153,6 +153,6 @@ class InterfaceGroup extends Base { AdminGroup::destroy(['hash' => $hash]); - return $this->buildSuccess([]); + return $this->buildSuccess(); } } diff --git a/application/admin/controller/InterfaceList.php b/application/admin/controller/InterfaceList.php index 29901ba..64b39cf 100644 --- a/application/admin/controller/InterfaceList.php +++ b/application/admin/controller/InterfaceList.php @@ -78,9 +78,9 @@ class InterfaceList extends Base { $res = AdminList::create($postData); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -98,11 +98,11 @@ class InterfaceList extends Base { 'hash' => $hash ]); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { cache('ApiInfo:' . $hash, null); - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -119,11 +119,11 @@ class InterfaceList extends Base { $res = AdminList::update($postData); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { cache('ApiInfo:' . $postData['hash'], null); - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -166,7 +166,7 @@ class InterfaceList extends Base { cache('ApiInfo:' . $hash, null); - return $this->buildSuccess([]); + return $this->buildSuccess(); } /** @@ -183,14 +183,14 @@ class InterfaceList extends Base { $tplOriginStr = file_get_contents($tplPath); $listInfo = AdminList::all(['status' => 1]); - $tplStr = ''; + $tplStr = []; foreach ($listInfo as $value) { - $tplStr .= 'Route::rule(\'' . addslashes($value->hash) . '\',\'api/' . addslashes($value->api_class) . '\', \'' . $methodArr[$value->method] . '\')->middleware([\'ApiAuth\', \'ApiPermission\', \'RequestFilter\', \'ApiLog\']);'; + array_push($tplStr, 'Route::rule(\'' . addslashes($value->hash) . '\',\'api/' . addslashes($value->api_class) . '\', \'' . $methodArr[$value->method] . '\')->middleware([\'ApiAuth\', \'ApiPermission\', \'RequestFilter\', \'ApiLog\']);'); } - $tplOriginStr = str_replace(['{$API_RULE}'], [$tplStr], $tplOriginStr); + $tplOriginStr = str_replace(['{$API_RULE}'], [implode($tplStr, "\n ")], $tplOriginStr); file_put_contents($apiRoutePath, $tplOriginStr); - return $this->buildSuccess([]); + return $this->buildSuccess(); } } diff --git a/application/admin/controller/Log.php b/application/admin/controller/Log.php index df9d32f..be8e98f 100644 --- a/application/admin/controller/Log.php +++ b/application/admin/controller/Log.php @@ -64,7 +64,7 @@ class Log extends Base { } AdminUserAction::destroy($id); - return $this->buildSuccess([]); + return $this->buildSuccess(); } diff --git a/application/admin/controller/Menu.php b/application/admin/controller/Menu.php index c94f496..784f73a 100644 --- a/application/admin/controller/Menu.php +++ b/application/admin/controller/Menu.php @@ -27,7 +27,7 @@ class Menu extends Base { return $this->buildSuccess([ 'list' => $list - ], '登录成功'); + ]); } /** @@ -42,9 +42,9 @@ class Menu extends Base { } $res = AdminMenu::create($postData); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -61,9 +61,9 @@ class Menu extends Base { 'hide' => $status ]); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -79,9 +79,9 @@ class Menu extends Base { } $res = AdminMenu::update($postData); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -101,7 +101,7 @@ class Menu extends Base { } else { AdminMenu::destroy($id); - return $this->buildSuccess([]); + return $this->buildSuccess(); } } diff --git a/application/admin/controller/Miss.php b/application/admin/controller/Miss.php index 4f7a164..dd32528 100644 --- a/application/admin/controller/Miss.php +++ b/application/admin/controller/Miss.php @@ -6,9 +6,9 @@ use app\util\ReturnCode; class Miss extends Base { public function index() { if ($this->request->isOptions()) { - return $this->buildSuccess([]); + return $this->buildSuccess(); } else { - return $this->buildFailed(ReturnCode::INVALID, '接口地址异常', []); + return $this->buildFailed(ReturnCode::INVALID, '接口地址异常'); } } } diff --git a/application/admin/controller/User.php b/application/admin/controller/User.php index b78017d..97e1401 100644 --- a/application/admin/controller/User.php +++ b/application/admin/controller/User.php @@ -95,14 +95,14 @@ class User extends Base { } $res = AdminUser::create($postData); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { AdminAuthGroupAccess::create([ 'uid' => $res->id, 'group_id' => $groups ]); - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -160,9 +160,13 @@ class User extends Base { 'status' => $status ]); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { - return $this->buildSuccess([]); + if($oldAdmin = cache('Login:' . $id)) { + cache('Login:' . $oldAdmin, null); + } + + return $this->buildSuccess(); } } @@ -186,7 +190,7 @@ class User extends Base { } $res = AdminUser::update($postData); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { $has = AdminAuthGroupAccess::get(['uid' => $postData['id']]); if ($has) { @@ -201,8 +205,11 @@ class User extends Base { 'group_id' => $groups ]); } + if($oldAdmin = cache('Login:' . $postData['id'])) { + cache('Login:' . $oldAdmin, null); + } - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -232,13 +239,16 @@ class User extends Base { unset($postData['head_img']); $res = AdminUser::update($postData); if ($res === false) { - return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败'); + return $this->buildFailed(ReturnCode::DB_SAVE_ERROR); } else { $userData = AdminUserData::get(['uid' => $postData['id']]); $userData->head_img = $headImg; $userData->save(); + if($oldWiki = cache('WikiLogin:' . $postData['id'])) { + cache('WikiLogin:' . $oldWiki, null); + } - return $this->buildSuccess([]); + return $this->buildSuccess(); } } @@ -259,8 +269,11 @@ class User extends Base { } AdminUser::destroy($id); AdminAuthGroupAccess::destroy(['uid' => $id]); + if($oldAdmin = cache('Login:' . $id)) { + cache('Login:' . $oldAdmin, null); + } - return $this->buildSuccess([]); + return $this->buildSuccess(); } diff --git a/application/api/controller/Base.php b/application/api/controller/Base.php index fad6eaa..3cf4c5d 100644 --- a/application/api/controller/Base.php +++ b/application/api/controller/Base.php @@ -20,30 +20,30 @@ class Base extends Controller { // $this->userInfo = ''; 这部分初始化用户信息可以参考admin模块下的Base去自行处理 } - public function buildSuccess($data, $msg = '操作成功', $code = ReturnCode::SUCCESS) { + public function buildSuccess($data = [], $msg = '操作成功', $code = ReturnCode::SUCCESS) { $return = [ 'code' => $code, 'msg' => $msg, 'data' => $data ]; - if ($this->debug) { + if (config('app.app_debug') && $this->debug) { $return['debug'] = $this->debug; } - return json($return); + return $return; } - public function buildFailed($code, $msg, $data = []) { + public function buildFailed($code, $msg = '操作失败', $data = []) { $return = [ 'code' => $code, 'msg' => $msg, 'data' => $data ]; - if ($this->debug) { + if (config('app.app_debug') && $this->debug) { $return['debug'] = $this->debug; } - return json($return); + return $return; } protected function debug($data) { diff --git a/application/util/ExceptionHandle.php b/application/util/ExceptionHandle.php new file mode 100644 index 0000000..c137683 --- /dev/null +++ b/application/util/ExceptionHandle.php @@ -0,0 +1,24 @@ + + */ + +namespace app\util; + +use Exception; +use think\exception\Handle; + +/** + * Class ExceptionHandle + * @package app\util + * 异常处理handle类 + * Detail see: https://www.kancloud.cn/manual/thinkphp5_1/354092 + */ +class ExceptionHandle extends Handle { + + public function render(Exception $e) { + return parent::render($e)->header(config('apiadmin.CROSS_DOMAIN')); + } +} diff --git a/application/wiki/controller/Api.php b/application/wiki/controller/Api.php index 0837d2c..95ba5c7 100644 --- a/application/wiki/controller/Api.php +++ b/application/wiki/controller/Api.php @@ -114,7 +114,9 @@ class Api extends Base { foreach ($item as $apiItem) { $_listInfo['api_info'][] = $apiInfo[$apiItem]; } - $listInfo[] = $_listInfo; + if (isset($_listInfo['api_info'])) { + $listInfo[] = $_listInfo; + } } } diff --git a/application/wiki/controller/Base.php b/application/wiki/controller/Base.php index a18656e..6a4e5b7 100644 --- a/application/wiki/controller/Base.php +++ b/application/wiki/controller/Base.php @@ -20,24 +20,24 @@ class Base extends Controller { $this->appInfo = $this->request->API_WIKI_USER_INFO; } - public function buildSuccess($data, $msg = '操作成功', $code = ReturnCode::SUCCESS) { + public function buildSuccess($data = [], $msg = '操作成功', $code = ReturnCode::SUCCESS) { $return = [ 'code' => $code, 'msg' => $msg, 'data' => $data ]; - return json($return); + return $return; } - public function buildFailed($code, $msg, $data = []) { + public function buildFailed($code, $msg = '操作失败', $data = []) { $return = [ 'code' => $code, 'msg' => $msg, 'data' => $data ]; - return json($return); + return $return; } } diff --git a/config/app.php b/config/app.php index bde326a..d599a21 100644 --- a/config/app.php +++ b/config/app.php @@ -141,6 +141,6 @@ return [ // 显示错误信息 'show_error_msg' => false, // 异常处理handle类 留空使用 \think\exception\Handle - 'exception_handle' => '', + 'exception_handle' => '\\app\\util\\ExceptionHandle', ];