NewsController.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\FrontController;
  4. use App\Models\NewsModels\News;
  5. use App\Servers\Common\CommonServer;
  6. class NewsController extends FrontController
  7. {
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. }
  12. /**
  13. * 获取新闻列表
  14. * @return \Illuminate\Http\JsonResponse
  15. */
  16. function getList(){
  17. //查询数据条件
  18. $where = [['is_del',0]];
  19. //获取数据
  20. $list = News::where($where)
  21. ->select(['id','image','cn_title','en_title','created_at'])
  22. ->orderBy('is_top','desc')
  23. ->orderBy('sort','asc')
  24. ->orderBy('created_at','desc')
  25. ->paginate(10);
  26. $cn_data = [];
  27. $en_data = [];
  28. foreach ($list as $value){
  29. $cn['title'] = $value['cn_title'];
  30. $cn['id'] = $value['id'];
  31. $cn['image'] = $value['image'];
  32. $cn['created_at'] = date('Y年m月d日',strtotime($value['created_at']));
  33. $en['title'] = $value['en_title'];
  34. $en['id'] = $value['id'];
  35. $en['image'] = $value['image'];
  36. $en['created_at'] = date('Y年m月d日',strtotime($value['created_at']));
  37. $cn_data[] = $cn;
  38. $en_data[] = $en;
  39. }
  40. $cn_datas['items'] = $cn_data;
  41. $en_datas['items'] = $en_data;
  42. return $this->apiResponseSuccess('获取列表成功', [
  43. 'cn' => $cn_datas,
  44. 'en' => $en_datas,
  45. 'total' => $list->total(),
  46. 'limit' => 10,
  47. ]);
  48. }
  49. /**
  50. * 获取新闻详情
  51. * @return \Illuminate\Http\JsonResponse
  52. */
  53. function getInfo(){
  54. $id = CommonServer::creatServer()->filtrationStr('id');//获取需要查询的记录id
  55. if(empty($id)) return $this->apiResponseError('请选择正确新闻');
  56. //查询数据
  57. $info = News::where('id',$id)->first();
  58. if(empty($info)) return $this->apiResponseError('新闻不存在');
  59. $data= [];
  60. $data['cn']['title'] = $info['cn_title'];
  61. $data['cn']['content'] = $info['cn_content'];
  62. $data['en']['title'] = $info['en_title'];
  63. $data['en']['content'] = $info['en_content'];
  64. $data['created_at'] = date('Y.m.d',strtotime($info['created_at']));
  65. return $this->apiResponseSuccess('获取成功', $data);
  66. }
  67. }