feat: 开放API文章相关
This commit is contained in:
96
app/openapi/controller/v1/Article.php
Normal file
96
app/openapi/controller/v1/Article.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\openapi\controller\v1;
|
||||
|
||||
use app\openapi\model\ArticleModel;
|
||||
|
||||
class Article
|
||||
{
|
||||
/**
|
||||
* 文章列表
|
||||
*/
|
||||
public function list()
|
||||
{
|
||||
$params = request()->get([
|
||||
'category_id',
|
||||
'language' => 'zh-cn',
|
||||
'page/d' => 1,
|
||||
'size/d' => 50
|
||||
]);
|
||||
|
||||
if ($params['size'] > 200) {
|
||||
// 每页不超过200条
|
||||
$params['size'] = 200;
|
||||
}
|
||||
|
||||
$articles = ArticleModel::with([
|
||||
'category' => fn($query) => $query->field('id, name')
|
||||
])
|
||||
->field([
|
||||
'id',
|
||||
'language_id',
|
||||
'category_id',
|
||||
'title',
|
||||
'desc',
|
||||
'image',
|
||||
'deleted_at'
|
||||
])
|
||||
->categoryId($params['category_id']??null)
|
||||
->language($params['language']??'zh-cn')
|
||||
->hidden(['language_id', 'category_id'])
|
||||
->paginate([
|
||||
'list_rows' => $params['size'],
|
||||
'page' => $params['page']
|
||||
])
|
||||
->each(function($it) {
|
||||
if (!empty($it['image']) && !str_starts_with($it['image'], 'http')) {
|
||||
$it['image'] = image_domain_concat($it['image']);
|
||||
}
|
||||
|
||||
return $it;
|
||||
});
|
||||
|
||||
return success('success', $articles);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文章详情
|
||||
*/
|
||||
public function detail()
|
||||
{
|
||||
$id = request()->param('id');
|
||||
|
||||
$article = ArticleModel::with([
|
||||
'category' => fn($query) => $query->field('id, name')
|
||||
])
|
||||
->withoutField([
|
||||
'language_id',
|
||||
'seo_title',
|
||||
'seo_keywords',
|
||||
'seo_desc',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'deleted_at'
|
||||
])
|
||||
->bypk($id)
|
||||
->hidden(['category_id'])
|
||||
->find();
|
||||
|
||||
if (is_null($article)) {
|
||||
return error('the article does not exist');
|
||||
}
|
||||
|
||||
// 图片处理
|
||||
if (!empty($article['image']) && !str_starts_with($article['image'], 'http')) {
|
||||
$article['image'] = image_domain_concat($article['image']);
|
||||
}
|
||||
|
||||
// 详情中图片处理
|
||||
if (!empty($article['content'])) {
|
||||
$article['content'] = html_image_replace($article['content']);
|
||||
}
|
||||
|
||||
return success('success', $article);
|
||||
}
|
||||
}
|
||||
50
app/openapi/controller/v1/ArticleCategory.php
Normal file
50
app/openapi/controller/v1/ArticleCategory.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\openapi\controller\v1;
|
||||
|
||||
use app\openapi\model\ArticleCategoryModel;
|
||||
|
||||
class ArticleCategory
|
||||
{
|
||||
/**
|
||||
* 文章分类列表
|
||||
*/
|
||||
public function list()
|
||||
{
|
||||
$params = request()->get([
|
||||
'parent_id',
|
||||
'language' => 'zh-cn',
|
||||
'page/d' => 1,
|
||||
'size/d' => 50
|
||||
]);
|
||||
|
||||
if ($params['size'] > 200) {
|
||||
// 每页不超过200条
|
||||
$params['size'] = 200;
|
||||
}
|
||||
|
||||
$categories = ArticleCategoryModel::withoutField([
|
||||
'language_id',
|
||||
'unique_label',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
])
|
||||
->language($params['language']??'zh-cn')
|
||||
->parent($params['parent_id']??null)
|
||||
->order(['sort' => 'asc', 'id' => 'desc'])
|
||||
->paginate([
|
||||
'list_rows' => $params['size'],
|
||||
'page' => $params['page'],
|
||||
])
|
||||
->each(function($item) {
|
||||
if (!empty($item['icon']) && !str_starts_with($item['icon'], 'http')){
|
||||
$item['icon'] = image_domain_concat($item['icon']);
|
||||
}
|
||||
|
||||
return $item;
|
||||
});
|
||||
|
||||
return success('success', $categories);
|
||||
}
|
||||
}
|
||||
@@ -29,8 +29,7 @@ class ProductCategory
|
||||
'unique_id',
|
||||
'related_tco_category',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'deleted_at'
|
||||
'updated_at'
|
||||
])
|
||||
->language($params['language']??'zh-cn')
|
||||
->parent($params['parent_id']??null)
|
||||
|
||||
Reference in New Issue
Block a user