完善接口数据

This commit is contained in:
yumo 2023-12-31 18:15:31 +08:00
parent cc15e83a70
commit a353e056be
10 changed files with 254 additions and 79 deletions

View File

@ -0,0 +1,58 @@
<?php
namespace app\api\controller;
use app\admin\extend\Util;
use app\common\constant\Data;
use app\common\model\Document;
use app\Request;
use think\Exception;
use think\facade\Log;
use think\Response;
class Article extends Base
{
public function list(){
$where = Util::postMore([
['cid',''],
['typeId', ''],
['orderBy', 'sort asc,create_time desc'],
['row', 100],
['void', 'field'],
['model', "article"],
['type', "find"],
['where', ""],
['display',1],
['ids',""],
['page',1],
['limit',20],
]);
$list = tpl_get_article_list($where['cid'], $where['row'], $where['orderby'], $where['model'], $where['type'], $where['where'], $where['display'], $where['ids']);
return app("json")->layui($list);
}
/**
* @param Request $request
* @return mixed
* @throws Exception
*/
public function detail(Request $request)
{
$where = Util::postMore([
['id', ''],
],$request);
if (empty($where['id'])){
return app("json")->fail("参数错误");
}
//获取该文章
$documentModel = new Document();
$article = $documentModel->getFullInfo($where['id'], Data::DOCUMENT_TYPE_ARTICLE);
$article['cover_path'] = file_cdn($article['cover_path']);
if (!$article) {
return app("json")->fail("文章不存在或已删除");
}
return app("json")->success($article,'code');
}
}

View File

@ -27,8 +27,4 @@ class Base extends BaseController
protected $userId; protected $userId;
// 初始化 // 初始化
protected function initialize()
{
parent::initialize();
}
} }

View File

@ -4,6 +4,7 @@ namespace app\api\controller;
use app\admin\controller\AuthController; use app\admin\controller\AuthController;
use app\common\constant\Data; use app\common\constant\Data;
use app\common\model\SystemConfig;
/** /**
* 应用入口 * 应用入口
@ -19,4 +20,13 @@ class Index extends Base
{ {
return app("json")->success("获取成功", 'code'); return app("json")->success("获取成功", 'code');
} }
public function config(){
$systemConfig = cache(Data::DATA_SYSTEM_CONFIG);
if (empty($systemConfig)) {
$systemConfig = SystemConfig::getSystemConfigValues();
cache(Data::DATA_SYSTEM_CONFIG, $systemConfig);
}
return app("json")->success($systemConfig,"code");
}
} }

View File

@ -0,0 +1,90 @@
<?php
namespace app\api\controller;
use app\admin\controller\AuthController;
use app\admin\extend\Util as Util;
use app\admin\model\Admin as adminModel;
use Exception;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Response;
class Login extends Base
{
/**
* 无需登录使用jwt
* @var array
*/
protected $noNeedLogin = ['login', 'register', 'forget', 'captcha', 'verify'];
/**
* 登录
* @return string
* @throws Exception
*/
public function login()
{
return $this->fetch();
}
/**
* 验证登录
* @return mixed
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function verify()
{
list($username, $password, $captcha) = Util::postMore(['username', 'password', 'captcha'], null, true);
if (empty($username) || empty($password)) return app("json")->fail("账号、密码和验证码不能为空!");
// 验证码验证
if (!captcha_check($captcha)) return app("json")->fail("验证码不正确!");
// 验证登录
if (!adminModel::login($username, $password)) return app("json")->fail(adminModel::getErrorInfo());
return app("json")->success("登录成功!");
}
/**
* 注册
* @return string
* @throws Exception
*/
public function register(): string
{
return $this->fetch();
}
/**
* 忘记密码
* @return string
* @throws Exception
*/
public function forget(): string
{
return $this->fetch();
}
/**
* 退出登陆
* @return mixed
* @throws Exception
*/
public function logout()
{
return adminModel::clearLoginInfo() ? $this->successfulNotice("操作成功", "/admin/login/login") : $this->failedNotice("操作失败", "/admin/index/index");
}
/**
* 验证码
* @return Response
*/
public function captcha(): Response
{
ob_clean();
return captcha();
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace app\api\controller;
class Page extends Base
{
}

View File

@ -0,0 +1,10 @@
<?php
namespace app\api\controller;
class User extends Base
{
public function info(){
}
}

View File

@ -4,6 +4,7 @@
namespace app\common\model; namespace app\common\model;
use think\db\exception\DbException;
use think\facade\Db; use think\facade\Db;
use think\Model; use think\Model;
@ -16,6 +17,22 @@ class BaseModel extends Model
private static $transaction = 0; private static $transaction = 0;
private static $DbInstance = []; private static $DbInstance = [];
/**
* 错误信息,解决调用无法明确错误的问题
* @var mixed
*/
protected $error;
/**
* 获取错误信息
* @access public
* @return mixed
*/
public function getError()
{
return $this->error;
}
//自动时间戳 //自动时间戳
protected $autoWriteTimestamp = true; protected $autoWriteTimestamp = true;
@ -30,53 +47,18 @@ class BaseModel extends Model
} }
/** /**
* 开启事务 * 查询一条数据是否存在
* @param $map
* @param string $field
* @return bool 是否存在
* @throws DbException
*/ */
public static function beginTrans() public static function isExist($map, $field = ''): bool
{ {
Db::startTrans(); $model = (new self);
} if (!is_array($map) && empty($field)) $field = $model->getPk();
$map = !is_array($map) ? [$field => $map] : $map;
/** return 0 < $model->where($map)->count();
* 根据结果提交滚回事务
* @param $res
*/
public static function checkTrans($res)
{
if ($res) {
self::commitTrans();
} else {
self::rollbackTrans();
}
}
/**
* 提交事务
*/
public static function commitTrans()
{
Db::commit();
}
/**
* 设置错误信息
* @param string $errorMsg
* @param bool $rollback
* @return bool
*/
protected static function setErrorInfo($errorMsg = self::DEFAULT_ERROR_MSG, $rollback = false)
{
if ($rollback) self::rollbackTrans();
self::$errorMsg = $errorMsg;
return false;
}
/**
* 关闭事务
*/
public static function rollbackTrans()
{
Db::rollback();
} }
} }

View File

@ -51,8 +51,11 @@ class Document extends BaseModel
* 获取文章信息 * 获取文章信息
* @param $id * @param $id
* @param string $type * @param string $type
* @param string $status * @param int $status
* @return array * @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/ */
public function getInfo($id, string $type = Data::DOCUMENT_TYPE_ARTICLE, $status = 1): array public function getInfo($id, string $type = Data::DOCUMENT_TYPE_ARTICLE, $status = 1): array
{ {
@ -183,4 +186,50 @@ class Document extends BaseModel
} }
return $res; return $res;
} }
/**
* 获取文章信息
* @param $id
* @param string $type
* @param int $status
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function getFullInfo($id, string $type = Data::DOCUMENT_TYPE_ARTICLE, $status = 1): array
{
if (empty($id)) {
return [];
}
$model = new Document();
$model = $model->alias('a')
->leftJoin('document_category c', 'c.id = a.category_id')
->leftJoin('user u', 'u.id = a.uid')
->field("a.*,p.content,c.title,c.alias,u.nickname,u.avatar,u.remark")
;
if (is_numeric($id)) {
$model->where("a.id", $id);
} else {
$model->where("a.alias", $id);
}
if ($type !== '') $model->where("a.type", $type);
if ($status !== '') $model->where("a.status", $status);
switch ($type) {
case Data::DOCUMENT_TYPE_ARTICLE:
$model->leftJoin('document_article p', 'a.id = p.id');
break;
case Data::DOCUMENT_TYPE_PAGE:
$model->leftJoin('document_page p', 'a.id = p.id');
break;
case Data::DOCUMENT_TYPE_PRODUCT:
$model->leftJoin('document_product p', 'a.id = p.id');
break;
}
$info = $model->find();
if (!$info) {
return [];
}
return $info->toArray();
}
} }

View File

@ -13,34 +13,5 @@ use think\Model;
trait ModelTrait trait ModelTrait
{ {
/**
* 错误信息,解决调用无法明确错误的问题
* @var mixed
*/
protected $error;
/**
* 获取错误信息
* @access public
* @return mixed
*/
public function getError()
{
return $this->error;
}
/**
* 查询一条数据是否存在
* @param $map
* @param string $field
* @return bool 是否存在
* @throws DbException
*/
public static function isExist($map, $field = ''): bool
{
$model = (new self);
if (!is_array($map) && empty($field)) $field = $model->getPk();
$map = !is_array($map) ? [$field => $map] : $map;
return 0 < $model->where($map)->count();
}
} }

View File

@ -105,6 +105,7 @@ function get_document_category_by_name($name, $field = false)
} }
} }
/** /**
* 获取一个文章分类 * 获取一个文章分类
*/ */