Browse Source

上传文件至 'application/admin/controller'

增加文章管理功能
Me 5 năm trước cách đây
mục cha
commit
f12bebfef8
1 tập tin đã thay đổi với 208 bổ sung0 xóa
  1. 208 0
      application/admin/controller/Article.php

+ 208 - 0
application/admin/controller/Article.php

@@ -0,0 +1,208 @@
+<?php
+namespace app\admin\controller;
+use think\Controller;
+use think\Request;
+use app\admin\model\ArticleModel;
+class Article extends Base
+{
+     
+
+    public function lst()//文章列表(自己做的)
+    {   
+
+        return view(); 
+       
+         
+    }
+
+
+
+    public function lst_data(Request $request){//文章列表json数据动态表格显示(分页)
+        $user = new ArticleModel();
+        $title = request()->param('title');//接收请求数据id
+        if(!empty($title)){//不为空执行搜索条件,为空则跳过判断条件直接显示列表页,
+            $where = ['title'=>$title];
+            //数据表获取总记录数           
+            $count = $user->where($where)->count();
+            //获取每页显示的条数
+            $limit= $request->param('limit');
+            //获取当前页码
+            $page= $request->param('page');
+            //limit的起始位置
+            $start=($page-1)*$limit; 
+            $list = $user
+                    ->limit("$start,$limit")
+                    ->where($where)
+                    ->select();
+            //返回数据
+            foreach ($list as $k => $v) {
+                $list[$k]['time'] = date('Y-m-d H:i:s',$v['time']);
+            }
+            return ["code"=>0,"msg"=>"成功","count"=>$count,"data"=>$list];
+        }
+        //数据表获取总记录数           
+        $count = $user->count();
+        //获取每页显示的条数
+        $limit= $request->param('limit');
+        //获取当前页码
+        $page= $request->param('page');
+        //limit的起始位置
+        $start=($page-1)*$limit;
+        $list = $user
+                ->limit("$start,$limit")
+                ->select();
+        //返回数据
+        foreach ($list as $k => $v) {
+            $list[$k]['time'] = date('Y-m-d H:i:s',$v['time']);
+        }
+        return ["code"=>0,"msg"=>"成功","count"=>$count,"data"=>$list];
+
+    }
+
+     public function add(){//添加
+        $user = new ArticleModel();
+        // $catename = db('cates')->select();
+        if(request()->isPost()){
+            $data = input('post.');
+            if($_FILES['pic']['tmp_name']){//上传图片
+                $data['pic']=$this->upload();//调用upload方法
+            }else{
+                 $data['pic']='';
+            }
+            $data['time']=time();
+
+            $add=$user->insert($data);
+            if($add){
+                // $logger->info("添加成功");
+                 echo  "<script>alert('添加信息成功!');window.parent.location.reload()</script>";//后台直接实现弹窗,操作完成并显示列表页
+            }else{
+                 echo  "<script>alert('添加信息失败');location.href='".url('article/add')."'</script>";
+            }
+        }
+        // $this->assign([
+        //     'catename'=>$catename,
+        // ]);
+        return view();
+        
+    }
+
+
+    public function edit(){//编辑 
+        $user = new ArticleModel();
+        $id = input('id');
+        // $catename = db('cates')->select();
+        $db = $user->where('id',$id)->find();
+        if(request()->isPost()){
+            $data = input('post.');
+            //处理图片上传
+            if($_FILES['pic']['tmp_name']){
+                $oldarticles=db('article')->field('pic')->find($data['id']);
+                $oldarticleImg=ADMINIMG.$oldarticles['pic'];
+                if(file_exists($oldarticleImg)){   
+                    @unlink($oldarticleImg);
+                }
+                $data['pic']=$this->upload();
+            }
+            $data['time'] = time();
+            $save=$user->update($data);
+            if($save!==false){
+                echo "<script>alert('修改信息成功!');window.parent.location.reload()</script>";//后台直接实现弹窗,操作完成并显示列表页
+            }else{
+                echo  "<script>alert('修改信息失败');location.href='".url('article/edit')."'</script>";
+            }
+        }
+        $this->assign([
+            'db'=>$db,
+            // 'catename'=>$catename,
+        ]);
+        return view();
+    } 
+ 
+
+    public function del(){//删除
+        $user = new ArticleModel();
+        if(request()->isPost()){
+            $id = input('id');
+            //先删除本地文件夹的图片
+            $pic=$user->field('pic')->where('id',$id)->find();
+            if(!empty($pic)){//判断不为空
+                $picSrc=ADMINIMG.$pic['pic'];
+                if(file_exists($picSrc)){
+                    @unlink($picSrc);
+                }
+            }
+            
+            //在删除数据库信息
+            $del=$user->where('id',$id)->delete();
+            if($del){
+                return 1;//删除成功
+                  
+            }else{
+                return 2;//删除失败
+                
+            }
+        }else{
+            return view();
+        }
+
+
+    } 
+
+    public function pdel(){//批量删除
+        $user = new ArticleModel();
+        if(request()->isPost()){
+
+            $id = input('post.');//接收前台id
+            $ids = implode(',',$id);//将id用逗号进行隔开
+            $where = [
+                'id'=>['in',$ids],
+            ];
+            //批量删除本地图片
+            $pic=$user->where($where)->column('pic');//查询满足条件的pic字段数组信息
+            if(!empty($pic)){//判断不为空
+                $pi = implode(',',$pic);//将pic(图片路径)字段用逗号进行隔开
+                foreach ($pic as $value) {//将拆分的路径进行循环输出
+                    $picSrc = ADMINIMG.$value;//将入口文件(public/index.php)定义的本地图片存放路径和$value(图片名称)进行拼接
+                    if(file_exists($picSrc)){//如果该路径下有满足条件的图片名称则进行删除,file_exists():判断文件是否存在
+                        @unlink($picSrc);//@unlink():删除图片
+                    }  
+                }
+            }
+            $list = $user->where($where)->delete();
+            if($list){
+               return 1;//删除成功
+            }else{
+                return 2;//删除失败    
+            }
+        }else{
+            return view();
+        }
+
+    }
+
+
+
+    //上传图片
+    public function upload(){
+        // 获取表单上传文件 例如上传了001.jpg
+        $file = request()->file('pic');
+        
+        // 移动到框架应用根目录/public/uploads/ 目录下
+        if($file){
+            $info = $file->move(ROOT_PATH . 'public' . DS . 'static'. DS .'uploads');
+            if($info){
+                return $info->getSaveName();
+            }else{
+                // 上传失败获取错误信息
+                echo $file->getError();die;
+            }
+        }
+    }
+       
+
+
+ 
+
+
+        
+}