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

84 lines
2.3 KiB

4 years ago
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\AgentProduct;
  5. use App\Models\Article;
  6. use Illuminate\Support\Facades\Storage;
  7. /**
  8. * 文章
  9. * Class ArticleController
  10. * @package App\Http\Controllers\Api
  11. */
  12. class ArticleController extends Controller
  13. {
  14. //文章列表
  15. public function index()
  16. {
  17. $list = Article::select('id', 'image', 'title', 'type', 'updated_at')
  18. ->orderBy('sort')->orderBy('id', 'DESC')
  19. ->simplePaginate()->toArray();
  20. //如果没有数据,且没有软删除的数据,显示默认的几条数据,主要用于新代理商入驻时显示默认文章
  21. /*if (empty($list['data'])) {
  22. if (Article::withTrashed()->where('agent_id', $this->agent_id)->count() === 0 && request('page', 1) < 2) {
  23. $list = Article::select('id', 'image', 'title', 'type', 'updated_at')
  24. ->orderBy('id')->simplePaginate()->toArray();
  25. }
  26. }*/
  27. $new_data = [];
  28. $prefix = Storage::disk('public')->url('');
  29. foreach ($list['data'] as $k => &$item) {
  30. $item['image'] = $prefix . $item['image'];
  31. //type=1是大图
  32. if ($item['type'] == 1) {
  33. $new_data[] = ['big' => $item];
  34. continue;
  35. }
  36. //普通列表
  37. $count = count($new_data);
  38. if ($count) {
  39. $new_data[$count - 1]['children'][] = $item;
  40. } else {
  41. $new_data[] = ['children' => [$item]];
  42. }
  43. }
  44. $list['data'] = $new_data;
  45. return $this->success($list);
  46. }
  47. //文章详情
  48. public function show()
  49. {
  50. $id = (int)request()->input('id');
  51. $article = Article::find($id);
  52. /*if (Article::withTrashed()->where('agent_id', $this->agent_id)->count() === 0) { //演示文章
  53. $article = Article::find($id);
  54. } else {
  55. $article = Article::where('agent_id', $this->agent_id)->find($id);
  56. }*/
  57. if (!$article) {
  58. return $this->error('文章不存在或已被删除');
  59. }
  60. $article->image = Storage::disk('public')->url('') . $article->image;
  61. if ($article->agent_product_ids) {
  62. $product = AgentProduct::list($this->agent_id)
  63. ->whereIn('id', $article->agent_product_ids)
  64. ->get();
  65. if (!$product->isEmpty()) {
  66. $prefix = Storage::disk('public')->url('');
  67. foreach ($product as &$v) {
  68. $v->pictures = array_map(fn($item) => $prefix . $item, $v->pictures);
  69. }
  70. }
  71. }
  72. $article->product = $product ?? [];
  73. return $this->success($article);
  74. }
  75. }