海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

58 lines
1.3 KiB

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Article;
use Illuminate\Support\Facades\Storage;
/**
* 文章
* Class ArticleController
* @package App\Http\Controllers\Api
*/
class ArticleController extends Controller
{
//文章列表
public function index()
{
$list = Article::where('agent_id', $this->agent_id)
->select('id', 'image', 'title', 'type', 'updated_at')
->orderBy('sort')->orderBy('id', 'DESC')
->simplePaginate()->toArray();
$new_data = [];
$prefix = Storage::disk('public')->url('');
foreach ($list['data'] as $k => &$item) {
$item['image'] = $prefix . $item['image'];
//type=1是大图
if ($item['type'] == 1) {
$new_data[] = ['big' => $item];
continue;
}
//普通列表
$count = count($new_data);
if ($count) {
$new_data[$count - 1]['children'][] = $item;
} else {
$new_data[] = ['children' => [$item]];
}
}
$list['data'] = $new_data;
return $this->success($list);
}
//文章详情
public function show()
{
$id = (int)request()->input('id');
$article = Article::where('agent_id', $this->agent_id)->find($id);
if (!$article) {
return $this->error('文章不存在或已被删除');
}
$article->image = Storage::disk('public')->url('') . $article->image;
return $this->success($article);
}
}