Goodscats.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\admin\controller\shop;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. use app\admin\model\Goodscats as CatsModel;
  6. /**
  7. * 商品分类
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Goodscats extends Backend
  12. {
  13. /**
  14. * Goodscats模型对象
  15. * @var \app\admin\model\Goodscats
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Goodscats();
  22. $this->categoryList=CatsModel::getTreeList();
  23. array_unshift($this->categoryList, ['id' => 0, 'name' => '无']);
  24. $this->assign('categoryList', $this->categoryList);
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. //当前是否为关联查询
  37. $this->relationSearch = false;
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags']);
  40. if ($this->request->isAjax())
  41. {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('keyField'))
  44. {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $list = $this->model
  49. ->where($where)
  50. ->order($sort, $order)
  51. // ->limit($offset, $limit)
  52. ->select();
  53. $total=count($list);
  54. $tree=Tree::instance();
  55. $tree->init($list,'parent_id');
  56. $list = $tree->getTreeList($tree->getTreeArray(0));
  57. $result = array("total" => $total, "rows" => $list);
  58. return json($result);
  59. }
  60. return $this->view->fetch();
  61. }
  62. public function select() {
  63. return $this->view->fetch();
  64. }
  65. function add()
  66. {
  67. if ($this->request->isPost()) {
  68. $params = $this->request->post("row/a");
  69. if ($params) {
  70. $params = $this->preExcludeFields($params);
  71. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  72. $params[$this->dataLimitField] = $this->auth->id;
  73. }
  74. if(empty($params['parent_id']))
  75. {
  76. $params['type']=1;
  77. }else{
  78. $type=get_table_column('goods_cats',$params['parent_id'], 'type');
  79. $params['type']=$type+1;
  80. }
  81. $result = $this->model->allowField(true)->save($params);
  82. if ($result !== false) {
  83. $this->success('新增成功');
  84. } else {
  85. $this->error(__('新增失败'));
  86. }
  87. }else{
  88. $this->error('参数不能为空');
  89. }
  90. }
  91. return parent::add();
  92. }
  93. function edit($ids = null)
  94. {
  95. $row = $this->model->get($ids);
  96. if (!$row) {
  97. $this->error(__('No Results were found'));
  98. }
  99. $adminIds = $this->getDataLimitAdminIds();
  100. if (is_array($adminIds)) {
  101. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  102. $this->error(__('You have no permission'));
  103. }
  104. }
  105. if ($this->request->isPost()) {
  106. $params = $this->request->post("row/a");
  107. if ($params) {
  108. $params = $this->preExcludeFields($params);
  109. if(empty($params['parent_id']))
  110. {
  111. $params['type']=1;
  112. }else{
  113. $type=get_table_column('goods_cats',$params['parent_id'], 'type');
  114. $params['type']=$type+1;
  115. }
  116. $result = $row->allowField(true)->save($params);
  117. if ($result !== false) {
  118. $this->success('修改成功');
  119. } else {
  120. $this->error(__('修改失败'));
  121. }
  122. }else{
  123. $this->error('参数不能为空');
  124. }
  125. }
  126. return parent::edit($ids);
  127. }
  128. }