feat: 开放API文章相关

This commit is contained in:
2025-05-21 16:59:48 +08:00
parent 9ecc9ac957
commit e712030e3e
7 changed files with 299 additions and 8 deletions

View File

@@ -1,2 +1,70 @@
<?php
// 这是系统自动生成的公共文件
if (!function_exists('image_domain_concat')) {
/**
* 图片域名拼接
* @param $path
* @return string
*/
function image_domain_concat($path)
{
if (empty($path)) {
return '';
}
$domain = env('OPENAPI.RESOURCE_IMAGES_DOMAIN');
if (empty($domain)) {
return $path;
}
return rtrim($domain, '/') . '/' . ltrim($path, '/');
}
}
if (!function_exists('video_domain_concat')) {
/**
* 视频域名拼接
* @param $path
* @return string
*/
function video_domain_concat($path)
{
if (empty($path)) {
return '';
}
$domain = env('OPENAPI.RESOURCE_VIDEOS_DOMAIN');
if (empty($domain)) {
return $path;
}
return rtrim($domain, '/') . '/' . ltrim($path, '/');
}
}
if (!function_exists('html_image_replace')) {
/**
* 替换html中的图片路径
* @param $html
* @return string
*/
function html_image_replace($html)
{
if (empty($html)) {
return '';
}
return preg_replace_callback('/<img[^>]+src\s*=\s*([\'"])((?:(?!\1).)*)\1[^>]*>/i',
function($matches) {
$src = $matches[2];
if (!empty($src) && !str_starts_with($src, 'http')) {
// 保留原始标签只替换src属性
return str_replace($src, image_domain_concat($src), $matches[0]);
}
return $matches[0];
},
$html
);
}
}

View 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);
}
}

View 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);
}
}

View File

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

View File

@@ -0,0 +1,34 @@
<?php
declare (strict_types = 1);
namespace app\openapi\model;
use app\common\model\ArticleCategoryBaseModel;
use think\facade\Db;
/**
* 文章分类模型
* @mixin \think\Model
*/
class ArticleCategoryModel extends ArticleCategoryBaseModel
{
// 所属语言范围查询
public function scopeLanguage($query, $language = 'zh-cn')
{
$query->whereExists(function($subquery) use($language) {
$lang_model = new LanguageModel;
$subquery->model($lang_model)
->name($lang_model->getName())
->field(['id'])
->where('id', '=', Db::raw($this->getTable() . '.language_id'))
->where('code', '=', $language);
});
}
// 所属上级分类范围查询
public function scopeParent($query, $parent_id)
{
if (is_null($parent_id)) return;
$query->where('pid', '=', $parent_id);
}
}

View File

@@ -0,0 +1,40 @@
<?php
declare (strict_types = 1);
namespace app\openapi\model;
use app\common\model\ArticleBaseModel;
use think\facade\Db;
/**
* 文章模型
* @mixin \think\Model
*/
class ArticleModel extends ArticleBaseModel
{
// 关联分类
public function category()
{
return $this->belongsTo(ArticleCategoryModel::class, 'category_id', 'id');
}
// 所属语言范围查询
public function scopeLanguage($query, $language = 'zh-cn')
{
$query->whereExists(function($subquery) use($language) {
$lang_model = new LanguageModel;
$subquery->model($lang_model)
->name($lang_model->getName())
->field(['id'])
->where('id', '=', Db::raw($this->getTable() . '.language_id'))
->where('code', '=', $language);
});
}
// 所属分类范围查询
public function scopeCategoryId($query, $category_id)
{
if (is_null($category_id)) return;
$query->where('category_id', '=', $category_id);
}
}

View File

@@ -18,11 +18,14 @@ Route::group('v1', function() {
Route::get('categories', 'v1.ProductCategory/list');
});
// 获取新闻动态
Route::get('news', 'v1.News/list');
Route::group('news', function() {
// 获取新闻详情
Route::get(':id', 'v1.News/detail');
// 获取文章动态
Route::get('articles', 'v1.Article/list');
Route::group('article', function() {
// 获取文章详情
Route::get(':id', 'v1.Article/detail')->when('id', 'number');
// 获取文章分类
Route::get('categories', 'v1.ArticleCategory/list');
});
})
->middleware(\app\openapi\middleware\Auth::class);
@@ -32,4 +35,5 @@ Route::group('v1', function() {
'visit_fail_response' => function (\think\middleware\Throttle $throttle, \think\Request $request, int $wait_seconds) {
return \think\Response::create('您的操作过于频繁, 请在 ' . $wait_seconds . ' 秒后再试。')->code(429);
},
]);
])
->completeMatch(true);