Cate.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\info;
  9. use app\allwin\controller\Common;
  10. use app\allwin\model\AllwinInfoCate;
  11. use app\allwin\model\AllwinInfo;
  12. use app\allwin\model\AllwinInfoTpl;
  13. class Cate extends Common{
  14. public function initialize() {
  15. parent::initialize();
  16. $this->assign('pathMaps',[['name'=>'信息分类','url'=>url("info.cate/index")]]);
  17. }
  18. /**
  19. * 列表
  20. */
  21. public function index(){
  22. $view['lists'] = AllwinInfoCate::where(['member_miniapp_id' => $this->member_miniapp_id])->order('sort desc,id desc')->paginate(20);
  23. $view['tabs'] = [
  24. ['name' =>'主题管理','url' =>url('info.cate/index'),'action' => 1],
  25. ['name' =>'模板库','url' =>url('info.tpl/index')],
  26. ['name' =>'推广设置','url' =>url('setting/info')],
  27. ];
  28. return view()->assign($view);
  29. }
  30. /**
  31. * 添加
  32. */
  33. public function add(){
  34. if(request()->isAjax()){
  35. $data = [
  36. 'member_miniapp_id' => $this->member_miniapp_id,
  37. 'title' => $this->request->param('title/s'),
  38. 'name' => $this->request->param('name/s'),
  39. 'sort' => $this->request->param('sort/d'),
  40. 'tpl_id' => $this->request->param('tpl_id/d',0),
  41. 'picture' => $this->request->param('picture/s')
  42. ];
  43. $validate = $this->validate($data,'Cate.news');
  44. if(true !== $validate){
  45. return json(['code'=>0,'msg'=>$validate]);
  46. }
  47. $result = AllwinInfoCate::edit($data);
  48. if($result){
  49. return json(['code'=>200,'url'=>url('info.cate/index'),'msg'=>'操作成功']);
  50. }else{
  51. return json(['code'=>0,'msg'=>'操作失败']);
  52. }
  53. }else{
  54. $view['tpl'] = AllwinInfoTpl::where($this->mini_program)->order('id asc')->select();
  55. return view('add',$view);
  56. }
  57. }
  58. //编辑
  59. public function edit(){
  60. if(request()->isAjax()){
  61. $data = [
  62. 'id' => $this->request->param('id/s'),
  63. 'title' => $this->request->param('title/s'),
  64. 'name' => $this->request->param('name/s'),
  65. 'sort' => $this->request->param('sort/d'),
  66. 'tpl_id' => $this->request->param('tpl_id/d'),
  67. 'picture' => $this->request->param('picture/s'),
  68. ];
  69. $validate = $this->validate($data,'Cate.edit');
  70. if(true !== $validate){
  71. return json(['code'=>0,'msg'=>$validate]);
  72. }
  73. if($data['tpl_id']){
  74. $tpl = AllwinInfoTpl::where($this->mini_program)->where(['id'=>$data['tpl_id']])->field('fields')->find();
  75. $data['fields'] = empty($tpl)?'[]' : $tpl->fields;
  76. }else{
  77. $data['fields'] = '[]';
  78. }
  79. //exit();
  80. $result = AllwinInfoCate::edit($data);
  81. if($result){
  82. return json(['code'=>200,'url'=>url('info.cate/index'),'msg'=>'操作成功']);
  83. }else{
  84. return json(['code'=>0,'msg'=>'操作失败']);
  85. }
  86. }else{
  87. $view['id'] = $this->request->param('id/d',0);
  88. $view['info']= AllwinInfoCate::where($this->mini_program)->where(['id' => $view['id']])->find();
  89. $view['tpl'] = AllwinInfoTpl::where($this->mini_program)->order('id asc')->select();
  90. return view('edit',$view);
  91. }
  92. }
  93. /**
  94. * 排序
  95. */
  96. public function sort(){
  97. if(request()->isAjax()){
  98. $data = [
  99. 'sort' => $this->request->param('sort/d'),
  100. 'id' => $this->request->param('id/d'),
  101. ];
  102. $validate = $this->validate($data,'Cate.sort');
  103. if(true !== $validate){
  104. return json(['code'=>0,'msg'=>$validate]);
  105. }
  106. $result = AllwinInfoCate::update(['sort'=>$data['sort'],'id' => $data['id']]);
  107. if($result){
  108. return enjson(200);
  109. }else{
  110. return enjson(0);
  111. }
  112. }
  113. }
  114. //删除
  115. public function delete(){
  116. $id = $this->request->param('id/d');
  117. $info = AllwinInfo::where(['cate_id' => $id])->count();
  118. if($info){
  119. return enjson(0,'删除失败,栏目中还包含内容');
  120. }
  121. $result = AllwinInfoCate::where(['id'=>$id,'member_miniapp_id' => $this->member_miniapp_id])->delete();
  122. if($result){
  123. return enjson(200);
  124. }else{
  125. return enjson(0);
  126. }
  127. }
  128. }