亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

產(chǎn)品縮略圖界面

Original 2019-05-25 17:53:54 347
abstract:1.控制器類<?phpnamespace app\admin\controller;use app\admin\controller\Common;use app\admin\model\ProductModel;use app\admin\model\ProductPicModel;use think\facade\Request;use think\facade\Session;clas

1.控制器類

<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\ProductModel;
use app\admin\model\ProductPicModel;
use think\facade\Request;
use think\facade\Session;

class ProductPic extends Common
{
   public function index()
   {
//        實(shí)例化模型
       $proPic = new ProductPicModel();
       $productPic = $proPic->order('id', 'desc')->paginate(4);
       $this->view->productPic = $productPic;
//        渲染產(chǎn)品縮略圖列表
       return $this->fetch();
   }

   public function add()
   {
//        查詢所有產(chǎn)品數(shù)據(jù)
       $product = ProductModel::all();
//        將數(shù)據(jù)賦值給模板
       $this->view->product = $product;
//        渲染產(chǎn)品縮略圖添加界面
       return $this->fetch();
   }

   public function upload()
   {
//獲取上傳圖片的信息
       $file = Request::file('file');
//    驗(yàn)證圖片信息并移動到指定目錄
       if ($info = $file->validate(['ext' => 'jpg,jpeg,png,gif'])->move('upload')) {
//            拼接圖像路徑
           $fileName = '/upload/' . $info->getSaveName();
//            返回成功信息
           return json([1, '上傳成功', 'data' => $fileName]);
       } else {
//            返回錯(cuò)誤信息
           return $file->getError();
       }
   }

   public function DoAdd()
   {
//        獲取提交數(shù)據(jù)
       $data = Request::param();
//        添加時(shí)間
       $data['time'] = time();
//        添加管理員
       $data['username'] = Session::get('username');
//         實(shí)例化模型
       $proPic = new ProductPicModel();
//        存儲并驗(yàn)證
       if ($proPic->save($data)) {
//            返回對應(yīng)的數(shù)據(jù)
           return ['res' => 1, 'msg' => '發(fā)布成功'];
       } else {
           return ['res' => 0, 'msg' => '發(fā)布失敗'];
       }
   }

   public function del()
   {
//        獲取要刪除產(chǎn)品的id
       $productId = Request::param('id');
//        實(shí)例化模型
       $productPic = new ProductPicModel();
//        刪除并驗(yàn)證
       if ($productPic->destroy($productId)) {
           //            返回對應(yīng)的數(shù)據(jù)
           return ['res' => 1, 'msg' => '刪除成功!'];
       } else {
           return ['res' => 0, 'msg' => '刪除失??!'];
       }
   }
}

2.模型類

<?php
namespace app\admin\model;
use \think\Model;

class ProductPicModel extends Model
{
  protected $table ='product_pic';
  protected $pk='id';
}

3.視圖類

(1)添加界面add.html

<!doctype html>
<html lang="en">
{include file="/public/header"}
<body>
<div class="x-body">
   <form class="layui-form">
       <div class="layui-form-item">
           <label for="product_id" class="layui-form-label">
               <span class="x-red">*</span>選擇產(chǎn)品
           </label>
           <div class="layui-input-inline">
               <select name="product_id" id="product_id">
                   {volist name="product" id="pro"}
                   <option value="{$pro.id}">{$pro.title}</option>
                   {/volist}
               </select>
           </div>
       </div>
       <div class="layui-form-item">
           <label class="layui-form-label">新聞圖片</label>
           <div class="layui-input-block">
               <button type="button" class="layui-btn" id="upload" >圖片上傳</button>
               <div class="layui-upload-list" id="thumbnail"></div>
           </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 src="/static/admin/lib/layui/layui.js"></script>
<script>
   layui.use(['form', 'layer','upload'], function () {
       $ = layui.jquery;
       var form = layui.form
           , layer = layui.layer;

       var $ = layui.jquery,
           upload = layui.upload;

       upload.render({
           elem: '#upload',
           url: "{:url('upload')}",
           multiple: true,
           before: function(obj){
               //預(yù)讀本地文件示例,不支持ie8
               obj.preview(function(index, file, result){
                   $('#thumbnail').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
               });
           },
           done: function(res){
               //上傳完畢
               $('#thumbnail').append('<input type="hidden" name="pic" id="pic" value="'+res.data +'" />')
           }
       });

       //監(jiān)聽提交
       form.on('submit(add)', function(data){
           console.log(data);
           //發(fā)異步,把數(shù)據(jù)提交給php
           $.post('{:url(\'DoAdd\')}',{
               'product_id':$('#product_id').val(),
               'pic':$('#pic').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>

(2)詳情界面index.html

<!doctype html>
<html lang="en">
{include file="/public/header"}
<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(\'ProductPic/add\')}')"><i class="layui-icon"></i>添加</button>
       <span class="x-right" style="line-height:40px">共有數(shù)據(jù):88 條</span>
   </xblock>
   <table class="layui-table">
       <thead>
       <tr>
           <th>圖片ID</th>
           <th>所屬產(chǎn)品</th>
           <th>圖片</th>
           <th>發(fā)布管理員</th>
           <th>發(fā)布日期</th>
           <th>操作</th>
       </tr>
       </thead>
       <tbody>
        {volist name="productPic" id="product"}
       <tr>
           <td>{$product.id}</td>
           <td>{:GetProTitle($product.product_id)}</td>
           <td><img src="{$product.pic}"></td>
           <td>{$product.username}</td>
           <td>{$product.time|date="Y-m-d"}</td>
           <td class="td-manage">
               <a title="刪除" onclick="member_del(this,'{$product.id}')" href="javascript:;">
                   <i class="layui-icon">&#xe640;</i>
               </a>
           </td>
       </tr>
       {/volist}
       </tbody>
   </table>
   <div class="page">
       <div>

       </div>
   </div>

</div>
<script>
   layui.use('laydate', function(){
       var laydate = layui.laydate;

       //執(zhí)行一個(gè)laydate實(shí)例
       laydate.render({
           elem: '#start' //指定元素
       });
       //執(zhí)行一個(gè)laydate實(shí)例
       laydate.render({
           elem: '#end' //指定元素
       });
   });
   /*用戶-刪除*/
   function member_del(obj,id){
       layer.confirm('確認(rèn)要刪除嗎?',function(index){
           //發(fā)異步刪除數(shù)據(jù)
           $.get('{:url(\'del\')}','id='+id,function (data) {
               if (data.res==1){
                   $(obj).parents("tr").remove();
                   layer.msg('已刪除!',{icon:1,time:1000});
               }
           })
       });
   }

</script>
</body>

</html>

Correcting teacher:天蓬老師Correction time:2019-05-27 09:10:57
Teacher's summary:你的代碼看上去應(yīng)該可以運(yùn)行沒問題, 但不宜細(xì)看, 里面有很多不是很合乎規(guī)范的東西, 例如, 在類中實(shí)例化其它它,就不是一個(gè)好習(xí)慣, 應(yīng)該通過依賴注入來解決

Release Notes

Popular Entries