GoodsCategory.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. /**
  6. * 商品-分类管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class GoodsCategory extends Backend
  11. {
  12. /**
  13. * GoodsCategory模型对象
  14. * @var \app\admin\model\GoodsCategory
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\GoodsCategory;
  21. // 必须将结果集转换为数组
  22. $ruleList = collection($this->model->field('*')->order('id ASC')->select())->toArray();
  23. foreach ($ruleList as $k => &$v) {
  24. $v['title'] = __($v['title']);
  25. }
  26. Tree::instance()->init($ruleList);
  27. $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
  28. $ruledata = [0 => __('None')];
  29. foreach ($this->rulelist as $k => &$v) {
  30. $ruledata[$v['id']] = $v['title'];
  31. }
  32. }
  33. /**
  34. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  35. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  36. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  37. */
  38. // /**
  39. // * 查看
  40. // */
  41. // public function index()
  42. // {
  43. // //当前是否为关联查询
  44. // $this->relationSearch = false;
  45. // //设置过滤方法
  46. // $this->request->filter(['strip_tags', 'trim']);
  47. // if ($this->request->isAjax()) {
  48. // //如果发送的来源是Selectpage,则转发到Selectpage
  49. // if ($this->request->request('keyField')) {
  50. // return $this->selectpage();
  51. // }
  52. // list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  53. //
  54. // $list = $this->model
  55. //
  56. // ->where($where)
  57. // ->order($sort, $order)
  58. // ->paginate($limit);
  59. //
  60. // foreach ($list as $row) {
  61. // $row->visible(['id','title','pid','image','weigh','createtime','updatetime','status_switch']);
  62. //
  63. // }
  64. //
  65. // $result = array("total" => $list->total(), "rows" => $list->items());
  66. //
  67. // return json($result);
  68. // }
  69. // return $this->view->fetch();
  70. // }
  71. public function index()
  72. {
  73. if ($this->request->isAjax()) {
  74. $list = $this->rulelist;
  75. $total = count($this->rulelist);
  76. $result = array("total" => $total, "rows" => $list);
  77. return json($result);
  78. }
  79. return $this->view->fetch();
  80. }
  81. /**
  82. * 添加
  83. */
  84. public function add()
  85. {
  86. if ($this->request->isAjax()) {
  87. $param = $this->request->request();
  88. $data = $param['row'];
  89. $data['createtime'] = time();
  90. $data['updatetime'] = time();
  91. // 添加
  92. $res = $this->model->insert($data);
  93. if ($res){
  94. $this->success();
  95. }else{
  96. $this->error();
  97. }
  98. }
  99. // 必须将结果集转换为数组
  100. // $dataList = collection(Category::order('weigh', 'desc')->where('status', 'normal')->select())->toArray();
  101. $dataList = collection($this->model->where(['status_switch'=>1])->select())->toArray();
  102. foreach ($dataList as $k => &$v) {
  103. $v['pid'] = __($v['pid']);
  104. }
  105. unset($v);
  106. Tree::instance()->init($dataList);
  107. $this->dataList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
  108. $data = [0 => __('None')];
  109. foreach ($this->dataList as $k => &$v) {
  110. $data[$v['id']] = $v['title'];
  111. }
  112. $this->view->assign("ruledata", $data);
  113. return $this->view->fetch();
  114. }
  115. /**
  116. * 修改
  117. */
  118. public function edit($ids = null)
  119. {
  120. if ($this->request->isAjax()) {
  121. $param = $this->request->request();
  122. $data = $param['row'];
  123. $data['createtime'] = time();
  124. $data['updatetime'] = time();
  125. // 添加
  126. $res = $this->model->where(['id'=>$ids])->update($data);
  127. if ($res){
  128. $this->success();
  129. }else{
  130. $this->error();
  131. }
  132. }
  133. // 必须将结果集转换为数组
  134. // $dataList = collection(Category::order('weigh', 'desc')->where('status', 'normal')->select())->toArray();
  135. $dataList = collection($this->model->where(['status_switch'=>1])->select())->toArray();
  136. foreach ($dataList as $k => &$v) {
  137. $v['pid'] = __($v['pid']);
  138. }
  139. unset($v);
  140. Tree::instance()->init($dataList);
  141. $this->dataList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
  142. $data = [0 => __('None')];
  143. foreach ($this->dataList as $k => &$v) {
  144. $data[$v['id']] = $v['title'];
  145. }
  146. $row = $this->model->get(['id' => $ids]);
  147. $this->view->assign("row", $row);
  148. $this->view->assign("ruledata", $data);
  149. return $this->view->fetch();
  150. }
  151. }