Article.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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, $article->id, $article->cid);
  62. TagArticleModel::delArticleTag($subTas, $article->id);
  63. } else {
  64. $params['userid'] = $this->getSysUser()->userid;
  65. $params['username'] = $this->getSysUser()->nickname == "" ?: $this->getSysUser()->username;
  66. unset($params['id']);
  67. $article = ArticleModel::create($params);
  68. $tags = explode(",", $article->keywords);
  69. TagArticleModel::saveArticleTag($tags, $article->id, $article->cid);
  70. }
  71. } catch (\Exception $e) {
  72. $msg = $e->getMessage();
  73. $this->error("错误提示:" . $msg);
  74. }
  75. $this->success('操作成功', (string) url('/sys/article/index'));
  76. } else {
  77. if ($id) {
  78. $data = ArticleModel::find($id);
  79. } else {
  80. $data = new ArticleModel();
  81. $data->content_type = $content_type;
  82. }
  83. $categories = CategoryModel::field('id, parent_id, name')->select();
  84. View::assign('category_tree', list_tree($categories));
  85. View::assign('data', $data);
  86. $template = $content_type ? 'savemd' : 'save';
  87. return View::fetch($template);
  88. }
  89. }
  90. protected function saveRomteImage($content)
  91. {
  92. $content = stripslashes($content);
  93. $img_array = [];
  94. // 匹配所有远程图片
  95. $pattern = '/src="(http[s]:\/\/.*)"/isU';
  96. preg_match_all($pattern, $content, $img_array);
  97. // 删除重复 url
  98. $img_arrays = array_unique($img_array[1]);
  99. foreach ($img_arrays as $value) {
  100. $filename = FileUtils::downloadUrlImg($value);
  101. $content = str_replace($value, $filename, $content);
  102. }
  103. return $content;
  104. }
  105. public function move()
  106. {
  107. $ids = $this->request->param('ids');
  108. $cid = $this->request->param('cid');
  109. $tablename = ArticleModel::getTable();
  110. $idss = implode(',', $ids);
  111. $sql = "update " . $tablename . " set cid=" . $cid . " where id IN (" . $idss . ");";
  112. try {
  113. Db::execute($sql);
  114. } catch (Exception $e) {
  115. return ReUtils::error();
  116. }
  117. return ReUtils::success();
  118. }
  119. }