胖虎 3 年之前
父節點
當前提交
a8f1cb4cf4
共有 2 個文件被更改,包括 145 次插入0 次删除
  1. 108 0
      app/Http/Controllers/AdminApi/BannerController.php
  2. 37 0
      app/Models/SysModels/Banner.php

+ 108 - 0
app/Http/Controllers/AdminApi/BannerController.php

@@ -0,0 +1,108 @@
+<?php
+
+namespace App\Http\Controllers\AdminApi;
+
+use App\Http\Controllers\AdminController;
+use App\Models\SysModels\Banner;
+
+class BannerController extends AdminController
+{
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * 获取轮播图列表
+     * @return \Illuminate\Http\JsonResponse
+     */
+    function getList(){
+        $title = request()->input('title','');//获取广告标题
+        //查询数据条件
+        $where = [['is_del',0]];
+        if($title) $where[] = ['title', 'like', "%$title%"];
+        //获取数据
+        $banner_list = Banner::where($where)
+            ->select(['id','cn_image','en_image','title','sort','link','created_at'])
+            ->orderBy('sort','asc')
+            ->paginate(10);
+
+        return $this->apiResponseSuccess('获取列表成功', [
+            'items' => $banner_list->items(),
+            'total' => $banner_list->total(),
+            'limit' => 10,
+        ]);
+    }
+    /**
+     * 获取记录详情
+     * @return \Illuminate\Http\JsonResponse
+     */
+    function getInfo(){
+        $id = request()->input('id','');//获取需要查询的记录id
+        if(empty($id)) return $this->apiResponseError('缺少必要参数');
+        //查询数据
+        $info = Banner::where('id',$id)->first();
+
+        if(empty($info)) return $this->apiResponseError('没有找到该记录');
+
+        return $this->apiResponseSuccess('获取成功', $info);
+    }
+
+    /**
+     * 编辑&新增
+     * @return \Illuminate\Http\JsonResponse
+     */
+    function saveBanner(){
+        $id = request()->input('id','');//记录id
+        $cn_image = request()->input('cn_image', '');//图片
+        $en_image = request()->input('en_image', '');//图片
+        $title = request()->input('title', '');//标题
+        $link = request()->input('link', ' ');//外部链接
+        $sort = request()->input('sort', '');//排序
+        if(empty($cn_image) || empty($en_image) || empty($title)){
+            return $this->apiResponseError('缺少必要参数');
+        }
+        $data['cn_image'] = $cn_image;
+        $data['en_image'] = $en_image;
+        $data['title'] = $title;
+        $data['link'] = !empty($link) ? $link : ' ';
+        $data['sort'] = !empty($sort) ? $sort : 50;
+
+        if(empty($id)){//不存在id就创建
+
+            $res = Banner::create($data);
+        }else{//存在就更新
+            $info = Banner::where('id',$id)->first();
+            if (empty($info)) {
+                return $this->apiResponseError('没有找到该记录');
+            }
+            $res = $info->update($data);
+        }
+
+        if ($res) {
+            return $this->apiResponseSuccess('操作成功');
+        } else {
+            return $this->apiResponseError('操作失败');
+        }
+    }
+
+    /**
+     * 删除(批量)轮播
+     * @return \Illuminate\Http\JsonResponse
+     */
+    function destroys(){
+        $ids = request()->input('ids','');//获取需要删除的id
+        if(empty($ids)) return $this->apiResponseError('缺少必要参数');
+        //数据条件
+        //数据条件
+        if(!is_array($ids)){
+            $ids = array_filter(explode(',', $ids));
+        }
+        $res = Banner::whereIn('id',$ids)->update(['is_del'=>1]);
+        if ($res) {
+            return $this->apiResponseSuccess('删除成功');
+        } else {
+            return $this->apiResponseError('删除失败');
+        }
+    }
+}

+ 37 - 0
app/Models/SysModels/Banner.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace App\Models\SysModels;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Banner extends Model
+{
+    /**
+     * 表名。
+     *
+     * @var string
+     */
+    protected $table = 'banners';
+
+    /**
+     * 与表关联的主键。
+     *
+     * @var string
+     */
+    protected $primaryKey = 'id';
+
+    /**
+     * 是否主动维护时间戳
+     *
+     * @var bool
+     */
+    public $timestamps = true;
+
+    /**
+     * 不能被批量赋值的属性
+     *
+     * @var array
+     */
+    protected $guarded = ['id', 'updated_at', 'created_at'];
+
+}