You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
267 lines
8.5 KiB
267 lines
8.5 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
|
// +----------------------------------------------------------------------
|
|
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
|
// +----------------------------------------------------------------------
|
|
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
|
// +----------------------------------------------------------------------
|
|
// | Author: 萤火科技 <admin@yiovo.com>
|
|
// +----------------------------------------------------------------------
|
|
declare (strict_types=1);
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\api\model\Article as ArticleModel;
|
|
use app\api\service\User as UserService;
|
|
use cores\exception\BaseException;
|
|
use think\db\exception\DataNotFoundException;
|
|
use think\db\exception\DbException;
|
|
use think\db\exception\ModelNotFoundException;
|
|
use think\response\Json;
|
|
|
|
/**
|
|
* 文章控制器
|
|
* Class Article
|
|
* @package app\api\controller
|
|
*/
|
|
class Article extends Controller
|
|
{
|
|
/**
|
|
* 文章列表
|
|
* @param int $categoryId
|
|
* @return Json
|
|
* @throws DbException
|
|
*/
|
|
public function list(int $categoryId = 0): Json
|
|
{
|
|
$model = new ArticleModel;
|
|
$title = (string)$this->request->param('title');
|
|
$list = $model->getList($categoryId, 15, $title);
|
|
return $this->renderSuccess(compact('list'));
|
|
}
|
|
|
|
/**
|
|
* 文章详情
|
|
* @param int $articleId
|
|
* @return Json
|
|
* @throws BaseException
|
|
*/
|
|
public function detail(int $articleId): Json
|
|
{
|
|
$detail = ArticleModel::getDetail($articleId);
|
|
return $this->renderSuccess(compact('detail'));
|
|
}
|
|
|
|
/**
|
|
* @notes:新增文章
|
|
* @return Json
|
|
* @throws BaseException
|
|
* @author: wanghousheng
|
|
*/
|
|
public function add(): Json
|
|
{
|
|
if (!UserService::isStore()) {
|
|
throwError("无权限", 403);
|
|
}
|
|
$title = $this->request->post('title');
|
|
if (!$title) {
|
|
return $this->renderError('标题不能为空');
|
|
}
|
|
$category_id = intval($this->request->post('category_id'));
|
|
if (!$category_id) {
|
|
return $this->renderError('分类不能为空');
|
|
}
|
|
$image_id = intval($this->request->post('image_id'));
|
|
if (!$image_id) {
|
|
return $this->renderError('图片不能为空');
|
|
}
|
|
$content = $this->request->post('content');
|
|
if (!$content) {
|
|
return $this->renderError('内容不能为空');
|
|
}
|
|
$sort = intval($this->request->post('sort', 100));
|
|
$status = intval($this->request->post('status', 1));
|
|
$data = compact('status', 'sort', 'content', 'image_id', 'category_id', 'title');
|
|
if ((new ArticleModel)->add($data)) {
|
|
return $this->renderSuccess('添加成功');
|
|
}
|
|
return $this->renderError('添加失败');
|
|
}
|
|
|
|
/**
|
|
* @notes:编辑文章
|
|
* @return Json
|
|
* @throws BaseException
|
|
* @author: wanghousheng
|
|
*/
|
|
public function edit(): Json
|
|
{
|
|
if (!UserService::isStore()) {
|
|
throwError("无权限", 403);
|
|
}
|
|
$articleId = intval($this->request->post('article_id'));
|
|
if (!$articleId) {
|
|
return $this->renderError('缺少必要参数');
|
|
}
|
|
$title = $this->request->post('title');
|
|
if (!$title) {
|
|
return $this->renderError('标题不能为空');
|
|
}
|
|
$category_id = intval($this->request->post('category_id'));
|
|
if (!$category_id) {
|
|
return $this->renderError('分类不能为空');
|
|
}
|
|
$image_id = intval($this->request->post('image_id'));
|
|
if (!$image_id) {
|
|
return $this->renderError('图片不能为空');
|
|
}
|
|
$content = $this->request->post('content');
|
|
if (!$content) {
|
|
return $this->renderError('内容不能为空');
|
|
}
|
|
$sort = intval($this->request->post('sort', 100));
|
|
$status = intval($this->request->post('status', 1));
|
|
$data = compact('status', 'sort', 'content', 'image_id', 'category_id', 'title');
|
|
// 文章详情
|
|
$model = ArticleModel::detail($articleId);
|
|
// 更新记录
|
|
if ($model->edit($data)) {
|
|
return $this->renderSuccess('更新成功');
|
|
}
|
|
return $this->renderError($model->getError() ?: '更新失败');
|
|
}
|
|
|
|
/**
|
|
* @notes:删除文章
|
|
* @return Json
|
|
* @throws BaseException
|
|
* @author: wanghousheng
|
|
*/
|
|
public function delete(): Json
|
|
{
|
|
if (!UserService::isStore()) {
|
|
throwError("无权限", 403);
|
|
}
|
|
$articleId = intval($this->request->post('article_id'));
|
|
if (!$articleId) {
|
|
return $this->renderError('缺少必要参数');
|
|
}
|
|
// 文章详情
|
|
$model = ArticleModel::detail($articleId);
|
|
if (!$model->setDelete()) {
|
|
return $this->renderError($model->getError() ?: '删除失败');
|
|
}
|
|
return $this->renderSuccess('删除成功');
|
|
}
|
|
|
|
/**
|
|
* @notes:添加分类
|
|
* @return Json
|
|
* @throws BaseException
|
|
* @author: wanghousheng
|
|
*/
|
|
public function addCategory(): Json
|
|
{
|
|
if (!UserService::isStore()) {
|
|
throwError("无权限", 403);
|
|
}
|
|
$name = $this->request->post('name');
|
|
if (!$name) {
|
|
return $this->renderError('名称不能为空');
|
|
}
|
|
$img_id = intval($this->request->post('img_id'));
|
|
if (!$img_id) {
|
|
return $this->renderError('图片不能为空');
|
|
}
|
|
$status = intval($this->request->post('status', 1));
|
|
$sort = intval($this->request->post('sort', 100));
|
|
$data = compact('status', 'sort', 'name', 'img_id');
|
|
if ((new \app\api\model\article\Category())->add($data)) {
|
|
return $this->renderSuccess('添加成功');
|
|
}
|
|
return $this->renderError('添加失败');
|
|
}
|
|
|
|
/**
|
|
* @notes:更新分类
|
|
* @return Json
|
|
* @throws BaseException
|
|
* @author: wanghousheng
|
|
*/
|
|
public function editCategory(): Json
|
|
{
|
|
if (!UserService::isStore()) {
|
|
throwError("无权限", 403);
|
|
}
|
|
$categoryId = intval($this->request->post('category_id'));
|
|
if (!$categoryId) {
|
|
return $this->renderError('缺少必要参数');
|
|
}
|
|
$name = $this->request->post('name');
|
|
if (!$name) {
|
|
return $this->renderError('名称不能为空');
|
|
}
|
|
$img_id = intval($this->request->post('img_id'));
|
|
if (!$img_id) {
|
|
return $this->renderError('图片不能为空');
|
|
}
|
|
$status = intval($this->request->post('status', 1));
|
|
$sort = intval($this->request->post('sort', 100));
|
|
$data = compact('status', 'sort', 'name', 'img_id');
|
|
// 分类详情
|
|
$model = \app\api\model\article\Category::detail($categoryId);
|
|
// 更新记录
|
|
if ($model->edit($data)) {
|
|
return $this->renderSuccess('更新成功');
|
|
}
|
|
return $this->renderError($model->getError() ?: '更新失败');
|
|
}
|
|
|
|
/**
|
|
* @notes:删除分类
|
|
* @return Json
|
|
* @throws BaseException
|
|
* @author: wanghousheng
|
|
*/
|
|
public function delCategory(): Json
|
|
{
|
|
if (!UserService::isStore()) {
|
|
throwError("无权限", 403);
|
|
}
|
|
$categoryId = intval($this->request->post('category_id'));
|
|
if (!$categoryId) {
|
|
return $this->renderError('缺少必要参数');
|
|
}
|
|
$model = \app\api\model\article\Category::detail($categoryId);
|
|
if (!$model->remove($categoryId)) {
|
|
return $this->renderError($model->getError() ?: '删除失败');
|
|
}
|
|
return $this->renderSuccess('删除成功');
|
|
}
|
|
|
|
/**
|
|
* @notes:分类列表
|
|
* @return Json
|
|
* @throws DataNotFoundException
|
|
* @throws DbException
|
|
* @throws ModelNotFoundException|BaseException
|
|
* @author: wanghousheng
|
|
*/
|
|
public function categoryList(): Json
|
|
{
|
|
if (!UserService::isStore()) {
|
|
throwError("无权限", 403);
|
|
}
|
|
$model = new \app\api\model\article\Category;
|
|
$list = $model->getList();
|
|
return $this->renderSuccess(compact('list'));
|
|
}
|
|
|
|
public function helpCenter(): Json
|
|
{
|
|
$model = new ArticleModel;
|
|
$list = $model->helpCenter();
|
|
return $this->renderSuccess(compact('list'));
|
|
}
|
|
}
|
|
|