Article.php 5.7 KB

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