Postbackklreferral.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\main\constants\PostbackConstants;
  5. use app\main\service\AdminService;
  6. use app\main\service\ReportKlService;
  7. use app\main\service\ToutiaoNotifyService;
  8. use think\Controller;
  9. use think\Request;
  10. /**
  11. *
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Postbackklreferral extends Backend
  16. {
  17. /**
  18. * @var \app\admin\model\Postbackklreferral
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = model('Postbackklreferral');
  25. $this->view->assign("stateList", $this->model->getStateList());
  26. $this->view->assign('rule_id', $this->request->get('rule_id'));
  27. $this->view->assign('type', $this->request->get('type'));
  28. $this->assignconfig('rule_id', $this->request->get('rule_id'));
  29. $this->assignconfig('type', $this->request->get('type'));
  30. }
  31. /**
  32. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  33. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  34. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  35. */
  36. /**
  37. * 查看
  38. */
  39. public function index()
  40. {
  41. //设置过滤方法
  42. $this->request->filter(['strip_tags']);
  43. if ($this->request->isAjax()) {
  44. //如果发送的来源是Selectpage,则转发到Selectpage
  45. if ($this->request->request('pkey_name')) {
  46. return $this->selectpage();
  47. }
  48. $where_u = [
  49. 'type' => $this->request->get('type'),
  50. 'rule_id' => $this->request->get('rule_id'),
  51. ];
  52. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  53. $total = $this->model
  54. ->where($where_u)
  55. ->where($where)
  56. ->order($sort, $order)
  57. ->count();
  58. $list = $this->model
  59. ->where($where_u)
  60. ->where($where)
  61. ->order($sort, $order)
  62. ->limit($offset, $limit)
  63. ->select();
  64. foreach ($list as $index => $item) {
  65. if ($item['money'] == '-1') {
  66. $list[$index]['money'] = ReportKlService::instance()->getValue($item['rule_id'], $item['type'], 'money')->data;
  67. if ($list[$index]['money'] == '-1') {
  68. $list[$index]['money'] = '未配置';
  69. } else {
  70. $list[$index]['money'] .= '(渠道配置)';
  71. }
  72. }
  73. if ($item['man_val'] == '-1') {
  74. $list[$index]['man_val'] = ReportKlService::instance()->getValue($item['rule_id'], $item['type'], 'man_val')->data;
  75. if ($list[$index]['man_val'] == '-1') {
  76. $list[$index]['man_val'] = '未配置';
  77. } else {
  78. $list[$index]['man_val'] .= '(渠道配置)';
  79. }
  80. }
  81. if ($item['kou_val'] == '-1') {
  82. $list[$index]['kou_val'] = ReportKlService::instance()->getValue($item['rule_id'], $item['type'], 'kou_val')->data;
  83. if ($list[$index]['kou_val'] == '-1') {
  84. $list[$index]['kou_val'] = '未配置';
  85. } else {
  86. $list[$index]['kou_val'] .= '(渠道配置)';
  87. }
  88. }
  89. }
  90. $result = array("total" => $total, "rows" => $list);
  91. return json($result);
  92. }
  93. return $this->view->fetch();
  94. }
  95. /**
  96. * 添加
  97. */
  98. public function add()
  99. {
  100. if ($this->request->isPost()) {
  101. $params = $this->request->post("row/a");
  102. $params['rule_id'] = $this->request->get("rule_id");
  103. $params['type'] = $this->request->get("type");
  104. $params['referral_id'] = explode(",", $params['referral_id']);
  105. $params['state'] = PostbackConstants::STATE_SHOW;
  106. $params['updatetime'] = $params['createtime'] = time();
  107. $oRule = model('postbackrules')->where('id', $params['rule_id'])->find();
  108. $channel_ids = $oRule['channel_ids'];
  109. if (!$channel_ids) {
  110. $this->error('请先添加渠道');
  111. }
  112. if ($channel_ids != '*') {
  113. $where['admin_id'] = ['in', $channel_ids];
  114. } else {
  115. $where = [];
  116. }
  117. $referralList = model('referral')
  118. ->where($where)
  119. ->whereIn('id', $params['referral_id'])
  120. ->column('id,admin_id');
  121. $exists = $this->model
  122. ->where('type', $params['type'])
  123. ->where('rule_id', $params['rule_id'])
  124. ->whereIn('referral_id', $params['referral_id'])
  125. ->column('referral_id');
  126. $ids = array_diff(array_keys($referralList), $exists);
  127. if ($ids) {
  128. $inserts = [];
  129. foreach ($ids as $id) {
  130. $params['referral_id'] = $id;
  131. $params['admin_id'] = $referralList[$id];
  132. $inserts[] = $params;
  133. }
  134. $this->model->insertAll($inserts);
  135. } else {
  136. $this->error('未查询到需要添加的推广链接');
  137. }
  138. $this->success();
  139. }
  140. return $this->view->fetch();
  141. }
  142. public function changestate($id, $state)
  143. {
  144. $row = $this->model->where([
  145. 'id' => $id,
  146. 'state' => $state,
  147. ])->find();
  148. if (!$row) {
  149. $this->error('未查询到可更新数据');
  150. }
  151. $update = [
  152. 'updatetime' => time(),
  153. ];
  154. if ($state == PostbackConstants::STATE_SHOW) {
  155. $update['state'] = PostbackConstants::STATE_HIDE;
  156. } else {
  157. $update['state'] = PostbackConstants::STATE_SHOW;
  158. }
  159. $status = $this->model->update($update, ['id' => $id]);
  160. if ($status) {
  161. $this->success();
  162. } else {
  163. $this->error('更新失败');
  164. }
  165. }
  166. }