123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- namespace App\Http\Controllers\AdminApi;
- use App\Http\Controllers\AdminController;
- use App\Models\NewsModels\News;
- use App\Servers\Common\CommonServer;
- class NewsController extends AdminController
- {
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * 获取列表
- * @return \Illuminate\Http\JsonResponse
- */
- function getList(){
- $title = request()->input('title','');//获取标题
- //查询数据条件
- $where = [['is_del',0]];
- //获取数据
- $list = News::where($where)
- ->where(function ($q) use ($title){
- if($title){
- $q->orWhere('cn_title','like',"%$title%");
- $q->orWhere('en_title','like',"%$title%");
- }
- })
- ->select(['id','image','cn_title','en_title','is_top','sort','created_at'])
- ->orderBy('id','desc')
- ->paginate(10);
- return $this->apiResponseSuccess('获取列表成功', [
- 'items' => $list->items(),
- 'total' => $list->total(),
- 'limit' => 10,
- ]);
- }
- /**
- * 获取记录详情
- * @return \Illuminate\Http\JsonResponse
- */
- function getInfo(){
- $id = request()->input('id','');//获取需要查询的记录id
- if(empty($id)) return $this->apiResponseError('缺少必要参数');
- //查询数据
- $info = News::where('id',$id)->first();
- if(empty($info)) return $this->apiResponseError('没有找到该记录');
- return $this->apiResponseSuccess('获取成功', $info);
- }
- /**
- * 编辑&新增
- * @return \Illuminate\Http\JsonResponse
- */
- function saveNews(){
- $id = request()->input('id','');//记录id
- $cn_title = CommonServer::creatServer()->filtrationStr('cn_title');
- $en_title = CommonServer::creatServer()->filtrationStr('en_title');
- $image = request()->input('image', '');//缩略图
- $cn_content = request()->input('cn_content', '');//
- $en_content = request()->input('en_content', '');//
- $sort = request()->input('sort', 50);//排序
- $is_top = request()->input('is_top', 0);//是否置顶
- if(empty($cn_title) || empty($en_title) || empty($image)){
- return $this->apiResponseError('缺少必要参数');
- }
- $data = compact('cn_title','en_title','cn_content','en_content','image','sort','is_top');
- if(empty($id)){//不存在id就创建
- $res = News::create($data);
- }else{//存在就更新
- $info = News::where('id',$id)->select(['id'])->first();
- if (empty($info)) {
- return $this->apiResponseError('没有找到该记录');
- }
- $res = $info->update($data);
- }
- if ($res) {
- return $this->apiResponseSuccess('操作成功');
- } else {
- return $this->apiResponseError('操作失败');
- }
- }
- /**
- * 删除(批量)文章
- * @return \Illuminate\Http\JsonResponse
- */
- function destroys(){
- $ids = request()->input('ids','');//获取需要删除的id
- if(empty($ids)) return $this->apiResponseError('缺少必要参数');
- //数据条件
- if(!is_array($ids)) return $this->apiResponseError('数据格式错误');
- $res = News::whereIn('id',$ids)->update(['is_del'=>1]);
- if ($res) {
- return $this->apiResponseSuccess('删除成功');
- } else {
- return $this->apiResponseError('删除失败');
- }
- }
- }
|