Postbackkladmin.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use app\main\constants\PostbackConstants;
  6. use app\main\service\AdminService;
  7. use app\main\service\ToutiaoNotifyService;
  8. use think\Config;
  9. /**
  10. *
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Postbackkladmin extends Backend
  15. {
  16. /**
  17. * @var \app\admin\model\Postbackkladmin
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = model('Postbackkladmin');
  24. $this->view->assign("stateList", $this->model->getStateList());
  25. $this->view->assign("typeList", $this->model->getTypeList());
  26. $this->view->assign('type', $this->request->get('type', 'tout'));
  27. $this->assignconfig('type', $this->request->get('type', 'tout'));
  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. if ($this->request->isAjax())
  42. {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('pkey_name'))
  45. {
  46. return $this->selectpage();
  47. }
  48. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  49. $where_u = [
  50. 'type'=>$this->request->get('type', 'tout')
  51. ];
  52. $total = $this->model
  53. ->where($where)
  54. ->where($where_u)
  55. ->order($sort, $order)
  56. ->count();
  57. $list = $this->model
  58. ->where($where)
  59. ->where($where_u)
  60. ->order($sort, $order)
  61. ->limit($offset, $limit)
  62. ->select();
  63. $ids = array_column($list, 'admin_id');
  64. $adminInfo = AdminService::instance()->getAdminModel()->whereIn('id', $ids)->column('id,nickname');
  65. foreach ($list as $index=>$item) {
  66. $list[$index]['nickname'] = '';
  67. if (array_key_exists($item['admin_id'], $adminInfo)) {
  68. $list[$index]['nickname'] = $adminInfo[$item['admin_id']];
  69. }
  70. if ($item['admin_id'] == '-1') {
  71. $list[$index]['nickname'] = '全部';
  72. }
  73. if ($list[$index]['money'] == '-1') {
  74. $list[$index]['money'] = Config::get('site.' . $item['type'] . '_money').'(全局配置)';
  75. }
  76. if ($list[$index]['kl'] == '-1') {
  77. $list[$index]['kl'] = Config::get('site.' . $item['type'] . '_kl').'(全局配置)';
  78. }
  79. }
  80. $result = array("total" => $total, "rows" => $list);
  81. return json($result);
  82. }
  83. return $this->view->fetch();
  84. }
  85. /**
  86. * 添加
  87. */
  88. public function add()
  89. {
  90. if ($this->request->isPost()) {
  91. $params = $this->request->post("row/a");
  92. $type = $this->request->get('type', 'tout');
  93. $time = time();
  94. $inserts = [];
  95. if ($params) {
  96. if ($params['channel'] == '0') {
  97. $ids[] = '-1';
  98. } else {
  99. $ids = explode(',', $params['admin_id']);
  100. $ids = AdminService::instance()->getAdminConfigModel()->whereIn('admin_id', $ids)->column('admin_id');
  101. }
  102. $exists = $this->model->whereIn('admin_id', $ids)->where('type', $type)->column('admin_id');
  103. $ids = array_diff($ids, $exists);
  104. if ($ids) {
  105. foreach ($ids as $id) {
  106. if ($id == '-1') {
  107. $inserts[] = [
  108. 'admin_id' => '-1',
  109. 'money' => '-1',
  110. 'kl' => '-1',
  111. 'updatetime' => $time,
  112. 'createtime' => $time,
  113. 'type' => $type,
  114. 'state' => PostbackConstants::STATE_SHOW,
  115. ];
  116. } else {
  117. $inserts[] = [
  118. 'admin_id' => $id,
  119. 'money' => $params['money'],
  120. 'kl' => $params['kl'],
  121. 'updatetime' => $time,
  122. 'createtime' => $time,
  123. 'type' => $type,
  124. 'state' => PostbackConstants::STATE_SHOW,
  125. ];
  126. }
  127. }
  128. }
  129. if ($inserts) {
  130. $this->model->insertAll($inserts);
  131. $this->success("新添加渠道配置:" . implode(',', $ids));
  132. } else {
  133. $this->error('该渠道已有配置项,不能重复创建相同渠道', '');
  134. }
  135. } else {
  136. $this->error(__('Parameter %s can not be empty', ''));
  137. }
  138. }
  139. return $this->view->fetch();
  140. }
  141. public function changestate($id, $state)
  142. {
  143. $row = $this->model->where([
  144. 'id' => $id,
  145. 'state' => $state,
  146. ])->find();
  147. if (!$row) {
  148. $this->error('未查询到可更新数据');
  149. }
  150. $update = [
  151. 'updatetime' => time(),
  152. ];
  153. if ($state == PostbackConstants::STATE_SHOW) {
  154. $update['state'] = PostbackConstants::STATE_HIDE;
  155. } else {
  156. $update['state'] = PostbackConstants::STATE_SHOW;
  157. }
  158. $status = $this->model->update($update, ['id' => $id]);
  159. if ($status) {
  160. $this->success();
  161. } else {
  162. $this->error('更新失败');
  163. }
  164. }
  165. public function global($type = 'tout')
  166. {
  167. if (!$this->request->isAjax()) {
  168. $data = model('Config')->whereIn('name', [$type.'_money', $type.'_kl'])->column('name,value');
  169. $money = $data[$type.'_money']??0;
  170. $kl = $data[$type.'_kl']??0;
  171. $this->view->assign(['money'=>$money, 'kl'=>$kl]);
  172. return $this->view->fetch();
  173. } else {
  174. $money = $this->request->request('money', 0);
  175. $kl = $this->request->request('kl', 0);
  176. $money_field = $type.'_money';
  177. $kl_field = $type.'_kl';
  178. model('Config')->update(['value'=>$money],['name'=>$money_field]);
  179. model('Config')->update(['value'=>$kl],['name'=>$kl_field]);
  180. Redis::instance()->del('site');
  181. $this->success();
  182. }
  183. }
  184. /**
  185. * 删除
  186. */
  187. public function del($ids = "")
  188. {
  189. if ($ids)
  190. {
  191. $admin_ids = $this->model->whereIn('id', $ids)->column('admin_id');
  192. $this->model->whereIn('id', $ids)->delete();
  193. model('Postbackklreferral')->where('type', $this->request->get('type'))->whereIn('admin_id', $admin_ids)->delete();
  194. $this->success();
  195. }
  196. $this->error(__('Parameter %s can not be empty', 'ids'));
  197. }
  198. }