Xw.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace app\admin\controller;
  3. use app\data\model\DataXw;
  4. use app\data\model\DataXwAd;
  5. use app\data\model\DataXwCategory;
  6. use think\admin\Controller;
  7. /**
  8. * 新闻报道
  9. * Class Xw
  10. * @package app\admin\controller\Xw
  11. */
  12. class Xw extends Controller
  13. {
  14. /**
  15. * 新闻列表
  16. * @auth true
  17. * @menu true
  18. */
  19. public function index(){
  20. $this->title='新闻列表';
  21. DataXw::mQuery()
  22. ->with('category')
  23. ->withCount(['comments','likes'])
  24. ->like('title')->dateBetween('create_time')->layTable();
  25. }
  26. /**
  27. * 新闻添加
  28. * @auth true
  29. * @menu true
  30. */
  31. public function add(){
  32. $this->xw_vali();
  33. $this->assign('category',DataXwCategory::show()->select());
  34. DataXw::mForm('form');
  35. }
  36. /**
  37. * 新闻编辑
  38. * @auth true
  39. * @menu true
  40. */
  41. public function edit(){
  42. $this->xw_vali();
  43. $this->assign('category',DataXwCategory::show()->select());
  44. DataXw::mForm('form');
  45. }
  46. protected function xw_vali(){
  47. if($this->request->isPost()){
  48. $this->_vali([
  49. 'title.max:50'=>'标题过长',
  50. ]);
  51. }
  52. }
  53. /**
  54. * 新闻删除
  55. * @auth true
  56. * @menu true
  57. */
  58. public function remove(){
  59. $ids=$this->request->post('id');
  60. DataXw::whereIn('id',$ids)->select()->each(function ($d){$d->delete();});
  61. $this->success('删除成功');
  62. }
  63. /**
  64. * 新闻分类
  65. * @auth true
  66. * @menu true
  67. */
  68. public function category(){
  69. $this->title='分类';
  70. DataXwCategory::mQuery()
  71. ->like('name')
  72. ->layTable();
  73. }
  74. /**
  75. * 新闻分类添加
  76. * @auth true
  77. * @menu true
  78. */
  79. public function category_add(){
  80. if($this->request->isPost()) {
  81. $this->cate_vali();
  82. }
  83. DataXwCategory::mForm('form_category');
  84. }
  85. protected function cate_vali(){
  86. $this->_vali([
  87. 'name.require'=>'分类名必须',
  88. 'name.max:50'=>'分类名过长',
  89. 'sort.require'=>'排序必须',
  90. 'sort.egt:0'=>'排序必须大于0',
  91. 'sort.integer'=>'排序必须是数字',
  92. ]);
  93. }
  94. /**
  95. * 新闻分类编辑
  96. * @auth true
  97. * @menu true
  98. */
  99. public function category_edit(){
  100. if($this->request->isPost()) {
  101. $this->cate_vali();
  102. }
  103. DataXwCategory::mForm('form_category');
  104. }
  105. /**
  106. * 新闻分类删除
  107. * @auth true
  108. * @menu true
  109. */
  110. public function category_del(){
  111. $id=$this->request->post('id');
  112. $all=DataXwCategory::whereIn('id',$id)->select();
  113. $all->each(function ($c){
  114. $c->delete();
  115. });
  116. $this->success('删除成功');
  117. }
  118. /**
  119. * 新闻分类上下架
  120. * @auth true
  121. * @menu true
  122. */
  123. public function cate_state(){
  124. DataXwCategory::mSave([
  125. 'status'=>$this->request->post('status'),
  126. ]);
  127. }
  128. /**
  129. * 新闻上下架
  130. * @auth true
  131. * @menu true
  132. */
  133. public function state(){
  134. DataXw::mSave([
  135. 'status'=>$this->request->post('status'),
  136. ]);
  137. }
  138. /**
  139. * 新闻广告列表
  140. * @auth true
  141. * @menu true
  142. */
  143. public function ad_index(){
  144. $this->title='广告列表';
  145. DataXwAd::mQuery()->layTable();
  146. }
  147. /**
  148. * 新闻广告添加
  149. * @auth true
  150. * @menu true
  151. */
  152. public function ad_add(){
  153. $this->ad_vali();
  154. $this->assign('category',DataXwCategory::show()->select());
  155. DataXwAd::mForm('ad_form');
  156. }
  157. /**
  158. * 新闻广告编辑
  159. * @auth true
  160. * @menu true
  161. */
  162. public function ad_edit(){
  163. $this->ad_vali();
  164. $this->assign('category',DataXwCategory::show()->select());
  165. DataXwAd::mForm('ad_form');
  166. }
  167. protected function ad_vali(){
  168. if($this->request->isPost()){
  169. $this->_vali([
  170. 'title.max:50'=>'标题过长',
  171. ]);
  172. }
  173. }
  174. /**
  175. * 新闻广告删除
  176. * @auth true
  177. * @menu true
  178. */
  179. public function ad_del(){
  180. $ids=$this->request->post('id');
  181. DataXwAd::whereIn('id',$ids)->select()->each(function ($d){$d->delete();});
  182. $this->success('删除成功');
  183. }
  184. /**
  185. * 新闻广告上下架
  186. * @auth true
  187. * @menu true
  188. */
  189. public function ad_state(){
  190. DataXwAd::mSave([
  191. 'status'=>$this->request->post('status'),
  192. ]);
  193. }
  194. /**
  195. * 表单结果处理
  196. * @param boolean $state
  197. */
  198. protected function _form_result(bool $state)
  199. {
  200. if ($state) {
  201. $this->success('保存成功!',in_array($this->request->action(),['add','edit'])? 'javascript:history.back()':'');
  202. }
  203. }
  204. /**
  205. * 新闻报道设置
  206. * @auth true
  207. * @menu true
  208. */
  209. public function config(){
  210. if($this->request->isGet()){
  211. $this->title='新闻报道设置';
  212. $this->assign('vo',sysconf('config_xw.'));
  213. $this->fetch();
  214. }else{
  215. $data=$this->_vali([
  216. 'split_num.require'=>'间隔数必须',
  217. 'user_avatar.require'=>'头像数必须',
  218. 'user_avatar.url'=>'头像链接有误',
  219. 'username.require'=>'名称必须',
  220. 'split_num.gt:0'=>'间隔数必须大于0',
  221. ]);
  222. sysconf('config_xw',$data);
  223. $this->success('保存成功');
  224. }
  225. }
  226. }