123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?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 think\Config;
- use think\Controller;
- use think\Request;
- /**
- *
- *
- * @icon fa fa-circle-o
- */
- class Postbackrules extends Backend
- {
- /**
- * PostbackRules模型对象
- */
- protected $model = null;
- protected $noNeedRight = ['globalconfig'];
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('Postbackrules');
- $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();
- $kl_config = Config::get('site.' . $this->request->get('type', 'tout') . '_report_kl');
- $kl_config = json_decode($kl_config, true);
- foreach ($list as $index => $item) {
- if ($list[$index]['money'] == '-1') {
- $list[$index]['money'] = Config::get('site.' . $item['type'] . '_report_money') . '(全局配置)';
- }
- if ($list[$index]['man_val'] == '-1') {
- $list[$index]['man_val'] = empty($kl_config) ? '' : $kl_config['man_val'] . '(全局配置)';
- }
- if ($list[$index]['kou_val'] == '-1') {
- $list[$index]['kou_val'] = empty($kl_config) ? '' : $kl_config['kou_val'] . '(全局配置)';
- }
- }
- $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");
- if ($params) {
- $params['type'] = $this->request->get('type', 'tout');
- if ($this->dataLimit) {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = basename(str_replace('\\', '/', get_class($this->model)));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
- $this->model->validate($validate);
- }
- $result = $this->model->allowField(true)->save($params);
- if ($result !== false) {
- $this->success();
- } else {
- $this->error($this->model->getError());
- }
- } catch (\think\exception\PDOException $e) {
- $this->error($e->getMessage());
- }
- }
- $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('更新失败');
- }
- }
- /**
- * 全局配置
- * @param string $type
- * @return string
- * @throws \think\Exception
- */
- public function globalconfig($type = 'tout')
- {
- if (!$this->request->isAjax()) {
- $data = model('Config')->whereIn('name', [$type . '_report_money', $type . '_report_kl'])->column('name,value');
- $money = $data[$type . '_report_money'] ?? 0;
- $kl = $data[$type . '_report_kl'] ?? json_encode(['man_val' => '', 'kou_val' => '']);
- $this->view->assign(['money' => $money, 'kl' => json_decode($kl, true)]);
- return $this->view->fetch();
- } else {
- $money = $this->request->request('money', 0);
- $man_val = $this->request->request('man_val', '');
- $kou_val = $this->request->request('kou_val', '');
- $money_field = $type . '_report_money';
- $kl_field = $type . '_report_kl';
- if (model("Config")->where('name', 'eq', $money_field)->find()) {
- model('Config')->update(['value' => $money], ['name' => $money_field]);
- } else {
- $moneyData = [
- 'name' => $money_field,
- 'group' => 'postback',
- 'title' => '金额阈值',
- 'tip' => '金额阈值',
- 'type' => 'string',
- 'value' => $money,
- 'content' => '',
- 'rule' => 'required',
- 'extend' => '',
- ];
- model('Config')->allowField(true)->insertGetId($moneyData);
- }
- if (model("Config")->where('name', 'eq', $kl_field)->find()) {
- model('Config')->update(['value' => json_encode(['man_val' => $man_val, 'kou_val' => $kou_val]),], ['name' => $kl_field]);
- } else {
- $klData = [
- 'name' => $kl_field,
- 'group' => 'postback',
- 'title' => '扣量阈值',
- 'tip' => '扣量阈值',
- 'type' => 'string',
- 'value' => json_encode(['man_val' => $man_val, 'kou_val' => $kou_val]),
- 'content' => '',
- 'rule' => 'required',
- 'extend' => '',
- ];
- model('Config')->allowField(true)->insertGetId($klData);
- }
- Redis::instance()->del('site');
- $this->success();
- }
- }
- }
|