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.

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