modelClass = new LangModel; } public function index() { if ($this->request->isAjax()) { [$page, $limit, $where, $sort, $order] = $this->buildTableParames(); $count = $this->modelClass->where($where)->count(); $data = $this->modelClass->where($where)->order($sort, $order)->page($page, $limit)->select(); return json(['code' => 0, 'count' => $count, 'data' => $data]); } return $this->fetch(); } public function add() { if ($this->request->isPost()) { $this->token(); $params = $this->request->post('row/a'); if (!$params) { $this->error('参数不能为空'); } Db::startTrans(); try { $this->validate($params, 'app\\admin\\validate\\Lang.add'); if (!empty($params['is_default'])) { Db::name('lang')->where('id', '>', 0)->update(['is_default' => 0]); } $this->modelClass->save($params); Db::commit(); } catch (ValidateException | \Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('新增成功'); } return $this->fetch(); } public function edit() { $id = $this->request->param('id/d'); $row = $this->modelClass->find($id); if (!$row) { $this->error('记录未找到'); } if ($this->request->isPost()) { $this->token(); $params = $this->request->post('row/a'); if (!$params) { $this->error('参数不能为空'); } Db::startTrans(); try { $validate = new \app\admin\validate\Lang(); $validate->scene('edit'); $validate->rule([ 'mark|语言标识' => 'require|regex:^[a-z]{2,20}$|unique:lang,mark,' . $id, 'code|标准语言码' => 'require|regex:^[a-z\\-]{2,20}$|unique:lang,code,' . $id, ]); $validate->check($params); if (!empty($params['is_default'])) { Db::name('lang')->where('id', '<>', $id)->update(['is_default' => 0]); } $row->save($params); Db::commit(); } catch (ValidateException | \Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('编辑成功'); } $this->assign('data', $row); return $this->fetch(); } public function del() { if (!$this->request->isPost()) { $this->error('未知参数'); } $ids = $this->request->param('id/a', []); if (empty($ids)) { $this->error('参数错误'); } $defaultCount = $this->modelClass->where('id', 'in', $ids)->where('is_default', 1)->count(); if ($defaultCount > 0) { $this->error('默认语言禁止删除,请先切换默认语言'); } $this->modelClass->where('id', 'in', $ids)->delete(); $this->success('删除成功'); } public function setDefault() { $id = $this->request->param('id/d'); $row = $this->modelClass->find($id); if (!$row) { $this->error('记录未找到'); } Db::startTrans(); try { Db::name('lang')->where('id', '>', 0)->update(['is_default' => 0]); $row->save(['is_default' => 1]); Db::commit(); } catch (\Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('默认语言设置成功'); } }