| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use fast\Tree;
- /**
- * 商品-分类管理
- *
- * @icon fa fa-circle-o
- */
- class GoodsCategory extends Backend
- {
- /**
- * GoodsCategory模型对象
- * @var \app\admin\model\GoodsCategory
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\GoodsCategory;
- // 必须将结果集转换为数组
- $ruleList = collection($this->model->field('*')->order('id ASC')->select())->toArray();
- foreach ($ruleList as $k => &$v) {
- $v['title'] = __($v['title']);
- }
- Tree::instance()->init($ruleList);
- $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
- $ruledata = [0 => __('None')];
- foreach ($this->rulelist as $k => &$v) {
- $ruledata[$v['id']] = $v['title'];
- }
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- // /**
- // * 查看
- // */
- // public function index()
- // {
- // //当前是否为关联查询
- // $this->relationSearch = false;
- // //设置过滤方法
- // $this->request->filter(['strip_tags', 'trim']);
- // if ($this->request->isAjax()) {
- // //如果发送的来源是Selectpage,则转发到Selectpage
- // if ($this->request->request('keyField')) {
- // return $this->selectpage();
- // }
- // list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- //
- // $list = $this->model
- //
- // ->where($where)
- // ->order($sort, $order)
- // ->paginate($limit);
- //
- // foreach ($list as $row) {
- // $row->visible(['id','title','pid','image','weigh','createtime','updatetime','status_switch']);
- //
- // }
- //
- // $result = array("total" => $list->total(), "rows" => $list->items());
- //
- // return json($result);
- // }
- // return $this->view->fetch();
- // }
- public function index()
- {
- if ($this->request->isAjax()) {
- $list = $this->rulelist;
- $total = count($this->rulelist);
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isAjax()) {
- $param = $this->request->request();
- $data = $param['row'];
- $data['createtime'] = time();
- $data['updatetime'] = time();
- // 添加
- $res = $this->model->insert($data);
- if ($res){
- $this->success();
- }else{
- $this->error();
- }
- }
- // 必须将结果集转换为数组
- // $dataList = collection(Category::order('weigh', 'desc')->where('status', 'normal')->select())->toArray();
- $dataList = collection($this->model->where(['status_switch'=>1])->select())->toArray();
- foreach ($dataList as $k => &$v) {
- $v['pid'] = __($v['pid']);
- }
- unset($v);
- Tree::instance()->init($dataList);
- $this->dataList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
- $data = [0 => __('None')];
- foreach ($this->dataList as $k => &$v) {
- $data[$v['id']] = $v['title'];
- }
- $this->view->assign("ruledata", $data);
- return $this->view->fetch();
- }
- /**
- * 修改
- */
- public function edit($ids = null)
- {
- if ($this->request->isAjax()) {
- $param = $this->request->request();
- $data = $param['row'];
- $data['createtime'] = time();
- $data['updatetime'] = time();
- // 添加
- $res = $this->model->where(['id'=>$ids])->update($data);
- if ($res){
- $this->success();
- }else{
- $this->error();
- }
- }
- // 必须将结果集转换为数组
- // $dataList = collection(Category::order('weigh', 'desc')->where('status', 'normal')->select())->toArray();
- $dataList = collection($this->model->where(['status_switch'=>1])->select())->toArray();
- foreach ($dataList as $k => &$v) {
- $v['pid'] = __($v['pid']);
- }
- unset($v);
- Tree::instance()->init($dataList);
- $this->dataList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
- $data = [0 => __('None')];
- foreach ($this->dataList as $k => &$v) {
- $data[$v['id']] = $v['title'];
- }
- $row = $this->model->get(['id' => $ids]);
- $this->view->assign("row", $row);
- $this->view->assign("ruledata", $data);
- return $this->view->fetch();
- }
- }
|