Article.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. /**
  15. * 文章管理
  16. */
  17. class Article extends Base
  18. {
  19. protected $modelName = "Article";
  20. public function index()
  21. {
  22. $categories = CategoryModel::articleCategoryies();
  23. $categories_url = [];
  24. $categories_name = [];
  25. foreach ($categories as $value) {
  26. $categories_url[$value->id] = $value->url;
  27. $categories_name[$value->id] = $value->category_name;
  28. }
  29. $category_tree = obj_tree($categories);
  30. $where = [];
  31. if ($cid = $this->app->request->param('cid')) {
  32. $where['cid'] = $cid;
  33. }
  34. $data = $this->modelName::where($where)->field('id,cid,title,username,hits,sort,create_time')->order(['sort desc', 'id desc'])->paginate();
  35. View::assign('category_tree', $category_tree);
  36. View::assign('categories_url', $categories_url);
  37. View::assign('categories_name', $categories_name);
  38. View::assign('data', $data);
  39. View::assign('cid', $cid);
  40. return View::fetch();
  41. }
  42. public function save($id = 0, $cid = 0)
  43. {
  44. if ($this->app->request->isPost()) {
  45. $params = $this->app->request->param();
  46. if (!$params['cid']) {
  47. $this->error('请选择栏目');
  48. }
  49. if ($params['title'] == '') {
  50. $this->error("标题不能为空");
  51. }
  52. $params['content'] = $params['editorValue'];
  53. unset($params['editorValue']);
  54. try {
  55. if ($params['id'] != 0) {
  56. $this->modelName::update($params);
  57. } else {
  58. $params['userid'] = $this->getSysUser()->userid;
  59. $params['username'] = $this->getSysUser()->username;
  60. unset($params['id']);
  61. $this->modelName::create($params);
  62. }
  63. } catch (\Exception $e) {
  64. $msg = $e->getMessage();
  65. $this->error("错误提示:".$msg);
  66. }
  67. $this->success('操作成功', (string) url('index?cid='.$params['cid']));
  68. } else {
  69. if (!$cid) {
  70. $this->error('请选择栏目');
  71. }
  72. if ($id) {
  73. $data = $this->modelName::find($id);
  74. } else {
  75. $data = null;
  76. }
  77. $category_info = CategoryModel::field('id, parent_id, category_name')->find($cid);
  78. View::assign('cid', $cid);
  79. View::assign('category_info', $category_info);
  80. View::assign('data', $data);
  81. return View::fetch();
  82. }
  83. }
  84. }