model = new \app\admin\model\SkillCategory; $this->view->assign("statusList", $this->model->getStatusList()); } /** * 查看分类列表 */ public function index() { $this->request->filter(['strip_tags', 'trim']); if (false === $this->request->isAjax()) { return $this->view->fetch(); } if ($this->request->request('keyField')) { return $this->selectpage(); } [$where, $sort, $order, $offset, $limit] = $this->buildparams(); $list = $this->model ->withCount('skills') ->where($where) ->order($sort, $order) ->paginate($limit); $result = ['total' => $list->total(), 'rows' => $list->items()]; return json($result); } /** * 编辑分类(当分类名称变更时同步更新技能字典冗余分类名) */ public function edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $oldName = (string)$row['name']; if (false === $this->request->isPost()) { $this->view->assign('row', $row); return $this->view->fetch(); } $params = $this->request->post('row/a', []); if (!$params) { $this->error(__('Parameter %s can not be empty', '')); } $params = $this->preExcludeFields($params); $result = $row->allowField(true)->save($params); if ($result !== false) { $newName = isset($params['name']) ? trim((string)$params['name']) : $oldName; if ($newName !== '' && $newName !== $oldName) { \think\Db::name('skill_dict')->where('category_id', (int)$row['id'])->update(['category' => $newName]); } $this->success(); } $this->error($row->getError() ?: __('No rows were updated')); } /** * 删除保护:分类下仍有关联技能时禁止物理删除 */ public function del($ids = null) { if (false === $this->request->isPost()) { $this->error(__('Invalid parameters')); } $ids = $ids ?: $this->request->post('ids'); $idArr = array_values(array_filter(array_map('intval', explode(',', (string)$ids)))); if (!$idArr) { $this->error(__('Parameter %s can not be empty', 'ids')); } $cnt = (int)\think\Db::name('skill_dict')->where('category_id', 'in', $idArr)->count(); if ($cnt > 0) { $this->error('所选分类下仍包含 ' . $cnt . ' 项技能标签,请先迁移或删除下属技能后再操作'); } return parent::del($ids); } }