新闻缩略图模块!

Original 2019-02-18 22:01:41 319
abstract:<?php /**  * Created by PhpStorm.  * User: Administrator  * Date: 2019-02-18  * Time: 20:28  */ namespace app\admin\contr
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-02-18
 * Time: 20:28
 */

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(1);

        //模板赋值
        $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');

        //验证图片类型,并且移动到指定目录
        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()
    {
        //获取删除的id
        $picId=Request::param('id');
        $newPic = new NewsPicModel();
        if ($newPic->destroy($picId)){
            return ['res'=>1];
        }

    }

}


//新闻缩略图模块 


Correcting teacher:查无此人Correction time:2019-02-19 09:13:29
Teacher's summary:完成的不错,上传图片时,目录最好用当前日期时间创建目录,一个文件夹过多文件,访问会慢。继续加油

Release Notes

Popular Entries