diff --git a/app/api/controller/Article.php b/app/api/controller/Article.php new file mode 100644 index 0000000..e9b3a40 --- /dev/null +++ b/app/api/controller/Article.php @@ -0,0 +1,58 @@ +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'); + } + + +} diff --git a/app/api/controller/Base.php b/app/api/controller/Base.php index f9976a0..d697cc9 100644 --- a/app/api/controller/Base.php +++ b/app/api/controller/Base.php @@ -27,8 +27,4 @@ class Base extends BaseController protected $userId; // 初始化 - protected function initialize() - { - parent::initialize(); - } } \ No newline at end of file diff --git a/app/api/controller/Index.php b/app/api/controller/Index.php index 49a543b..62abfd8 100644 --- a/app/api/controller/Index.php +++ b/app/api/controller/Index.php @@ -4,6 +4,7 @@ namespace app\api\controller; use app\admin\controller\AuthController; use app\common\constant\Data; +use app\common\model\SystemConfig; /** * 应用入口 @@ -19,4 +20,13 @@ class Index extends Base { 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"); + } } \ No newline at end of file diff --git a/app/api/controller/Login.php b/app/api/controller/Login.php new file mode 100644 index 0000000..361d31c --- /dev/null +++ b/app/api/controller/Login.php @@ -0,0 +1,90 @@ +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(); + } +} \ No newline at end of file diff --git a/app/api/controller/Page.php b/app/api/controller/Page.php new file mode 100644 index 0000000..a3cfa2f --- /dev/null +++ b/app/api/controller/Page.php @@ -0,0 +1,8 @@ +error; + } + //自动时间戳 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(); - } - - /** - * 根据结果提交滚回事务 - * @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(); + $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(); } } \ No newline at end of file diff --git a/app/common/model/Document.php b/app/common/model/Document.php index 2a5172a..e427c0d 100644 --- a/app/common/model/Document.php +++ b/app/common/model/Document.php @@ -51,8 +51,11 @@ class Document extends BaseModel * 获取文章信息 * @param $id * @param string $type - * @param string $status + * @param int $status * @return array + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException */ public function getInfo($id, string $type = Data::DOCUMENT_TYPE_ARTICLE, $status = 1): array { @@ -183,4 +186,50 @@ class Document extends BaseModel } 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(); + } } \ No newline at end of file diff --git a/app/common/model/ModelTrait.php b/app/common/model/ModelTrait.php index 70d7714..4074dc0 100644 --- a/app/common/model/ModelTrait.php +++ b/app/common/model/ModelTrait.php @@ -13,34 +13,5 @@ use think\Model; 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(); - } } \ No newline at end of file diff --git a/app/index/common.php b/app/index/common.php index 25d6b1f..04a7b57 100644 --- a/app/index/common.php +++ b/app/index/common.php @@ -105,6 +105,7 @@ function get_document_category_by_name($name, $field = false) } } + /** * 获取一个文章分类 */