Article.php 5.8 KB

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