GoodsCate.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\mall\controller;
  15. use library\Controller;
  16. use library\service\MenuService;
  17. use library\tools\Data;
  18. use think\Db;
  19. /**
  20. * 分类管理
  21. * Class GoodsCate
  22. * @package app\admin\controller
  23. */
  24. class GoodsCate extends Controller
  25. {
  26. /**
  27. * 当前操作数据库
  28. * @var string
  29. */
  30. protected $table = 'GoodsCate';
  31. /**
  32. * 分类管理
  33. * @auth true
  34. * @menu true
  35. * @throws \think\Exception
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. * @throws \think\exception\DbException
  39. * @throws \think\exception\PDOException
  40. */
  41. public function index()
  42. {
  43. $this->title = '分类管理';
  44. $query = $this->_query($this->table)->where('is_deleted',0)->order('id desc ,id asc')->page(false);
  45. }
  46. /**
  47. * 列表数据处理
  48. * @param array $data
  49. */
  50. protected function _index_page_filter(&$data)
  51. {
  52. foreach ($data as &$vo) {
  53. $vo['ids'] = join(',', Data::getArrSubIds($data, $vo['id']));
  54. }
  55. $data = Data::arr2table($data);
  56. }
  57. /**
  58. * 添加分类
  59. * @auth true
  60. * @throws \think\Exception
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. * @throws \think\exception\DbException
  64. * @throws \think\exception\PDOException
  65. */
  66. public function add()
  67. {
  68. $this->_form($this->table, 'form');
  69. }
  70. /**
  71. * 编辑分类
  72. * @auth true
  73. * @throws \think\Exception
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. * @throws \think\exception\DbException
  77. * @throws \think\exception\PDOException
  78. */
  79. public function edit()
  80. {
  81. $this->_form($this->table, 'form');
  82. }
  83. /**
  84. * 表单数据处理
  85. * @param array $vo
  86. * @throws \ReflectionException
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. * @throws \think\exception\DbException
  90. */
  91. protected function _form_filter(&$vo)
  92. {
  93. if ($this->request->isGet()) {
  94. // 读取系统功能节点
  95. $this->nodes = MenuService::instance()->getList();
  96. // 选择自己的上级分类
  97. if (empty($vo['pid']) && $this->request->get('pid', '0')) $vo['pid'] = $this->request->get('pid', '0');
  98. // 列出可选上级分类
  99. $menus = Db::name($this->table)->where(['status' => '1','is_deleted'=>0])->order('sort desc,id asc')->column('id,pid,title,logo');
  100. $this->menus = Data::arr2table(array_merge($menus, [['id' => '0', 'pid' => '-1', 'title' => '顶部分类']]));
  101. if (isset($vo['id'])) foreach ($this->menus as $key => $menu) if ($menu['id'] === $vo['id']) $vo = $menu;
  102. foreach ($this->menus as $key => &$menu) {
  103. if ($menu['spt'] >= 2) unset($this->menus[$key]);
  104. if (isset($vo['spt']) && $vo['spt'] <= $menu['spt']) unset($this->menus[$key]);
  105. }
  106. }
  107. if($this->request->isPost() && in_array($this->request->action(),['add','edit'])){
  108. if(!isset($vo['id']) && $vo['pid']){
  109. $plev = Db::name($this->table)->where('id',$vo['pid'])->value('lev');
  110. $vo['lev'] = $plev + 1;
  111. }else if(isset($vo['id'])){
  112. $pid = Db::name($this->table)->where('id',$vo['id'])->value('pid');
  113. $vo['lev'] = Db::name($this->table)->where('id',$pid)->value('lev') + 1;
  114. }
  115. }
  116. }
  117. /**
  118. * 启用分类
  119. * @auth true
  120. * @throws \think\Exception
  121. * @throws \think\exception\PDOException
  122. */
  123. public function resume()
  124. {
  125. $this->_save($this->table, ['status' => '1']);
  126. }
  127. /**
  128. * 禁用分类
  129. * @auth true
  130. * @throws \think\Exception
  131. * @throws \think\exception\PDOException
  132. */
  133. public function forbid()
  134. {
  135. $this->_save($this->table, ['status' => '0']);
  136. }
  137. /**
  138. * 删除分类
  139. * @auth true
  140. * @throws \think\Exception
  141. * @throws \think\exception\PDOException
  142. */
  143. public function remove()
  144. {
  145. $this->_delete($this->table);
  146. }
  147. }