pull/1/head
wanghousheng 10 months ago
parent 88d7261937
commit e5f7186fc3
  1. 148
      app/api/controller/Article.php
  2. 11
      app/api/model/Article.php
  3. 36
      app/api/model/article/Category.php

@ -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;

@ -98,6 +98,17 @@ class Article extends ArticleModel
return static::preload($list, ['image', 'category']);
}
/**
* 新增记录
* @param array $data
* @return bool
*/
public function add(array $data): bool
{
$data['store_id'] = self::$storeId;
return $this->save($data);
}
public function helpCenter()
{
$cat = CategoryModel::where(['status' => 1])->with(['catImg'])->order('sort desc')->select()->toArray();

@ -12,6 +12,7 @@ declare (strict_types=1);
namespace app\api\model\article;
use app\api\model\Article as ArticleModel;
use app\common\model\article\Category as CategoryModel;
/**
@ -44,4 +45,39 @@ class Category extends CategoryModel
return $this->getList(['status' => 1]);
}
public function add(array $data): bool
{
// 保存记录
$data['store_id'] = self::$storeId;
return $this->save($data);
}
/**
* 编辑记录
* @param array $data
* @return bool
*/
public function edit(array $data): bool
{
// 保存记录
return $this->save($data);
}
/**
* 删除商品分类
* @param int $categoryId
* @return bool
*/
public function remove(int $categoryId): bool
{
// 判断是否存在文章
$articleCount = ArticleModel::getArticleTotal(['category_id' => $categoryId]);
if ($articleCount > 0) {
$this->error = '该分类下存在' . $articleCount . '个文章,不允许删除';
return false;
}
// 删除记录
return $this->delete();
}
}

Loading…
Cancel
Save