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.

273 lines
8.5 KiB

8 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 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['pubdate'] = time();//更新时间
  76. $_POST['addtime'] = time();//添加时间
  77. $_POST['user_id'] = $_SESSION['admin_user_info']['id']; // 发布者id
  78. //关键词
  79. if(!empty($_POST["keywords"]))
  80. {
  81. $_POST['keywords']=str_replace("",",",$_POST["keywords"]);
  82. }
  83. else
  84. {
  85. if(!empty($_POST["title"]))
  86. {
  87. $title=$_POST["title"];
  88. $title=str_replace("","",$title);
  89. $title=str_replace(",","",$title);
  90. $_POST['keywords']=get_keywords($title);//标题分词
  91. }
  92. }
  93. if(isset($_POST["dellink"]) && $_POST["dellink"]==1 && !empty($content)){$content=replacelinks($content,array(sysconfig('CMS_BASEHOST')));} //删除非站内链接
  94. $_POST['body']=$content;
  95. //提取第一个图片为缩略图
  96. if(isset($_POST["autolitpic"]) && $_POST["autolitpic"] && empty($litpic))
  97. {
  98. if(getfirstpic($content))
  99. {
  100. //获取文章内容的第一张图片
  101. $imagepath = '.'.getfirstpic($content);
  102. //获取后缀名
  103. preg_match_all ("/\/(.+)\.(gif|jpg|jpeg|bmp|png)$/iU",$imagepath,$out, PREG_PATTERN_ORDER);
  104. $saveimage='./uploads/'.date('Y/m',time()).'/'.basename($imagepath,'.'.$out[2][0]).'-lp.'.$out[2][0];
  105. //生成缩略图,按照原图的比例生成一个最大为240*180的缩略图
  106. \Intervention\Image\Facades\Image::make($imagepath)->resize(sysconfig('CMS_IMGWIDTH'), sysconfig('CMS_IMGHEIGHT'))->save($saveimage);
  107. //缩略图路径
  108. $_POST['litpic']='/uploads/'.date('Y/m',time()).'/'.basename($imagepath,'.'.$out[2][0]).'-lp.'.$out[2][0];
  109. }
  110. }
  111. $res = $this->getLogic()->add($_POST);
  112. if($res['code']==ReturnData::SUCCESS)
  113. {
  114. success_jump($res['msg'], route('admin_article'));
  115. }
  116. else
  117. {
  118. error_jump($res['msg']);
  119. }
  120. }
  121. public function edit()
  122. {
  123. if(!empty($_GET["id"])){$id = $_GET["id"];}else {$id="";}if(preg_match('/[0-9]*/',$id)){}else{exit;}
  124. $data['id'] = $id;
  125. $data['post'] = object_to_array($this->getLogic()->getOne(['id'=>$id]), 1);
  126. return view('admin.article.edit', $data);
  127. }
  128. public function doedit()
  129. {
  130. if(!empty($_POST["id"])){$id = $_POST["id"];unset($_POST["id"]);}else{$id="";exit;}
  131. $litpic="";if(!empty($_POST["litpic"])){$litpic = $_POST["litpic"];}else{$_POST['litpic']="";} //缩略图
  132. if(empty($_POST["description"])){if(!empty($_POST["body"])){$_POST['description']=cut_str($_POST["body"]);}} //description
  133. $content="";if(!empty($_POST["body"])){$content = $_POST["body"];}
  134. $_POST['pubdate'] = time();//更新时间
  135. if(!empty($_POST["keywords"]))
  136. {
  137. $_POST['keywords']=str_replace("",",",$_POST["keywords"]);
  138. }
  139. else
  140. {
  141. if(!empty($_POST["title"]))
  142. {
  143. $title=$_POST["title"];
  144. $title=str_replace("","",$title);
  145. $title=str_replace(",","",$title);
  146. $_POST['keywords']=get_keywords($title);//标题分词
  147. }
  148. }
  149. if(isset($_POST["dellink"]) && $_POST["dellink"]==1 && !empty($content)){$content=replacelinks($content,array(CMS_BASEHOST));} //删除非站内链接
  150. $_POST['body']=$content;
  151. //提取第一个图片为缩略图
  152. if(isset($_POST["autolitpic"]) && $_POST["autolitpic"] && empty($litpic))
  153. {
  154. if(getfirstpic($content))
  155. {
  156. //获取文章内容的第一张图片
  157. $imagepath = '.'.getfirstpic($content);
  158. //获取后缀名
  159. preg_match_all ("/\/(.+)\.(gif|jpg|jpeg|bmp|png)$/iU",$imagepath,$out, PREG_PATTERN_ORDER);
  160. $saveimage='./uploads/'.date('Y/m',time()).'/'.basename($imagepath,'.'.$out[2][0]).'-lp.'.$out[2][0];
  161. //生成缩略图,按照原图的比例生成一个最大为240*180的缩略图
  162. \Intervention\Image\Facades\Image::make($imagepath)->resize(sysconfig('CMS_IMGWIDTH'), sysconfig('CMS_IMGHEIGHT'))->save($saveimage);
  163. //缩略图路径
  164. $_POST['litpic']='/uploads/'.date('Y/m',time()).'/'.basename($imagepath,'.'.$out[2][0]).'-lp.'.$out[2][0];
  165. }
  166. }
  167. $res = $this->getLogic()->edit($_POST,array('id'=>$id));
  168. if($res['code']==ReturnData::SUCCESS)
  169. {
  170. success_jump($res['msg'], route('admin_article'));
  171. }
  172. else
  173. {
  174. error_jump($res['msg']);
  175. }
  176. }
  177. //删除文章
  178. public function del()
  179. {
  180. if(!empty($_GET["id"])){$id = $_GET["id"];}else{error_jump("删除失败!请重新提交");}
  181. if(DB::table("article")->whereIn("id", explode(',', $id))->delete())
  182. {
  183. success_jump("$id ,删除成功");
  184. }
  185. else
  186. {
  187. error_jump("$id ,删除失败!请重新提交");
  188. }
  189. }
  190. //重复文章列表
  191. public function repetarc()
  192. {
  193. $data['posts'] = object_to_array(DB::table('article')->select(DB::raw('title,count(*) AS count'))->orderBy('count', 'desc')->groupBy('title')->having('count', '>', 1)->get());
  194. return view('admin.article.repetarc', $data);
  195. }
  196. //推荐文章
  197. public function recommendarc()
  198. {
  199. if(!empty($_GET["id"])){$id = $_GET["id"];}else{error_jump("您访问的页面不存在或已被删除!");} //if(preg_match('/[0-9]*/',$id)){}else{exit;}
  200. $data['tuijian'] = 1;
  201. if(DB::table("article")->whereIn("id", explode(',', $id))->update($data))
  202. {
  203. success_jump("$id ,推荐成功");
  204. }
  205. else
  206. {
  207. error_jump("$id ,推荐失败!请重新提交");
  208. }
  209. }
  210. //检测重复文章数量
  211. public function articleexists()
  212. {
  213. $res = '';
  214. $where = function ($query) use ($res) {
  215. if(isset($_REQUEST["title"]))
  216. {
  217. $query->where('title', $_REQUEST["title"]);
  218. }
  219. if(isset($_REQUEST["id"]))
  220. {
  221. $query->where('id', '<>', $_REQUEST["id"]);
  222. }
  223. };
  224. return DB::table("article")->where($where)->count();
  225. }
  226. }