Article.php 6.0 KB

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