123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use think\Controller;
- use think\Request;
- use think\Db;
- /**
- * 渠道独家管理
- *
- * @icon fa fa-circle-o
- */
- class Exclusive extends Backend
- {
- /**
- * @var \app\admin\model\Exclusive
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->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;
- }
- }
|