Article.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <<<<<<< HEAD
  2. <?php
  3. declare(strict_types=1);
  4. /**
  5. * +----------------------------------------------------------------------
  6. * 后台文章控制制器
  7. * @author huwhis@163.com
  8. * @version 0.0.1
  9. * +----------------------------------------------------------------------
  10. */
  11. namespace app\sys\controller;
  12. // 引入框架内置类
  13. use think\facade\View;
  14. use think\facade\Config;
  15. use app\common\model\Category as CategoryModel;
  16. use app\common\model\Article as ArticleModel;
  17. use app\common\service\FileService;
  18. /**
  19. * 文章管理控制器
  20. */
  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('category_tree', $category_tree);
  32. View::assign('list', $list);
  33. View::assign('cid', $cid);
  34. return View::fetch();
  35. }
  36. public function save($content_type = 0, $id = 0)
  37. {
  38. if ($this->app->request->isPost()) {
  39. $params = $this->app->request->param();
  40. if (!$params['cid']) {
  41. $this->error('请选择栏目');
  42. }
  43. if ($params['title'] == '') {
  44. $this->error("标题不能为空");
  45. }
  46. $params['content'] = isset($params['content']) ? $params['content'] : '';
  47. if ($content_type == 0) {
  48. $params['content'] = $this->saveRomteImage($params['content']);
  49. }
  50. $params['keywords'] = str_replace(' ', '', $params['keywords']);
  51. try {
  52. if ($params['id'] != 0) {
  53. ArticleModel::update($params);
  54. } else {
  55. $params['userid'] = $this->getSysUser()->userid;
  56. $params['username'] = $this->getSysUser()->nickname;
  57. unset($params['id']);
  58. ArticleModel::create($params);
  59. }
  60. } catch (\Exception $e) {
  61. $msg = $e->getMessage();
  62. $this->error("错误提示:" . $msg);
  63. }
  64. $this->success('操作成功', (string) url('index?cid=' . $params['cid']));
  65. } else {
  66. if ($id) {
  67. $data = ArticleModel::find($id);
  68. } else {
  69. $data = new ArticleModel();
  70. $data->content_type = $content_type;
  71. }
  72. $categories = CategoryModel::field('id, parent_id, name')->select();
  73. View::assign('category_tree', list_tree($categories));
  74. View::assign('data', $data);
  75. $template = $content_type ? 'savemd' : 'save';
  76. return View::fetch($template);
  77. }
  78. }
  79. protected function saveRomteImage($content)
  80. {
  81. $fileService = new FileService();
  82. $content = stripslashes ($content);
  83. $img_array = [];
  84. // 匹配所有远程图片
  85. $pattern = '/src="(http[s]:\/\/.*)"/isU';
  86. preg_match_all ($pattern,$content,$img_array);
  87. // 删除重复 url
  88. $img_arrays = array_unique ($img_array[1]);
  89. foreach ($img_arrays as $value) {
  90. $file = $fileService->downloadUrlImg($value);
  91. $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
  92. $filename = Config::get('filesystem.disks.public.url') . '/' . str_replace('\\', '/', $savename);
  93. // dump($filename);
  94. $content = str_replace($value, $filename, $content);
  95. }
  96. return $content;
  97. }
  98. }
  99. =======
  100. <?php
  101. declare(strict_types=1);
  102. /**
  103. * +----------------------------------------------------------------------
  104. * 后台文章控制制器
  105. * @author huwhis@163.com
  106. * @version 0.0.1
  107. * +----------------------------------------------------------------------
  108. */
  109. namespace app\sys\controller;
  110. // 引入框架内置类
  111. use think\facade\View;
  112. use think\facade\Config;
  113. use app\common\model\Category as CategoryModel;
  114. use app\common\model\Article as ArticleModel;
  115. use app\common\facade\FileUtils;
  116. use app\common\model\FileManager as FileManagerModel;
  117. class Article extends Base
  118. {
  119. protected $modelName = "Article";
  120. public function index()
  121. {
  122. $categories = CategoryModel::select();
  123. $category_tree = list_tree($categories);
  124. $cid = $this->app->request->param('cid');
  125. $params = $this->app->request->param();
  126. $list = ArticleModel::queryPage($params);
  127. View::assign([
  128. 'category_tree' => $category_tree,
  129. 'list' => $list,
  130. 'cid' => $cid
  131. ]);
  132. return View::fetch();
  133. }
  134. public function save($content_type = 0, $id = 0)
  135. {
  136. if ($this->app->request->isPost()) {
  137. $params = $this->app->request->param();
  138. if (!$params['cid']) {
  139. $this->error('请选择栏目');
  140. }
  141. if ($params['title'] == '') {
  142. $this->error("标题不能为空");
  143. }
  144. $params['content'] = isset($params['content']) ? $params['content'] : '';
  145. if ($content_type == 0) {
  146. $username = $this->getSysUser()->username;
  147. $params['content'] = $this->saveRomteImage($params['content'],(int)$params['id'],(int) $params['cjid'], $username);
  148. }
  149. $params['keywords'] = trim($params['keywords']);
  150. try {
  151. if ($params['id'] != 0) {
  152. ArticleModel::updateOne($params);
  153. } else {
  154. $params['userid'] = $this->getSysUser()->userid;
  155. $params['username'] = $this->getSysUser()->nickname;
  156. unset($params['id']);
  157. ArticleModel::createOne($params);
  158. }
  159. } catch (\Exception $e) {
  160. $msg = $e->getMessage();
  161. $this->error("错误提示:" . $msg);
  162. }
  163. $this->success('操作成功', (string) url('index?cid=' . $params['cid']));
  164. } else {
  165. if ($id) {
  166. $data = ArticleModel::find($id);
  167. } else {
  168. $data = new ArticleModel();
  169. $data->content_type = $content_type;
  170. }
  171. $data->cjid = time();
  172. $categories = CategoryModel::field('id, parent_id, name')->select();
  173. View::assign('category_tree', list_tree($categories));
  174. View::assign('data', $data);
  175. $template = $content_type ? 'savemd' : 'save';
  176. return View::fetch($template);
  177. }
  178. }
  179. protected function saveRomteImage($content, $infoid=0, $cjid=0, $username='system')
  180. {
  181. $content = stripslashes ($content);
  182. $img_array = [];
  183. // 匹配所有远程图片
  184. $pattern = '/src="(http[s]:\/\/.*)"/isU';
  185. preg_match_all ($pattern,$content,$img_array);
  186. // 删除重复 url
  187. $img_arrays = array_unique ($img_array[1]);
  188. foreach ($img_arrays as $value) {
  189. $file = FileUtils::downloadUrlImg($value);
  190. $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
  191. FileManagerModel::saveFileInfo($file, $savename, $file->getOriginalName, $infoid, $cjid, $username);
  192. // 删除临时文件
  193. @unlink($file->getRealPath());
  194. $filename = Config::get('filesystem.disks.public.url') . '/' . str_replace('\\', '/', $savename);
  195. // dump($filename);
  196. $content = str_replace($value, $filename, $content);
  197. }
  198. return $content;
  199. }
  200. }
  201. >>>>>>> 78b76253c8ce5873016cf837373af5e30ac80c86