BannerController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Http\Controllers\AdminApi;
  3. use App\Http\Controllers\AdminController;
  4. use App\Models\SysModels\Banner;
  5. class BannerController extends AdminController
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. }
  11. /**
  12. * 获取轮播图列表
  13. * @return \Illuminate\Http\JsonResponse
  14. */
  15. function getList(){
  16. $title = request()->input('title','');//获取广告标题
  17. //查询数据条件
  18. $where = [['is_del',0]];
  19. if($title) $where[] = ['title', 'like', "%$title%"];
  20. //获取数据
  21. $banner_list = Banner::where($where)
  22. ->select(['id','cn_image','en_image','title','url','sort','created_at'])
  23. ->orderBy('sort','asc')
  24. ->paginate(10);
  25. return $this->apiResponseSuccess('获取列表成功', [
  26. 'items' => $banner_list->items(),
  27. 'total' => $banner_list->total(),
  28. 'limit' => 10,
  29. ]);
  30. }
  31. /**
  32. * 获取记录详情
  33. * @return \Illuminate\Http\JsonResponse
  34. */
  35. function getInfo(){
  36. $id = request()->input('id','');//获取需要查询的记录id
  37. if(empty($id)) return $this->apiResponseError('缺少必要参数');
  38. //查询数据
  39. $info = Banner::where('id',$id)->first();
  40. if(empty($info)) return $this->apiResponseError('没有找到该记录');
  41. return $this->apiResponseSuccess('获取成功', $info);
  42. }
  43. /**
  44. * 编辑&新增
  45. * @return \Illuminate\Http\JsonResponse
  46. */
  47. function saveBanner(){
  48. $id = request()->input('id','');//记录id
  49. $cn_image = request()->input('cn_image', '');//图片
  50. $en_image = request()->input('en_image', '');//图片
  51. $title = request()->input('title', '');//标题
  52. $url = request()->input('url','');//跳转链接
  53. $sort = request()->input('sort', '');//排序
  54. if(empty($cn_image) || empty($en_image) || empty($title)){
  55. return $this->apiResponseError('缺少必要参数');
  56. }
  57. $data['cn_image'] = $cn_image;
  58. $data['en_image'] = $en_image;
  59. $data['title'] = $title;
  60. $data['url'] = !empty($url) ? $url : '';
  61. $data['sort'] = !empty($sort) ? $sort : 50;
  62. if(empty($id)){//不存在id就创建
  63. $res = Banner::create($data);
  64. }else{//存在就更新
  65. $info = Banner::where('id',$id)->first();
  66. if (empty($info)) {
  67. return $this->apiResponseError('没有找到该记录');
  68. }
  69. $res = $info->update($data);
  70. }
  71. if ($res) {
  72. return $this->apiResponseSuccess('操作成功');
  73. } else {
  74. return $this->apiResponseError('操作失败');
  75. }
  76. }
  77. /**
  78. * 删除(批量)轮播
  79. * @return \Illuminate\Http\JsonResponse
  80. */
  81. function destroys(){
  82. $ids = request()->input('ids','');//获取需要删除的id
  83. if(empty($ids)) return $this->apiResponseError('缺少必要参数');
  84. //数据条件
  85. if(!is_array($ids)) return $this->apiResponseError('数据格式错误');
  86. $res = Banner::whereIn('id',$ids)->update(['is_del'=>1]);
  87. if ($res) {
  88. return $this->apiResponseSuccess('删除成功');
  89. } else {
  90. return $this->apiResponseError('删除失败');
  91. }
  92. }
  93. }