You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
8.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Admin\CommonController;
  4. use DB;
  5. use App\Common\ReturnData;
  6. use Illuminate\Http\Request;
  7. use App\Http\Logic\ArticleLogic;
  8. class ArticleController extends CommonController
  9. {
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. }
  14. public function getLogic()
  15. {
  16. return new ArticleLogic();
  17. }
  18. public function index()
  19. {
  20. $res = '';
  21. $where = function ($query) use ($res) {
  22. if(isset($_REQUEST["keyword"]))
  23. {
  24. $query->where('title', 'like', '%'.$_REQUEST['keyword'].'%');
  25. }
  26. if(isset($_REQUEST["typeid"]) && $_REQUEST["typeid"]!=0)
  27. {
  28. $query->where('typeid', $_REQUEST["typeid"]);
  29. }
  30. if(isset($_REQUEST["id"]))
  31. {
  32. $query->where('typeid', $_REQUEST["id"]);
  33. }
  34. if(isset($_REQUEST["ischeck"]))
  35. {
  36. $query->where('ischeck', $_REQUEST["ischeck"]); //未审核过的文章
  37. }
  38. };
  39. $posts = $this->getLogic()->getPaginate($where, array('id', 'desc'));
  40. $data['posts'] = $posts;
  41. return view('admin.article.index', $data);
  42. //if(!empty($_GET["id"])){$id = $_GET["id"];}else {$id="";}if(preg_match('/[0-9]*/',$id)){}else{exit;}
  43. /* if(!empty($id)){$map['typeid']=$id;}
  44. $Article = M("Article")->field('id')->where($map);
  45. $counts = $Article->count();
  46. $pagesize =CMS_PAGESIZE;$page =0;
  47. if($counts % $pagesize){ //取总数据量除以每页数的余数
  48. $pages = intval($counts/$pagesize) + 1; //如果有余数,则页数等于总数据量除以每页数的结果取整再加一,如果没有余数,则页数等于总数据量除以每页数的结果
  49. }else{$pages = $counts/$pagesize;}
  50. if(!empty($_GET["page"])){$page = $_GET["page"]-1;$nextpage=$_GET["page"]+1;$previouspage=$_GET["page"]-1;}else{$page = 0;$nextpage=2;$previouspage=0;}
  51. if($counts>0){if($page>$pages-1){exit;}}
  52. $start = $page*$pagesize;
  53. $Article = M("Article")->field('id,typeid,title,pubdate,click,litpic,tuijian')->where($map)->order('id desc')->limit($start,$pagesize)->select();
  54. $this->counts = $counts;
  55. $this->pages = $pages;
  56. $this->page = $page;
  57. $this->nextpage = $nextpage;
  58. $this->previouspage = $previouspage;
  59. $this->id = $id;
  60. $this->posts = $Article; */
  61. //echo '<pre>';
  62. //print_r($Article);
  63. //return $this->fetch();
  64. }
  65. public function add()
  66. {
  67. $data = '';
  68. if(!empty($_REQUEST["catid"])){$data['catid'] = $_REQUEST["catid"];}else{$data['catid'] = 0;}
  69. return view('admin.article.add', $data);
  70. }
  71. public function doadd()
  72. {
  73. $litpic="";if(!empty($_POST["litpic"])){$litpic = $_POST["litpic"];}else{$_POST['litpic']="";} //缩略图
  74. if(empty($_POST["description"])){if(!empty($_POST["body"])){$_POST['description']=cut_str($_POST["body"]);}} //description
  75. $content="";if(!empty($_POST["body"])){$content = $_POST["body"];}
  76. $_POST['pubdate'] = time();//更新时间
  77. $_POST['addtime'] = time();//添加时间
  78. $_POST['user_id'] = $_SESSION['admin_user_info']['id']; // 发布者id
  79. //关键词
  80. if(!empty($_POST["keywords"]))
  81. {
  82. $_POST['keywords']=str_replace("",",",$_POST["keywords"]);
  83. }
  84. else
  85. {
  86. if(!empty($_POST["title"]))
  87. {
  88. $title=$_POST["title"];
  89. $title=str_replace("","",$title);
  90. $title=str_replace(",","",$title);
  91. $_POST['keywords']=get_keywords($title);//标题分词
  92. }
  93. }
  94. if(isset($_POST["dellink"]) && $_POST["dellink"]==1 && !empty($content)){$content=replacelinks($content,array(sysconfig('CMS_BASEHOST')));} //删除非站内链接
  95. $_POST['body']=$content;
  96. //提取第一个图片为缩略图
  97. if(isset($_POST["autolitpic"]) && $_POST["autolitpic"] && empty($litpic))
  98. {
  99. if(getfirstpic($content))
  100. {
  101. //获取文章内容的第一张图片
  102. $imagepath = '.'.getfirstpic($content);
  103. //获取后缀名
  104. preg_match_all ("/\/(.+)\.(gif|jpg|jpeg|bmp|png)$/iU",$imagepath,$out, PREG_PATTERN_ORDER);
  105. $saveimage='./uploads/'.date('Y/m',time()).'/'.basename($imagepath,'.'.$out[2][0]).'-lp.'.$out[2][0];
  106. //生成缩略图,按照原图的比例生成一个最大为240*180的缩略图
  107. \Intervention\Image\Facades\Image::make($imagepath)->resize(sysconfig('CMS_IMGWIDTH'), sysconfig('CMS_IMGHEIGHT'))->save($saveimage);
  108. //缩略图路径
  109. $_POST['litpic']='/uploads/'.date('Y/m',time()).'/'.basename($imagepath,'.'.$out[2][0]).'-lp.'.$out[2][0];
  110. }
  111. }
  112. $res = $this->getLogic()->add($_POST);
  113. if($res['code']==ReturnData::SUCCESS)
  114. {
  115. success_jump($res['msg'], route('admin_article'));
  116. }
  117. else
  118. {
  119. error_jump($res['msg']);
  120. }
  121. }
  122. public function edit()
  123. {
  124. if(!empty($_GET["id"])){$id = $_GET["id"];}else {$id="";}if(preg_match('/[0-9]*/',$id)){}else{exit;}
  125. $data['id'] = $id;
  126. $data['post'] = object_to_array($this->getLogic()->getOne(['id'=>$id]), 1);
  127. return view('admin.article.edit', $data);
  128. }
  129. public function doedit()
  130. {
  131. if(!empty($_POST["id"])){$id = $_POST["id"];unset($_POST["id"]);}else{$id="";exit;}
  132. $litpic="";if(!empty($_POST["litpic"])){$litpic = $_POST["litpic"];}else{$_POST['litpic']="";} //缩略图
  133. if(empty($_POST["description"])){if(!empty($_POST["body"])){$_POST['description']=cut_str($_POST["body"]);}} //description
  134. $content="";if(!empty($_POST["body"])){$content = $_POST["body"];}
  135. $_POST['pubdate'] = time();//更新时间
  136. if(!empty($_POST["keywords"]))
  137. {
  138. $_POST['keywords']=str_replace("",",",$_POST["keywords"]);
  139. }
  140. else
  141. {
  142. if(!empty($_POST["title"]))
  143. {
  144. $title=$_POST["title"];
  145. $title=str_replace("","",$title);
  146. $title=str_replace(",","",$title);
  147. $_POST['keywords']=get_keywords($title);//标题分词
  148. }
  149. }
  150. if(isset($_POST["dellink"]) && $_POST["dellink"]==1 && !empty($content)){$content=replacelinks($content,array(CMS_BASEHOST));} //删除非站内链接
  151. $_POST['body']=$content;
  152. //提取第一个图片为缩略图
  153. if(isset($_POST["autolitpic"]) && $_POST["autolitpic"] && empty($litpic))
  154. {
  155. if(getfirstpic($content))
  156. {
  157. //获取文章内容的第一张图片
  158. $imagepath = '.'.getfirstpic($content);
  159. //获取后缀名
  160. preg_match_all ("/\/(.+)\.(gif|jpg|jpeg|bmp|png)$/iU",$imagepath,$out, PREG_PATTERN_ORDER);
  161. $saveimage='./uploads/'.date('Y/m',time()).'/'.basename($imagepath,'.'.$out[2][0]).'-lp.'.$out[2][0];
  162. //生成缩略图,按照原图的比例生成一个最大为240*180的缩略图
  163. \Intervention\Image\Facades\Image::make($imagepath)->resize(sysconfig('CMS_IMGWIDTH'), sysconfig('CMS_IMGHEIGHT'))->save($saveimage);
  164. //缩略图路径
  165. $_POST['litpic']='/uploads/'.date('Y/m',time()).'/'.basename($imagepath,'.'.$out[2][0]).'-lp.'.$out[2][0];
  166. }
  167. }
  168. $res = $this->getLogic()->edit($_POST,array('id'=>$id));
  169. if($res['code']==ReturnData::SUCCESS)
  170. {
  171. success_jump($res['msg'], route('admin_article'));
  172. }
  173. else
  174. {
  175. error_jump($res['msg']);
  176. }
  177. }
  178. //删除文章
  179. public function del()
  180. {
  181. if(!empty($_GET["id"])){$id = $_GET["id"];}else{error_jump("删除失败!请重新提交");}
  182. if(DB::table("article")->whereIn("id", explode(',', $id))->delete())
  183. {
  184. success_jump("$id ,删除成功");
  185. }
  186. else
  187. {
  188. error_jump("$id ,删除失败!请重新提交");
  189. }
  190. }
  191. //重复文章列表
  192. public function repetarc()
  193. {
  194. $data['posts'] = object_to_array(DB::table('article')->select(DB::raw('title,count(*) AS count'))->orderBy('count', 'desc')->groupBy('title')->having('count', '>', 1)->get());
  195. return view('admin.article.repetarc', $data);
  196. }
  197. //推荐文章
  198. public function recommendarc()
  199. {
  200. if(!empty($_GET["id"])){$id = $_GET["id"];}else{error_jump("您访问的页面不存在或已被删除!");} //if(preg_match('/[0-9]*/',$id)){}else{exit;}
  201. $data['tuijian'] = 1;
  202. if(DB::table("article")->whereIn("id", explode(',', $id))->update($data))
  203. {
  204. success_jump("$id ,推荐成功");
  205. }
  206. else
  207. {
  208. error_jump("$id ,推荐失败!请重新提交");
  209. }
  210. }
  211. //检测重复文章数量
  212. public function articleexists()
  213. {
  214. $res = '';
  215. $where = function ($query) use ($res) {
  216. if(isset($_REQUEST["title"]))
  217. {
  218. $query->where('title', $_REQUEST["title"]);
  219. }
  220. if(isset($_REQUEST["id"]))
  221. {
  222. $query->where('id', '<>', $_REQUEST["id"]);
  223. }
  224. };
  225. return DB::table("article")->where($where)->count();
  226. }
  227. }