123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- namespace App\Http\Controllers\Common;
- use App\Models\BannerModel;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- class Banners extends Controller
- {
- private $bannerModel;
- public function __construct()
- {
- $this->bannerModel = new BannerModel();
- }
- /**
- * 添加广告
- * @param Request $request
- * @return array
- */
- public function store(Request $request)
- {
- $banner = $this->bannerModel->fill($request->toArray());
- if ($banner->save()) {
- return array(
- 'code' => 200,
- 'message' => 'success',
- 'data' => [
- 'banner_id' => $banner->id
- ],
- );
- } else {
- return array(
- 'code' => 505,
- 'message' => '数据库异常'
- );
- }
- }
- /**
- * 更新广告
- * @param Request $request
- * @param int $banner_id
- * @return array
- */
- public function update(Request $request, int $banner_id)
- {
- $banner = $this->bannerModel->find($banner_id);
- if (collect($banner)->isEmpty()) {
- return array(
- 'code' => 101,
- 'message' => '参数错误'
- );
- }
- $banner->fill($request->toArray());
- if ($banner->save()) {
- return array(
- 'code' => 200,
- 'message' => 'success'
- );
- } else {
- return array(
- 'code' => 505,
- 'message' => '数据库异常'
- );
- }
- }
- /**
- * 删除广告
- * @param int $banner_id
- * @return array
- * @throws \Exception
- */
- public function delete(int $banner_id)
- {
- $banner = $this->bannerModel->find($banner_id);
- if (collect($banner)->isEmpty()) {
- return array(
- 'code' => 101,
- 'message' => '参数错误'
- );
- }
- if ($banner->delete()) {
- return array(
- 'code' => 200,
- 'message' => 'success'
- );
- } else {
- return array(
- 'code' => 505,
- 'message' => '数据库异常'
- );
- }
- }
- /**
- * 获取广告信息
- * @param int $banner_id
- * @return array
- */
- public function get(int $banner_id)
- {
- $banner = $this->bannerModel->find($banner_id);
- if (collect($banner)->isEmpty()) {
- return array(
- 'code' => 101,
- 'message' => '参数错误'
- );
- }
- return array(
- 'code' => 200,
- 'message' => 'success',
- 'data' => $banner
- );
- }
- /**
- * 获取某位置的广告
- * @param $position
- * @return array
- */
- public function listByPosition($position)
- {
- $banners = $this->bannerModel->where('position', $position)->orderBy('sort', 'desc')->get();
- return array(
- 'code' => 200,
- 'message' => 'success',
- 'data' => $banners
- );
- }
- }
|