海南旅游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.
 
 
 

80 lines
2.1 KiB

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\AgentProduct;
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();
//如果没有数据,且没有软删除的数据,显示默认的几条数据,主要用于新代理商入驻时显示默认文章
if (empty($list['data'])) {
if (Article::withTrashed()->where('agent_id', $this->agent_id)->count() === 0 && request('page', 1) < 2) {
$list = Article::select('id', 'image', 'title', 'type', 'updated_at')
->orderBy('id')->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;
if ($article->agent_product_ids) {
$product = AgentProduct::list($this->agent_id)
->whereIn('id', $article->agent_product_ids)
->get();
if (!$product->isEmpty()) {
$prefix = Storage::disk('public')->url('');
foreach ($product as &$v) {
$v->pictures = array_map(fn($item) => $prefix . $item, $v->pictures);
}
}
}
$article->product = $product ?? [];
return $this->success($article);
}
}