返回新闻模块对应模......登陆

新闻模块对应模块的代码

肖凌2019-12-15 20:52:48295

//comm.php 判断是否登录公共基类

<?php

        namespace app\admin\controller;
        use think\Controller;
        use think\facade\Session;
        class Comm extends Controller{
        public function __construct(){
        parent::__construct(); if(!Session::has('username')){ $this->error('您还未登录,请登录!','login/login'); } } }


<?php// 应用公共文件common.php

 function GetTitle($new_id){
return Db::connect('tp5139')->table('news')->where('id',$new_id)->value('title');}
function GetPic($new_pic){
return Db::connect('tp5139')->table('newpic')->where('new_id',$new_pic)->value('pic');}

//新闻控制器类news.php

<?php

        namespace app\admin\controller;
        use app\admin\controller\Comm;
        use think\facade\Request;
        use think\facade\Session;
        use app\admin\model\NewsModel;
        class News extends Comm{

                //新闻列表
                public function index(){
                        $res = NewsModel::order('id','desc')->paginate(5);
                        $this->view->res = $res;
                        return $this->view->fetch();

                         }

                //添加新闻

                public function add(){

                          return $this->view->fetch();

                         }

                //执行添加
                public function DoAdd(){
                            $data = Request::param();
                            $data['username'] = Session::get('username');
                            $data['time'] = time();
                            $news = NewsModel::where('title',$data['title'])->find();
                            if($news==true){ return ['code'=>1,'msg'=>'新闻标题已存在']; }
                            if(NewsModel::create($data)){
                                      return ['code'=>0,'msg'=>'新闻添加成功']; }else{ return ['code'=>2,'msg'=>'新闻添加失败']; }
                        }

                //编辑新闻
                public function edit(){
                            $id = Request::get('id');
                            $res = NewsModel::where('id',$id)->find();
                            $this->view->res = $res;
                            return $this->view->fetch();

                     }

                //执行编辑
                public function DoEdit(){
                            $data = Request::param(); unset($data['select']);
                            $data['username'] = Session::get('username');
                            $data['time'] = time();
                            //dump($data);
                            $res = NewsModel::where('id',$data['id'])->data($data)->update();
                            if($res){
                                        return ['code'=>0,'msg'=>'更新成功']; }else{ return ['code'=>1,'msg'=>'更新失败']; } }

                    //删除新闻
                    public function del(){
                                $id = Request::get('id');
                                $res = NewsModel::destroy($id);
                                if($res){ return ['code'=>0]; }else{ return ['code'=>1]; }
                                }

                    //上传图片
                    public function upload(){
                                $file = Request::file('img');
                                $info = $file->validate(['ext'=>'jpg,jpeg,png,gif'])->move('upload');
                                if($info)

                                           {  

                                            return json(['errno'=>0,'data'=>['/upload/'.$info->getSaveName()]]);                                             }

             }

     }

//新闻列表页 index.html

{include file="public/head"}    

<body>    

<div class="x-nav">      

<span class="layui-breadcrumb">        

<a href="">首页</a>        

<a> <cite>新闻列表</cite></a>

</span>      

<a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">        

<i class="layui-icon" style="line-height:30px">ဂ</i></a>   

</div>    

<div class="x-body">      

        <xblock>

                <button class="layui-btn" onclick="x_admin_show('添加用户','{:url(\'news/add\')}')"><i class="layui-icon"></i>添加</button>

                <span class="x-right" style="line-height:40px">共有数据:{$total} 条</span>      </xblock>

              <table class="layui-table">

                    <thead>

                               <tr>

                                <th>新闻ID</th>

                                <th>新闻标题</th>

                                <th>新闻图片</th>

                                <th>新闻简介</th>

                                <th>发布管理员</th>

                                <th>发布日期</th>

                                <th >操作</th>

                        </tr>

                </thead>

                <tbody>

                {volist name='res' id='val'}

                      <tr>

                            <td>{$val.id}</td>

                            <td>{$val.title}</td>

                            <td><img src="{:GetPic($val.id)}"></td>

                            <td>{$val.desc}</td>

                            <td>{$val.username}</td>

                            <td>{$val.time | date='Y-m-d H:i:s'}</td>

                            <td class="td-manage">

                            <a title="查看"  onclick="x_admin_show('编辑','{:url(\'edit\')}?id={$val.id}')" href="javascript:;">

                <i class="layui-icon">&#xe63c;</i>

                 </a>

                  <a title="删除" onclick="member_del(this,'{$val.id}')" href="javascript:;">

                <i class="layui-icon">&#xe640;</i>

              </a>

            </td>

          </tr>

          {/volist}

        </tbody>

      </table>

      <div class="page"> 

                       <div>

                              {$res | raw} 

                       </div>

          </div>
    </div>

    <script>

                  layui.use('laydate', function(){

                        var laydate = layui.laydate;

                    //执行一个laydate实例

                    laydate.render({

                              elem: '#start' //指定元素        });
                                //执行一个laydate实例

                                laydate.render({ 

                                elem: '#end' //指定元素

                                }); 

                         });
               /*用户-停用*/

               /*用户-删除*/ 

             function member_del(obj,id){

                          layer.confirm('确认要删除吗?',function(index){

                                  //发异步删除数据

                              $.get('{:url(\'del\')}','id='+id,function(data){

                                            if(data['code']==0){ 

                                                       $(obj).parents("tr").remove(); 

                                                           layer.msg('已删除!',{icon:1,time:1000}); 

                                            }else{ 

                                                           layer.msg('删除失败',{icon:0,time:1000}); 

                                                }

                                          })

                                        });

                                      }

                    </script>

             </body>

        </html>

//添加新闻add.html

{include file="public/head"}  

<script type="text/javascript" src="_STATIC_/js/wangEditor.js"></script>

  <body>

    <div class="x-body">        <form class="layui-form">          <div class="layui-form-item">              <label for="username" class="layui-form-label">                  <span class="x-red">*</span>新闻标题              </label>              <div class="layui-input-inline">                  <input type="text" id="title" name="title" required="" lay-verify="required"                  autocomplete="off" class="layui-input">              </div>          </div>          <div class="layui-form-item">              <label for="username" class="layui-form-label">                  <span class="x-red">*</span>新闻分类              </label>              <div class="layui-input-inline">                  <select id="shipping" name="shipping" class="valid">                    <option value="1">热门新闻</option>                    <option value="2">紧急新闻</option>                  </select>              </div>          </div>    <div class="layui-form-item">            <label for="desc" class="layui-form-label">                <span class="x-red">*</span>简介            </label>
            <div class="layui-input-inline">                <textarea placeholder="请输入内容" id="desc" name="desc" class="layui-textarea"></textarea>            </div>        </div>         <div class="layui-form-item layui-form-text">            <label for="desc" class="layui-form-label">                内容            </label>            <div class="layui-input-block" id="editor">            </div>        </div>          <div class="layui-form-item">              <label for="L_repass" class="layui-form-label">              </label>              <button  class="layui-btn" lay-filter="add" lay-submit="">                  发布              </button>          </div>      </form>    </div>    <script>        layui.use(['form','layer'], function(){            $ = layui.jquery;          var form = layui.form          ,layer = layui.layer;           //配置wangEditor富文本编辑器        ////将要用到的对象添加到全局        var E = window.wangEditor
        //生成editor对象        var editor = new E('#editor')
        //设置图片上传的控件名称:类似于input的name属性,供接口获取图片信息使用        editor.customConfig.uploadFileName = 'img'
        //设置服务上的图片上传处理接口脚本        editor.customConfig.uploadImgServer = '{:url(\'upload\')}'
        //创建出富文件编辑器        editor.create()                  //自定义验证规则          form.verify({            nikename: function(value){              if(value.length < 5){                return '昵称至少得5个字符啊';              }            }            ,pass: [/(.+){6,12}$/, '密码必须6到12位']            ,repass: function(value){                if($('#L_pass').val()!=$('#L_repass').val()){                    return '两次密码不一致';                }            }          });
          //监听提交          form.on('submit(add)', function(data){            console.log(data);            $.post(                    '{:url(\'DoAdd\')}',                    {                        'title' : $('#title').val(),                        'desc' : $('#desc').val(),                        'content' : editor.txt.html(),                        'select' : $('#shipping option:selected').val()                    },                    function (data){                        if(data['code']==0){                              layer.alert(data['msg'], {icon: 6},function () {                                  // 获得frame索引                                  var index = parent.layer.getFrameIndex(window.name);                                  //关闭当前frame                                  parent.layer.close(index);                              });                        }
                    }

              )
            return false;          });                            });    </script>  </body>
</html>

//编辑新闻edit.html

{include file="public/head"}  <script type="text/javascript" src="_STATIC_/js/wangEditor.js"></script>  <body>    <div class="x-body">        <form class="layui-form">          <input type="hidden" name="id" id="id" value="{$res.id}">          <div class="layui-form-item">              <label for="username" class="layui-form-label">                  <span class="x-red">*</span>新闻标题              </label>              <div class="layui-input-inline">                  <input type="text" id="title" name="title" required="" lay-verify="required"                  autocomplete="off" class="layui-input" value="{$res.title}">              </div>          </div>          <div class="layui-form-item">              <label for="username" class="layui-form-label">                  <span class="x-red">*</span>新闻分类              </label>              <div class="layui-input-inline">                  <select id="shipping" name="shipping" class="valid">                    <option value="1">热门新闻</option>                    <option value="2">紧急新闻</option>                  </select>              </div>          </div>    <div class="layui-form-item">            <label for="desc" class="layui-form-label">                <span class="x-red">*</span>简介            </label>
            <div class="layui-input-inline">                <textarea placeholder="请输入内容" id="desc" name="desc" class="layui-textarea">{$res.desc}</textarea>            </div>        </div>         <div class="layui-form-item layui-form-text">            <label for="desc" class="layui-form-label">                内容            </label>            <div class="layui-input-block" id="editor">{:htmlspecialchars_decode($res.content)}            </div>        </div>          <div class="layui-form-item">              <label for="L_repass" class="layui-form-label">              </label>              <button  class="layui-btn" lay-filter="add" lay-submit="">                  发布              </button>          </div>      </form>    </div>    <script>        layui.use(['form','layer'], function(){            $ = layui.jquery;          var form = layui.form          ,layer = layui.layer;           //配置wangEditor富文本编辑器        ////将要用到的对象添加到全局        var E = window.wangEditor
        //生成editor对象        var editor = new E('#editor')
        //设置图片上传的控件名称:类似于input的name属性,供接口获取图片信息使用        editor.customConfig.uploadFileName = 'img'
        //设置服务上的图片上传处理接口脚本        editor.customConfig.uploadImgServer = '{:url(\'upload\')}'
        //创建出富文件编辑器        editor.create()                  //自定义验证规则          form.verify({            nikename: function(value){              if(value.length < 5){                return '昵称至少得5个字符啊';              }            }            ,pass: [/(.+){6,12}$/, '密码必须6到12位']            ,repass: function(value){                if($('#L_pass').val()!=$('#L_repass').val()){                    return '两次密码不一致';                }            }          });
          //监听提交          form.on('submit(add)', function(data){            console.log(data);            $.post(                    '{:url(\'DoEdit\')}',                    {                        'id':$('#id').val(),                        'title' : $('#title').val(),                        'desc' : $('#desc').val(),                        'content' : editor.txt.html(),                        'select' : $('#shipping option:selected').val()                    },                    function (data){                        if(data['code']==0){                              layer.alert(data['msg'], {icon: 6},function () {                                  // 获得frame索引                                  var index = parent.layer.getFrameIndex(window.name);                                  //关闭当前frame                                  parent.layer.close(index);                              });                        }else{                              layer.alert(data['msg'], {icon: 6},function () {                                  // 获得frame索引                                  var index = parent.layer.getFrameIndex(window.name);&am

最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送
  • PHP中文网