From b46a9f13ad290e381d9b68c067c0fa30423587f1 Mon Sep 17 00:00:00 2001 From: zhaoxiang Date: Mon, 26 Feb 2018 11:25:52 +0800 Subject: [PATCH] =?UTF-8?q?modified=20=E7=BB=86=E8=8A=82=E8=B0=83=E6=95=B4?= =?UTF-8?q?=EF=BC=8C=E6=96=B0=E5=A2=9E=E5=8A=A0=E6=96=87=E6=A1=A3=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/controller/App.php | 1 - application/admin/controller/Base.php | 15 ---- application/admin/controller/User.php | 6 +- application/api/controller/BuildToken.php | 8 ++ application/apiRoute.php | 34 ++++++++- application/util/Tools.php | 15 ++++ application/wiki/controller/Base.php | 75 ++++++++++++++++++- application/wiki/controller/Index.php | 70 ++++++++++++++++-- application/wiki/view/index/calculation.html | 10 +-- application/wiki/view/index/errorCode.html | 4 +- application/wiki/view/index/index.html | 77 +++++++++++--------- application/wiki/view/index/login.html | 4 +- 12 files changed, 249 insertions(+), 70 deletions(-) diff --git a/application/admin/controller/App.php b/application/admin/controller/App.php index 839323a..e1f6503 100644 --- a/application/admin/controller/App.php +++ b/application/admin/controller/App.php @@ -70,7 +70,6 @@ class App extends Base { $groupArr = ApiGroup::all(); $groupArr = Tools::buildArrFromObj($groupArr); $res['groupInfo'] = array_column($groupArr, 'name', 'hash'); - $res['groupInfo']['default'] = '默认分组'; $id = $this->request->get('id', 0); if ($id) { $appInfo = ApiApp::get($id)->toArray(); diff --git a/application/admin/controller/Base.php b/application/admin/controller/Base.php index 0ce50e4..18893ab 100644 --- a/application/admin/controller/Base.php +++ b/application/admin/controller/Base.php @@ -43,21 +43,6 @@ class Base extends Controller { return $return; } - /** - * 将二维数组变成指定key - * @param $array - * @param $keyName - * @author zhaoxiang - * @return array - */ - protected function buildArrByNewKey($array, $keyName = 'id') { - $list = array(); - foreach ($array as $item) { - $list[$item[$keyName]] = $item; - } - return $list; - } - protected function debug($data) { if ($data) { $this->debug[] = $data; diff --git a/application/admin/controller/User.php b/application/admin/controller/User.php index ed57d90..b258a3d 100644 --- a/application/admin/controller/User.php +++ b/application/admin/controller/User.php @@ -56,13 +56,13 @@ class User extends Base { $query->whereIn('uid', $idArr); }); $userData = Tools::buildArrFromObj($userData); - $userData = $this->buildArrByNewKey($userData, 'uid'); + $userData = Tools::buildArrByNewKey($userData, 'uid'); $userGroup = ApiAuthGroupAccess::all(function($query) use ($idArr) { $query->whereIn('uid', $idArr); }); $userGroup = Tools::buildArrFromObj($userGroup); - $userGroup = $this->buildArrByNewKey($userGroup, 'uid'); + $userGroup = Tools::buildArrByNewKey($userGroup, 'uid'); foreach ($listInfo as $key => $value) { if (isset($userData[$value['id']])) { @@ -140,7 +140,7 @@ class User extends Base { $query->whereIn('uid', $uidArr); }); $userData = Tools::buildArrFromObj($userData); - $userData = $this->buildArrByNewKey($userData, 'uid'); + $userData = Tools::buildArrByNewKey($userData, 'uid'); foreach ($userInfo as $key => $value) { if (isset($userData[$value['id']])) { diff --git a/application/api/controller/BuildToken.php b/application/api/controller/BuildToken.php index 145720f..a756a8c 100644 --- a/application/api/controller/BuildToken.php +++ b/application/api/controller/BuildToken.php @@ -49,6 +49,14 @@ class BuildToken extends Base { return $this->buildSuccess($return); } + public function e1() { + return $this->buildSuccess('e1'); + } + + public function e2() { + return $this->buildSuccess('e2'); + } + /** * 根据AppSecret和数据生成相对应的身份认证秘钥 * @param $appSecret diff --git a/application/apiRoute.php b/application/apiRoute.php index f1e6452..8744858 100644 --- a/application/apiRoute.php +++ b/application/apiRoute.php @@ -9,10 +9,18 @@ $afterBehavior = ['\app\api\behavior\ApiAuth', '\app\api\behavior\RequestFilter' return [ '[api]' => [ - '58bf98c1dcb63' => [ + '5a9363c133719' => [ 'api/BuildToken/getAccessToken', ['method' => 'get', 'after_behavior' => $afterBehavior] ], + '5a93646b40ab5' => [ + 'api/BuildToken/e1', + ['method' => 'get', 'after_behavior' => $afterBehavior] + ], + '5a93648c769f8' => [ + 'api/BuildToken/e2', + ['method' => 'get', 'after_behavior' => $afterBehavior] + ], '__miss__' => ['api/Miss/index'], ], '[wiki]' => [ @@ -20,6 +28,30 @@ return [ 'wiki/index/login', ['method' => 'get'] ], + 'doLogin' => [ + 'wiki/index/doLogin', + ['method' => 'post'] + ], + 'index' => [ + 'wiki/index/index', + ['method' => 'get'] + ], + 'calculation' => [ + 'wiki/index/calculation', + ['method' => 'get'] + ], + 'errorCode' => [ + 'wiki/index/errorCode', + ['method' => 'get'] + ], + 'detail/:hash' => [ + 'wiki/index/detail', + ['method' => 'get'] + ], + 'logout' => [ + 'wiki/index/logout', + ['method' => 'get'] + ], '__miss__' => ['api/Miss/index'], ], ]; diff --git a/application/util/Tools.php b/application/util/Tools.php index b1007e0..2b62a69 100644 --- a/application/util/Tools.php +++ b/application/util/Tools.php @@ -97,4 +97,19 @@ class Tools { return $arr; } + + /** + * 将二维数组变成指定key + * @param $array + * @param $keyName + * @author zhaoxiang + * @return array + */ + public static function buildArrByNewKey($array, $keyName = 'id') { + $list = array(); + foreach ($array as $item) { + $list[$item[$keyName]] = $item; + } + return $list; + } } diff --git a/application/wiki/controller/Base.php b/application/wiki/controller/Base.php index d28dcec..8eb1a00 100644 --- a/application/wiki/controller/Base.php +++ b/application/wiki/controller/Base.php @@ -8,12 +8,83 @@ namespace app\wiki\controller; +use think\Config; use think\Controller; +use think\exception\HttpResponseException; +use think\Request; +use think\Response; +use think\View as ViewTemplate; +use think\Url; class Base extends Controller { - public function _initialize() { + protected $appInfo; + public function checkLogin() { + $appInfo = session('app_info'); + if ($appInfo) { + $this->appInfo = json_decode($appInfo, true); + } else { + $this->redirect(url('/wiki/login')); + } } -} \ No newline at end of file + public function error($msg = '', $url = null, $data = '', $wait = 3, array $header = []) { + if (is_null($url)) { + $url = Request::instance()->isAjax() ? '' : 'javascript:history.back(-1);'; + } elseif ('' !== $url && !strpos($url, '://') && 0 !== strpos($url, '/')) { + $url = Url::build($url); + } + + $type = 'html'; + $result = [ + 'code' => 0, + 'msg' => $msg, + 'data' => $data, + 'url' => $url, + 'wait' => $wait, + ]; + + if ('html' == strtolower($type)) { + $template = Config::get('template'); + $view = Config::get('view_replace_str'); + + $result = ViewTemplate::instance($template, $view) + ->fetch(Config::get('dispatch_error_tmpl'), $result); + } + + $response = Response::create($result, $type)->header($header); + + throw new HttpResponseException($response); + } + + public function success($msg = '', $url = null, $data = '', $wait = 3, array $header = []) { + if (is_null($url) && !is_null(Request::instance()->server('HTTP_REFERER'))) { + $url = Request::instance()->server('HTTP_REFERER'); + } elseif ('' !== $url && !strpos($url, '://') && 0 !== strpos($url, '/')) { + $url = Url::build($url); + } + + $type = 'html'; + $result = [ + 'code' => 1, + 'msg' => $msg, + 'data' => $data, + 'url' => $url, + 'wait' => $wait, + ]; + + if ('html' == strtolower($type)) { + $template = Config::get('template'); + $view = Config::get('view_replace_str'); + + $result = ViewTemplate::instance($template, $view) + ->fetch(Config::get('dispatch_success_tmpl'), $result); + } + + $response = Response::create($result, $type)->header($header); + + throw new HttpResponseException($response); + } + +} diff --git a/application/wiki/controller/Index.php b/application/wiki/controller/Index.php index e521767..ba387fa 100644 --- a/application/wiki/controller/Index.php +++ b/application/wiki/controller/Index.php @@ -8,15 +8,36 @@ namespace app\wiki\controller; +use app\model\ApiApp; use app\model\ApiFields; +use app\model\ApiGroup; use app\model\ApiList; use app\util\DataType; use app\util\ReturnCode; +use app\util\Tools; class Index extends Base { + /** + * 获取应用列表 + * @return \think\response\View + * @throws \think\exception\DbException + * @author zhaoxiang + */ public function index() { - return $this->fetch(); + $this->checkLogin(); + + $groupInfo = ApiGroup::all(); + $groupInfo = Tools::buildArrFromObj($groupInfo); + $groupInfo = Tools::buildArrByNewKey($groupInfo, 'hash'); + + $this->appInfo = ApiApp::get(['app_id' => $this->appInfo['app_id']]); + $this->appInfo['app_api_show'] = json_decode($this->appInfo['app_api_show'], true); + + return view('', [ + 'groupInfo' => $groupInfo, + 'appInfo' => $this->appInfo + ]); } public function detail() { @@ -58,10 +79,13 @@ class Index extends Base { } public function calculation() { - return $this->fetch(); + $this->checkLogin(); + + return view(); } public function errorCode() { + $this->checkLogin(); $codeArr = ReturnCode::getConstants(); $errorInfo = array( ReturnCode::SUCCESS => '请求成功', @@ -81,21 +105,55 @@ class Index extends Base { ReturnCode::AUTH_ERROR => '权限认证失败', ReturnCode::OTHER_LOGIN => '别的终端登录', ReturnCode::VERSION_INVALID => 'API版本非法', + ReturnCode::CURL_ERROR => 'CURL操作异常', + ReturnCode::RECORD_NOT_FOUND => '记录未找到', + ReturnCode::DELETE_FAILED => '删除失败', + ReturnCode::ADD_FAILED => '添加记录失败', + ReturnCode::UPDATE_FAILED => '添加记录失败', ReturnCode::PARAM_INVALID => '数据类型非法', ReturnCode::ACCESS_TOKEN_TIMEOUT => '身份令牌过期', ReturnCode::SESSION_TIMEOUT => 'SESSION过期', ReturnCode::UNKNOWN => '未知错误', ReturnCode::EXCEPTION => '系统异常', - ReturnCode::CURL_ERROR => 'CURL操作异常' ); - $this->assign('errorInfo', $errorInfo); - $this->assign('codeArr', $codeArr); - return $this->fetch(); + return view('', [ + 'errorInfo' => $errorInfo, + 'codeArr' => $codeArr + ]); } public function login() { return view(); } + /** + * 处理wiki登录 + * @throws \think\Exception + * @throws \think\exception\DbException + * @author zhaoxiang + */ + public function doLogin() { + $appId = $this->request->post('appId'); + $appSecret = $this->request->post('appSecret'); + + $appInfo = ApiApp::get(['app_id' => $appId, 'app_secret' => $appSecret]); + if (!empty($appInfo)) { + if ($appInfo->app_status) { + //保存用户信息和登录凭证 + session('app_info', json_encode($appInfo)); + $this->success('登录成功', url('/wiki/index')); + } else { + $this->error('当前应用已被封禁,请联系管理员'); + } + } else { + $this->error('AppId或AppSecret错误'); + } + } + + public function logout() { + session('app_info', null); + $this->success('退出成功', url('/wiki/login')); + } + } diff --git a/application/wiki/view/index/calculation.html b/application/wiki/view/index/calculation.html index 023958f..dc33662 100644 --- a/application/wiki/view/index/calculation.html +++ b/application/wiki/view/index/calculation.html @@ -10,7 +10,7 @@

{:config('apiAdmin.APP_NAME')} - 算法说明

- +
@@ -18,10 +18,10 @@
简介
-

当前算法主要服务于获取身份令牌(AccessToken)所进行的身份认证秘钥的计算。在请求高级接口的时候,系统会验证应用的合法性,也就是验证AccessToken。所以AccessToken是请求API的必要参数。

-

在请求获取AccessToken的接口时候,服务器会对用户合法性进行核验,具体的接口请求字段,请参看具体的接口文档。

+

当前算法主要服务于获取身份令牌(AccessToken)所进行的身份认证秘钥(signature)的计算。在请求高级接口的时候,系统会验证应用的合法性,也就是验证AccessToken。所以AccessToken是请求API的必要参数。

+

在请求获取AccessToken的接口时候,服务器会对用户合法性(signature)进行核验,具体的接口请求字段,请参看具体的接口文档。

一、获取app_id和app_secret
-

目前获取应用ID和应用秘钥是人工手动发放,请联系后台开发。请注意:app_secret非常重要请妥善保管

+

目前获取应用ID和应用秘钥是由系统管理员发放,如果你还没有请联系管理员。请注意:app_secret非常重要请妥善保管

二、准备加密对象,并且根据字段名降序排序
//排序好后应当是如下所示的数据
 {
@@ -41,4 +41,4 @@
     
- \ No newline at end of file + diff --git a/application/wiki/view/index/errorCode.html b/application/wiki/view/index/errorCode.html index 9575dda..cdb8533 100644 --- a/application/wiki/view/index/errorCode.html +++ b/application/wiki/view/index/errorCode.html @@ -10,7 +10,7 @@

{:config('apiAdmin.APP_NAME')} - 错误码说明

- + @@ -42,4 +42,4 @@ - \ No newline at end of file + diff --git a/application/wiki/view/index/index.html b/application/wiki/view/index/index.html index 8cf1549..d5fe739 100644 --- a/application/wiki/view/index/index.html +++ b/application/wiki/view/index/index.html @@ -25,43 +25,54 @@ - \ No newline at end of file + diff --git a/application/wiki/view/index/login.html b/application/wiki/view/index/login.html index 914e4ca..5f47cd6 100644 --- a/application/wiki/view/index/login.html +++ b/application/wiki/view/index/login.html @@ -69,7 +69,7 @@ 欢迎使用{:config('apiAdmin.APP_NAME')}在线文档 - +
@@ -93,4 +93,4 @@
- \ No newline at end of file +