海南旅游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::where('agent_id', $this->agent_id)
  18. ->select('id', 'image', 'title', 'type', 'updated_at')
  19. ->orderBy('sort')->orderBy('id', 'DESC')
  20. ->simplePaginate()->toArray();
  21. //如果没有数据,且没有软删除的数据,显示默认的几条数据,主要用于新代理商入驻时显示默认文章
  22. if (empty($list['data'])) {
  23. if (Article::withTrashed()->where('agent_id', $this->agent_id)->count() === 0 && request('page', 1) < 2) {
  24. $list = Article::select('id', 'image', 'title', 'type', 'updated_at')
  25. ->orderBy('id')->simplePaginate()->toArray();
  26. }
  27. }
  28. $new_data = [];
  29. $prefix = Storage::disk('public')->url('');
  30. foreach ($list['data'] as $k => &$item) {
  31. $item['image'] = $prefix . $item['image'];
  32. //type=1是大图
  33. if ($item['type'] == 1) {
  34. $new_data[] = ['big' => $item];
  35. continue;
  36. }
  37. //普通列表
  38. $count = count($new_data);
  39. if ($count) {
  40. $new_data[$count - 1]['children'][] = $item;
  41. } else {
  42. $new_data[] = ['children' => [$item]];
  43. }
  44. }
  45. $list['data'] = $new_data;
  46. return $this->success($list);
  47. }
  48. //文章详情
  49. public function show()
  50. {
  51. $id = (int)request()->input('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. }