Article.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Controller;
  4. use think\Request;
  5. use app\admin\model\ArticleModel;
  6. class Article extends Base
  7. {
  8. public function lst()//文章列表
  9. {
  10. dump(222222222);die;
  11. return view();
  12. }
  13. public function lst_data(Request $request){//文章列表json数据动态表格显示(分页)
  14. $user = new ArticleModel();
  15. $title = request()->param('title');//接收请求数据id
  16. $where='1=1';//没有搜索条件则默认查询所有数据
  17. if(!empty($title)){//不为空执行搜索条件,为空则跳过判断条件直接显示列表页,
  18. $where = ['title'=>$title];
  19. }
  20. //数据表获取总记录数
  21. $count = $user->where($where)->count();
  22. //获取每页显示的条数
  23. $limit= $request->param('limit');
  24. //获取当前页码
  25. $page= $request->param('page');
  26. //limit的起始位置
  27. $start=($page-1)*$limit;
  28. $list = $user
  29. ->limit("$start,$limit")
  30. ->where($where)
  31. ->select();
  32. return ["code"=>0,"msg"=>"成功","count"=>$count,"data"=>$list];
  33. }
  34. public function add(){//添加
  35. $user = new ArticleModel();
  36. // $catename = db('cates')->select();
  37. if(request()->isPost()){
  38. $data = input('post.');
  39. if($_FILES['pic']['tmp_name']){//上传图片
  40. $data['pic']=$this->upload();//调用upload方法
  41. }else{
  42. $data['pic']='';
  43. }
  44. $data['time']=date('Y-m-d',time());
  45. $add=$user->insert($data);
  46. if($add){
  47. // $logger->info("添加成功");
  48. echo "<script>alert('添加信息成功!');window.parent.location.reload()</script>";//后台直接实现弹窗,操作完成并显示列表页
  49. }else{
  50. echo "<script>alert('添加信息失败');location.href='".url('article/add')."'</script>";
  51. }
  52. }
  53. return view();
  54. }
  55. public function edit(){//编辑
  56. $user = new ArticleModel();
  57. $id = input('id');
  58. // $catename = db('cates')->select();
  59. $db = $user->where('id',$id)->find();
  60. if(request()->isPost()){
  61. $data = input('post.');
  62. //处理图片上传
  63. if($_FILES['pic']['tmp_name']){
  64. $oldarticles=db('article')->field('pic')->find($data['id']);
  65. $oldarticleImg=ADMINIMG.$oldarticles['pic'];
  66. if(file_exists($oldarticleImg)){
  67. @unlink($oldarticleImg);
  68. }
  69. $data['pic']=$this->upload();
  70. }
  71. $data['time'] = time();
  72. $save=$user->update($data);
  73. if($save!==false){
  74. echo "<script>alert('修改信息成功!');window.parent.location.reload()</script>";//后台直接实现弹窗,操作完成并显示列表页
  75. }else{
  76. echo "<script>alert('修改信息失败');location.href='".url('article/edit')."'</script>";
  77. }
  78. }
  79. $this->assign([
  80. 'db'=>$db,
  81. // 'catename'=>$catename,
  82. ]);
  83. return view();
  84. }
  85. public function del(){//删除
  86. $user = new ArticleModel();
  87. if(request()->isPost()){
  88. $id = input('id');
  89. //先删除本地文件夹的图片
  90. $pic=$user->field('pic')->where('id',$id)->find();
  91. if(!empty($pic)){//判断不为空
  92. $picSrc=ADMINIMG.$pic['pic'];
  93. if(file_exists($picSrc)){
  94. @unlink($picSrc);
  95. }
  96. }
  97. //在删除数据库信息
  98. $del=$user->where('id',$id)->delete();
  99. if($del){
  100. return 1;//删除成功
  101. }else{
  102. return 2;//删除失败
  103. }
  104. }
  105. }
  106. public function pdel(){//批量删除
  107. $user = new ArticleModel();
  108. if(request()->isPost()){
  109. $id = input('post.');//接收前台id
  110. $ids = implode(',',$id);//将id用逗号进行隔开
  111. $where = [
  112. 'id'=>['in',$ids],
  113. ];
  114. //批量删除本地图片
  115. $pic=$user->where($where)->column('pic');//查询满足条件的pic字段数组信息
  116. if(!empty($pic)){//判断不为空
  117. $pi = implode(',',$pic);//将pic(图片路径)字段用逗号进行隔开
  118. foreach ($pic as $value) {//将拆分的路径进行循环输出
  119. $picSrc = ADMINIMG.$value;//将入口文件(public/index.php)定义的本地图片存放路径和$value(图片名称)进行拼接
  120. if(file_exists($picSrc)){//如果该路径下有满足条件的图片名称则进行删除,file_exists():判断文件是否存在
  121. @unlink($picSrc);//@unlink():删除图片
  122. }
  123. }
  124. }
  125. $list = $user->where($where)->delete();
  126. if($list){
  127. return 1;//删除成功
  128. }else{
  129. return 2;//删除失败
  130. }
  131. }
  132. }
  133. //上传图片
  134. public function upload(){
  135. // 获取表单上传文件 例如上传了001.jpg
  136. $file = request()->file('pic');
  137. // 移动到框架应用根目录/public/uploads/ 目录下
  138. if($file){
  139. $info = $file->move(ROOT_PATH . 'public' . DS . 'static'. DS .'uploads');
  140. if($info){
  141. return $info->getSaveName();
  142. }else{
  143. // 上传失败获取错误信息
  144. echo $file->getError();die;
  145. }
  146. }
  147. }
  148. }