Postbackchannel.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\main\constants\AdminConstants;
  5. use think\Controller;
  6. use think\Request;
  7. use app\common\model\Postbackrules;
  8. /**
  9. * 管理员管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Postbackchannel extends Backend
  14. {
  15. /**
  16. * @var \app\common\model\Admin
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = model('admin');
  23. $ruleId = $this->request->get('rule_id');
  24. $this->assignconfig('rule_id', $ruleId);
  25. $action = $this->request->get('action', 'relation');
  26. $this->assignconfig('action', $action);
  27. $this->view->assign('action', $action);
  28. }
  29. /**
  30. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  31. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  32. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33. */
  34. /**
  35. * 查看
  36. */
  37. public function index()
  38. {
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags']);
  41. $ruleId = $this->request->get('rule_id');
  42. $mPostbackrules = new Postbackrules();
  43. $sChannelIds = $mPostbackrules->where('id', $ruleId)->value('channel_ids');
  44. if ($sChannelIds == '*') {
  45. $this->assignconfig('channels', '*');
  46. $this->view->assign('channels', '*');
  47. } else {
  48. $this->view->assign('channels', '');
  49. }
  50. if ($this->request->isAjax()) {
  51. //如果发送的来源是Selectpage,则转发到Selectpage
  52. if ($this->request->request('pkey_name')) {
  53. return $this->selectpage();
  54. }
  55. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  56. $action = $this->request->get('action', 'relation');
  57. $channelIds = explode(',', $sChannelIds);
  58. if (!$channelIds) {
  59. array_push($channelIds, 0);
  60. }
  61. $aWhere = [
  62. 'aga.group_id' => AdminConstants::ADMIN_GROUP_ID_CHANNEL
  63. ];
  64. $all = 0;
  65. if ($action == 'relation') {
  66. if ($sChannelIds == '*') {
  67. $aWhere['id'] = '-1';
  68. } else {
  69. $aWhere['id'] = ['not in', $channelIds];
  70. }
  71. } else {
  72. if ($sChannelIds != '*') {
  73. $aWhere['id'] = ['in', $channelIds];
  74. } else {
  75. $all = 1;
  76. }
  77. }
  78. $total = $this->model
  79. ->alias('a')
  80. ->where($aWhere)
  81. ->where($where)
  82. ->join('auth_group_access aga', 'aga.uid=a.id')
  83. ->join('admin_extend ae', 'ae.admin_id=a.id')
  84. ->order($sort, $order)
  85. ->count();
  86. $list = $this->model
  87. ->alias('a')
  88. ->where($aWhere)
  89. ->where($where)
  90. ->join('auth_group_access aga', 'aga.uid=a.id')
  91. ->join('admin_extend ae', 'ae.admin_id=a.id')
  92. ->order($sort, $order)
  93. ->limit($offset, $limit)
  94. ->select();
  95. $result = array("total" => $total, "rows" => $list, 'all'=>$all);
  96. return json($result);
  97. }
  98. return $this->view->fetch();
  99. }
  100. /**
  101. * 批量修改渠道
  102. */
  103. public function batch_relation()
  104. {
  105. $channelids = $this->request->param('channel_ids');
  106. $rule_id = $this->request->param('rule_id');
  107. $action = $this->request->param('action');
  108. $mRule = new Postbackrules();
  109. $ruleData = $mRule->where(['id' => $rule_id])->find();
  110. $origin_array = explode(',', $ruleData['channel_ids']);
  111. $admin_ids = explode(',', $channelids);
  112. $updateData = [];
  113. if ($action == 'add') {
  114. //添加全部渠道
  115. if ($channelids == '-1') {
  116. $updateData['channel_ids'] = ['*'];
  117. } else {
  118. $updateData['channel_ids'] = array_unique(array_merge($origin_array, $admin_ids));;
  119. }
  120. } else {
  121. //清空所有渠道
  122. if ($channelids == '-1') {
  123. $updateData = ['channel_ids' => []];
  124. model('Postbackklreferral')
  125. ->where('rule_id', $rule_id)
  126. ->delete();
  127. } else {
  128. $updateData['channel_ids'] = array_unique(array_diff($origin_array, $admin_ids));
  129. model('Postbackklreferral')
  130. ->where('rule_id', $rule_id)
  131. ->whereIn('admin_id', $admin_ids)
  132. ->delete();
  133. }
  134. }
  135. //清空空值
  136. $updateData['channel_ids'] = array_filter($updateData['channel_ids'], function ($value) {
  137. return $value;
  138. });
  139. $updateData['channel_ids'] = implode(',', $updateData['channel_ids']);
  140. $mRule->update($updateData, ['id' => $rule_id]);
  141. $this->success();
  142. }
  143. }