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(); } } }