123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use app\common\library\Redis;
- use app\main\constants\PostbackConstants;
- use app\main\service\AdminService;
- use app\main\service\ToutiaoNotifyService;
- use think\Config;
- /**
- *
- *
- * @icon fa fa-circle-o
- */
- class Postbackkladmin extends Backend
- {
- /**
- * @var \app\admin\model\Postbackkladmin
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('Postbackkladmin');
- $this->view->assign("stateList", $this->model->getStateList());
- $this->view->assign("typeList", $this->model->getTypeList());
- $this->view->assign('type', $this->request->get('type', 'tout'));
- $this->assignconfig('type', $this->request->get('type', 'tout'));
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
- * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- */
- public function index()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax())
- {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('pkey_name'))
- {
- return $this->selectpage();
- }
- [$where, $sort, $order, $offset, $limit] = $this->buildparams();
- $where_u = [
- 'type'=>$this->request->get('type', 'tout')
- ];
- $total = $this->model
- ->where($where)
- ->where($where_u)
- ->order($sort, $order)
- ->count();
- $list = $this->model
- ->where($where)
- ->where($where_u)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- $ids = array_column($list, 'admin_id');
- $adminInfo = AdminService::instance()->getAdminModel()->whereIn('id', $ids)->column('id,nickname');
- foreach ($list as $index=>$item) {
- $list[$index]['nickname'] = '';
- if (array_key_exists($item['admin_id'], $adminInfo)) {
- $list[$index]['nickname'] = $adminInfo[$item['admin_id']];
- }
- if ($item['admin_id'] == '-1') {
- $list[$index]['nickname'] = '全部';
- }
- if ($list[$index]['money'] == '-1') {
- $list[$index]['money'] = Config::get('site.' . $item['type'] . '_money').'(全局配置)';
- }
- if ($list[$index]['kl'] == '-1') {
- $list[$index]['kl'] = Config::get('site.' . $item['type'] . '_kl').'(全局配置)';
- }
- }
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- $type = $this->request->get('type', 'tout');
- $time = time();
- $inserts = [];
- if ($params) {
- if ($params['channel'] == '0') {
- $ids[] = '-1';
- } else {
- $ids = explode(',', $params['admin_id']);
- $ids = AdminService::instance()->getAdminConfigModel()->whereIn('admin_id', $ids)->column('admin_id');
- }
- $exists = $this->model->whereIn('admin_id', $ids)->where('type', $type)->column('admin_id');
- $ids = array_diff($ids, $exists);
- if ($ids) {
- foreach ($ids as $id) {
- if ($id == '-1') {
- $inserts[] = [
- 'admin_id' => '-1',
- 'money' => '-1',
- 'kl' => '-1',
- 'updatetime' => $time,
- 'createtime' => $time,
- 'type' => $type,
- 'state' => PostbackConstants::STATE_SHOW,
- ];
- } else {
- $inserts[] = [
- 'admin_id' => $id,
- 'money' => $params['money'],
- 'kl' => $params['kl'],
- 'updatetime' => $time,
- 'createtime' => $time,
- 'type' => $type,
- 'state' => PostbackConstants::STATE_SHOW,
- ];
- }
- }
- }
- if ($inserts) {
- $this->model->insertAll($inserts);
- $this->success("新添加渠道配置:" . implode(',', $ids));
- } else {
- $this->error('该渠道已有配置项,不能重复创建相同渠道', '');
- }
- } else {
- $this->error(__('Parameter %s can not be empty', ''));
- }
- }
- return $this->view->fetch();
- }
- public function changestate($id, $state)
- {
- $row = $this->model->where([
- 'id' => $id,
- 'state' => $state,
- ])->find();
- if (!$row) {
- $this->error('未查询到可更新数据');
- }
- $update = [
- 'updatetime' => time(),
- ];
- if ($state == PostbackConstants::STATE_SHOW) {
- $update['state'] = PostbackConstants::STATE_HIDE;
- } else {
- $update['state'] = PostbackConstants::STATE_SHOW;
- }
- $status = $this->model->update($update, ['id' => $id]);
- if ($status) {
- $this->success();
- } else {
- $this->error('更新失败');
- }
- }
- public function global($type = 'tout')
- {
- if (!$this->request->isAjax()) {
- $data = model('Config')->whereIn('name', [$type.'_money', $type.'_kl'])->column('name,value');
- $money = $data[$type.'_money']??0;
- $kl = $data[$type.'_kl']??0;
- $this->view->assign(['money'=>$money, 'kl'=>$kl]);
- return $this->view->fetch();
- } else {
- $money = $this->request->request('money', 0);
- $kl = $this->request->request('kl', 0);
- $money_field = $type.'_money';
- $kl_field = $type.'_kl';
- model('Config')->update(['value'=>$money],['name'=>$money_field]);
- model('Config')->update(['value'=>$kl],['name'=>$kl_field]);
- Redis::instance()->del('site');
- $this->success();
- }
- }
- /**
- * 删除
- */
- public function del($ids = "")
- {
- if ($ids)
- {
- $admin_ids = $this->model->whereIn('id', $ids)->column('admin_id');
- $this->model->whereIn('id', $ids)->delete();
- model('Postbackklreferral')->where('type', $this->request->get('type'))->whereIn('admin_id', $admin_ids)->delete();
- $this->success();
- }
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- }
|