Article.php 3.6 KB

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