InfoController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Info;
  4. use Illuminate\Support\Facades\Auth;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Http\Request;
  7. class InfoController extends Controller
  8. {
  9. //获取公告信息列表
  10. public function getInfoList(Request $request){
  11. $input=$request->all();
  12. $page_size=$input['page_size'];
  13. $page_index=$input['page_index'];
  14. $num = ($page_index - 1) * $page_size;
  15. $search_name=$input['search_name'];
  16. $where=[];
  17. $count=Info::where($where)
  18. ->count();
  19. if($count==0){
  20. $this->error('400001','没有数据');
  21. }
  22. $list=Info::where($where)
  23. ->orderBy('is_top','desc')
  24. ->orderBy('top_at','desc')
  25. ->orderBy('created_at', 'desc')
  26. ->skip($num)->take($page_size)
  27. ->get();
  28. if(empty($list)){
  29. return $this->error('400002','没有获取到数据');
  30. }
  31. return $this->success_list($list,'success',$count);
  32. }
  33. //添加公告信息
  34. public function uploadInfo(Request $request){
  35. $input=$request->all();
  36. // $data['title']=$input['title'];
  37. $data['adminid']=Auth::user()->id;
  38. $data['author']=$input['author'];
  39. $data['status']=1;
  40. $data['is_top']=0;// 1表示置顶,0表示不置顶
  41. $data['contents']=$input['contents'];
  42. $row=Info::create($data);
  43. if($row){
  44. Log::info('管理员:'.Auth::user()->name.'(id='.Auth::user()->id.')添加公告成功,公告id:'.$row->id);
  45. return $this->success([]);
  46. }
  47. return $this->error();
  48. }
  49. //截取简介
  50. function make_excerpt($value, $length = 200)
  51. {
  52. $excerpt = trim(preg_replace('/\r\n|\r|\n+/', ' ', strip_tags($value)));
  53. return str_limit($excerpt, $length);
  54. }
  55. //修改公告信息
  56. public function updateInfo(Request $request){
  57. $input=$request->all();
  58. // $data['title']=$input['title'];
  59. $data['adminid']=Auth::user()->id;
  60. $data['author']=$input['author'];
  61. $data['contents']=$input['contents'];
  62. $row=Info::where('id',$input['id'])->update($data);
  63. if($row){
  64. Log::info('管理员:'.Auth::user()->name.'(id='.Auth::user()->id.')修改公告成功,公告id:'.$input['id']);
  65. return $this->success([]);
  66. }
  67. return $this->error();
  68. }
  69. /* 公告置顶 */
  70. public function setInfoTop(Request $request){
  71. $input=$request->all();
  72. $info=Info::find($input['id']);
  73. $info->is_top=($info->is_top=='0')?'1':'0';
  74. $info->top_at=date("Y-m-d H:i:s");
  75. $row=$info->save();
  76. if($row){
  77. Log::info('管理员:'.Auth::user()->name.'(id='.Auth::user()->id.')置顶公告成功,公告id:'.$input['id']);
  78. return $this->success([]);
  79. } else {
  80. return $this->error();
  81. }
  82. }
  83. /* 公告禁用 */
  84. public function disableInfo(Request $request){
  85. $input=$request->all();
  86. $info=Info::find($input['id']);
  87. $info->disabled=($info->disabled==0)?1:0;
  88. $row=$info->save();
  89. if($row){
  90. Log::info('管理员:'.Auth::user()->name.'(id='.Auth::user()->id.')禁用公告成功,公告id:'.$input['id']);
  91. return $this->success([]);
  92. } else {
  93. return $this->error();
  94. }
  95. }
  96. //删除社区信息
  97. public function destoryInfo(Request $request){
  98. $input=$request->all();
  99. $info=Info::find($input['id']);
  100. $row=$info->delete();
  101. if($row){
  102. Log::info('管理员:'.Auth::user()->name.'(id='.Auth::user()->id.')删除公告成功,公告id:'.$input['id']);
  103. return $this->success([]);
  104. }
  105. return $this->error();
  106. }
  107. }