| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 | <?phpnamespace app\admin\controller\unishop;use app\common\controller\Backend;use fast\Tree;use think\Db;use think\Exception;use think\exception\PDOException;use think\exception\ValidateException;/** * 产品管理 * * @icon fa fa-circle-o */class Product extends Backend{    /**     * 快速搜索时执行查找的字段     */    protected $searchFields = 'title';    /**     * Multi方法可批量修改的字段     */    protected $multiFields = 'switch';    /**     * product模型对象     * @var \app\admin\model\unishop\Product     */    protected $model = null;    public function _initialize()    {        parent::_initialize();        $this->model = new \app\admin\model\unishop\Product;        $servers = \app\admin\model\unishop\Config::getByName('server');        $this->assign('servers',json_decode($servers['value']));    }    /**     * 添加     */    public function add()    {        if ($this->request->isPost()) {            $params = $this->request->post("row/a");            if ($params) {                $params = $this->preExcludeFields($params);                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {                    $params[$this->dataLimitField] = $this->auth->id;                }                $result = false;                Db::startTrans();                try {                    //是否采用模型验证                    if ($this->modelValidate) {                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;                        $this->model->validateFailException(true)->validate($validate);                    }                    // 查看规格有没有添加                    if (!empty($params['use_spec']) && $params['use_spec'] == 1) {                        if (empty($params['specList']) || empty($params['specTableList']) || $params['specList'] == '""' || $params['specTableList'] == '""' || $params['specList'] == '[]' || $params['specTableList'] == '[]') {                            throw new Exception('规格不能为空');                        }                    }                    $params['server'] = implode(',',$params['server']);                    $result = $this->model->allowField(true)->save($params);                    Db::commit();                } catch (ValidateException $e) {                    Db::rollback();                    $this->error($e->getMessage());                } catch (PDOException $e) {                    Db::rollback();                    $this->error($e->getMessage());                } catch (Exception $e) {                    Db::rollback();                    $this->error($e->getMessage());                }                if ($result !== false) {                    $this->success();                } else {                    $this->error(__('No rows were inserted'));                }            }            $this->error(__('Parameter %s can not be empty', ''));        }        $this->view->assign('categoryList', $this->build_category_select('row[category_id]', 'product' , 0));        return $this->view->fetch();    }    /**     * 查看     */    public function index()    {        //设置过滤方法        $this->request->filter(['strip_tags']);        if ($this->request->isAjax()) {            //如果发送的来源是Selectpage,则转发到Selectpage            if ($this->request->request('keyField')) {                return $this->selectpage();            }            list($where, $sort, $order, $offset, $limit) = $this->buildparams();            $total = $this->model                ->where($where)                ->count();            $list = $this->model                ->with([                    'category' => function($query) {                        $query->with('parent');                    },                    'delivery'                ])                ->where($where)                ->order($sort, $order)                ->limit($offset, $limit)                ->select();            $list = collection($list)->toArray();            $result = array("total" => $total, "rows" => $list);            return json($result);        }        return $this->view->fetch();    }    public function selectpage(){        return parent::selectpage();    }    /**     * 编辑     */    public function edit($ids = null)    {        $row = $this->model->get($ids);        if (!$row) {            $this->error(__('No Results were found'));        }        $adminIds = $this->getDataLimitAdminIds();        if (is_array($adminIds)) {            if (!in_array($row[$this->dataLimitField], $adminIds)) {                $this->error(__('You have no permission'));            }        }        if ($this->request->isPost()) {            $params = $this->request->post("row/a");            if ($params) {                $params = $this->preExcludeFields($params);                $result = false;                Db::startTrans();                try {                    //是否采用模型验证                    if ($this->modelValidate) {                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;                        $row->validateFailException(true)->validate($validate);                    }                    // 查看规格有没有添加                    if (!empty($params['use_spec']) && $params['use_spec'] == 1) {                        if (empty($params['specList']) || empty($params['specTableList']) || $params['specList'] == '""' || $params['specTableList'] == '""' || $params['specList'] == '[]' || $params['specTableList'] == '[]') {                            throw new Exception('规格不能为空');                        }                    }                    $params['server'] = implode(',',$params['server']);                    $result = $row->allowField(true)->save($params);                    Db::commit();                } catch (ValidateException $e) {                    Db::rollback();                    $this->error($e->getMessage());                } catch (PDOException $e) {                    Db::rollback();                    $this->error($e->getMessage());                } catch (Exception $e) {                    Db::rollback();                    $this->error($e->getMessage());                }                if ($result !== false) {                    $this->success();                } else {                    $this->error(__('No rows were updated'));                }            }            $this->error(__('Parameter %s can not be empty', ''));        }        $this->view->assign("row", $row);        $this->view->assign('categoryList', $this->build_category_select('row[category_id]', 'product' ,$row->category_id));        return $this->view->fetch();    }    /**     * 生成分类下拉列表框     * @param string $name     * @param string $type     * @param mixed $selected     * @param array $attr     * @param array $header     * @return string     */    protected function build_category_select($name, $type, $selected = null, $attr = [], $header = [])    {        $tree = Tree::instance();        $tree->init(\app\admin\model\unishop\Category::getCategoryArray($type), 'pid');        $categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');        $categorydata = $header ? $header : [];        foreach ($categorylist as $k => $v) {            $categorydata[$v['id']] = $v['name'];        }        $attr = array_merge(['id' => "c-{$name}", 'class' => 'form-control selectpicker'], $attr);        return build_select($name, $categorydata, $selected, $attr);    }    /**     * 选择附件     */    public function select()    {        if ($this->request->isAjax()) {            return $this->index();        }        return $this->view->fetch();    }    /**     * 编辑     */    public function copy($ids = null)    {        $row = $this->model->get($ids);        if (!$row) {            $this->error(__('No Results were found'));        }        $adminIds = $this->getDataLimitAdminIds();        if (is_array($adminIds)) {            if (!in_array($row[$this->dataLimitField], $adminIds)) {                $this->error(__('You have no permission'));            }        }        if ($this->request->isPost()) {            $params = $this->request->post("row/a");            if ($params) {                $params = $this->preExcludeFields($params);                $result = false;                Db::startTrans();                try {                    //是否采用模型验证                    if ($this->modelValidate) {                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;                        $row->validateFailException(true)->validate($validate);                    }                    // 查看规格有没有添加                    if (!empty($params['use_spec']) && $params['use_spec'] == 1) {                        if (empty($params['specList']) || empty($params['specTableList']) || $params['specList'] == '""' || $params['specTableList'] == '""' || $params['specList'] == '[]' || $params['specTableList'] == '[]') {                            throw new Exception('规格不能为空');                        }                    }                    $params['server'] = implode(',',$params['server']);                    $result = $this->model->allowField(true)->save($params);                    Db::commit();                } catch (ValidateException $e) {                    Db::rollback();                    $this->error($e->getMessage());                } catch (PDOException $e) {                    Db::rollback();                    $this->error($e->getMessage());                } catch (Exception $e) {                    Db::rollback();                    $this->error($e->getMessage());                }                if ($result !== false) {                    $this->success();                } else {                    $this->error(__('No rows were updated'));                }            }            $this->error(__('Parameter %s can not be empty', ''));        }        $this->view->assign("row", $row);        $this->view->assign('categoryList', $this->build_category_select('row[category_id]', 'product' ,$row->category_id));        return $this->view->fetch();    }}
 |