mirror of
https://gitee.com/apiadmin/ApiAdmin.git
synced 2024-11-29 18:47:35 +08:00
modified 完善文档部分
This commit is contained in:
parent
24073bcc84
commit
b54c0b002a
188
app/controller/wiki/Api.php
Normal file
188
app/controller/wiki/Api.php
Normal file
@ -0,0 +1,188 @@
|
||||
<?php
|
||||
declare (strict_types=1);
|
||||
/**
|
||||
* @since 2019-08-11
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
|
||||
namespace app\controller\wiki;
|
||||
|
||||
use app\model\AdminApp;
|
||||
use app\model\AdminFields;
|
||||
use app\model\AdminGroup;
|
||||
use app\model\AdminList;
|
||||
use app\util\DataType;
|
||||
use app\util\ReturnCode;
|
||||
use app\util\Tools;
|
||||
use think\Response;
|
||||
|
||||
class Api extends Base {
|
||||
|
||||
public function errorCode(): Response {
|
||||
$codeArr = ReturnCode::getConstants();
|
||||
$codeArr = array_flip($codeArr);
|
||||
$result = [];
|
||||
$errorInfo = [
|
||||
ReturnCode::SUCCESS => '请求成功',
|
||||
ReturnCode::INVALID => '非法操作',
|
||||
ReturnCode::DB_SAVE_ERROR => '数据存储失败',
|
||||
ReturnCode::DB_READ_ERROR => '数据读取失败',
|
||||
ReturnCode::CACHE_SAVE_ERROR => '缓存存储失败',
|
||||
ReturnCode::CACHE_READ_ERROR => '缓存读取失败',
|
||||
ReturnCode::FILE_SAVE_ERROR => '文件读取失败',
|
||||
ReturnCode::LOGIN_ERROR => '登录失败',
|
||||
ReturnCode::NOT_EXISTS => '不存在',
|
||||
ReturnCode::JSON_PARSE_FAIL => 'JSON数据格式错误',
|
||||
ReturnCode::TYPE_ERROR => '类型错误',
|
||||
ReturnCode::NUMBER_MATCH_ERROR => '数字匹配失败',
|
||||
ReturnCode::EMPTY_PARAMS => '丢失必要数据',
|
||||
ReturnCode::DATA_EXISTS => '数据已经存在',
|
||||
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 => '系统异常',
|
||||
];
|
||||
|
||||
foreach ($errorInfo as $key => $value) {
|
||||
$result[] = [
|
||||
'en_code' => $codeArr[$key],
|
||||
'code' => $key,
|
||||
'chinese' => $value,
|
||||
];
|
||||
}
|
||||
|
||||
return $this->buildSuccess([
|
||||
'data' => $result,
|
||||
'co' => config('apiadmin.APP_NAME') . ' ' . config('apiadmin.APP_VERSION')
|
||||
]);
|
||||
}
|
||||
|
||||
public function login(): Response {
|
||||
$appId = $this->request->post('username');
|
||||
$appSecret = $this->request->post('password');
|
||||
|
||||
$appInfo = (new AdminApp())->where('app_id', $appId)->where('app_secret', $appSecret)->find();
|
||||
if (!empty($appInfo)) {
|
||||
if ($appInfo->app_status) {
|
||||
//保存用户信息和登录凭证
|
||||
$appInfo = $appInfo->toArray();
|
||||
|
||||
$apiAuth = md5(uniqid() . time());
|
||||
cache('WikiLogin:' . $apiAuth, $appInfo, config('apiadmin.ONLINE_TIME'));
|
||||
cache('WikiLogin:' . $appInfo['id'], $apiAuth, config('apiadmin.ONLINE_TIME'));
|
||||
$appInfo['apiAuth'] = $apiAuth;
|
||||
|
||||
return $this->buildSuccess($appInfo, '登录成功');
|
||||
} else {
|
||||
return $this->buildFailed(ReturnCode::LOGIN_ERROR, '当前应用已被封禁,请联系管理员');
|
||||
}
|
||||
} else {
|
||||
return $this->buildFailed(ReturnCode::LOGIN_ERROR, 'AppId或AppSecret错误');
|
||||
}
|
||||
}
|
||||
|
||||
public function groupList(): Response {
|
||||
$groupInfo = (new AdminGroup())->select();
|
||||
$apiInfo = (new AdminList())->select();
|
||||
|
||||
$listInfo = [];
|
||||
if ($this->appInfo['app_id'] === -1) {
|
||||
$_apiInfo = [];
|
||||
foreach ($apiInfo as $aVal) {
|
||||
$_apiInfo[$aVal['group_hash']][] = $aVal;
|
||||
}
|
||||
foreach ($groupInfo as $gVal) {
|
||||
if (isset($_apiInfo[$gVal['hash']])) {
|
||||
$gVal['api_info'] = $_apiInfo[$gVal['hash']];
|
||||
}
|
||||
$listInfo[] = $gVal;
|
||||
}
|
||||
} else {
|
||||
$apiInfo = Tools::buildArrFromObj($apiInfo, 'hash');
|
||||
$groupInfo = Tools::buildArrFromObj($groupInfo, 'hash');
|
||||
$app_api_show = json_decode($this->appInfo['app_api_show'], true);
|
||||
foreach ($app_api_show as $key => $item) {
|
||||
$_listInfo = $groupInfo[$key];
|
||||
foreach ($item as $apiItem) {
|
||||
$_listInfo['api_info'][] = $apiInfo[$apiItem];
|
||||
}
|
||||
if (isset($_listInfo['api_info'])) {
|
||||
$listInfo[] = $_listInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->buildSuccess([
|
||||
'data' => $listInfo,
|
||||
'co' => config('apiadmin.APP_NAME') . ' ' . config('apiadmin.APP_VERSION')
|
||||
]);
|
||||
}
|
||||
|
||||
public function detail(): Response {
|
||||
$hash = $this->request->get('hash');
|
||||
if (!$hash) {
|
||||
return $this->buildFailed(ReturnCode::NOT_EXISTS, '缺少必要参数');
|
||||
}
|
||||
|
||||
$apiList = (new AdminList())->whereIn('hash', $hash)->find();
|
||||
if (!$apiList) {
|
||||
return $this->buildFailed(ReturnCode::NOT_EXISTS, '接口hash非法');
|
||||
}
|
||||
$request = (new AdminFields())->where('hash', $hash)->where('type', 0)->select();
|
||||
$response = (new AdminFields())->where('hash', $hash)->where('type', 1)->select();
|
||||
$dataType = array(
|
||||
DataType::TYPE_INTEGER => 'Integer',
|
||||
DataType::TYPE_STRING => 'String',
|
||||
DataType::TYPE_BOOLEAN => 'Boolean',
|
||||
DataType::TYPE_ENUM => 'Enum',
|
||||
DataType::TYPE_FLOAT => 'Float',
|
||||
DataType::TYPE_FILE => 'File',
|
||||
DataType::TYPE_ARRAY => 'Array',
|
||||
DataType::TYPE_OBJECT => 'Object',
|
||||
DataType::TYPE_MOBILE => 'Mobile'
|
||||
);
|
||||
|
||||
$groupInfo = (new AdminGroup())->where('hash', $apiList['group_hash'])->find();
|
||||
$groupInfo->hot = $groupInfo->hot + 1;
|
||||
$groupInfo->save();
|
||||
|
||||
if ($apiList['hash_type'] === 1) {
|
||||
$url = $this->request->domain() . '/api/' . $apiList['api_class'];
|
||||
} else {
|
||||
$url = $this->request->domain() . '/api/' . $hash;
|
||||
}
|
||||
|
||||
return $this->buildSuccess([
|
||||
'request' => $request,
|
||||
'response' => $response,
|
||||
'dataType' => $dataType,
|
||||
'apiList' => $apiList,
|
||||
'url' => $url,
|
||||
'co' => config('apiadmin.APP_NAME') . ' ' . config('apiadmin.APP_VERSION')
|
||||
]);
|
||||
}
|
||||
|
||||
public function logout(): Response {
|
||||
$ApiAuth = $this->request->header('ApiAuth');
|
||||
cache('WikiLogin:' . $ApiAuth, null);
|
||||
cache('WikiLogin:' . $this->appInfo['id'], null);
|
||||
|
||||
$oldAdmin = cache('Login:' . $ApiAuth);
|
||||
if ($oldAdmin) {
|
||||
$oldAdmin = json_decode($oldAdmin, true);
|
||||
cache('Login:' . $ApiAuth, null);
|
||||
cache('Login:' . $oldAdmin['id'], null);
|
||||
}
|
||||
|
||||
return $this->buildSuccess([], '登出成功');
|
||||
}
|
||||
}
|
43
app/controller/wiki/Base.php
Normal file
43
app/controller/wiki/Base.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
declare (strict_types=1);
|
||||
/**
|
||||
* 工程基类
|
||||
* @since 2017/02/28 创建
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
|
||||
namespace app\controller\wiki;
|
||||
|
||||
use app\BaseController;
|
||||
use app\util\ReturnCode;
|
||||
use think\Response;
|
||||
|
||||
class Base extends BaseController {
|
||||
|
||||
protected $appInfo;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(App());
|
||||
$this->appInfo = $this->request->API_WIKI_USER_INFO;
|
||||
}
|
||||
|
||||
public function buildSuccess($data = [], $msg = '操作成功', $code = ReturnCode::SUCCESS): Response {
|
||||
$return = [
|
||||
'code' => $code,
|
||||
'msg' => $msg,
|
||||
'data' => $data
|
||||
];
|
||||
|
||||
return json($return);
|
||||
}
|
||||
|
||||
public function buildFailed($code, $msg = '操作失败', $data = []): Response {
|
||||
$return = [
|
||||
'code' => $code,
|
||||
'msg' => $msg,
|
||||
'data' => $data
|
||||
];
|
||||
|
||||
return json($return);
|
||||
}
|
||||
}
|
@ -9,16 +9,16 @@ use think\facade\Route;
|
||||
|
||||
Route::group('wiki', function() {
|
||||
Route::rule(
|
||||
'Api/login', 'wiki/Api/login', 'post'
|
||||
'Api/login', 'wiki.Api/login', 'post'
|
||||
);
|
||||
Route::group('Api', function() {
|
||||
Route::rule('login', 'wiki/Api/login', 'post');
|
||||
Route::rule('errorCode', 'wiki/Api/errorCode', 'get');
|
||||
Route::rule('groupList', 'wiki/Api/groupList', 'get');
|
||||
Route::rule('detail', 'wiki/Api/detail', 'get');
|
||||
Route::rule('logout', 'wiki/Api/logout', 'get');
|
||||
})->middleware(['WikiAuth']);
|
||||
Route::rule('login', 'wiki.Api/login', 'post');
|
||||
Route::rule('errorCode', 'wiki.Api/errorCode', 'get');
|
||||
Route::rule('groupList', 'wiki.Api/groupList', 'get');
|
||||
Route::rule('detail', 'wiki.Api/detail', 'get');
|
||||
Route::rule('logout', 'wiki.Api/logout', 'get');
|
||||
})->middleware([app\middleware\WikiAuth::class]);
|
||||
|
||||
//MISS路由定义
|
||||
Route::miss('admin/Miss/index');
|
||||
})->middleware('AdminResponse');
|
||||
Route::miss('admin.Miss/index');
|
||||
})->middleware(app\middleware\AdminResponse::class);
|
||||
|
Loading…
Reference in New Issue
Block a user