Commit ac5c4c71 authored by 蔡闯's avatar 蔡闯

新闻接口修改

parent 1e9f35b6
<?php <?php
namespace app\shequ\controller; namespace app\shequ\controller;
//社区新闻模块 //社区新闻模块
use app\common\controller\Common; use app\common\controller\Common;
use think\facade\Db; use think\facade\Db;
use think\facade\Request; use think\facade\Request;
class News extends Base class News extends Base
{ {
protected $middleware = ['app\middleware\CommunityLoginCheck::class','app\middleware\CommunityAfter::class']; protected $middleware = ['app\middleware\CommunityLoginCheck::class','app\middleware\CommunityAfter::class'];
//添加 | 修改新闻栏目 //添加 | 修改新闻栏目
public function createNewsType() { public function createNewsType() {
$news_type_id = Request::param('news_type_id'); $news_type_id = Request::param('news_type_id');
$data['name'] = Request::param('name'); $data['name'] = Request::param('name');
$data['sort_id'] = Request::param('sort_id'); $data['sort_id'] = Request::param('sort_id');
$data['img'] = Request::param('img'); $data['img'] = Request::param('img');
if($news_type_id) { if($news_type_id) {
$where['news_type_id'] = $news_type_id; $where['news_type_id'] = $news_type_id;
$where['village_id'] = $this->village_id; $where['village_id'] = $this->village_id;
$operation = Db::name('news_type')->where($where)->save($data); $operation = Db::name('news_type')->where($where)->save($data);
} else{ } else{
$data['village_id'] = $this->village_id; $data['village_id'] = $this->village_id;
$operation = Db::name('news_type')->save($data); $operation = Db::name('news_type')->save($data);
} }
if($operation) { if($operation) {
return $this->returnJson([],'success'); return $this->returnJson([],'success');
} else { } else {
return $this->returnJson([],'操作失败!',400); return $this->returnJson([],'操作失败!',400);
} }
} }
//删除新闻栏目 //删除新闻栏目
public function deleteNewsType() { public function deleteNewsType() {
$news_type_id = Request::param('news_type_id'); $news_type_id = Request::param('news_type_id');
$where['news_type_id'] = $news_type_id; $where['news_type_id'] = $news_type_id;
$where['village_id'] = $this->village_id; $where['village_id'] = $this->village_id;
$del = Db::name('news_type')->where($where)->delete(); $del = Db::name('news_type')->where($where)->delete();
//删除相关的所有新闻 //删除相关的所有新闻
$del1 = Db::name('news')->where($where)->delete(); $del1 = Db::name('news')->where($where)->delete();
if($del || $del1) { if($del || $del1) {
return $this->returnJson([],'success'); return $this->returnJson([],'success');
} else { } else {
return $this->returnJson([],'删除失败',200); return $this->returnJson([],'删除失败',200);
} }
} }
//新闻栏目列表 //新闻栏目列表
public function NewsTypeList() { public function NewsTypeList() {
$where['village_id'] = $this->village_id; $where['village_id'] = $this->village_id;
$page = Request::param('page',1); $page = Request::param('page',1);
$data = Db::name('news_type')->where($where)->page($page,config('app.limit'))->order('sort_id','desc')->select()->toArray(); $data = Db::name('news_type')->where($where)->page($page,config('app.limit'))->order('sort_id','desc')->select()->toArray();
$total = Db::name('news_type')->where($where)->count(); $total = Db::name('news_type')->where($where)->count();
$res['total'] = $total; $res['total'] = $total;
$res['data'] = $data; $res['data'] = $data;
return $this->returnJson($res,'success'); return $this->returnJson($res,'success');
} }
//添加|修改新闻 //添加|修改新闻
public function createNews() { public function createNews() {
$news_id = Request::param('news_id'); $news_id = Request::param('news_id');
$data['title'] = Request::param('title'); $data['title'] = Request::param('title');
$data['content'] = Request::param('content'); $data['content'] = Request::param('content');
$data['author'] = Request::param('author'); $data['author'] = Request::param('author');
$data['news_type_id'] = Request::param('news_type_id'); $data['news_type_id'] = Request::param('news_type_id');
$data['create_time'] = time(); $data['create_time'] = time();
$data['pic'] = Request::param('pic'); $data['pic'] = Request::param('pic');
$data['sort_id'] = Request::param('sort_id',1); $data['sort_id'] = Request::param('sort_id',1);
$data['village_id'] = $this->village_id; $data['is_recommend'] = Request::param('is_recommend',0);//是否放首页推荐
if($news_id) { $data['recommend_sort_id'] = Request::param('recommend_sort_id',1);//首页推荐排序id
$where['news_id'] = Request::param('news_id'); $data['village_id'] = $this->village_id;
$add = Db::name('news')->where($where)->save($data); if($news_id) {
} else { $where['news_id'] = Request::param('news_id');
$add = Db::name('news')->save($data); $add = Db::name('news')->where($where)->save($data);
} else {
} $add = Db::name('news')->save($data);
if($add) {
return $this->returnJson([],'success'); }
} else { if($add) {
return $this->returnJson([],'error',400); return $this->returnJson([],'success');
} } else {
return $this->returnJson([],'error',400);
}
}
//删除新闻 }
public function deleteNews() {
$where['news_id'] = Request::param('news_id'); //删除新闻
$del = Db::name('news')->where($where)->delete(); public function deleteNews() {
if($del) { $where['news_id'] = Request::param('news_id');
return $this->returnJson([],'success'); $del = Db::name('news')->where($where)->delete();
} else { if($del) {
return $this->returnJson([],'删除失败!',400); return $this->returnJson([],'success');
} } else {
} return $this->returnJson([],'删除失败!',400);
}
//新闻列表 }
public function newsList() {
$page = Request::param('page',1); //新闻列表
$where[]= ['n.village_id','=',$this->village_id]; public function newsList() {
$news_type_id = Request::param('news_type_id'); $page = Request::param('page',1);
$title = Request::param('title'); $where[]= ['n.village_id','=',$this->village_id];
if($news_type_id) { $news_type_id = Request::param('news_type_id');
$where[] = ['n.news_type_id','=',$news_type_id]; $title = Request::param('title');
} if($news_type_id) {
if($title) { $where[] = ['n.news_type_id','=',$news_type_id];
$where[] = ['n.title','like','%'.$title.'%']; }
} if($title) {
$total = Db::name('news')->alias('n')->leftJoin('news_type nt','n.news_type_id = nt.news_type_id')->where($where)->count(); $where[] = ['n.title','like','%'.$title.'%'];
$data = Db::name('news')->alias('n')->leftJoin('news_type nt','n.news_type_id = nt.news_type_id')->order(['n.sort_id'=>'desc','n.create_time'=>'desc']) }
->field('n.news_id,n.news_type_id,n.title,n.author,n.create_time,n.sort_id,nt.name') $total = Db::name('news')->alias('n')->leftJoin('news_type nt','n.news_type_id = nt.news_type_id')->where($where)->count();
->page($page,config('app.limit'))->where($where)->select()->toArray(); $data = Db::name('news')->alias('n')->leftJoin('news_type nt','n.news_type_id = nt.news_type_id')->order(['n.sort_id'=>'desc','n.create_time'=>'desc'])
$res['total'] = $total; ->field('n.news_id,n.news_type_id,n.title,n.author,n.create_time,n.sort_id,nt.name')
$res['data'] = Common::changeField($data); ->page($page,config('app.limit'))->where($where)->select()->toArray();
return $this->returnJson($res,'success'); $res['total'] = $total;
} $res['data'] = Common::changeField($data);
return $this->returnJson($res,'success');
//查看新闻详情 }
public function detailNews() {
$news_id = Request::param('news_id'); //查看新闻详情
$where[] = ['n.news_id','=',$news_id]; public function detailNews() {
$data = Db::name('news')->alias('n')->leftJoin('news_type nt','n.news_type_id = nt.news_type_id')->where($where)->find(); $news_id = Request::param('news_id');
return $this->returnJson($data,'success'); $where[] = ['n.news_id','=',$news_id];
} $data = Db::name('news')->alias('n')->leftJoin('news_type nt','n.news_type_id = nt.news_type_id')->where($where)->find();
return $this->returnJson($data,'success');
}
//base图片上传
public function uploadImgBase64() {
$dir = "/upload/village/".$this->village_id."/"; //base图片上传
$base_img = Request::param('base_img'); public function uploadImgBase64() {
$res = uploadImgs($dir,$base_img); $dir = "/upload/village/".$this->village_id."/";
if($res['code']==200){ $base_img = Request::param('base_img');
$data['data'] = $res['msg']; $res = uploadImgs($dir,$base_img);
$data['path'] = $res['path']; if($res['code']==200){
return $this->returnJson($data); $data['data'] = $res['msg'];
} else { $data['path'] = $res['path'];
return $this->returnJson([],$res['msg'],400); return $this->returnJson($data);
} } else {
return $this->returnJson([],$res['msg'],400);
} }
//fiel图片上传 }
public function uploadImg() {
$file = request()->file('img'); //fiel图片上传
$ext = $file->getOriginalExtension(); public function uploadImg() {
if(!in_array($ext,['pjpeg','jpeg','jpg','gif','bmp','png'])) { $file = request()->file('img');
return $this->returnJson([],'请上传图片文件',400); $ext = $file->getOriginalExtension();
} if(!in_array($ext,['pjpeg','jpeg','jpg','gif','bmp','png'])) {
$savename = \think\facade\Filesystem::putFile('', $file,'datea'); return $this->returnJson([],'请上传图片文件',400);
if(isHTTPS()){ }
$http = "https://"; $savename = \think\facade\Filesystem::putFile('', $file,'datea');
} else { if(isHTTPS()){
$http = "http://"; $http = "https://";
} } else {
$data['path'] = $http.$_SERVER['SERVER_NAME']."/upload/".$savename; $http = "http://";
$data['data'] = "/upload/".$savename; }
return $this->returnJson($data,'success'); $data['path'] = $http.$_SERVER['SERVER_NAME']."/upload/".$savename;
$data['data'] = "/upload/".$savename;
} return $this->returnJson($data,'success');
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment