Webmenu.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\AdminBase;
  4. use app\admin\model\Webmenu AS WebmenuModel;
  5. use app\common\util\Tabel;
  6. use app\common\util\Form;
  7. use util\Tree;
  8. class Webmenu extends AdminBase
  9. {
  10. protected $validate = '';
  11. protected function _initialize()
  12. {
  13. parent::_initialize();
  14. $this->model = new WebmenuModel();
  15. }
  16. public function index($type=0)
  17. {
  18. $type = intval($type);
  19. $map = [];
  20. if ($type>0) {
  21. $map = ['type'=>['in',[0,$type]]];
  22. }
  23. $listdb = Tree::config(['title' => 'name'])->toList(
  24. WebmenuModel::where($map)->order('list desc,id desc')->column(true)
  25. );
  26. $tab = [
  27. //['id','ID','text'],
  28. ['style','图标','icon'],
  29. ['title_display','链接名称','text'],
  30. ['type','所属','select2',['头部通用','PC头部菜单','wap头部菜单','wap底部菜单']],
  31. ['list','排序值','text.edit'],
  32. ['ifshow','是否显示','switch'],
  33. ['target','新窗口打开','yesno'],
  34. //['right_button', '操作', 'btn'],
  35. ['right_button', '操作', 'callback',function($value,$rs){
  36. if($rs['pid']>0)$value=str_replace('_tag="add"', 'style="display:none;"', $value);
  37. return $value;
  38. },'__data__'],
  39. ];
  40. $table = Tabel::make($listdb,$tab)
  41. ->addTopButton('add',['title'=>'添加菜单','href'=>url('add',['type'=>$type])])
  42. ->addTopButton('delete')
  43. ->addRightButton('edit')
  44. ->addRightButton('delete')
  45. //->addPageTips('省份管理')
  46. //->addOrder('id,list')
  47. ->addPageTitle('网站菜单管理')
  48. ->addNav( [
  49. 0=>['title' => '所有菜单', 'url' =>url('index',['type'=>'0'])],
  50. 1=>['title' => 'PC头部菜单', 'url' =>url('index',['type'=>1])],
  51. 2=>['title' => 'wap头部菜单', 'url' =>url('index',['type'=>2])],
  52. 3=>['title' => 'wap底部菜单', 'url' =>url('index',['type'=>3])],
  53. ],$type)
  54. ->addRightButton('add',['title'=>'添加下级菜单','href'=>url('add',['pid'=>'__id__'])]);
  55. return $table::fetch();
  56. }
  57. public function add($pid=0,$type=0){
  58. if ($this->request->isPost()) {
  59. $data = $this->request->post();
  60. if (!empty($this -> validate)) { //验证数据
  61. $result = $this -> validate($data, $this -> validate);
  62. if (true !== $result) $this -> error($result);
  63. }
  64. if($data['name']==''){
  65. $this->error('名称不能为空!');
  66. }
  67. if(WebmenuModel::create($data)){
  68. $this->success('创建成功 ', url('index',['type'=>$data['type']]) );
  69. } else {
  70. $this->error('创建失败');
  71. }
  72. }
  73. if($pid){
  74. $info = WebmenuModel::get($pid);
  75. }else{
  76. $info['type'] = intval($type);
  77. }
  78. $array = WebmenuModel::where('type','in',[0,$info['type']])->where('pid',0)->column('id,name');
  79. $form = Form::make()
  80. ->addPageTips('父菜单为PC或WAP的话,子菜单设置通用无效')
  81. ->addSelect('pid','父级菜单','',$array,$pid)
  82. ->addText('name','菜单名称')
  83. ->addText('url','菜单链接')
  84. ->addRadio('type','使用范围','',['头部通用','PC头部菜单','wap头部菜单','wap底部菜单'],0)
  85. ->addRadio('target','是否新窗口打开','',['本窗口打开','新窗口打开'],0)
  86. ->addIcon('style','图标')
  87. ->addText('activate','选中属性[:频道链接填上频道的目录名时,呆在该频道的话,菜单就是选中状态,一般留空]')
  88. ->addPageTitle('添加菜单');
  89. return $form::fetch();
  90. }
  91. public function edit($id=0){
  92. if ($this->request->isPost()) {
  93. $data = $this -> request -> post();
  94. if (!empty($this -> validate)) { //验证数据
  95. $result = $this -> validate($data, $this -> validate);
  96. if (true !== $result) $this -> error($result);
  97. }
  98. if (WebmenuModel::update($data)) {
  99. $this->success('修改成功',url('index',['type'=>$data['type']]));
  100. } else {
  101. $this->error('修改失败');
  102. }
  103. }
  104. $info = WebmenuModel::get($id);
  105. $array = WebmenuModel::where('type','in',[0,$info['type']])->where('pid',0)->column('id,name');
  106. $form = Form::make([],$info)
  107. ->addPageTips('父菜单为PC或WAP的话,子菜单设置通用无效')
  108. ->addPageTitle('修改菜单')
  109. ->addSelect('pid','父级菜单','',$array)
  110. ->addText('name','名称')
  111. ->addText('url','菜单链接')
  112. ->addRadio('type','使用范围','',['头部通用','PC头部菜单','wap头部菜单','wap底部菜单'])
  113. ->addRadio('target','是否新窗口打开','',['本窗口打开','新窗口打开'])
  114. ->addRadio('ifshow','是否隐藏','',['隐藏','显示(不隐藏)'])
  115. ->addNumber('list','排序值')
  116. ->addIcon('style','图标')
  117. ->addText('activate','选中属性[:频道链接填上频道的目录名时,呆在该频道的话,菜单就是选中状态,一般留空]','主页就输入index,商城就输入shop以此类推,不过最好是后面加个-横框线因为栏目的话,可以定义为shop-18')
  118. ->addHidden('id',$id);
  119. return $form->fetch();
  120. }
  121. public function delete($ids){
  122. if (empty($ids)) {
  123. $this -> error('ID有误');
  124. }
  125. $ids = is_array($ids)?$ids:[$ids];
  126. if (WebmenuModel::destroy($ids)) {
  127. $this->success('删除成功','index');
  128. } else {
  129. $this->error('删除失败');
  130. }
  131. }
  132. }