Shortchannel.php 9.1 KB

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