model = model('AwardLists'); $this->view->assign("typeList", $this->model->getTypeList()); $this->view->assign("stateList", $this->model->getStateList()); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法 * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 添加 */ public function add() { if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { /* * 已经弃用,如果为了兼容老版可取消注释 foreach ($params as $k => &$v) { $v = is_array($v) ? implode(',', $v) : $v; } */ if ($this->dataLimit) { $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); } if ($params['type'] == 2){ $params['content'] = str_replace(array("\r\n", "\r", "\n"), ",", $params['book_content']); if (count(explode(',',$params['content'])) >50){ $this->error('添加的书籍ID不能超过50本'); } } $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', '')); } return $this->view->fetch(); } /** * 编辑 */ 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) { /* * 已经弃用,如果为了兼容老版可取消注释 foreach ($params as $k => &$v) { $v = is_array($v) ? implode(',', $v) : $v; } */ try { //是否采用模型验证 if ($this->modelValidate) { $name = basename(str_replace('\\', '/', get_class($this->model))); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate; $row->validate($validate); } if ($params['type'] == 2){ $params['content'] = str_replace(array("\r\n", "\r", "\n"), ",", $params['book_content']); if (count(explode(',',$params['content'])) >50){ $this->error('添加的书籍ID不能超过50本'); } } $result = $row->allowField(true)->save($params); if ($result !== false) { $this->success(); } else { $this->error($row->getError()); } } catch (\think\exception\PDOException $e) { $this->error($e->getMessage()); } } $this->error(__('Parameter %s can not be empty', '')); } $this->view->assign("row", $row); return $this->view->fetch(); } public function ajaxLapse() { $id = $this->request->param('id',0); $state = $this->request->param('state',0); if ( !$id || !$state ){ $this->error('操作失败'); } $state = $state == 2 ? 1 : 2; $obj = $this->model->find($id); $obj->state=$state; $obj->save(); $this->success('操作成功'); } public function show() { //设置过滤方法 $this->request->filter(['strip_tags']); if ($this->request->isAjax()){ list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $total = $this->model ->where($where) ->order($sort, $order) ->count(); $list = $this->model ->where($where) ->order($sort, $order) ->limit($offset, $limit) ->select(); $result = array("total" => $total, "rows" => $list); return json($result); } return $this->view->fetch(); } public function InitCache() { $list = $this->model->select(); if ($list){ $rdList = []; foreach($list as $k => $v){ $temp = json_encode($v); $rdList[] = $temp; } unset($list); $redisKey = ActivityConstants::AWARD_LIST_SET; Redis::instance()->del($redisKey); Redis::instance()->sAddArray($redisKey,$rdList); } $this->success('操作成功'); } }