Article.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * +----------------------------------------------------------------------
  5. * 后台文章控制制器
  6. * @author huwhis@163.com
  7. * @version 0.0.1
  8. * +----------------------------------------------------------------------
  9. */
  10. namespace app\controller\sys;
  11. // 引入框架内置类
  12. use think\Exception;
  13. use think\facade\Db;
  14. use think\facade\View;
  15. use app\model\Category as CategoryModel;
  16. use app\model\Article as ArticleModel;
  17. use app\model\TagArticle as TagArticleModel;
  18. use app\utils\FileUtils;
  19. use app\utils\ReUtils;
  20. class Article extends Base
  21. {
  22. protected $modelName = "Article";
  23. public function index()
  24. {
  25. $categories = CategoryModel::select();
  26. $category_tree = list_tree($categories);
  27. $cid = $this->app->request->param('cid');
  28. $params = $this->app->request->param();
  29. $list = ArticleModel::queryPage($params);
  30. View::assign([
  31. 'category_tree' => $category_tree,
  32. 'list' => $list,
  33. 'cid' => $cid
  34. ]);
  35. return View::fetch();
  36. }
  37. public function save($content_type = 0, $id = 0)
  38. {
  39. if ($this->app->request->isPost()) {
  40. $params = $this->app->request->param();
  41. if (!$params['cid']) {
  42. $this->error('请选择栏目');
  43. }
  44. if ($params['title'] == '') {
  45. $this->error("标题不能为空");
  46. }
  47. if ($params['titlepic'] && preg_match('/(http[s]:\/\/.*)/isU', $params['titlepic'])) {
  48. $params['titlepic'] = FileUtils::downloadUrlImg($params['titlepic']);
  49. }
  50. $params['content'] = isset($params['content']) ? $params['content'] : '';
  51. $params['content'] = $this->saveRomteImage($params['content']);
  52. $params['keywords'] = trim($params['keywords']);
  53. try {
  54. if ($params['id'] != 0) {
  55. $oldKeywords = ArticleModel::getKeywordsById((int) $params['id']);
  56. $article = ArticleModel::update($params);
  57. $oldTags = explode(",", $oldKeywords);
  58. $newTags = explode(",", $article->keywords);
  59. $addTas = array_diff($newTags, $oldTags);
  60. $subTas = array_diff($oldTags, $newTags);
  61. TagArticleModel::saveArticleTag($addTas, (int) $article->id, (int)$article->cid);
  62. TagArticleModel::delArticleTag($subTas, (int) $article->id);
  63. } else {
  64. $params['userid'] = $this->getSysUser()->userid;
  65. $params['username'] = $this->getSysUser()->nickname != "" ? $this->getSysUser()->nickname : $this->getSysUser()->username;
  66. unset($params['id']);
  67. $article = ArticleModel::create($params);
  68. $tags = explode(",", $article->keywords);
  69. TagArticleModel::saveArticleTag($tags, (int) $article->id, (int) $article->cid);
  70. }
  71. } catch (\Exception $e) {
  72. $this->error("错误提示:" . $e->getMessage());
  73. }
  74. $this->success('操作成功', (string) url('/sys/article/index'));
  75. } else {
  76. if ($id) {
  77. $data = ArticleModel::find($id);
  78. } else {
  79. $data = new ArticleModel();
  80. $data->content_type = $content_type;
  81. }
  82. $categories = CategoryModel::field('id, parent_id, name')->select();
  83. View::assign('category_tree', list_tree($categories));
  84. View::assign('data', $data);
  85. $template = $content_type ? 'savemd' : 'save';
  86. return View::fetch($template);
  87. }
  88. }
  89. protected function saveRomteImage($content)
  90. {
  91. $content = stripslashes($content);
  92. $img_array = [];
  93. // 匹配所有远程图片
  94. $pattern = '/src="(http[s]:\/\/.*)"/isU';
  95. preg_match_all($pattern, $content, $img_array);
  96. // 删除重复 url
  97. $img_arrays = array_unique($img_array[1]);
  98. foreach ($img_arrays as $value) {
  99. $filename = FileUtils::downloadUrlImg($value);
  100. $content = str_replace($value, $filename, $content);
  101. }
  102. return $content;
  103. }
  104. public function move()
  105. {
  106. $ids = $this->request->param('ids');
  107. $cid = $this->request->param('cid');
  108. $tablename = ArticleModel::getTable();
  109. $idss = implode(',', $ids);
  110. $sql = "update " . $tablename . " set cid=" . $cid . " where id IN (" . $idss . ");";
  111. try {
  112. Db::execute($sql);
  113. } catch (Exception $e) {
  114. return ReUtils::error($e->getMessage());
  115. }
  116. return ReUtils::success();
  117. }
  118. /**
  119. * 删除
  120. * @param int|array $id info id
  121. * @return array
  122. */
  123. public function delete($id)
  124. {
  125. if ($this->request->isPost()) {
  126. if (ArticleModel::destroy($id)) {
  127. if (is_array($id)) {
  128. TagArticleModel::where('aid',"IN",$id)->delete();
  129. } else {
  130. TagArticleModel::where('aid',"=",$id)->delete();
  131. }
  132. return ReUtils::success();
  133. } else {
  134. return ReUtils::error();
  135. }
  136. }
  137. }
  138. }