Article.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\service\FileService;
  17. /**
  18. * 文章管理控制器
  19. */
  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('category_tree', $category_tree);
  31. View::assign('list', $list);
  32. View::assign('cid', $cid);
  33. return View::fetch();
  34. }
  35. public function save($content_type = 0, $id = 0)
  36. {
  37. if ($this->app->request->isPost()) {
  38. $params = $this->app->request->param();
  39. if (!$params['cid']) {
  40. $this->error('请选择栏目');
  41. }
  42. if ($params['title'] == '') {
  43. $this->error("标题不能为空");
  44. }
  45. $params['content'] = isset($params['content']) ? $params['content'] : '';
  46. if ($content_type == 0) {
  47. $params['content'] = $this->saveRomteImage($params['content']);
  48. }
  49. $params['keywords'] = str_replace(' ', '', $params['keywords']);
  50. try {
  51. if ($params['id'] != 0) {
  52. ArticleModel::update($params);
  53. } else {
  54. $params['userid'] = $this->getSysUser()->userid;
  55. $params['username'] = $this->getSysUser()->nickname;
  56. unset($params['id']);
  57. ArticleModel::create($params);
  58. }
  59. } catch (\Exception $e) {
  60. $msg = $e->getMessage();
  61. $this->error("错误提示:" . $msg);
  62. }
  63. $this->success('操作成功', (string) url('index?cid=' . $params['cid']));
  64. } else {
  65. if ($id) {
  66. $data = ArticleModel::find($id);
  67. } else {
  68. $data = new ArticleModel();
  69. $data->content_type = $content_type;
  70. }
  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. $fileService = new FileService();
  81. $content = stripslashes ($content);
  82. $img_array = [];
  83. // 匹配所有远程图片
  84. $pattern = '/src="(http[s]:\/\/.*)"/isU';
  85. preg_match_all ($pattern,$content,$img_array);
  86. // 删除重复 url
  87. $img_arrays = array_unique ($img_array[1]);
  88. foreach ($img_arrays as $value) {
  89. $file = $fileService->downloadUrlImg($value);
  90. $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
  91. $filename = Config::get('filesystem.disks.public.url') . '/' . str_replace('\\', '/', $savename);
  92. // dump($filename);
  93. $content = str_replace($value, $filename, $content);
  94. }
  95. return $content;
  96. }
  97. }