Banners.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Http\Controllers\Common;
  3. use App\Models\BannerModel;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. class Banners extends Controller
  7. {
  8. private $bannerModel;
  9. public function __construct()
  10. {
  11. $this->bannerModel = new BannerModel();
  12. }
  13. /**
  14. * 添加广告
  15. * @param Request $request
  16. * @return array
  17. */
  18. public function store(Request $request)
  19. {
  20. $banner = $this->bannerModel->fill($request->toArray());
  21. if ($banner->save()) {
  22. return array(
  23. 'code' => 200,
  24. 'message' => 'success',
  25. 'data' => [
  26. 'banner_id' => $banner->id
  27. ],
  28. );
  29. } else {
  30. return array(
  31. 'code' => 505,
  32. 'message' => '数据库异常'
  33. );
  34. }
  35. }
  36. /**
  37. * 更新广告
  38. * @param Request $request
  39. * @param int $banner_id
  40. * @return array
  41. */
  42. public function update(Request $request, int $banner_id)
  43. {
  44. $banner = $this->bannerModel->find($banner_id);
  45. if (collect($banner)->isEmpty()) {
  46. return array(
  47. 'code' => 101,
  48. 'message' => '参数错误'
  49. );
  50. }
  51. $banner->fill($request->toArray());
  52. if ($banner->save()) {
  53. return array(
  54. 'code' => 200,
  55. 'message' => 'success'
  56. );
  57. } else {
  58. return array(
  59. 'code' => 505,
  60. 'message' => '数据库异常'
  61. );
  62. }
  63. }
  64. /**
  65. * 删除广告
  66. * @param int $banner_id
  67. * @return array
  68. * @throws \Exception
  69. */
  70. public function delete(int $banner_id)
  71. {
  72. $banner = $this->bannerModel->find($banner_id);
  73. if (collect($banner)->isEmpty()) {
  74. return array(
  75. 'code' => 101,
  76. 'message' => '参数错误'
  77. );
  78. }
  79. if ($banner->delete()) {
  80. return array(
  81. 'code' => 200,
  82. 'message' => 'success'
  83. );
  84. } else {
  85. return array(
  86. 'code' => 505,
  87. 'message' => '数据库异常'
  88. );
  89. }
  90. }
  91. /**
  92. * 获取广告信息
  93. * @param int $banner_id
  94. * @return array
  95. */
  96. public function get(int $banner_id)
  97. {
  98. $banner = $this->bannerModel->find($banner_id);
  99. if (collect($banner)->isEmpty()) {
  100. return array(
  101. 'code' => 101,
  102. 'message' => '参数错误'
  103. );
  104. }
  105. return array(
  106. 'code' => 200,
  107. 'message' => 'success',
  108. 'data' => $banner
  109. );
  110. }
  111. /**
  112. * 获取某位置的广告
  113. * @param $position
  114. * @return array
  115. */
  116. public function listByPosition($position)
  117. {
  118. $banners = $this->bannerModel->where('position', $position)->orderBy('sort', 'desc')->get();
  119. return array(
  120. 'code' => 200,
  121. 'message' => 'success',
  122. 'data' => $banners
  123. );
  124. }
  125. }