123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?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
-
- $where='1=1';//没有搜索条件则默认查询所有数据
- 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
- ->alias('a')
- ->field('a.*,b.catename')
- ->join('column b','a.column_id=b.id')
- ->limit("$start,$limit")
- ->where($where)
- ->select();
-
- return ["code"=>0,"msg"=>"成功","count"=>$count,"data"=>$list];
- }
- public function add(){//添加
- $user = new ArticleModel();
- if(request()->isPost()){
- $data = input('post.');
- if($_FILES['pic']['tmp_name']){//上传图片
- $data['pic']=$this->upload();//调用upload方法
- }
- if($data['keywords']==''){//解决keywords为''
- $data['keywords']=null;
- }
- $data['time']=date('Y-m-d',time());
- $add=$user->insert($data);
- if($add){
- echo "<script>alert('添加信息成功!');window.parent.location.reload()</script>";//后台直接实现弹窗,操作完成并显示列表页
- }else{
- echo "<script>alert('添加信息失败');location.href='".url('article/add')."'</script>";
- }
- }
- $column = db('column')->select();
- $this->assign([
- 'column'=>$column,//栏目列表
- ]);
- return view();
-
- }
- public function edit(){//编辑
- $user = new ArticleModel();
- $id = input('id');
- $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();
- }
- if($data['keywords']==''){//解决keywords为''
- $data['keywords']=null;
- }
- $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>";
- }
- }
- $column = db('column')->select();
- $this->assign([
- 'db'=>$db,//文章数据
- 'column'=>$column,//导航栏数据
- ]);
- 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;//删除失败
-
- }
- }
- }
- 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;//删除失败
- }
- }
- }
- //上传图片
- 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;
- }
- }
- }
-
-
-
- }
|