Article.php 4.5 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\sys\controller;
  11. // 引入框架内置类
  12. use think\Exception;
  13. use think\facade\Db;
  14. use think\facade\Config;
  15. use think\facade\View;
  16. use app\common\model\Category as CategoryModel;
  17. use app\common\model\Article as ArticleModel;
  18. use app\common\facade\FileUtils;
  19. use app\common\utils\ReUtils;
  20. use app\common\model\FileManager as FileManagerModel;
  21. class Article extends Base
  22. {
  23. protected $modelName = "Article";
  24. public function index()
  25. {
  26. $categories = CategoryModel::select();
  27. $category_tree = list_tree($categories);
  28. $cid = $this->app->request->param('cid');
  29. $params = $this->app->request->param();
  30. $list = ArticleModel::queryPage($params);
  31. View::assign([
  32. 'category_tree' => $category_tree,
  33. 'list' => $list,
  34. 'cid' => $cid
  35. ]);
  36. return View::fetch();
  37. }
  38. public function save($content_type = 0, $id = 0)
  39. {
  40. if ($this->app->request->isPost()) {
  41. $params = $this->app->request->param();
  42. if (!$params['cid']) {
  43. $this->error('请选择栏目');
  44. }
  45. if ($params['title'] == '') {
  46. $this->error("标题不能为空");
  47. }
  48. $params['content'] = isset($params['content']) ? $params['content'] : '';
  49. if ($content_type == 0) {
  50. $username = $this->getSysUser()->username;
  51. $params['content'] = $this->saveRomteImage($params['content'], (int)$params['id'], (int) $params['cjid'], $username);
  52. }
  53. $params['keywords'] = trim($params['keywords']);
  54. try {
  55. if ($params['id'] != 0) {
  56. ArticleModel::update($params);
  57. } else {
  58. $params['userid'] = $this->getSysUser()->userid;
  59. $params['username'] = $this->getSysUser()->nickname;
  60. unset($params['id']);
  61. ArticleModel::create($params);
  62. }
  63. } catch (\Exception $e) {
  64. $msg = $e->getMessage();
  65. $this->error("错误提示:" . $msg);
  66. }
  67. $this->success('操作成功', (string) url('index?cid=' . $params['cid']));
  68. } else {
  69. if ($id) {
  70. $data = ArticleModel::find($id);
  71. } else {
  72. $data = new ArticleModel();
  73. $data->content_type = $content_type;
  74. }
  75. $data->cjid = time();
  76. $categories = CategoryModel::field('id, parent_id, name')->select();
  77. View::assign('category_tree', list_tree($categories));
  78. View::assign('data', $data);
  79. $template = $content_type ? 'savemd' : 'save';
  80. return View::fetch($template);
  81. }
  82. }
  83. protected function saveRomteImage($content, $infoid = 0, $cjid = 0, $username = 'system')
  84. {
  85. $content = stripslashes($content);
  86. $img_array = [];
  87. // 匹配所有远程图片
  88. $pattern = '/src="(http[s]:\/\/.*)"/isU';
  89. preg_match_all($pattern, $content, $img_array);
  90. // 删除重复 url
  91. $img_arrays = array_unique($img_array[1]);
  92. foreach ($img_arrays as $value) {
  93. $file = FileUtils::downloadUrlImg($value);
  94. $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
  95. FileManagerModel::saveFileInfo($file, $savename, $file->getOriginalName, $infoid, $cjid, $username);
  96. // 删除临时文件
  97. @unlink($file->getRealPath());
  98. $filename = Config::get('filesystem.disks.public.url') . '/' . str_replace('\\', '/', $savename);
  99. // dump($filename);
  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();
  115. }
  116. return ReUtils::success();
  117. }
  118. }