1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace App\Http\Controllers\Api;
- use App\Http\Controllers\FrontController;
- use App\Models\NewsModels\News;
- use App\Servers\Common\CommonServer;
- class NewsController extends FrontController
- {
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * 获取新闻列表
- * @return \Illuminate\Http\JsonResponse
- */
- function getList(){
- //查询数据条件
- $where = [['is_del',0]];
- //获取数据
- $list = News::where($where)
- ->select(['id','image','cn_title','en_title','created_at'])
- ->orderBy('is_top','desc')
- ->orderBy('sort','asc')
- ->orderBy('created_at','desc')
- ->paginate(10);
- $cn_data = [];
- $en_data = [];
- foreach ($list as $value){
- $cn['title'] = $value['cn_title'];
- $cn['id'] = $value['id'];
- $cn['image'] = $value['image'];
- $cn['created_at'] = date('Y年m月d日',strtotime($value['created_at']));
- $en['title'] = $value['en_title'];
- $en['id'] = $value['id'];
- $en['image'] = $value['image'];
- $en['created_at'] = date('Y年m月d日',strtotime($value['created_at']));
- $cn_data[] = $cn;
- $en_data[] = $en;
- }
- $cn_datas['items'] = $cn_data;
- $en_datas['items'] = $en_data;
- return $this->apiResponseSuccess('获取列表成功', [
- 'cn' => $cn_datas,
- 'en' => $en_datas,
- 'total' => $list->total(),
- 'limit' => 10,
- ]);
- }
- /**
- * 获取新闻详情
- * @return \Illuminate\Http\JsonResponse
- */
- function getInfo(){
- $id = CommonServer::creatServer()->filtrationStr('id');//获取需要查询的记录id
- if(empty($id)) return $this->apiResponseError('请选择正确新闻');
- //查询数据
- $info = News::where('id',$id)->first();
- if(empty($info)) return $this->apiResponseError('新闻不存在');
- $data= [];
- $data['cn']['title'] = $info['cn_title'];
- $data['cn']['content'] = $info['cn_content'];
- $data['en']['title'] = $info['en_title'];
- $data['en']['content'] = $info['en_content'];
- $data['created_at'] = date('Y.m.d',strtotime($info['created_at']));
- return $this->apiResponseSuccess('获取成功', $data);
- }
- }
|