123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?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','url','sort','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', '');//标题
- $url = request()->input('url','');//跳转链接
- $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['url'] = !empty($url) ? $url : '';
- $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)) return $this->apiResponseError('数据格式错误');
- $res = Banner::whereIn('id',$ids)->update(['is_del'=>1]);
- if ($res) {
- return $this->apiResponseSuccess('删除成功');
- } else {
- return $this->apiResponseError('删除失败');
- }
- }
- }
|