|
|
|
@ -13,6 +13,11 @@ 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; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -26,7 +31,7 @@ class Article extends Controller |
|
|
|
|
* 文章列表 |
|
|
|
|
* @param int $categoryId |
|
|
|
|
* @return Json |
|
|
|
|
* @throws \think\db\exception\DbException |
|
|
|
|
* @throws DbException |
|
|
|
|
*/ |
|
|
|
|
public function list(int $categoryId = 0): Json |
|
|
|
|
{ |
|
|
|
@ -40,7 +45,7 @@ class Article extends Controller |
|
|
|
|
* 文章详情 |
|
|
|
|
* @param int $articleId |
|
|
|
|
* @return Json |
|
|
|
|
* @throws \cores\exception\BaseException |
|
|
|
|
* @throws BaseException |
|
|
|
|
*/ |
|
|
|
|
public function detail(int $articleId): Json |
|
|
|
|
{ |
|
|
|
@ -48,6 +53,145 @@ class Article extends Controller |
|
|
|
|
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 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; |
|
|
|
|