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.

294 lines
9.8 KiB

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