1
0
mirror of https://gitee.com/apiadmin/ApiAdmin.git synced 2025-04-06 03:58:00 +08:00

modified 细节调整,新增加文档支持

This commit is contained in:
zhaoxiang 2018-02-26 11:25:52 +08:00
parent f5673158b1
commit b46a9f13ad
12 changed files with 249 additions and 70 deletions

@ -70,7 +70,6 @@ class App extends Base {
$groupArr = ApiGroup::all(); $groupArr = ApiGroup::all();
$groupArr = Tools::buildArrFromObj($groupArr); $groupArr = Tools::buildArrFromObj($groupArr);
$res['groupInfo'] = array_column($groupArr, 'name', 'hash'); $res['groupInfo'] = array_column($groupArr, 'name', 'hash');
$res['groupInfo']['default'] = '默认分组';
$id = $this->request->get('id', 0); $id = $this->request->get('id', 0);
if ($id) { if ($id) {
$appInfo = ApiApp::get($id)->toArray(); $appInfo = ApiApp::get($id)->toArray();

@ -43,21 +43,6 @@ class Base extends Controller {
return $return; return $return;
} }
/**
* 将二维数组变成指定key
* @param $array
* @param $keyName
* @author zhaoxiang <zhaoxiang051405@gmail.com>
* @return array
*/
protected function buildArrByNewKey($array, $keyName = 'id') {
$list = array();
foreach ($array as $item) {
$list[$item[$keyName]] = $item;
}
return $list;
}
protected function debug($data) { protected function debug($data) {
if ($data) { if ($data) {
$this->debug[] = $data; $this->debug[] = $data;

@ -56,13 +56,13 @@ class User extends Base {
$query->whereIn('uid', $idArr); $query->whereIn('uid', $idArr);
}); });
$userData = Tools::buildArrFromObj($userData); $userData = Tools::buildArrFromObj($userData);
$userData = $this->buildArrByNewKey($userData, 'uid'); $userData = Tools::buildArrByNewKey($userData, 'uid');
$userGroup = ApiAuthGroupAccess::all(function($query) use ($idArr) { $userGroup = ApiAuthGroupAccess::all(function($query) use ($idArr) {
$query->whereIn('uid', $idArr); $query->whereIn('uid', $idArr);
}); });
$userGroup = Tools::buildArrFromObj($userGroup); $userGroup = Tools::buildArrFromObj($userGroup);
$userGroup = $this->buildArrByNewKey($userGroup, 'uid'); $userGroup = Tools::buildArrByNewKey($userGroup, 'uid');
foreach ($listInfo as $key => $value) { foreach ($listInfo as $key => $value) {
if (isset($userData[$value['id']])) { if (isset($userData[$value['id']])) {
@ -140,7 +140,7 @@ class User extends Base {
$query->whereIn('uid', $uidArr); $query->whereIn('uid', $uidArr);
}); });
$userData = Tools::buildArrFromObj($userData); $userData = Tools::buildArrFromObj($userData);
$userData = $this->buildArrByNewKey($userData, 'uid'); $userData = Tools::buildArrByNewKey($userData, 'uid');
foreach ($userInfo as $key => $value) { foreach ($userInfo as $key => $value) {
if (isset($userData[$value['id']])) { if (isset($userData[$value['id']])) {

@ -49,6 +49,14 @@ class BuildToken extends Base {
return $this->buildSuccess($return); return $this->buildSuccess($return);
} }
public function e1() {
return $this->buildSuccess('e1');
}
public function e2() {
return $this->buildSuccess('e2');
}
/** /**
* 根据AppSecret和数据生成相对应的身份认证秘钥 * 根据AppSecret和数据生成相对应的身份认证秘钥
* @param $appSecret * @param $appSecret

@ -9,10 +9,18 @@ $afterBehavior = ['\app\api\behavior\ApiAuth', '\app\api\behavior\RequestFilter'
return [ return [
'[api]' => [ '[api]' => [
'58bf98c1dcb63' => [ '5a9363c133719' => [
'api/BuildToken/getAccessToken', 'api/BuildToken/getAccessToken',
['method' => 'get', 'after_behavior' => $afterBehavior] ['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'], '__miss__' => ['api/Miss/index'],
], ],
'[wiki]' => [ '[wiki]' => [
@ -20,6 +28,30 @@ return [
'wiki/index/login', 'wiki/index/login',
['method' => 'get'] ['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'], '__miss__' => ['api/Miss/index'],
], ],
]; ];

@ -97,4 +97,19 @@ class Tools {
return $arr; return $arr;
} }
/**
* 将二维数组变成指定key
* @param $array
* @param $keyName
* @author zhaoxiang <zhaoxiang051405@gmail.com>
* @return array
*/
public static function buildArrByNewKey($array, $keyName = 'id') {
$list = array();
foreach ($array as $item) {
$list[$item[$keyName]] = $item;
}
return $list;
}
} }

@ -8,12 +8,83 @@
namespace app\wiki\controller; namespace app\wiki\controller;
use think\Config;
use think\Controller; 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 { 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'));
}
} }
} 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);
}
}

@ -8,15 +8,36 @@
namespace app\wiki\controller; namespace app\wiki\controller;
use app\model\ApiApp;
use app\model\ApiFields; use app\model\ApiFields;
use app\model\ApiGroup;
use app\model\ApiList; use app\model\ApiList;
use app\util\DataType; use app\util\DataType;
use app\util\ReturnCode; use app\util\ReturnCode;
use app\util\Tools;
class Index extends Base { class Index extends Base {
/**
* 获取应用列表
* @return \think\response\View
* @throws \think\exception\DbException
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public function index() { 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() { public function detail() {
@ -58,10 +79,13 @@ class Index extends Base {
} }
public function calculation() { public function calculation() {
return $this->fetch(); $this->checkLogin();
return view();
} }
public function errorCode() { public function errorCode() {
$this->checkLogin();
$codeArr = ReturnCode::getConstants(); $codeArr = ReturnCode::getConstants();
$errorInfo = array( $errorInfo = array(
ReturnCode::SUCCESS => '请求成功', ReturnCode::SUCCESS => '请求成功',
@ -81,21 +105,55 @@ class Index extends Base {
ReturnCode::AUTH_ERROR => '权限认证失败', ReturnCode::AUTH_ERROR => '权限认证失败',
ReturnCode::OTHER_LOGIN => '别的终端登录', ReturnCode::OTHER_LOGIN => '别的终端登录',
ReturnCode::VERSION_INVALID => 'API版本非法', 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::PARAM_INVALID => '数据类型非法',
ReturnCode::ACCESS_TOKEN_TIMEOUT => '身份令牌过期', ReturnCode::ACCESS_TOKEN_TIMEOUT => '身份令牌过期',
ReturnCode::SESSION_TIMEOUT => 'SESSION过期', ReturnCode::SESSION_TIMEOUT => 'SESSION过期',
ReturnCode::UNKNOWN => '未知错误', ReturnCode::UNKNOWN => '未知错误',
ReturnCode::EXCEPTION => '系统异常', 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() { public function login() {
return view(); return view();
} }
/**
* 处理wiki登录
* @throws \think\Exception
* @throws \think\exception\DbException
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
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'));
}
} }

@ -10,7 +10,7 @@
<div class="ui text container" style="max-width: none !important;"> <div class="ui text container" style="max-width: none !important;">
<div class="ui floating message"> <div class="ui floating message">
<h1 class="ui header">{:config('apiAdmin.APP_NAME')} - 算法说明 </h1> <h1 class="ui header">{:config('apiAdmin.APP_NAME')} - 算法说明 </h1>
<a href="{:url('/wiki/group')}"> <a href="{:url('/wiki/index')}">
<button class="ui green button" style="margin-top: 15px">返回接口文档</button> <button class="ui green button" style="margin-top: 15px">返回接口文档</button>
</a> </a>
<div class="ui red message"> <div class="ui red message">
@ -18,10 +18,10 @@
</div> </div>
<div class="ui piled segment"> <div class="ui piled segment">
<div class="ui horizontal divider">简介</div> <div class="ui horizontal divider">简介</div>
<p>当前算法主要服务于获取身份令牌AccessToken所进行的身份认证秘钥的计算。在请求高级接口的时候系统会验证应用的合法性也就是验证AccessToken。所以AccessToken是请求API的必要参数。</p> <p>当前算法主要服务于获取身份令牌AccessToken所进行的身份认证秘钥signature的计算。在请求高级接口的时候系统会验证应用的合法性也就是验证AccessToken。所以AccessToken是请求API的必要参数。</p>
<p>在请求获取AccessToken的接口时候服务器会对用户合法性进行核验具体的接口请求字段请参看具体的接口文档。</p> <p>在请求获取AccessToken的接口时候服务器会对用户合法性signature进行核验,具体的接口请求字段,请参看具体的接口文档。</p>
<div class="ui horizontal divider">一、获取app_id和app_secret</div> <div class="ui horizontal divider">一、获取app_id和app_secret</div>
<p>目前获取应用ID和应用秘钥是人工手动发放,请联系后台开发<b>请注意app_secret非常重要请妥善保管</b></p> <p>目前获取应用ID和应用秘钥是由系统管理员发放,如果你还没有请联系管理员<b>请注意app_secret非常重要请妥善保管</b></p>
<div class="ui horizontal divider">二、准备加密对象,并且根据字段名降序排序</div> <div class="ui horizontal divider">二、准备加密对象,并且根据字段名降序排序</div>
<pre>//排序好后应当是如下所示的数据 <pre>//排序好后应当是如下所示的数据
{ {
@ -41,4 +41,4 @@
</div> </div>
</div> </div>
</body> </body>
</html> </html>

@ -10,7 +10,7 @@
<div class="ui text container" style="max-width: none !important;"> <div class="ui text container" style="max-width: none !important;">
<div class="ui floating message"> <div class="ui floating message">
<h1 class="ui header">{:config('apiAdmin.APP_NAME')} - 错误码说明</h1> <h1 class="ui header">{:config('apiAdmin.APP_NAME')} - 错误码说明</h1>
<a href="{:url('/wiki/group')}"> <a href="{:url('/wiki/index')}">
<button class="ui green button" style="margin-top: 15px">返回接口文档</button> <button class="ui green button" style="margin-top: 15px">返回接口文档</button>
</a> </a>
<table class="ui red celled striped table"> <table class="ui red celled striped table">
@ -42,4 +42,4 @@
</div> </div>
</div> </div>
</body> </body>
</html> </html>

@ -25,43 +25,54 @@
</div> </div>
</div> </div>
<div class="ui cards four column"> <div class="ui cards four column">
<div class="card column"> {volist name="appInfo['app_api_show']" id="vo"}
<a class="image" href="#"> {php}
<img src="http://static10.photo.sina.com.cn/middle/5a3ab1b1x9961016a8699&690"> $apiLength = count($vo);
</a> {/php}
<div class="content"> {if condition="$apiLength"}
<a class="header"> <div class="card column">
XXX分组 <a class="image" href="{:url('/wiki/detail/' . $key)}">
</a> <img src="http://static10.photo.sina.com.cn/middle/5a3ab1b1x9961016a8699&690">
<a class="meta">
<span class="group" style="font-size: 0.8em;">2017-07-17</span>
</a>
<a class="description" style="display: block;font-size: 0.8em;">
{php}
$vo['success_place_description'] = '需要啥需要啥需要啥需要啥需要啥需要啥需要啥需要啥需要啥需要啥需要啥需要啥';
$len = mb_strlen($vo['success_place_description'], 'utf8');
if($len > 31) {
echo mb_substr($vo['success_place_description'], 0, 31, 'utf8') . ' ...';
} else {
echo $vo['success_place_description'];
}
{/php}
</a> </a>
<div class="content">
<a class="header" href="{:url('/wiki/detail/' . $key)}">
{$groupInfo[$key]['name']}
</a>
<a class="meta" href="{:url('/wiki/detail/' . $key)}">
<span class="group" style="font-size: 0.8em;">{:date('Y-m-d', $groupInfo[$key]['updateTime'])}</span>
</a>
<a class="description" style="display: block;font-size: 0.8em;" href="{:url('/wiki/detail/' . $key)}">
{php}
$len = mb_strlen($groupInfo[$key]['description'], 'utf8');
if($len > 31) {
echo mb_substr($groupInfo[$key]['description'], 0, 31, 'utf8') . ' ...';
} else {
echo $groupInfo[$key]['description'];
}
if ($groupInfo[$key]['hot'] >= 10000) {
$hot = sprintf("%.1f", $groupInfo[$key]['hot']/10000) . '万';
} else {
$hot = $groupInfo[$key]['hot'];
}
{/php}
</a>
</div>
<div class="extra content" style="font-size: 0.9em">
<a class="right floated created" href="{:url('/wiki/detail/' . $key)}">
<i class="fire icon"></i>
热度{$hot}
</a>
<a class="friends" href="{:url('/wiki/detail/' . $key)}">
<i class="cubes icon"></i>
共{$apiLength}个接口
</a>
</div>
</div> </div>
<div class="extra content" style="font-size: 0.9em"> {/if}
<a class="right floated created"> {/volist}
<i class="fire icon"></i>
热度29.1万
</a>
<a class="friends">
<i class="cubes icon"></i>
共190个接口
</a>
</div>
</div>
</div> </div>
<p>&copy; Powered By <a href="http://www.apiadmin.org/" target="_blank">{:config('apiAdmin.APP_NAME')} {:config('apiAdmin.APP_VERSION')}</a> <p> <p>&copy; Powered By <a href="http://www.apiadmin.org/" target="_blank">{:config('apiAdmin.APP_NAME')} {:config('apiAdmin.APP_VERSION')}</a> <p>
</div> </div>
</div> </div>
</body> </body>
</html> </html>

@ -69,7 +69,7 @@
欢迎使用{:config('apiAdmin.APP_NAME')}在线文档 欢迎使用{:config('apiAdmin.APP_NAME')}在线文档
</div> </div>
</h2> </h2>
<form class="ui large form"> <form class="ui large form" method="post" action="{:url('/wiki/doLogin')}">
<div class="ui stacked segment"> <div class="ui stacked segment">
<div class="field"> <div class="field">
<div class="ui left icon input"> <div class="ui left icon input">
@ -93,4 +93,4 @@
</div> </div>
</div> </div>
</body> </body>
</html> </html>