Newscate.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 栏目管理
  7. */
  8. namespace app\allwin\controller;
  9. use filter\Filter;
  10. class Newscate extends Common{
  11. public function initialize() {
  12. parent::initialize();
  13. $this->assign('pathMaps', [['name'=>'新闻分类','url'=>url("newscate/index")]]);
  14. }
  15. /**
  16. * 列表
  17. */
  18. public function index(){
  19. $view['lists'] = model('NewsCate')->where(['member_miniapp_id' => $this->member_miniapp_id])->order('sort desc,id desc')->paginate(20);
  20. return view()->assign($view);
  21. }
  22. /**
  23. * 添加
  24. */
  25. public function add(){
  26. if(request()->isAjax()){
  27. $data = [
  28. 'member_miniapp_id' => $this->member_miniapp_id,
  29. 'title' => input('post.title/s'),
  30. 'name' => input('post.name/s'),
  31. 'sort' => input('post.sort/d'),
  32. 'picture' => input('post.picture/s')
  33. ];
  34. $validate = $this->validate($data,'Cate.news');
  35. if(true !== $validate){
  36. return json(['code'=>0,'msg'=>$validate]);
  37. }
  38. $result = model('NewsCate')->edit($data);
  39. if($result){
  40. return json(['code'=>200,'url'=>url('newscate/index'),'msg'=>'操作成功']);
  41. }else{
  42. return json(['code'=>0,'msg'=>'操作失败']);
  43. }
  44. }else{
  45. return view();
  46. }
  47. }
  48. //编辑
  49. public function edit(){
  50. if(request()->isAjax()){
  51. $data = [
  52. 'id' => input('post.id/s'),
  53. 'title' => input('post.title/s'),
  54. 'name' => input('post.name/s'),
  55. 'sort' => input('post.sort/d'),
  56. 'picture' => input('post.picture/s'),
  57. ];
  58. $validate = $this->validate($data,'Cate.news');
  59. if(true !== $validate){
  60. return json(['code'=>0,'msg'=>$validate]);
  61. }
  62. $result = model('NewsCate')->edit($data);
  63. if($result){
  64. return json(['code'=>200,'url'=>url('newscate/index'),'msg'=>'操作成功']);
  65. }else{
  66. return json(['code'=>0,'msg'=>'操作失败']);
  67. }
  68. }else{
  69. $id = input('get.id/d');
  70. $info = model('NewsCate')->get($id);
  71. if(!$info){
  72. $this->error("404 NOT FOUND");
  73. }
  74. $view['info'] = $info;
  75. return view('edit',$view);
  76. }
  77. }
  78. /**
  79. * 排序
  80. */
  81. public function sort(){
  82. if(request()->isAjax()){
  83. $data = [
  84. 'sort' => input('post.sort/d'),
  85. 'id' => input('post.id/d'),
  86. ];
  87. $validate = $this->validate($data,'Cate.sort');
  88. if(true !== $validate){
  89. return json(['code'=>0,'msg'=>$validate]);
  90. }
  91. $result = model('NewsCate')->save(['sort'=>$data['sort']],['id' => $data['id']]);
  92. if($result){
  93. return json(['code'=>200,'msg'=>'操作成功']);
  94. }else{
  95. return json(['code'=>0,'msg'=>'操作失败']);
  96. }
  97. }
  98. }
  99. //删除
  100. public function delete(){
  101. $id = input('get.id/d');
  102. $goods = model('News')->get(['cate_id' => $id]);
  103. if($goods){
  104. return json(['code'=>403,'msg'=>'删除失败,栏目中还包含内容']);
  105. }
  106. $result = model('NewsCate')->where(['id'=>$id,'member_miniapp_id' => $this->member_miniapp_id])->delete();
  107. if($result){
  108. return json(['code'=>200,'msg'=>'操作成功']);
  109. }else{
  110. return json(['code'=>403,'msg'=>'删除失败']);
  111. }
  112. }
  113. }