model = model('Notice'); $this->view->assign("showLevelList", $this->model->getShowLevelList()); $this->view->assign("dialogTypeList", $this->model->getDialogTypeList()); $this->view->assign("noticeTypeList", $this->model->getNoticeTypeList()); $this->view->assign("statusList", $this->model->getStatusList()); $this->view->assign("importantTypeList", $this->model->getImportantTypeList()); $groupList = model("AuthGroup")->where("status = 'normal'")->column("id,name"); $this->view->assign("groupsList",$groupList); $this->assignconfig("groupcount",count($groupList)); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法 * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index() { //设置过滤方法 $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('pkey_name')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $notice_type = $this->request->get("notice_type", '1'); $whereArr[] = ['status', '=', 'normal']; $csallarr = [ AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN, AdminConstants::ADMIN_GROUP_ID_ADMIN, AdminConstants::ADMIN_GROUP_ID_OPERATOR, AdminConstants::ADMIN_GROUP_ID_CONFIG_MANAGER ]; if(in_array($this->group,$csallarr)){ $whereArr = []; } $whereArr[] = ['notice_type', '=', $notice_type]; $whereArr = function($query) use ($whereArr) { foreach ($whereArr as $k => $v) { if (is_array($v)) { call_user_func_array([$query, 'where'], $v); } else { $query->where($v); } } }; $list = $this->model ->where($where) ->where($whereArr) ->order($sort, $order) ->select(); foreach ($list as$k => &$v){ $v['groupsname'] = implode(",",model("AuthGroup")->where("id","in",$v['groups'])->column("name")); if(!in_array($this->group,$csallarr) && strpos($v['groups'],"$this->group")===false) unset($list[$k]); } $list = array_values($list); $result = array("total" => count($list), "rows" => array_slice($list,$offset,$limit)); return json($result); } return $this->view->fetch(); } /** * 添加 */ public function add() { if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { $params['groups'] = $params['groups']?implode(",",$params['groups']):''; if(empty($params['groups'])){ $this->error("显示范围必选"); } if (isset($params['pop_num']) && ($params['pop_num']>10 || $params['pop_num']<0) ){ $this->error("弹出次数不在规定范围内"); } 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); } $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', '')); } $this->view->assign("notice_type",$this->request->get("notice_type")); 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) { $params['groups'] = $params['groups']?implode(",",$params['groups']):''; if(empty($params['groups'])){ $this->error("显示范围必选"); } if (isset($params['pop_num']) && ($params['pop_num']>10 || $params['pop_num']<0) ){ $this->error("弹出次数不在规定范围内"); } /* * 已经弃用,如果为了兼容老版可取消注释 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); } $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', '')); } $checkall = 0; if(implode(",",model("AuthGroup")->where("status","normal")->column("id"))==$row['groups']){ $checkall = 1; } $this->view->assign("checkall", $checkall); $this->view->assign("row", $row); return $this->view->fetch(); } /** * 展示 */ public function show($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')); } } $isFirst = 0; // 清除登陆后公告弹窗 if (Session::has('notice_id')) { $isFirst = 1; Session::delete('notice_id'); } // 记录访问log switch ($row->dialog_type) { case 1: //只弹一次 $data = [ 'notice_id' => $row->id, 'admin_id' => $this->auth->id ]; $noticeLog = model('Notice_log'); $res = $noticeLog->where($data)->find(); if (!$res) { $noticeLog->save($data); } break; case 2: //每次登陆 break; case 3: //每天一次 $data = [ 'notice_id' => $row->id, 'admin_id' => $this->auth->id ]; $noticeLog = model('Notice_log'); $res = $noticeLog->where($data)->where('createtime', '>', strtotime(date("Y-m-d")))->find(); if (!$res) { $noticeLog->save($data); } break; case 5: $data = [ 'notice_id' => $row->id, 'admin_id' => $this->auth->id ]; if ($isFirst == 1){ model('Notice_log')->save($data); } break; default: } $this->view->assign("row", $row); return $this->view->fetch(); } }