model = model('BookListCh'); $this->vipAdminBindModel = model('VipAdminBind'); $this->view->assign("statusList", $this->model->getStatusList()); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法 * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index() { //设置过滤方法 $this->request->filter(['strip_tags']); $limitShow = $this->group == 3 ? true : false; //是否控制显示链接 if ($this->group == 4 && !empty($this->auth->agent_id)) { //判断是否是配号代理 $limitShow = true; } if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('pkey_name')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $maps = []; //判断角色 $maps['admin_id'] = $this->auth->id; if (in_array($this->group, [7, 8])) { //vip 或 vip运营只显示自己 $maps['admin_id'] = $this->auth->id; } else { //找账号绑定信息 $bindModel = $this->vipAdminBindModel->where('admin_id_slave', 'eq', $this->auth->id)->select(); if ($bindModel) { $vipAdminIds = array_column($bindModel, 'admin_id_master'); $vipAdminIds[] = $this->auth->id; $maps['admin_id'] = ['in', $vipAdminIds]; } } $total = $this->model ->where($where) ->where($maps) ->order($sort, $order) ->count(); $list = $this->model ->where($where) ->where($maps) ->order($sort, $order) ->limit($offset, $limit) ->select(); if ($limitShow) { if (!empty($list)) { foreach ($list as &$row) { $row['self'] = $row['admin_id'] == $this->auth->id ? 1: 0; $url = '/index/index/booklistch?id='.$row['id']; $row['url'] = getCurrentDomain($this->getCurrentAccountChannelId(), $url); } } } $result = array("total" => $total, "rows" => $list); return json($result); } $this->assignconfig('group', $this->group); $this->assignconfig('limitShow', $limitShow); return $this->view->fetch(); } /** * 添加 */ public function add() { if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { $params['admin_id'] = $this->auth->id; /* * 已经弃用,如果为了兼容老版可取消注释 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); } $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 ->where('id', 'eq', $ids) ->where('admin_id', 'eq', $this->auth->id) ->find(); 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); } $result = $row->allowField(true)->save($params); if ($result !== false) { $this->model->rmCache($row['id']); $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); $this->assignconfig('row', $row); return $this->view->fetch(); } /** * 删除 */ public function del($ids = "") { if ($ids) { $pk = $this->model->getPk(); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { $count = $this->model->where($this->dataLimitField, 'in', $adminIds); } $list = $this->model->where($pk, 'in', $ids)->select(); $count = 0; foreach ($list as $k => $v) { $this->model->rmCache($v['id']); $count += $v->delete(); } if ($count) { $this->success(); } else { $this->error(__('No rows were deleted')); } } $this->error(__('Parameter %s can not be empty', 'ids')); } /** * 清除缓存 */ public function clearCache($ids = ''){ if($ids){ $this->model->rmCache($ids); $this->success('清除成功'); } $this->error(__('Parameter %s can not be empty', 'ids')); } }