NewsController.php 3.5 KB

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