摘要:<?php /** * Created by PhpStorm. * User: Admin * Date: 2019/4/5 * Time: 20:03 */ namespace app\admin\controller; use
<?php
/**
* Created by PhpStorm.
* User: Admin
* Date: 2019/4/5
* Time: 20:03
*/
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()
{
//查询数据
$new_pic = NewsPicModel::all();
$this->view->new=$new_pic;
return $this->fetch();
}
//渲染添加页面
public function add()
{
//查询新闻title数据
$new = NewsModel::all();
$this->view->new=$new;
return $this->fetch();
}
//图片上传
public function upload()
{
//获取图片信息
$pic = Request::file('file');
//验证图片并移到指定目录下
if($info = $pic->validate(['ext'=>'jpg,png,gif'])->move('smallImg')){
//获取图片路劲
return json([1,'上传成功','data'=>'/smallImg/'.$info->getSaveName()]);
}else{
return $pic->getError();
}
}
//添加缩略图数据
public function DoAdd()
{
//获取前台提交过来的数据
$data = Request::param();
$data['username']=Session::get('username');
$data['time']=time();
//判断数据是否添加成功
$news = new NewsPicModel();
if($news->save($data)){
return ['res'=>1,'msg'=>'添加成功!'];
}else{
return ['res'=>0,'msg'=>'添加失败!'];
}
}
//数据删除
public function del()
{
//获取前台提交的id
$id = Request::param('id');
$res = NewsPicModel::destroy($id);
if($res){
return ['res'=>0,'msg'=>'删除成功!'];
}else{
return ['res'=>0,'msg'=>'删除失败!'];
}
}
}
批改老师:西门大官人批改时间:2019-04-08 10:12:35
老师总结:数据的添加,尽量手动赋值,这样可以避免前台传过来非法的字段,造成数据库报错,泄漏数据库信息