model = model('\app\admin\model\stock\Customer'); $customerList = collection($this->model->order('weigh')->select())->toArray(); Tree::instance()->init($customerList); $customer = []; $this->customerList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name'); $primary = array(array('name' => '==请选择==', 'id' => '0')); $result = array_merge_recursive($primary, $this->customerList); foreach ($result as $k => $v) { $customer[$v['id']] = $v['name']; } $this->view->assign('customer', $customer); $this->view->assign("enabledmarkList", $this->model->getEnabledmarkList()); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index() { if ($this->request->isAjax()) { $list = $this->customerList; $total = count($this->customerList); $result = array("total" => $total, "rows" => $list); return json($result); } return $this->view->fetch(); } /** * 添加 */ public function add() { if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $params[$this->dataLimitField] = $this->auth->id; } try { //是否采用模型验证 if ($this->modelValidate) { $name = basename(str_replace('\\', '/', get_class($this->model))); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate; $this->model->validate($validate); } $result = $this->model->allowField(true)->save($params); if ($result !== false) { $this->success(); } else { $this->error($this->model->getError()); } } catch (\think\exception\PDOException $e) { $this->error($e->getMessage()); } } $this->error(__('Parameter %s can not be empty', '')); } $result = $this->model->where('pid', 0)->order('encode desc')->limit(1)->select(); if ($result) { $suffix = (int) ($result[0]['encode']); $suffix ++; } else { $suffix = 0; } $encode = sprintf("%02d", $suffix); $this->view->assign('encode', $encode); return $this->view->fetch(); } /** * 根据父级部门生成部门代码 */ public function madeEncode() { if ($this->request->isPost()) { $id = $this->request->post('id'); if ($id || $id == 0) { try { $result = $this->model->where('pid', $id)->field('Encode')->order('Encode desc')->limit(1)->select(); $Encode = ''; if (count($result) > 0) { $EncodeArr = explode('.', $result[0]['Encode']); $EncodeArr[count($EncodeArr) - 1] = sprintf("%03d", $EncodeArr[count($EncodeArr) - 1] + 1); $Encode = implode('.', $EncodeArr); } else { $result = $this->model->where('id', $id)->field('Encode')->order('Encode desc')->limit(1)->select(); if (count($result) > 0) { $Encode = $result[0]['Encode'] . '.001'; } else { $Encode = '001'; } } return json($Encode); } catch (\think\exception\PDOException $e) { $this->error($e->getMessage()); } } $this->error(__('Parameter %s can not be empty', '')); } $this->error('只接受POST请求', ''); } /** * 获取部门树 */ public function getwdTree() { if ($this->auth->isSuperAdmin()) { $categoryList = collection($this->model->select())->toArray(); } else { $categoryList = collection($this->model->where('id', $this->auth->customerid)->select())->toArray(); $categoryList[0]['pid'] = 0; } Tree::instance()->init($categoryList); $result = Tree::instance()->getwdTreeArray(0, 'name'); return json($result); } }