????:view\product\index.html代碼{include file="/public/head"} <body> <div class="x-nav"> <
view\product\index.html代碼
{include file="/public/head"} <body> <div class="x-nav"> <span class="layui-breadcrumb"> <a href="">首頁</a> <a href="">演示</a> <a> <cite>導(dǎo)航元素</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('添加產(chǎn)品','{:url(\'Product/add\')}')"><i class="layui-icon"></i>添加</button> <span class="x-right" style="line-height:40px">共有數(shù)據(jù):{$productTotal} 條</span> </xblock> <table class="layui-table"> <thead> <tr> <th>產(chǎn)品ID</th> <th>產(chǎn)品標(biāo)題</th> <th>產(chǎn)品分類</th> <th>產(chǎn)品圖片</th> <th>產(chǎn)品簡介</th> <th>發(fā)布管理員</th> <th>發(fā)布日期</th> <th >操作</th> </tr> </thead> <tbody> {volist name="products" id="product"} <tr> <td>{$product.id}</td> <td>{$product.title}</td> <td>{:GetSortTitle($product.sort)}</td> <td><img src="{:GetProductPic($product.id)}" width="100"></td> <td>{$product.desc}</td> <td>{$product.username}</td> <td>{$product.time|date="Y-m-d"}</td> <td class="td-manage"> <a title="查看" onclick="x_admin_show('編輯','{:url(\'Product/edit\')}?id={$product.id}')" href="javascript:;"> <i class="layui-icon"></i> </a> <a title="刪除" onclick="member_del(this,'{$product.id}')" href="javascript:;"> <i class="layui-icon"></i> </a> </td> </tr> {/volist} </tbody> </table> <div class="page"> <div> {$products|raw} </div> </div> </div> <script> layui.use('laydate', function(){ var laydate = layui.laydate; //執(zhí)行一個laydate實例 laydate.render({ elem: '#start' //指定元素 }); //執(zhí)行一個laydate實例 laydate.render({ elem: '#end' //指定元素 }); }); /*用戶-刪除*/ function member_del(obj,id){ layer.confirm('確認要刪除嗎?',function(index){ //發(fā)異步刪除數(shù)據(jù) $.get('{:url(\'del\')}','id='+id,function(data){ if(data.res == 1){ $(obj).parents("tr").remove(); layer.msg(data.msg,{icon:1,time:1000}); } }); }); } </script> </body> </html>
view\product\edit.html代碼
{include file="/public/head"} <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>產(chǎn)品標(biāo)題 </label> <div class="layui-input-inline"> <input type="text" id="title" name="title" value="{$product.title}" required="" lay-verify="title" autocomplete="off" class="layui-input"> <input type="hidden" id="id" name="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="請輸入內(nèi)容" id="desc" name="desc" class="layui-textarea">{$product.desc}</textarea> </div> </div> <div class="layui-form-item layui-form-text"> <label for="editor" class="layui-form-label"> 內(nèi)容 </label> <div class="layui-input-block" id="editor"> {:htmlspecialchars_decode($product.content)} </div> </div> <div class="layui-form-item"> <label for="once" class="layui-form-label"> <span class="x-red">*</span>一次價格 </label> <div class="layui-input-inline"> <input type="text" id="once" name="once" value="{$product.once}" required="" lay-verify="once" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label for="over_night" class="layui-form-label"> <span class="x-red">*</span>一夜價格 </label> <div class="layui-input-inline"> <input type="text" id="over_night" name="over_night" value="{$product.over_night}" required="" lay-verify="over_night" autocomplete="off" class="layui-input"> </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 type="text/javascript" src="__ADMIN__/js/wangEditor.js"></script> <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') //設(shè)置圖片上傳的控件名稱:類似于input的name屬性,供接口獲取圖片信息使用 editor.customConfig.uploadFileName = 'img' //設(shè)置服務(wù)上的圖片上傳處理接口腳本 editor.customConfig.uploadImgServer = '{:url(\'upload\')}' //創(chuàng)建出富文件編輯器 editor.create() //自定義驗證規(guī)則 form.verify({ title: function(value){ if(value.length < 2){ return '產(chǎn)品標(biāo)題至少得2個字符啊'; } } }); //監(jiān)聽提交 form.on('submit(add)', function(data){ console.log(data); //發(fā)異步,把數(shù)據(jù)提交給php $.post('{:url(\'DoEdit\')}',{ 'title':$('#title').val(), 'id':$('#id').val(), 'desc':$('#desc').val(), 'content':editor.txt.html(), 'once':$('#once').val(), 'over_night':$('#over_night').val() },function(data){ if(data.res == 1){ layer.alert(data.msg, {icon: 6},function () { // 獲得frame索引 var index = parent.layer.getFrameIndex(window.name); //關(guān)閉當(dāng)前frame parent.layer.close(index); }); }else{ layer.alert(data.msg, {icon: 6},function () { // 獲得frame索引 var index = parent.layer.getFrameIndex(window.name); //關(guān)閉當(dāng)前frame parent.layer.close(index); }); } }); return false; }); }); </script> </body> </html>
controller\Product.php代碼
<?php namespace app\admin\controller; use app\admin\controller\Common; use app\admin\model\ProductModel; use app\admin\model\SortModel; use think\facade\Request; use think\facade\Session; class Product extends Common { public function index(){ //取得產(chǎn)品總數(shù) $productTotal = ProductModel::count(); //實例化模型 $product = new ProductModel(); //查詢數(shù)據(jù)并按id降序排列,每一頁為8條數(shù)據(jù) $products = $product->order('id','desc')->paginate(8); //將數(shù)據(jù)賦值給模板 $this->view->productTotal=$productTotal; $this->view->products=$products; //渲染產(chǎn)品列表 return $this->fetch(); } public function add(){ //實例化并查詢分類 $sorts = SortModel::order('id','desc')->all(); //數(shù)據(jù)賦值到模板 $this->view->sorts=$sorts; //渲染產(chǎn)品添加界面 return $this->fetch(); } public function upload(){ //獲取上傳圖片信息 $file = Request::file('img'); //驗證圖片信息并移動到指定目錄 if($info = $file->validate(['ext'=>'jpeg,jpg,png,gif'])->move('upload')){ //返回上傳成功信息 return json(['errno'=>0,'data'=>['/upload/'.$info->getSaveName()]]); }else{ //返回錯誤信息 return $file->getError(); } } public function DoAdd(){ //獲取前臺提交過來的數(shù)據(jù) $data = Request::param(); $title = $data['title']; //將產(chǎn)品標(biāo)題作為查詢條件來查詢數(shù)據(jù) $info = ProductModel::where('title',$title)->find(); //驗證產(chǎn)品標(biāo)題是否重復(fù) if($info == true){ //重復(fù)返回提示信息 return ['res'=>0,'msg'=>'產(chǎn)品標(biāo)題重復(fù)!']; } //加入發(fā)布的時間 $data['time'] = time(); //加入發(fā)布產(chǎn)品的管理員名稱 $data['username'] = Session::get('username'); //實例化模型 $product = new ProductModel(); //進行添加并驗證 if($product->save($data)){ //返回發(fā)布成功的信息 return ['res'=>1,'msg'=>'產(chǎn)品發(fā)布成功!']; }else{ //返回發(fā)布失敗的信息 return ['res'=>0,'msg'=>'產(chǎn)品發(fā)布失??!']; } } public function edit(){ //接受傳遞過來的產(chǎn)品id $proId = Request::param('id'); //查詢產(chǎn)品id對應(yīng)的產(chǎn)品信息 $product = ProductModel::get($proId); //將數(shù)據(jù)賦值到模板 $this->view->product = $product; //渲染產(chǎn)品修改界面 return $this->fetch(); } public function DoEdit(){ //獲取提交過來的數(shù)據(jù) $data= Request::param(); //加入發(fā)布的時間 $data['time'] = time(); //加入發(fā)布產(chǎn)品的管理員名稱 $data['username'] = Session::get('username'); //進行修改操作 $product = new ProductModel(); //驗證修改結(jié)果 $info = $product->save([ 'title'=>$data['title'], 'desc'=>$data['desc'], 'content'=>$data['content'], 'once'=>$data['once'], 'over_night'=>$data['over_night'], 'username'=>$data['username'], 'time'=>$data['time'] ],['id'=>$data['id']]); if($info){ //返回修改成功信息 return ['res'=>1,'msg'=>'產(chǎn)品更新成功!']; }else{ //返回修改失敗信息 return ['res'=>0,'msg'=>'產(chǎn)品更新失?。?#39;]; } } public function del() { //獲取需要刪除的產(chǎn)品id $proId = Request::param('id'); //實例化模型 $product = new ProductModel(); //刪除并驗證 if($product->destroy($proId)){ //返回刪除成功提示 return ['res'=>1,'msg'=>'產(chǎn)品刪除成功!']; }else{ //返回刪除失敗信息 return ['res'=>0,'msg'=>'產(chǎn)品刪除失敗!']; } } } ?>
?? ???:天蓬老師?? ??:2018-12-23 10:24:52
???? ??:作業(yè)寫的很好,注釋很清晰明確,繼續(xù)努力,加油