新闻模块的代码~!

Original 2019-02-18 20:26:10 400
abstract://新闻模块 News.php<?php /**  * Created by PhpStorm.  * User: Administrator  * Date: 2019-02-18  * Time: 14:31  */ namespace&

//新闻模块 

News.php

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-02-18
 * Time: 14:31
 */

namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\NewsModel;
use think\facade\Request;
use think\facade\Session;
class News extends Common
{
    public function index()
    {
        $news= new NewsModel();
        $new=$news->order('id','desc')->paginate(1);

        $this->view->new=$new;

        //渲染新闻列表
        return $this->fetch();
    }
    public function add()
    {
        //渲染添加界面
        return $this->fetch();
    }
    public function DoAdd()
    {
        //获得数据
        $data=Request::param();
        $data['time']=time();

        $data['username']=Session::get('username');
        //验证标题是否重复
        $title=$data['title'];
        $news = NewsModel::where('title',"$title")->find();
        //判断是否重复
        if($news==true){
            return ['res'=>0,'msg'=>'新闻标题重复!'];
        }

        $new = new NewsModel();
        if($new->save($data)){
            return ['res'=>1,'msg'=>'发布成功!'];
        } else {
            return ['res'=>0,'msg'=>'发布失败!'];
        }
    }

    public function upload()
    {
        // 获取上传的图片信息
        $file = Request::file('img');
        // 验证图片信息并移动到指定目录
        if ($info = $file->validate(['ext' => 'jpg,jpeg,png,gif'])->move('upload')) {
            // 返回上传成功信息
            return json(['errno' => 0, 'data' => ['/upload/' . $info->getSaveName()]]);
        } else {
            // 返回错误信息
            return $file->getError();
        }
    }

    public  function edit()
    {
        //接受传递过来的id
        $newId = Request::param('id');

        $new= NewsModel::get($newId);

        //将数据赋值到模板
        $this->view->new=$new;


        //渲染界面
        return $this->fetch();
    }

    public function DoEdit()
    {
        //获取提交的数据
        $data = Request::param();
        //实例化模型
        $new = new NewsModel();

        //修改操作
        $res = $new->save([
            'title'=>$data['title'],
            'desc' =>$data['desc'],
            'content'=>$data['content'],
            'username'=>Session::get('username'),
            'time'=>time(),
        ],['id'=>$data['id']]);

        if($res){
            //返回修改成功信息
            return ['res'=>1,'msg'=>'修改成功!'];
        }else{
            return ['res'=>0,'msg'=>'修改失败!'];
        }




    }

    public function del()
    {
        //获取删除的id
        $newId = Request::param('id');

        //实例化new模型
        $new = new NewsModel();

        //删除并验证
        if($new->destroy($newId)){
            //返回信息
            return ['res'=>1,'msg'=>'删除成功!'];
        }else{
            //返回信息
            return ['res'=>0,'msg'=>'删除失败!'];
        }
    }
}


Correcting teacher:韦小宝Correction time:2019-02-19 09:08:34
Teacher's summary:像使用框架来完成这种很简单的项目 基本上的增删改查都很相似 只有一些业务逻辑上的不同而已 写的很不错 继续加油吧!

Release Notes

Popular Entries