Item.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 = DataNewsItem::mQuery();
  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 = DataNewsItem::mQuery();
  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. */
  55. public function add()
  56. {
  57. $this->title = '添加文章内容';
  58. DataNewsItem::mForm('form');
  59. }
  60. /**
  61. * 编辑文章内容
  62. * @auth true
  63. */
  64. public function edit()
  65. {
  66. $this->title = '编辑文章内容';
  67. DataNewsItem::mForm('form');
  68. }
  69. /**
  70. * 表单数据处理
  71. * @param array $data
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\DbException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. */
  76. protected function _form_filter(array &$data)
  77. {
  78. if (empty($data['code'])) {
  79. $data['code'] = CodeExtend::uniqidNumber(20, 'A');
  80. }
  81. if ($this->request->isGet()) {
  82. $model = DataNewsMark::mk()->where(['status' => 1, 'deleted' => 0]);
  83. $this->marks = $model->order('sort desc,id desc')->select()->toArray();
  84. $data['mark'] = str2arr($data['mark'] ?? '');
  85. } else {
  86. $data['mark'] = arr2str($data['mark'] ?? []);
  87. }
  88. }
  89. /**
  90. * 表单结果处理
  91. * @param boolean $state
  92. */
  93. protected function _form_result(bool $state)
  94. {
  95. if ($state) {
  96. $this->success('文章内容保存成功!', 'javascript:history.back()');
  97. }
  98. }
  99. /**
  100. * 修改文章状态
  101. * @auth true
  102. */
  103. public function state()
  104. {
  105. DataNewsItem::mSave($this->_vali([
  106. 'status.in:0,1' => '状态值范围异常!',
  107. 'status.require' => '状态值不能为空!',
  108. ]));
  109. }
  110. /**
  111. * 删除文章内容
  112. * @auth true
  113. */
  114. public function remove()
  115. {
  116. DataNewsItem::mDelete();
  117. }
  118. }