Item.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace app\data\controller\news;
  3. use app\data\model\DataNewsItem;
  4. use app\data\model\DataNewsMark;
  5. use app\data\service\NewsService;
  6. use think\admin\Controller;
  7. use think\admin\extend\CodeExtend;
  8. /**
  9. * 文章内容管理
  10. * Class Item
  11. * @package app\data\controller\news
  12. */
  13. class Item extends Controller
  14. {
  15. /**
  16. * 文章内容管理
  17. * @auth true
  18. * @menu true
  19. * @throws \think\db\exception\DataNotFoundException
  20. * @throws \think\db\exception\DbException
  21. * @throws \think\db\exception\ModelNotFoundException
  22. */
  23. public function index()
  24. {
  25. $this->title = '文章内容管理';
  26. $query = $this->_query(DataNewsItem::class);
  27. $query->like('mark,name')->dateBetween('create_at');
  28. $query->where(['deleted' => 0])->order('sort desc,id desc')->page();
  29. }
  30. /**
  31. * 文章内容选择器
  32. * @login true
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public function select()
  38. {
  39. $query = $this->_query(DataNewsItem::class);
  40. $query->equal('status')->like('name')->dateBetween('create_at');
  41. $query->where(['deleted' => 0])->order('sort desc,id desc')->page();
  42. }
  43. /**
  44. * 列表数据处理
  45. * @param array $data
  46. */
  47. protected function _page_filter(array &$data)
  48. {
  49. NewsService::instance()->buildData($data);
  50. }
  51. /**
  52. * 添加文章内容
  53. * @auth true
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. */
  58. public function add()
  59. {
  60. $this->title = '添加文章内容';
  61. $this->_form(DataNewsItem::class, 'form');
  62. }
  63. /**
  64. * 编辑文章内容
  65. * @auth true
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. */
  70. public function edit()
  71. {
  72. $this->title = '编辑文章内容';
  73. $this->_form(DataNewsItem::class, 'form');
  74. }
  75. /**
  76. * 表单数据处理
  77. * @param array $data
  78. * @throws \think\db\exception\DataNotFoundException
  79. * @throws \think\db\exception\DbException
  80. * @throws \think\db\exception\ModelNotFoundException
  81. */
  82. protected function _form_filter(array &$data)
  83. {
  84. if (empty($data['code'])) {
  85. $data['code'] = CodeExtend::uniqidNumber(20, 'A');
  86. }
  87. if ($this->request->isGet()) {
  88. $model = DataNewsMark::mk()->where(['status' => 1, 'deleted' => 0]);
  89. $this->marks = $model->order('sort desc,id desc')->select()->toArray();
  90. $data['mark'] = str2arr($data['mark'] ?? '');
  91. } else {
  92. $data['mark'] = arr2str($data['mark'] ?? []);
  93. }
  94. }
  95. /**
  96. * 表单结果处理
  97. * @param boolean $state
  98. */
  99. protected function _form_result(bool $state)
  100. {
  101. if ($state) {
  102. $this->success('文章内容保存成功!', 'javascript:history.back()');
  103. }
  104. }
  105. /**
  106. * 修改文章状态
  107. * @auth true
  108. * @throws \think\db\exception\DbException
  109. */
  110. public function state()
  111. {
  112. $this->_save(DataNewsItem::class, $this->_vali([
  113. 'status.in:0,1' => '状态值范围异常!',
  114. 'status.require' => '状态值不能为空!',
  115. ]));
  116. }
  117. /**
  118. * 删除文章内容
  119. * @auth true
  120. * @throws \think\db\exception\DbException
  121. */
  122. public function remove()
  123. {
  124. $this->_delete(DataNewsItem::class);
  125. }
  126. }