Article.php 4.5 KB

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