Guideagent.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use think\Controller;
  6. use think\Request;
  7. /**
  8. * 倒粉域名与渠道关联管理
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Guideagent extends Backend
  13. {
  14. /**
  15. * GuideRelation模型对象
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = model('GuideRelation');
  22. }
  23. /**
  24. * 查看
  25. *
  26. * @author liues@dianzhong.com
  27. * @date 2018-08-16 11:44:18
  28. * @return html|json
  29. */
  30. public function index()
  31. {
  32. //设置过滤方法
  33. $this->request->filter(['strip_tags']);
  34. if ($this->request->isAjax())
  35. {
  36. //如果发送的来源是Selectpage,则转发到Selectpage
  37. if ($this->request->request('pkey_name'))
  38. {
  39. return $this->selectpage();
  40. }
  41. list($where, $sort, $order, $offset, $limit) = $this->buildparams(null, true);
  42. $total = $this->model->alias('gr')
  43. ->join('admin a','a.id=gr.admin_id', 'LEFT')
  44. ->join('admin_extend ae','ae.admin_id=a.id', "LEFT")
  45. ->join('auth_group_access ag','ag.uid= a.id', "LEFT")
  46. ->join('admin a2','a2.id=ae.create_by', "LEFT")
  47. ->join('guide_domain gd','gd.id=gr.domain_id', 'LEFT')
  48. ->where(['ag.group_id' => 4])
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->group('gr.admin_id')
  52. ->count();
  53. $fields = implode(',', [
  54. 'gr.*',
  55. 'GROUP_CONCAT(gr.id) AS id',
  56. 'a.username as username',
  57. 'a.nickname as nickname',
  58. 'ae.create_by',
  59. 'a2.username as create_username',
  60. 'a2.nickname as create_nickname',
  61. 'GROUP_CONCAT(gd.id) AS hostid_concat',
  62. 'GROUP_CONCAT(gd.host) AS host_concat',
  63. 'GROUP_CONCAT(FROM_UNIXTIME(gr.createtime)) AS createtime_concat',
  64. 'GROUP_CONCAT(FROM_UNIXTIME(gr.updatetime)) AS updatetime_concat'
  65. ]);
  66. $list = $this->model->alias('gr')
  67. ->field($fields)
  68. ->join('admin a','a.id=gr.admin_id', 'LEFT')
  69. ->join('admin_extend ae','ae.admin_id=a.id', "LEFT")
  70. ->join('auth_group_access ag','ag.uid= a.id', "LEFT")
  71. ->join('admin a2','a2.id=ae.create_by', "LEFT")
  72. ->join('guide_domain gd','gd.id=gr.domain_id', 'LEFT')
  73. ->where(['ag.group_id' => 4])
  74. ->where($where)
  75. ->order($sort, $order)
  76. ->group('gr.admin_id')
  77. ->limit($offset, $limit)
  78. ->select();
  79. $result = array("total" => $total, "rows" => $list);
  80. return json($result);
  81. }
  82. return $this->view->fetch();
  83. }
  84. /**
  85. * 添加/批量添加
  86. *
  87. * @author liues@dianzhong.com
  88. * @date 2018-08-16 13:30:34
  89. * @return html|json
  90. */
  91. public function add()
  92. {
  93. if ($this->request->isPost())
  94. {
  95. $hostIds = array_filter($this->request->post('host_ids/a'));
  96. $channelIds = array_filter($this->request->post('channel_ids/a'));
  97. if(!empty($hostIds) && !empty($channelIds))
  98. {
  99. $params = array();
  100. foreach ($hostIds as $hostId)
  101. {
  102. foreach($channelIds as $channelId)
  103. {
  104. $exists = model('GuideRelation')->where(['admin_id'=>$channelId,'domain_id'=>$hostId])->find();
  105. if(empty($exists))
  106. {
  107. $params[] = ['admin_id' => $channelId, 'domain_id' => $hostId];
  108. }
  109. }
  110. }
  111. try
  112. {
  113. //是否采用模型验证
  114. if ($this->modelValidate)
  115. {
  116. $name = basename(str_replace('\\', '/', get_class($this->model)));
  117. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  118. $this->model->validate($validate);
  119. }
  120. $result = $this->model->allowField(true)->saveAll($params);
  121. if ($result !== false)
  122. {
  123. $this->success();
  124. }
  125. else
  126. {
  127. $this->error($this->model->getError());
  128. }
  129. }
  130. catch (\think\exception\PDOException $e)
  131. {
  132. $this->error($e->getMessage());
  133. }
  134. }
  135. $this->error('请选择相应的关联关系');
  136. }
  137. else
  138. {
  139. $rows = $this->request->get('rows/a');
  140. //域名列表
  141. $where = ['status'=>'1'];
  142. if($rows['host']){
  143. $where['host'] = ['LIKE', sprintf('%%%s%%', $rows['host'])];
  144. }
  145. $hosts = model('GuideDomain')
  146. ->field('id,host,status')
  147. ->where($where)
  148. ->order('id', 'desc')
  149. ->select();
  150. $this->assign('hosts', $hosts);
  151. //渠道商列表
  152. $where = ['admin.status'=>'normal','ae.distribute'=>'1','ag.group_id' => 4];
  153. if($rows['channel_id']){
  154. $where['admin.id'] = ['=', $rows['channel_id']];
  155. }
  156. if($rows['channel_username']){
  157. $where['admin.username'] = ['LIKE', sprintf('%%%s%%', $rows['channel_username'])];
  158. }
  159. if($rows['channel_nickname']){
  160. $where['admin.nickname'] = ['LIKE', sprintf('%%%s%%', $rows['channel_nickname'])];
  161. }
  162. if($rows['create_id']){
  163. $where['ae.create_by'] = ['=', $rows['create_id']];
  164. }
  165. if($rows['create_username']){
  166. $where['a2.username'] = ['LIKE', sprintf('%%%s%%', $rows['create_username'])];
  167. }
  168. if($rows['create_nickname']){
  169. $where['a2.nickname'] = ['LIKE', sprintf('%%%s%%', $rows['create_nickname'])];
  170. }
  171. $channels = model('Admin')
  172. ->field('admin.id,admin.username,admin.nickname,admin.status,ae.create_by,a2.username as create_username,a2.nickname as create_nickname')
  173. ->join('admin_extend ae','ae.admin_id=admin.id', "LEFT")
  174. ->join('admin a2','a2.id=ae.create_by', "LEFT")
  175. ->join('auth_group_access ag','ag.uid= admin.id', "LEFT")
  176. ->where($where) //3渠道商
  177. ->order('admin.id', 'desc')
  178. ->select();
  179. $this->assign('channels', $channels);
  180. $this->assign('rows', $rows);
  181. return $this->view->fetch();
  182. }
  183. }
  184. /**
  185. * 编辑
  186. *
  187. * @author liues@dianzhong.com
  188. * @date 2018-08-16 13:30:53
  189. * @return html|json
  190. */
  191. public function edit($ids = NULL)
  192. {
  193. $this->error('请删除对应项,重新绑定');
  194. }
  195. /**
  196. * 删除
  197. */
  198. public function del($ids = "")
  199. {
  200. if ($ids)
  201. {
  202. $pk = $this->model->getPk();
  203. // $adminIds = $this->getDataLimitAdminIds();
  204. // if (is_array($adminIds))
  205. // {
  206. // $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  207. // }
  208. $list = $this->model->where($pk, 'in', $ids)->select();
  209. $count = 0;
  210. // 组合一个admin_id为key的二维数组,value为domain_id一维数组
  211. $guideRelationAdmin = [];
  212. foreach ($list as $k => $v) {
  213. $guideRelationAdmin[$v['admin_id']][] = $v['domain_id'];
  214. }
  215. foreach ($guideRelationAdmin as $k => $v) {
  216. if (!model('GuideRelation')->where(['admin_id' => $k])->where('domain_id', 'not in', $v)->find()) { //除此短链外,没有任何其他短链了
  217. model('AdminConfig')->where(['admin_id' => $k])->update(['guide_domain' => '0']);
  218. model('AdminConfig')->delAdminInfoAllCache($k);
  219. }
  220. }
  221. foreach ($list as $k => $v)
  222. {
  223. $count += $v->delete();
  224. }
  225. if ($count)
  226. {
  227. $this->success();
  228. }
  229. else
  230. {
  231. $this->error(__('No rows were deleted'));
  232. }
  233. }
  234. $this->error(__('Parameter %s can not be empty', 'ids'));
  235. }
  236. }