Postbackreferral.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\model\Referral;
  5. use think\Controller;
  6. use think\Request;
  7. /**
  8. * 导粉推广文案
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Postbackreferral extends Backend
  13. {
  14. /**
  15. * @var Referral
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = model('referral');
  22. $ruleId = $this->request->get('rule_id');
  23. $this->assignconfig('rule_id', $ruleId);
  24. $action = $this->request->get('action', 'relation');
  25. $this->assignconfig('action', $action);
  26. $this->view->assign('action', $action);
  27. $type = $this->request->get('type', 'tout');
  28. $this->assignconfig('type', $type);
  29. }
  30. /**
  31. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  32. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  33. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  34. */
  35. /**
  36. * 查看
  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();
  50. $ruleId = $this->request->get('rule_id');
  51. $action = $this->request->get('action', 'relation');
  52. $rules = model('postbackrules')->where('id', $ruleId)->find();
  53. $referralIds = explode(',', $rules['referral_ids']);
  54. if ($rules['channel_ids']) {
  55. $channelIds = explode(',', $rules['channel_ids']);
  56. if (!$referralIds) {
  57. array_push($referralIds, 0);
  58. }
  59. $aWhere = [
  60. 'admin_id'=>['in', $channelIds]
  61. ];
  62. if ($action == 'relation') {
  63. if ($rules['referral_ids'] == '*') {
  64. $aWhere['id'] = '-1';
  65. } else {
  66. $aWhere['id'] = ['not in', $referralIds];
  67. }
  68. } else {
  69. if ($rules['referral_ids'] != '*') {
  70. $aWhere['id'] = ['in', $referralIds];
  71. }
  72. }
  73. $total = $this->model
  74. ->where($aWhere)
  75. ->where($where)
  76. ->order($sort, $order)
  77. ->count();
  78. $list = $this->model
  79. ->where($aWhere)
  80. ->where($where)
  81. ->order($sort, $order)
  82. ->limit($offset, $limit)
  83. ->select();
  84. $result = array("total" => $total, "rows" => $list);
  85. } else {
  86. $result = array("total" => 0, "rows" => []);
  87. }
  88. return json($result);
  89. }
  90. return $this->view->fetch();
  91. }
  92. public function batch_relation()
  93. {
  94. $referralIds = $this->request->param('referral_ids');
  95. $rule_id = $this->request->param('rule_id');
  96. $action = $this->request->param('action');
  97. $mRule = model('postbackrules');
  98. $ruleData = $mRule->where(['id' => $rule_id])->find();
  99. $originReferralIds = explode(',', $ruleData['referral_ids']);
  100. $paramReferralIds = explode(',', $referralIds);
  101. //查询有效的推广id
  102. if ($ruleData['channel_ids'] != '*') {
  103. $channelIds = explode(',', $ruleData['channel_ids']);
  104. $existReferralIds = model('referral')->whereIn('admin_id', $channelIds)->whereIn('id', $paramReferralIds)->column('id');
  105. $paramReferralIds = array_intersect($paramReferralIds, $existReferralIds);
  106. }
  107. if ($action == 'add') {
  108. if ($referralIds == '-1') {
  109. $update = ['*'];
  110. } else {
  111. $update = array_unique(array_merge($originReferralIds, $paramReferralIds));;
  112. }
  113. } else {
  114. if ($referralIds == '-1') {
  115. $update = [];
  116. } else {
  117. $update = array_unique(array_diff($originReferralIds, $paramReferralIds));
  118. }
  119. }
  120. $update = array_filter($update, function($value){
  121. return $value;
  122. });
  123. $mRule->update(['referral_ids' => implode(',', $update)], ['id' => $rule_id]);
  124. $this->success();
  125. }
  126. }