Article.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 app\common\model\Category as CategoryModel;
  14. use app\common\model\Article as ArticleModel;
  15. /**
  16. * 文章管理
  17. */
  18. class Article extends Base
  19. {
  20. protected $modelName = "Article";
  21. public function index()
  22. {
  23. $categories = CategoryModel::select();
  24. $category_tree = list_tree($categories);
  25. $cid = $this->app->request->param('cid');
  26. $params = $this->app->request->param();
  27. $list = ArticleModel::queryPage($params);
  28. View::assign('category_tree', $category_tree);
  29. View::assign('list', $list);
  30. View::assign('cid', $cid);
  31. return View::fetch();
  32. }
  33. public function save($content_type = 0, $id = 0)
  34. {
  35. if ($this->app->request->isPost()) {
  36. $params = $this->app->request->param();
  37. if (!$params['cid']) {
  38. $this->error('请选择栏目');
  39. }
  40. if ($params['title'] == '') {
  41. $this->error("标题不能为空");
  42. }
  43. $params['content'] = isset($params['content']) ? $params['content'] : '';
  44. try {
  45. if ($params['id'] != 0) {
  46. ArticleModel::update($params);
  47. } else {
  48. $params['userid'] = $this->getSysUser()->userid;
  49. $params['username'] = $this->getSysUser()->username;
  50. unset($params['id']);
  51. ArticleModel::create($params);
  52. }
  53. } catch (\Exception $e) {
  54. $msg = $e->getMessage();
  55. $this->error("错误提示:" . $msg);
  56. }
  57. $this->success('操作成功', (string) url('index?cid=' . $params['cid']));
  58. } else {
  59. if ($id) {
  60. $data = ArticleModel::find($id);
  61. } else {
  62. $data = new ArticleModel();
  63. }
  64. $categories = CategoryModel::field('id, parent_id, name')->select();
  65. View::assign('category_tree', list_tree($categories));
  66. View::assign('data', $data);
  67. $template = $content_type ? 'savemd' : 'save';
  68. return View::fetch($template);
  69. }
  70. }
  71. }