Article.php 6.1 KB

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