* Rebuild api lib code.

This commit is contained in:
hufangzhou
2022-04-25 14:11:51 +08:00
parent 409115aaaa
commit 84f763e4b9
2 changed files with 103 additions and 117 deletions

View File

@@ -151,7 +151,7 @@ class api extends control
->get();
/* Check version is exist. */
if(!empty($data->version) and $this->api->getReleaseByVersion($libID, $data->version))
if(!empty($data->version) and $this->api->getRelease($libID, 'byVersion', $data->version))
{
return $this->sendError($this->lang->api->noUniqueVersion);
}
@@ -257,16 +257,7 @@ class api extends control
if(!empty($_POST))
{
$now = helper::now();
$userId = $this->app->user->account;
$data = fixer::input('post')
->skipSpecial('attribute')
->add('lib', $struct->lib)
->add('editedBy', $userId)
->add('editedDate', $now)
->get();
$changes = $this->api->updateStruct($structID, $data);
$changes = $this->api->updateStruct($structID);
if(dao::isError()) return $this->sendError(dao::getError());
$actionID = $this->action->create('apistruct', $structID, 'Edited');
$this->action->logHistory($actionID, $changes);
@@ -434,12 +425,13 @@ class api extends control
$this->setMenu($api->lib);
$this->getTypeOptions($api->lib);
$this->view->title = $api->title . $this->lang->api->edit;
$this->view->gobackLink = $this->createLink('api', 'index', "libID={$api->lib}&moduleID={$api->module}");
$this->view->user = $this->app->user->account;
$this->view->allUsers = $this->loadModel('user')->getPairs('devfirst|noclosed');;
$this->view->moduleOptionMenu = $this->loadModel('tree')->getOptionMenu($api->lib, 'api', $startModuleID = 0);
$this->view->moduleID = $api->module ? (int)$api->module : (int)$this->cookie->lastDocModule;
$this->view->title = $api->title . $this->lang->api->edit;
$this->display();
}
@@ -456,24 +448,11 @@ class api extends control
{
if(!empty($_POST))
{
$now = helper::now();
$params = fixer::input('post')
->trim('title,path')
->remove('type')
->skipSpecial('params,response')
->add('addedBy', $this->app->user->account)
->add('addedDate', $now)
->add('editedBy', $this->app->user->account)
->add('editedDate', $now)
->add('version', 1)
->setDefault('product,module', 0)
->get();
$api = $this->api->create();
if($api === false) return $this->sendError(dao::getError());
$apiID = $this->api->create($params);
if(empty($apiID)) return $this->sendError(dao::getError());
$this->action->create('api', $apiID, 'Created');
return $this->sendSuccess(array('locate' => helper::createLink('api', 'index', "libID={$params->lib}&moduleID=0&apiID=$apiID")));
$this->action->create('api', $api->id, 'Created');
return $this->sendSuccess(array('locate' => helper::createLink('api', 'index', "libID={$api->lib}&moduleID=0&apiID={$api->id}")));
}
$libs = $this->doc->getLibs('api', '', $libID);
@@ -500,6 +479,8 @@ class api extends control
}
/**
* Delete an api.
*
* @param int $apiID
* @param string $confirm
* @access public
@@ -509,8 +490,7 @@ class api extends control
{
if($confirm == 'no')
{
$tips = $this->lang->api->confirmDelete;
return print(js::confirm($tips, inlink('delete', "apiID=$apiID&confirm=yes")));
return print(js::confirm($this->lang->api->confirmDelete, inlink('delete', "apiID=$apiID&confirm=yes")));
}
else
{
@@ -529,7 +509,7 @@ class api extends control
}
/**
* Get params type options by scope
* AJAX: Get params type options by scope.
*
* @access public
* @return void
@@ -545,9 +525,10 @@ class api extends control
}
/**
* Get ref options by ajax.
* AJAX: Get ref options.
*
* @param int $libID
* @param int $libID
* @param int $structID
* @access public
* @return void
*/
@@ -558,8 +539,8 @@ class api extends control
$options = array();
foreach($res as $item)
{
if($item->id == $structID)
continue;
if($item->id == $structID) continue;
$options[$item->id] = $item->name;
}
@@ -567,9 +548,9 @@ class api extends control
}
/**
* Get ref info by ajax.
* AJAX: Get ref info.
*
* @param int $refID
* @param int $refID
* @access public
* @return void
*/
@@ -580,8 +561,10 @@ class api extends control
}
/**
* Ajax get all child module.
* AJAX: Get all child module.
*
* @param int $libID
* @param string $type
* @access public
* @return void
*/
@@ -596,8 +579,8 @@ class api extends control
/**
* Set doc menu by method name.
*
* @param int $libID
* @param int $moduleID
* @param int $libID
* @param int $moduleID
* @access public
* @return void
*/
@@ -675,7 +658,7 @@ EOT;
foreach($libs as $key => $lib)
{
$selected = $key == $libID ? 'selected' : '';
$output .= html::a(inlink('index', "libID=$key"), $lib->name, '', "class='$selected' data-app='{$this->app->tab}'");
$output .= html::a(inlink('index', "libID=$key"), $lib->name, '', "class='$selected' data-app='{$this->app->tab}'");
}
$output .= "</div></div></div></div></div>";
@@ -684,7 +667,7 @@ EOT;
if(!empty($versions))
{
$versionName = $version > 0 ? $versions[$version]->version : $this->lang->api->defaultVersion;
$output .= <<<EOT
$output .= <<<EOT
<div class='btn-group angle-btn'>
<div class='btn-group'>
<button id='currentBranch' data-toggle='dropdown' type='button' class='btn btn-limit'>{$versionName} <span class='caret'></span>
@@ -698,12 +681,12 @@ EOT;
<div class='table-col'>
<div class='list-group'>
EOT;
$selected = $version > 0 ? '' : 'selected';
$output .= html::a(inlink('index', "libID=$libID&moduleID=0&apiID=0&version=0&release=0"), $this->lang->api->defaultVersion, '', "class='$selected'");
$selected = $version > 0 ? '' : 'selected';
$output .= html::a(inlink('index', "libID=$libID&moduleID=0&apiID=0&version=0&release=0"), $this->lang->api->defaultVersion, '', "class='$selected'");
foreach($versions as $key => $item)
{
$selected = $key == $version ? 'selected' : '';
$output .= html::a(inlink('index', "libID=$libID&moduleID=0&apiID=0&version=0&release=$key"), $item->version, '', "class='$selected' data-app='{$this->app->tab}'");
$output .= html::a(inlink('index', "libID=$libID&moduleID=0&apiID=0&version=0&release=$key"), $item->version, '', "class='$selected' data-app='{$this->app->tab}'");
}
$output .= "</div></div></div></div></div>";
}
@@ -743,7 +726,7 @@ EOT;
$newParams = array_shift($params);
foreach($params as $param)
{
$sign = strpos($param, '=') !== false ? '&' : ',';
$sign = strpos($param, '=') !== false ? '&' : ',';
$newParams .= $sign . $param;
}

View File

@@ -12,17 +12,17 @@
class apiModel extends model
{
/* Status. */
const STATUS_DOING = 'doing';
const STATUS_DONE = 'done';
const STATUS_DOING = 'doing';
const STATUS_DONE = 'done';
const STATUS_HIDDEN = 'hidden';
/* Scope. */
const SCOPE_QUERY = 'query';
const SCOPE_QUERY = 'query';
const SCOPE_FORM_DATA = 'formData';
const SCOPE_PATH = 'path';
const SCOPE_BODY = 'body';
const SCOPE_HEADER = 'header';
const SCOPE_COOKIE = 'cookie';
const SCOPE_PATH = 'path';
const SCOPE_BODY = 'body';
const SCOPE_HEADER = 'header';
const SCOPE_COOKIE = 'cookie';
/* Params. */
const PARAMS_TYPE_CUSTOM = 'custom';
@@ -68,7 +68,7 @@ class apiModel extends model
/**
* Delete a lib publish.
*
* @param int $id
* @param int $id
* @access public
* @return void
*/
@@ -82,29 +82,43 @@ class apiModel extends model
/**
* Create an api doc.
*
* @param object $params
* @access public
* @return int
* @return object | bool
*/
public function create($params)
public function create()
{
$this->dao->insert(TABLE_API)->data($params)
$now = helper::now();
$data = fixer::input('post')
->trim('title,path')
->remove('type')
->skipSpecial('params,response')
->add('addedBy', $this->app->user->account)
->add('addedDate', $now)
->add('editedBy', $this->app->user->account)
->add('editedDate', $now)
->add('version', 1)
->setDefault('product,module', 0)
->get();
$this->dao->insert(TABLE_API)->data($data)
->autoCheck()
->batchCheck($this->config->api->create->requiredFields, 'notempty')
->exec();
$params->id = $this->dao->lastInsertID();
if(dao::isError()) return false;
$apiSpec = $this->getApiSpecByData($params);
$data->id = $this->dao->lastInsertID();
$apiSpec = $this->getApiSpecByData($data);
$this->dao->replace(TABLE_API_SPEC)->data($apiSpec)->exec();
return dao::isError() ? false : $params->id;
return $data;
}
/**
* Create a global struct
* Create a global struct.
*
* @param object $data
* @param object $data
* @access public
* @return int
*/
@@ -116,6 +130,8 @@ class apiModel extends model
->batchCheck($this->config->api->struct->requiredFields, 'notempty')
->exec();
if(dao::isError()) return false;
$id = $this->dao->lastInsertID();
/* Create a struct version. */
@@ -130,19 +146,28 @@ class apiModel extends model
);
$this->dao->insert(TABLE_APISTRUCT_SPEC)->data($version)->exec();
return dao::isError() ? 0 : $id;
if(dao::isError()) return false;
return $id;
}
/**
* Update a struct.
*
* @param int $id
* @param object $data
* @param int $id
* @access public
* @return array
*/
public function updateStruct($id, $data)
public function updateStruct($id)
{
$now = helper::now();
$data = fixer::input('post')
->skipSpecial('attribute')
->add('lib', $struct->lib)
->add('editedBy', $this->app->user->account)
->add('editedDate', $now)
->get();
$old = $this->dao->findByID($id)->from(TABLE_APISTRUCT)->fetch();
unset($data->addedBy);
@@ -190,11 +215,10 @@ class apiModel extends model
return false;
}
$now = helper::now();
$account = $this->app->user->account;
$data = fixer::input('post')
$now = helper::now();
$data = fixer::input('post')
->skipSpecial('params,response')
->add('editedBy', $account)
->add('editedBy', $this->app->user->account)
->add('editedDate', $now)
->add('version', $oldApi->version)
->setDefault('product,module', 0)
@@ -259,36 +283,21 @@ class apiModel extends model
}
/**
* Get release by version.
* Get release.
*
* @param int $libID
* @param string $version
* @param int $libID
* @param string $type
* @param int $param
* @access public
* @return object
* @access public
*/
public function getReleaseByVersion($libID, $version)
public function getRelease($libID = 0, $type = '', $param = 0)
{
$model = $this->dao->select('*')
->from(TABLE_API_LIB_RELEASE)
->where('version')->eq($version)
->andWhere('lib')->eq($libID)
->fetch();
if($model) $model->snap = json_decode($model->snap, true);
return $model;
}
/**
* Get release by id.
*
* @param int $id
* @access public
* @return array
*/
public function getReleaseById($id)
{
$model = $this->dao->select('*')
->from(TABLE_API_LIB_RELEASE)
->where('id')->eq($id)
$model = $this->dao->select('*')->from(TABLE_API_LIB_RELEASE)
->where('1 = 1')
->beginIF($libID)->andWhere('lib')->eq($libID)->fi()
->beginIF($type == 'byVersion')->andWhere('version')->eq($param)->fi()
->beginIF($type == 'byId')->andWhere('id')->eq($param)->fi()
->fetch();
if($model) $model->snap = json_decode($model->snap, true);
return $model;
@@ -303,10 +312,7 @@ class apiModel extends model
*/
public function getReleaseListByApi($libID)
{
$versions = $this->dao->select('*')->from(TABLE_API_LIB_RELEASE)
->where('lib')->eq($libID)
->fetchAll('id');
return $versions;
return $this->dao->select('*')->from(TABLE_API_LIB_RELEASE)->where('lib')->eq($libID)->fetchAll('id');
}
@@ -323,7 +329,7 @@ class apiModel extends model
{
if($release)
{
$rel = $this->getReleaseById($release);
$rel = $this->getRelease(0, 'byId', $release);
foreach($rel->snap['apis'] as $api)
{
if($api['id'] == $id) $version = $api['version'];
@@ -338,9 +344,7 @@ class apiModel extends model
$fields = 'api.*,doc.name as libName,module.name as moduleName';
}
$model = $this->dao
->select($fields)
->from(TABLE_API)->alias('api')
$model = $this->dao->select($fields)->from(TABLE_API)->alias('api')
->beginIF($version)->leftJoin(TABLE_API_SPEC)->alias('spec')->on('api.id = spec.doc')->fi()
->leftJoin(TABLE_DOCLIB)->alias('doc')->on('api.lib = doc.id')
->leftJoin(TABLE_MODULE)->alias('module')->on('api.module = module.id')
@@ -359,8 +363,9 @@ class apiModel extends model
/**
* Get api list by release.
*
* @param object $release
* @param string $where
* @param object $release
* @param string $where
* @access public
* @return array
*/
public function getApiListByRelease($release, $where = '')
@@ -380,20 +385,19 @@ class apiModel extends model
}
/**
* Get api doc list by module id
* Get api doc list by module id.
*
* @param int $libID
* @param int $moduleID
* @param int $release
* @param int $libID
* @param int $moduleID
* @param int $release
* @return array $list
* @author thanatos thanatos915@163.com
*/
public function getListByModuleId($libID = 0, $moduleID = 0, $release = 0)
{
/* Get release info. */
if($release > 0)
{
$rel = $this->getReleaseById($release);
$rel = $this->getRelease(0, 'byId', $release);
$where = "1=1 and lib = $libID ";
if($moduleID > 0)
@@ -413,7 +417,6 @@ class apiModel extends model
}
else
{
if($moduleID > 0)
{
$sub = $this->dao->select('id')->from(TABLE_MODULE)->where('FIND_IN_SET(' . $moduleID . ', path)')->processSQL();
@@ -585,7 +588,7 @@ class apiModel extends model
foreach($_POST as $key => $value) $param .= ',' . $key . '=' . $value;
$param = ltrim($param, ',');
}
$url = rtrim($host, '/') . inlink('getModel', "moduleName=$moduleName&methodName=$methodName&params=$param", 'json');
$url = rtrim($host, '/') . inlink('getModel', "moduleName=$moduleName&methodName=$methodName&params=$param", 'json');
$url .= $this->config->requestType == "PATH_INFO" ? '?' : '&';
$url .= $this->config->sessionVar . '=' . session_id();
}
@@ -596,7 +599,7 @@ class apiModel extends model
foreach($_POST as $key => $value) $param .= '&' . $key . '=' . $value;
$param = ltrim($param, '&');
}
$url = rtrim($host, '/') . helper::createLink($moduleName, $methodName, $param, 'json');
$url = rtrim($host, '/') . helper::createLink($moduleName, $methodName, $param, 'json');
$url .= $this->config->requestType == "PATH_INFO" ? '?' : '&';
$url .= $this->config->sessionVar . '=' . session_id();
}