摘要:<?php namespace app\admin\controller; use app\admin\controller\Common; use app\admin\model\NewsModel; use app\admin\model\NewsPicModel; use think\facade\Request; use
<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\NewsModel;
use app\admin\model\NewsPicModel;
use think\facade\Request;
use think\facade\Session;
class NewsPic extends Common
{
public function index(){
//查询操作
$newPic = new NewsPicModel();
$pics = $newPic->order('id','desc')->paginate(6);
$this->view->pics = $pics;
//渲染新闻缩略图列表
return $this->fetch();
}
public function add(){
//在下拉条中导入新闻数据,将数据赋值给模板
$news = NewsModel::all();
$this->view->news=$news;
//渲染新闻缩略图的添加界面
return $this->fetch();
}
//上传缩略图
public function upload(){
//获取上传图片信息
$file = Request::file('file');
//validate完成自动验证,后缀。验证并移动到指定目录upload
if($info = $file->validate(['ext'=>'jpg,jpeg,png,gif'])->move('upload')){
//将图片路径进行拼接
$fileName = '/upload/'.$info->getSaveName();//获取图片名称
//返回上传成功的提示信息
return json([1,'上传成功!','data'=>$fileName]);
}else{
//返回上传失败的错误信息
return $file->getError();
}
}
public function DoAdd()
{
//获取提交的数据
$data = Request::param();
$data['time']=time();
$data['username']=Session::get('username');
//进行存储操作
$newPic = new NewsPicModel();
if($newPic->save($data)){
return ['res'=>1,'msg'=>'发布成功!'];
}else{
return ['res'=>0,'msg'=>'发布失败!'];
}
}
public function del(){
$picId=Request::param('id');
$newPic = new NewsPicModel();
if($newPic->destroy($picId)){
return['res'=>1,'msg'=>'已删除'];
}else{
return['res'=>0,'msg'=>'删除失败'];
}
}
}
批改老师:天蓬老师批改时间:2019-04-12 14:10:12
老师总结:return['res'=>0,'msg'=>'删除失败']; return 与数组之间有空格吗? 看不出来呀