diff --git a/app/admin/controller/v1/Article.php b/app/admin/controller/v1/Article.php index 34c6c686..1edef4e4 100644 --- a/app/admin/controller/v1/Article.php +++ b/app/admin/controller/v1/Article.php @@ -3,7 +3,151 @@ declare (strict_types = 1); namespace app\admin\controller\v1; +use app\admin\model\v1\ArticleModel; +use app\admin\validate\v1\ArticleValidate; + class Article { - // + // 文章列表 + public function index() + { + $param = request()->param([ + 'title', + 'category_id', + 'created_at', + 'page/d' => 1, + 'limit/d' => 10, + ]); + + $article = ArticleModel::withoutField([ + 'language_id', + 'category_id', + 'author', + 'source', + 'desc', + 'link', + 'content', + 'updated_at', + 'deleted_at', + 'seo_title', + 'seo_keywords', + 'seo_desc' + ]) + ->category($param['category_id']) + ->withSearch(['title', 'created_at'], (function() use($param) { + $condition = [ + 'title' => $param['title'], + ]; + if (isset($param['created_at'])) { + $condition = [ + 'created_at' => explode(',', $param['created_at']) + ]; + } + return $condition; + })()) + ->order('sort', 'desc') + ->page($param['page'], $param['limit']) + ->select(); + + return success('获取成功', $article); + } + + // 文章详情 + public function read() + { + $article = ArticleModel::withoutField([ + 'language_id', + 'updated_at', + 'deleted_at', + ]) + ->id(request()->param('id')) + ->find(); + if (is_null($article)) { + return error('文章不存在'); + } + + return success('获取成功', $article); + } + + // 添加文章 + public function save() + { + $post = request()->post([ + 'category_id', + 'title', + 'image', + 'link', + 'sort' => 0, + 'desc', + 'content', + 'recommend', + 'release_time', + 'seo_title', + 'seo_keywords', + 'seo_desc', + ]); + $data = array_merge($post, ['language_id' => request()->lang_id]); + + $validate = new ArticleValidate; + if (!$validate->check($data)) { + return error($validate->getError()); + } + + $article = new ArticleModel; + if (!$article->save($data)) { + return error('操作失败'); + } + return success('操作成功'); + } + + // 更新文章 + public function update() + { + $id = request()->param('id'); + $put = request()->put([ + 'category_id', + 'title', + 'image', + 'link', + 'sort', + 'desc', + 'content', + 'recommend', + 'release_time', + 'seo_title', + 'seo_keywords', + 'seo_desc', + ]); + $data = array_merge($put, ['language_id' => request()->lang_id]); + + $validate = new ArticleValidate; + if (!$validate->check(array_merge($data, ['id' => $id]))) { + return error($validate->getError()); + } + + $article = ArticleModel::id($id)->find(); + if (is_null($article)) { + return error('请确认操作对象是否存在'); + } + + if (!$article->save($data)) { + return error('操作失败'); + } + return success('操作成功'); + } + + // 删除文章 + public function delete() + { + $param = request()->param('id'); + $article = ArticleModel::id($param['id'])->find(); + if (is_null($article)) { + return error('请确认操作对象是否存在'); + } + + if (!$article->useSoftDelete('deleted_at', date('Y-m-d H:m:s', time()))->delete()) { + return error('操作失败'); + } + return success('操作成功'); + } } diff --git a/app/admin/controller/v1/Captcha.php b/app/admin/controller/v1/Captcha.php index 91edd478..2c3790b9 100644 --- a/app/admin/controller/v1/Captcha.php +++ b/app/admin/controller/v1/Captcha.php @@ -26,6 +26,7 @@ class Captcha // 输出验证码 return success('获取验证码成功!', [ 'token' => $token, + 'code' => $captcha['code'], 'captcha' => $captcha["img"], ]); } diff --git a/app/admin/model/v1/ArticleModel.php b/app/admin/model/v1/ArticleModel.php index 43ca5bdd..c71ecb9d 100644 --- a/app/admin/model/v1/ArticleModel.php +++ b/app/admin/model/v1/ArticleModel.php @@ -4,12 +4,18 @@ declare (strict_types = 1); namespace app\admin\model\v1; use think\Model; +use think\model\concern\SoftDelete; /** * @mixin \think\Model */ class ArticleModel extends Model { + // 启用软件删除 + use SoftDelete; + // 软删除标记数据字段 + protected $deleteTime = 'deleted_at'; + // 表名 protected $name = 'article'; @@ -19,7 +25,7 @@ class ArticleModel extends Model // 字段信息 protected $schema = [ 'id' => 'int', - 'languge_id' => 'int', + 'language_id' => 'int', 'category_id' => 'int', 'title' => 'string', 'author' => 'string', @@ -40,4 +46,34 @@ class ArticleModel extends Model 'updated_at' => 'datetime', 'deleted_at' => 'datetime', ]; + + // 搜索名称 + public function searchTitleAttr($query, $value, $data) + { + $query->where('title', 'like', '%' . $value . '%'); + } + + // 搜索发布时间 + public function searchCreatedAtAttr($query, $value, $data) + { + if (is_array($value)) { + if (count($value) == 2) { + $query->whereBetweenTime('created_at', $value[0], $value[1]); + } else { + $query->whereTime('created_at', '>=', $value[0]); + } + } + } + + // 主键查询 + public function scopeId($query, $value) + { + $query->where('id', '=', $value); + } + + // 分类查询 + public function scopeCategory($query, $value) + { + $query->where('category_id', '=', $value); + } } diff --git a/app/admin/route/v1.php b/app/admin/route/v1.php index 24083568..2d9c04bd 100644 --- a/app/admin/route/v1.php +++ b/app/admin/route/v1.php @@ -28,6 +28,21 @@ Route::group('v1', function () { // 文章模块 Route::group('article', function () { + // 文章列表 + Route::get('index', 'Article/index'); + + // 文章详情 + Route::get('read/:id', 'Article/read'); + + // 文章新增 + Route::post('save', 'Article/save'); + + // 文章更新 + Route::put('update/:id', 'Article/update'); + + // 文章删除 + Route::delete('delete/:id', 'Article/delete'); + // 文章分类 Route::group('category', function () { // 分类列表 diff --git a/app/admin/validate/v1/ArticleValidate.php b/app/admin/validate/v1/ArticleValidate.php new file mode 100644 index 00000000..7e65567b --- /dev/null +++ b/app/admin/validate/v1/ArticleValidate.php @@ -0,0 +1,54 @@ + ['规则1','规则2'...] + * + * @var array + */ + protected $rule = [ + 'category_id' => 'require|integer', + 'title' => 'require|max:64', + 'image' => 'max:125', + 'link' => 'max:255', + 'desc' => 'max:255', + 'sort' => 'integer', + 'content' => 'require', + 'recommend' => 'require|in:0,1', + 'release_time' => 'date', + 'seo_title' => 'max:255', + 'seo_keywords' => 'max:255', + 'seo_desc' => 'max:255' + ]; + + /** + * 定义错误信息 + * 格式:'字段名.规则名' => '错误信息' + * + * @var array + */ + protected $message = [ + 'category_id.require' => '分类不能为空', + 'category_id.integer' => '分类格式不正确', + 'title.require' => '标题不能为空', + 'title.max' => '标题长度不能超过64个字符', + 'image.max' => '图片地址长度不能超过125个字符', + 'link.max' => '链接地址长度不能超过255个字符', + 'desc.max' => '描述长度不能超过255个字符', + 'sort.integer' => '排序格式不正确', + 'content.require' => '内容不能为空', + 'recommend.require' => '推荐状态不能为空', + 'recommend.in' => '推荐状态的值必须是0或1', + 'release_time.date' => '发布时间格式不正确', + 'seo_title.max' => 'SEO标题长度不能超过255个字符', + 'seo_keywords.max' => 'SEO关键字长度不能超过255个字符', + 'seo_desc.max' => 'SEO描述长度不能超过255个字符' + ]; +}