123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use app\common\library\Redis;
- use app\common\model\Admin;
- use think\Controller;
- use think\Request;
- /**
- * 短链域名与渠道关联管理
- *
- * @icon fa fa-circle-o
- */
- class Shortchannel extends Backend
- {
- /**
- * ShortRelation模型对象
- */
- protected $model = null;
- /**
- * 是否开启Validate验证
- */
- protected $modelValidate = true;
- /**
- * 初始化数据
- */
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('ShortRelation');
- }
- /**
- * 查看
- *
- * @author liues@dianzhong.com
- * @date 2018-08-16 11:44:18
- * @return html|json
- */
- 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(null, true);
- $total = $this->model->alias('sr')
- ->join('admin a','a.id=sr.admin_id', 'LEFT')
- ->join('admin_extend ae','ae.admin_id=a.id', "LEFT")
- ->join('auth_group_access ag','ag.uid= a.id', "LEFT")
- ->join('admin a2','a2.id=ae.create_by', "LEFT")
- ->join('short_domain sd','sd.id=sr.domain_id', 'LEFT')
- ->where(['ag.group_id' => 3])
- ->where($where)
- ->group('sr.admin_id')
- ->count();
- $fields = implode(',', [
- 'sr.*',
- 'GROUP_CONCAT(sr.id) AS id',
- 'a.username as username',
- 'a.nickname as nickname',
- 'ae.create_by',
- 'a2.username as create_username',
- 'a2.nickname as create_nickname',
- 'GROUP_CONCAT(sd.id) AS hostid_concat',
- 'GROUP_CONCAT(sd.host) AS host_concat',
- 'GROUP_CONCAT(FROM_UNIXTIME(sr.createtime)) AS createtime_concat',
- 'GROUP_CONCAT(FROM_UNIXTIME(sr.updatetime)) AS updatetime_concat'
- ]);
- $list = $this->model->alias('sr')
- ->field($fields)
- ->join('admin a','a.id=sr.admin_id', 'LEFT')
- ->join('admin_extend ae','ae.admin_id=a.id', "LEFT")
- ->join('auth_group_access ag','ag.uid= a.id', "LEFT")
- ->join('admin a2','a2.id=ae.create_by', "LEFT")
- ->join('short_domain sd','sd.id=sr.domain_id', 'LEFT')
- ->where(['ag.group_id' => 3])
- ->where($where)
- ->order($sort, $order)
- ->group('sr.admin_id')
- ->limit($offset, $limit)
- ->select();
- //渠道商开户人列表
- $create_list = $offset<=0 ? model('Admin')->getListByGroup(2, [], 'admin.id,admin.username,admin.nickname') : [];
- $result = array("total" => $total, "rows" => $list, "create_list" => $create_list);
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 添加/批量添加
- *
- * @author liues@dianzhong.com
- * @date 2018-08-16 13:30:34
- * @return html|json
- */
- public function add()
- {
- if ($this->request->isPost())
- {
- $hostIds = array_filter($this->request->post('host_ids/a'));
- $channelIds = array_filter($this->request->post('channel_ids/a'));
- if(!empty($hostIds) && !empty($channelIds))
- {
- $params = array();
- foreach ($hostIds as $hostId)
- {
- foreach($channelIds as $channelId)
- {
- $exists = model('ShortRelation')->where(['admin_id'=>$channelId,'domain_id'=>$hostId])->find();
- if(empty($exists))
- {
- $params[] = ['admin_id' => $channelId, 'domain_id' => $hostId];
- }
- }
- }
- 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)->saveAll($params);
- if ($result !== false)
- {
- $this->success();
- }
- else
- {
- $this->error($this->model->getError());
- }
- }
- catch (\think\exception\PDOException $e)
- {
- $this->error($e->getMessage());
- }
- }
- $this->error('请选择相应的关联关系');
- }
- else
- {
- $rows = $this->request->get('rows/a');
- //域名列表
- $where = ['status'=>'1'];
- if($rows['host']){
- $where['host'] = ['LIKE', sprintf('%%%s%%', $rows['host'])];
- }
- $hosts = model('ShortDomain')
- ->field('id,host,status')
- ->where($where)
- ->order('id', 'desc')
- ->select();
- $this->assign('hosts', $hosts);
- //渠道商列表
- $where = ['admin.status'=>'normal','ag.group_id' => 3];
- if(isset($rows['channel_id']) && !empty($rows['channel_id'])){
- $where['admin.id'] = ['=', $rows['channel_id']];
- }
- if(isset($rows['channel_username']) && !empty($rows['channel_username'])){
- $where['admin.username'] = ['LIKE', sprintf('%%%s%%', $rows['channel_username'])];
- }
- if(isset($rows['channel_nickname']) && !empty($rows['channel_nickname'])){
- $where['admin.nickname'] = ['LIKE', sprintf('%%%s%%', $rows['channel_nickname'])];
- }
- if(isset($rows['create_id']) && !empty($rows['create_id'])){
- $where['ae.create_by'] = ['=', $rows['create_id']];
- }
- $channels = model('Admin')
- ->field('admin.id,admin.username,admin.nickname,admin.status,ae.create_by,a2.username as create_username,a2.nickname as create_nickname')
- ->join('admin_extend ae','ae.admin_id=admin.id', "LEFT")
- ->join('admin a2','a2.id=ae.create_by', "LEFT")
- ->join('auth_group_access ag','ag.uid= admin.id', "LEFT")
- ->where($where) //3渠道商
- ->order('admin.id', 'desc')
- ->select();
- $this->assign('channels', $channels);
- //渠道商开户人列表
- $create_list = model('Admin')->getListByGroup(2, [], 'admin.id,admin.username,admin.nickname');
- $this->assign('create_list', $create_list);
- $this->assign('rows', $rows);
- return $this->view->fetch();
- }
- }
- /**
- * 编辑
- *
- * @author liues@dianzhong.com
- * @date 2018-08-16 13:30:53
- * @return html|json
- */
- public function edit($ids = NULL)
- {
- $this->error('请删除对应项,重新绑定');
- }
- /**
- * 删除
- */
- 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;
- // 组合一个admin_id为key的二维数组,value为domain_id一维数组
- $shortRelationAdmin = [];
- foreach ($list as $k => $v) {
- $shortRelationAdmin[$v['admin_id']][] = $v['domain_id'];
- }
- foreach ($shortRelationAdmin as $k => $v) {
- if (!model('ShortRelation')->where(['admin_id' => $k])->where('domain_id', 'not in', $v)->find()) { //除此短链外,没有任何其他短链了
- model('AdminConfig')->where(['admin_id' => $k])->update(['guide_domain' => '0']);
- model('AdminConfig')->delAdminInfoAllCache($k);
- }
- }
- foreach ($list as $k => $v)
- {
- $count += $v->delete();
- }
- if ($count)
- {
- $this->success();
- }
- else
- {
- $this->error(__('No rows were deleted'));
- }
- }
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- }
|