abstract:<?php namespace app\admin\controller; use app\admin\controller\Commn; use think\facade\Request; use think\facade\Session; use app\admin\model\Newsl; /** * */ class News extends Commn {
<?php
namespace app\admin\controller;
use app\admin\controller\Commn;
use think\facade\Request;
use think\facade\Session;
use app\admin\model\Newsl;
/**
*
*/
class News extends Commn
{
//新闻展示
public function index()
{
$res = Newsl::order('news_id','desc')->paginate(4);
$page = $res->render();
$this->assign('page',$page);
$this->assign('res',$res);
return $this->fetch();
}
//新闻添加页面
public function add()
{
return $this->fetch();
}
//图片上传页面
public function uplode()
{
//获取图片信息
$file = Request::file('img');
if ($info = $file->validate(['ext'=>'jpg,png,jpeg,gif'])->move('upload')){
//返回上次成功信息
return json(['errno'=>0,'data'=>["/upload/" .$info->getSaveName()]]);
}else{
echo $info->getError();
}
}
//新闻添加处理页面
public function add_do()
{
$data = Request::param();
$data['username'] = Session::get('username');
$data['create_time'] = time();
//验证标题的唯一性
if ($res = newsL::where('title',$data['title'])->find()) {
return json(['code'=>0,'msg'=>'文章标题已经存在']);
}
if ($res = newsl::insert($data)) {
return json(['code'=>1,'msg'=>'添加文章成功']);
}else{
return json(['code'=>2,'msg'=>'添加操作异常,请重新操作']);
}
}
//修改新闻方法
public function upl()
{
$data = Request::param();
$news_id = $data['news_id'];
$res = newsl::where('news_id',$data['news_id'])->find();
$this->assign('res',$res);
return $this->fetch();
}
//修改新闻处理方法
public function upl_do()
{
$data = Request::param();
$data['create_time'] = time();
$news_id = $data['news_id'];
$res = newsl::where('news_id',$data['news_id'])->find();
if ($data['title']==$res['title']) {
$info = newsl::where('news_id',$data['news_id'])->update($data);
if ($info == true) {
return json(['code'=>1,'msg'=>'修改新闻成功']);
}else{
return json(['code'=>2,'msg'=>'修改操作异常,请重新操作']);
}
}else{
$ture = newsl::where('title',$data['title'])->all();
if ($ture == true) {
return json(['code'=>3,'msg'=>'文章标题已经存在,不能重复添加']);
}else{
$upldate = newsl::where('news_id',$data['news_id'])->update($data);
if ($update == true) {
return json(['code'=>1,'msg'=>'修改新闻成功']);
}
}
}
}
public function del()
{
$res = Request::param();
$news_id = $res['id'];
if ($res = Newsl::where('news_id',$news_id)->delete()) {
return json(['code'=>1,'msg'=>'修改新闻成功']);
}else{
return json(['code'=>2,'msg'=>'删除新闻成功']);
}
}
}
?>
Correcting teacher:查无此人Correction time:2019-06-17 09:31:10
Teacher's summary:完成的不错。后台cms管理系统,就是对数据进行操作。操作越简单越好。继续加油。