model = model('Exclusive'); } /** * 默认生成的控制器所继承的父类中有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(); $filter = $this->request->get("filter", ''); $filter = json_decode($filter, true); $where = []; $wherecnkey = ''; $wherecikey = ''; if ($filter) { foreach ($filter as $k => $v) { if($k=='channel_nickname'){ $wherecnkey = $v; }elseif($k=='channel_id'){ $wherecikey = $v; }else{ $where[$k] = $v; } } } //搜索渠道名称 if(!empty($wherecnkey) || !empty($wherecikey)){ $ren = $this->getChannelIdsByNameAndId($wherecnkey,$wherecikey); if(!empty($ren)){ $wherecn['id'] = ['in',implode(',',$ren)]; }else{ $wherecn['id'] = ['eq','']; } }else{ $wherecn = []; } $total = $this->model ->where($where) ->where($wherecn) ->order($sort, $order) ->count(); $list = $this->model ->where($where) ->where($wherecn) ->order($sort, $order) ->limit($offset, $limit) ->select(); $result = array("total" => $total, "rows" => $list); return json($result); } 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) { $cbwhere['eid'] = ['eq',$v['id']]; model('ExclusiveChannel')->where($cbwhere)->delete(); model('ExclusiveBook')->where($cbwhere)->delete(); $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 bindchannels($ids){ $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $bindids = ''; $bind_ids = $this->get_bind_channel_ids($ids); if(!empty($bind_ids)){ $bindids = implode(',',$bind_ids); } $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(); $filter = $this->request->get("filter", ''); $filter = json_decode($filter, true); $where = []; $wherekey = ''; $whereukey = ''; if ($filter) { foreach ($filter as $k => $v) { if($k=='nickname'){ $wherekey = $v; }elseif($k=='username'){ $whereukey = $v; }else{ $where[$k] = $v; } } } //搜索公众号昵称 if(!empty($wherekey)){ $whereuser['ad.nickname'] = ["like","%{$wherekey}%"]; }else{ $whereuser = []; } //搜索用户名 if(!empty($whereukey)){ $wheresub['ad.username'] = ["like","%{$whereukey}%"]; }else{ $wheresub = []; } $total = model('admin')->alias('ad') ->join('admin_config ac','ad.id=ac.admin_id') ->where($where) ->where($wheresub) ->where($whereuser) ->order($sort, $order) ->count(); $list = model('admin')->alias('ad') ->join('admin_config ac','ad.id=ac.admin_id') ->where($where) ->where($wheresub) ->where($whereuser) ->field(['ad.id,ad.username,ad.nickname']) ->order($sort, $order) ->limit($offset, $limit) ->select(); $result = array("total" => $total, "rows" => $list); return json($result); } $this->view->assign("row", $row); $this->assignconfig("ids", $ids); $this->assignconfig("bindids", $bindids); return $this->view->fetch(); } //查看渠道 public function showchannels($ids){ $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $bindids = ''; $bind_ids = $this->get_bind_channel_ids($ids); if(!empty($bind_ids)){ $bindids = implode(',',$bind_ids); } if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('pkey_name')) { return $this->selectpage(); } $this->model = model('admin'); list($where, $sort, $order, $offset, $limit) = $this->buildparams(null, true); if(empty($bindids)){ $wherebind['admin.id'] = ['eq','']; }else{ $wherebind['admin.id'] = ['in',$bindids]; } $total = model('admin') ->join('admin_config ac','admin.id=ac.admin_id') ->where($where) ->where($wherebind) ->order($sort, $order) ->count(); $list = model('admin') ->join('admin_config ac','admin.id=ac.admin_id') ->where($where) ->where($wherebind) ->field(['admin.id,admin.username,admin.nickname']) ->order($sort, $order) ->limit($offset, $limit) ->select(); $result = array("total" => $total, "rows" => $list); return json($result); } $this->assignconfig("ids", $ids); $this->assignconfig("bindids", $bindids); $this->view->assign("row", $row); return $this->view->fetch(); } //批量关联渠道 public function batch_bind_channels(){ if ($this->request->isPost()) { $params = $this->request->post(); if(empty($params['eid']) || empty($params['ids'])){ $this->error('参数错误!'); } $data = []; foreach($params['ids'] as $id){ $data[] = [ 'eid' => $params['eid'], 'cid' => $id ]; } $rs = model('ExclusiveChannel')->insertAll($data); // $sql = 'INSERT INTO `exclusive_channel`(`eid`,`cid`) VALUES '; // foreach($params['ids'] as $id){ // $sql .= ' ('.$params['eid'].','.$id.'),'; // } // $sql = rtrim($sql,','); // $rs = Db::query($sql); if($rs){ $this->success('操作成功'); }else{ $this->error(json_encode($rs,JSON_UNESCAPED_UNICODE)); } } } //批量取消关联渠道 public function batch_unbind_channels(){ if ($this->request->isPost()) { $params = $this->request->post(); if(empty($params['eid']) || empty($params['ids'])){ $this->error('参数错误!'); } $cwhere['eid'] = ['eq',$params['eid']]; $cwhere['cid'] = ['in',implode(',',$params['ids'])]; $rs = model('ExclusiveChannel')->where($cwhere)->delete(); // $sql = 'DELETE FROM `exclusive_channel` where eid='.$params['eid'].' and cid in ('; // foreach($params['ids'] as $id){ // $sql .= $id.','; // } // $sql = rtrim($sql,','); // $sql .= ')'; // $rs = Db::execute($sql); if($rs){ $this->success('操作成功'); }else{ $this->error(json_encode($rs,JSON_UNESCAPED_UNICODE)); } } } //获取绑定的渠道id private function get_bind_channel_ids($ids){ $rs = model('ExclusiveChannel')->where('eid','eq',$ids)->column('cid'); return $rs; } //获取所有渠道id private function get_all_channel_ids(){ $rs = model('Admin')->column('id'); // $sql = "SELECT ad.id FROM admin as ad INNER JOIN admin_config as ac ON ad.id=ac.admin_id where ac.json!=''"; // $rs = Db::query($sql); return $rs; } //关联全部渠道 public function bind_all_channels(){ if ($this->request->isPost()) { $params = $this->request->post(); if(empty($params['ids'])){ $this->error('参数错误!'); } $all_ids = $this->get_all_channel_ids(); $exist_ids= $this->get_bind_channel_ids($params['ids']); $diff_ids = array_diff($all_ids,$exist_ids); if(empty($diff_ids)){ $this->success('操作成功'); } $data = []; foreach($diff_ids as $id){ $data[] = [ 'eid' => $params['ids'], 'cid' => $id ]; } $rs = model('ExclusiveChannel')->insertAll($data); // $sql = 'INSERT INTO `exclusive_channel`(`eid`,`cid`) VALUES '; // foreach($diff_ids as $id){ // $sql .= ' ('.$params['ids'].','.$id.'),'; // } // $sql = rtrim($sql,','); // $rs = Db::query($sql); if($rs){ $this->success('操作成功'); }else{ $this->error(json_encode($rs,JSON_UNESCAPED_UNICODE)); } } } //取消关联全部渠道 public function unbind_all_channels(){ if ($this->request->isPost()) { $params = $this->request->post(); if(empty($params['ids'])){ $this->error('参数错误!'); } $rs = model('ExclusiveChannel')->where('eid','eq',$params['ids'])->delete(); // $sql = 'DELETE FROM `exclusive_channel` where eid='.$params['ids']; // $rs = Db::execute($sql); if($rs){ $this->success('操作成功'); }else{ $this->error(json_encode($rs,JSON_UNESCAPED_UNICODE)); } } } //关联书籍 public function bindbooks($ids){ $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $bindids = ''; $bind_ids = $this->get_bind_book_ids($ids); if(!empty($bind_ids)){ $bindids = implode(',',$bind_ids); } if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('pkey_name')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $whereb['state'] = ['eq',1]; $whereb['cansee'] = ['eq',1]; $total = model('book') ->where($where) ->where($whereb) ->order($sort, $order) ->count(); $list = model('book') ->where($where) ->where($whereb) ->field(['id,name']) ->order($sort, $order) ->limit($offset, $limit) ->select(); $result = array("total" => $total, "rows" => $list); return json($result); } $this->view->assign("row", $row); $this->assignconfig("ids", $ids); $this->assignconfig("bindids", $bindids); return $this->view->fetch(); } //查看书籍 public function showbooks($ids){ $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $bindids = ''; $bind_ids = $this->get_bind_book_ids($ids); if(!empty($bind_ids)){ $bindids = implode(',',$bind_ids); } if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('pkey_name')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); if(empty($bindids)){ $wherebind['id'] = ['eq','']; }else{ $wherebind['id'] = ['in',$bindids]; } $total = model('book') ->where($where) ->where($wherebind) ->order($sort, $order) ->count(); $list = model('book') ->where($where) ->where($wherebind) ->field(['id,name']) ->order($sort, $order) ->limit($offset, $limit) ->select(); $result = array("total" => $total, "rows" => $list); return json($result); } $this->assignconfig("ids", $ids); $this->assignconfig("bindids", $bindids); $this->view->assign("row", $row); return $this->view->fetch(); } //批量关联书籍 public function batch_bind_books(){ if ($this->request->isPost()) { $params = $this->request->post(); if(empty($params['eid']) || empty($params['ids'])){ $this->error('参数错误!'); } $data = []; foreach($params['ids'] as $id){ $data[] = [ 'eid' => $params['eid'], 'bid' => $id ]; } $rs = model('ExclusiveBook')->insertAll($data); // $sql = 'INSERT INTO `exclusive_book`(`eid`,`bid`) VALUES '; // foreach($params['ids'] as $id){ // $sql .= ' ('.$params['eid'].','.$id.'),'; // } // $sql = rtrim($sql,','); // $rs = Db::query($sql); if($rs){ $this->success('操作成功'); }else{ $this->error(json_encode($rs,JSON_UNESCAPED_UNICODE)); } } } //批量取消书籍 public function batch_unbind_books(){ if ($this->request->isPost()) { $params = $this->request->post(); if(empty($params['eid']) || empty($params['ids'])){ $this->error('参数错误!'); } $bwhere = [ 'eid' => $params['eid'], 'bid' => ['in',implode(',',$params['ids'])] ]; $rs = model('ExclusiveBook')->where($bwhere)->delete(); // $sql = 'DELETE FROM `exclusive_book` where eid='.$params['eid'].' and bid in ('; // foreach($params['ids'] as $id){ // $sql .= $id.','; // } // $sql = rtrim($sql,','); // $sql .= ')'; // $rs = Db::execute($sql); if($rs){ $this->success('操作成功'); }else{ $this->error(json_encode($rs,JSON_UNESCAPED_UNICODE)); } } } //获取绑定的书籍id private function get_bind_book_ids($ids){ $rs = model('ExclusiveBook')->where('eid','eq',$ids)->column('bid'); return $rs; } //取消关联书籍 public function unbind_all_books(){ if ($this->request->isPost()) { $params = $this->request->post(); if(empty($params['ids'])){ $this->error('参数错误!'); } $rs = model('ExclusiveBook')->where('eid','eq',$params['ids'])->delete(); // $sql = 'DELETE FROM `exclusive_book` where eid='.$params['ids']; // $rs = Db::execute($sql); if($rs){ $this->success('操作成功'); }else{ $this->error(json_encode($rs,JSON_UNESCAPED_UNICODE)); } } } //搜索渠道名称 private function getChannelIdsByNameAndId($name, $id){ $time = time(); $ers = []; if(empty($name) && empty($id)){ return $ers; } $cwhere = [ 'e.endtime' => ['>',$time], 'e.status' => ['eq',1] ]; $whereEq = []; $whereIn = []; if(!empty($id)){ $whereEq['ec.cid'] = ['eq', $id]; } if(!empty($name)){ $rs = model('Admin')->where('nickname','LIKE','%'.$name.'%')->column('id'); if(!empty($rs)){ $whereIn['ec.cid'] = ['in',implode(',',$rs)]; } else { return $ers; } } $ers = model('ExclusiveChannel')->alias('ec') ->join('exclusive e',' ec.eid=e.id') ->where($cwhere) ->where($whereEq) ->where($whereIn) ->column('distinct ec.eid'); return $ers; } }