企业站的产品管理模块

原创 2018-11-14 10:24:39 180
摘要:控制器代码:<?php namespace app\admin\controller; use app\admin\controller\Common; use think\facade\Request; use app\admin\model\ProductModel; use think\facade\Session; class&nb
  1. 控制器代码:

  2. <?php
    namespace app\admin\controller;
    use app\admin\controller\Common;
    use think\facade\Request;
    use app\admin\model\ProductModel;
    use think\facade\Session;
    
    class Product extends Common
    {
    	public function index(ProductModel $product){
    		$product = $product->order('id','desc')->paginate(8);
        	$page = $product->render();
        	$count = $product->count(1);
        	$this->view->product = $product;
        	$this->view->page = $page;
        	$this->view->count = $count;
    		//渲染产品列表
    		return $this->fetch();
    	}
    	public function add(){
    		//渲染添加界面
    		return $this->fetch();
    	}
        public function DoAdd(ProductModel $product){
            $data=Request::param();
            $data['time']=time();
            $data['username']=Session::get('username');
            $title=$data['title'];
            $res=ProductModel::where('title',$title)->find();
            if ($res) {
            	return ['res'=>0,'msg'=>'产品标题重复!'];
            }
            if($product->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(ProductModel $product){
        	$productId=Request::param('id');
        	$product=ProductModel::where('id',$productId)->find();
        	$this->view->product=$product;
        	//编辑新闻
            return $this->fetch();
        }
        public function DoEdit(ProductModel $product){
         	//获取前台提交的数据
        	$data=Request::param();
            $res=$product->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(ProductModel $product){
        	$productId=Request::param('id');
        	if($product->destroy($productId)){
        		return ['res'=>1,'msg'=>'删除成功'];
        	}else{
            	return ['res'=>0,'msg'=>'删除失败'];
            }
        }
    }
    ?>

    3.视图中index.html代码:

  3. {include file="/public/header"}
      <body>
        <div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="">首页</a>
            <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(\'Product/add\')}')"><i class="layui-icon"></i>添加</button>
            <span class="x-right" style="line-height:40px">共有数据:{$count} 条</span>
          </xblock>
          <table class="layui-table">
            <thead>
              <tr>
                <th>产品ID</th>
                <th>产品标题</th>
                <th>产品图片</th>
                <th>产品简介</th>
                <th>产品分类</th>
                <th>发布管理员</th>
                <th>发布日期</th>
                <th >操作</th>
                </tr>
            </thead>
            <tbody>
            {volist name='product' id='products'}
              <tr>
                <td>{$products.id}</td>
                <td>{$products.title}</td>
                <td><img src="#"></td>
                <td>{$products.desc}</td>
                <td>{$products.sort}</td>
                <td>{$products.username}</td>
                <td>{$products.time|date='Y-m-d H:s:i'}</td>
                <td class="td-manage">
                  <a title="查看"  onclick="x_admin_show('编辑','{:url(\'Product/edit\')}?id={$products.id}')" href="javascript:;">
                    <i class="layui-icon">&#xe63c;</i>
                  </a>
                  <a title="删除" onclick="member_del(this,'{$products.id}')" href="javascript:;">
                    <i class="layui-icon">&#xe640;</i>
                  </a>
                </td>
              </tr>
              {/volist}
            </tbody>
          </table>
    
          <div class="page">      
          {$page|raw}
          </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.res==1) {              
                  $(obj).parents("tr").remove();
                  layer.msg(data.msg,{icon:1,time:1000});}
                  })
                  $(obj).parents("tr").remove();
                  layer.msg(data.msg,{icon:1,time:1000});
              });
          }
        </script>
      </body>
    
    </html>

    4.视图中add.html代码:

  4. {include file="/public/header"}
      <body>
        <div class="x-body">
            <form class="layui-form">
              <div class="layui-form-item">
                  <label for="title" 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="0">垃圾产品</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个字符啊';
                  }
                }
              });
    
              //监听提交
              form.on('submit(add)', function(data){
                console.log(data);
                //发异步,把数据提交给php
                $.post('{:url("Product/DoAdd")}',{
                  'title':$('#title').val(),
                  'sort':$('#shipping').val(),
                  'desc':$('#desc').val(),
                  'content':editor.txt.html(),
                },function(data){
                  if (data.res) {
                      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);
                        //关闭当前frame
                        parent.layer.close(index);
                    });
                  }
                })
    
                return false;
              });
              
              
            });
        </script>
      </body>
    
    </html>

    5.视图中edit.html代码

  5. {include file="/public/header"}
      <body>
        <div class="x-body">
            <form class="layui-form">
              <div class="layui-form-item">
                  <label for="title" 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="{$product.title}">
                      <input type="hidden" name="id" id="id" value="{$product.id}">
                  </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">{$product.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($product.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个字符啊';
                  }
                }
              });
    
              //监听提交
              form.on('submit(add)', function(data){
                console.log(data);
                //发异步,把数据提交给php
                $.post('{:url("Product/DoEdit")}',{
                  'title':$('#title').val(),
                  'desc':$('#desc').val(),
                  'id':$('#id').val(),
                  'content':editor.txt.html(),
                },function(data){
                  if (data.res) {
                      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);
                        //关闭当前frame
                        parent.layer.close(index);
                    });
                  }
                })
                return false;
              });
            });
        </script>
      </body>
    
    </html>

批改老师:查无此人批改时间:2018-11-14 10:51:22
老师总结:完成的不错,可以把执行结果也截图发出来,这样就更完美了。

发布手记

热门词条