model = model('AdManage'); $this->assign('bannerPositions', $this->model->getBannerPosition()); $this->assign('screenPositions', $this->model->getScreenPosition()); $this->assign('welfarePositions', $this->model->getWelfarePosition()); $this->assign('adTypes', $this->model->getAdType()); } /** * 默认生成的控制器所继承的父类中有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(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $total = $this->model ->where($where) ->order($sort, $order) ->count(); $list = $this->model ->join('ad_user_group', 'ad_manage.user_group_id = ad_user_group.id', 'left') ->where($where) ->field('ad_manage.*, ad_user_group.group_name, ad_user_group.group_type') ->order('ad_manage.id', $order) ->limit($offset, $limit) ->select(); $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) { /* * 已经弃用,如果为了兼容老版可取消注释 foreach ($params as $k => &$v) { $v = is_array($v) ? implode(',', $v) : $v; } */ 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); } $this->_validateParams($params); $params['show_position'] = implode(',', $params['show_position']); $this->_welfareJsonEncode($params); $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(); } /** * 校验表单 * @param $params * @return bool */ private function _validateParams(&$params) { if ($params['ad_type'] == 2) { $params['show_type'] = 0; // 福利广告 $tar_arr = $this->model->getWelfarePosition(); $params['show_position'] = $roolPosition = array_column($tar_arr, 'id'); $result = array_intersect($roolPosition, $params['show_position']); }elseif ($params['ad_type'] == 1) { $params['show_type'] = 0; // 插屏广告 $tar_arr = $this->model->getScreenPosition(); $roolPosition = array_column($tar_arr, 'id'); $result = array_intersect($roolPosition, $params['show_position']); if (in_array(19, $params['show_position'])) { if ((integer)$params['chapter_start_num'] <= 0) { $this->error('请填写阅读页插屏(整章)开始章节数'); } if ((integer)$params['chapter_step_num'] <= 0) { $this->error('阅读页插屏(整章)间隔章节数'); } } } else { $params['chapter_num'] = 0; // Banner广告 $tar_arr = $this->model->getBannerPosition(); $roolPosition = array_column($tar_arr, 'id'); $result = array_intersect($roolPosition, $params['show_position']); } if (count($result) == 0) { $this->error('请选择展示位置'); return false; } else { $params['show_position'] = $result; } if (!in_array(AdConstants::AD_P_READ_SCREEN, $params['show_position'])) { $params['chapter_num'] = 0; } if(in_array(AdConstants::AD_P_READ_SCREEN, $params['show_position']) && $params['chapter_num'] == 0){ $this->error('勾选阅读器插屏后,请填写间隔章节数'); return false; } } /** * 将福利广告的参数压缩成JSON格式 * @param $params */ private function _welfareJsonEncode(&$params) { $welfare_params = ['float_pop_icon', 'float_pop_chapter_num', 'recharge_pop_icon', 'smart_title', 'smart_deputy_title', 'smart_icon', 'day_receive_num', 'give_kandian']; $welfare_arr = []; foreach ($welfare_params as $k => $item) { $welfare_arr[$item] = $params[$item] ?? ''; unset($params[$item]); } $params['welfare_json'] = json_encode($welfare_arr); } /** * 将福利广告的JSON格式 解析为 数组 * @param $row */ private function _welfareJsonDecode(AdManage &$row) { if($row -> ad_type == 2){ $welfare_arr = json_decode($row->welfare_json, true); foreach ($welfare_arr as $k=>$value){ $row->$k = $value; } } } /** * 编辑 */ public function edit($ids = NULL) { $row = $this->model->get($ids); $this->_welfareJsonDecode($row); // if (!$row) $this->error(__('No Results were found')); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { if (!in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } } if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { /* * 已经弃用,如果为了兼容老版可取消注释 foreach ($params as $k => &$v) { $v = is_array($v) ? implode(',', $v) : $v; } */ try { //是否采用模型验证 if ($this->modelValidate) { $name = basename(str_replace('\\', '/', get_class($this->model))); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate; $row->validate($validate); } $this->_validateParams($params); $params['show_position'] = implode(',', $params['show_position']); $this->_welfareJsonEncode($params); $result = $row->allowField(true)->save($params); if ($result !== false) { // 编辑时,删除缓存 Redis::instance()->del(AdConstants::AD_PLAN . $ids); $this->success(); } else { $this->error($row->getError()); } } catch (\think\exception\PDOException $e) { $this->error($e->getMessage()); } } $this->error(__('Parameter %s can not be empty', '')); } $this->view->assign("row", $row); $this->view->assign('show_position', explode(',', $row->show_position)); return $this->view->fetch(); } /** * 删除 */ public function del($ids = "") { if ($ids) { $pk = $this->model->getPk(); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { $count = $this->model->where($this->dataLimitField, 'in', $adminIds); } $list = $this->model->where($pk, 'in', $ids)->select(); $count = 0; foreach ($list as $k => $v) { $count += $v->delete(); } if ($count) { $this->success(); } else { $this->error(__('No rows were deleted')); } } $this->error(__('Parameter %s can not be empty', 'ids')); } /** * 广告计划关联用户组ID */ public function ajaxSaveUserGroup() { $params = $this->request->param(); $rst = $this->model->where('id', 'eq', $params['ad_id'])->update(['user_group_id' => $params['user_group_id'], 'flag'=> 1]); if ($rst) { $arr = [ 'code' => 0, 'msg' =>'成功' ]; echo json_encode($arr); }else{ $arr = [ 'code' => 1, 'msg' =>'关联用户组出错' ]; echo json_encode($arr); } } /** * Ajax 更新所有的广告计划缓存 */ public function ajaxUpdateCache() { AdPlanService::instance()->refreshAllAd(); $arr = [ 'code' => 0, 'msg' => '成功' ]; echo json_encode($arr); } /** * 批量生效 | 批量失效 */ public function batchValidate() { $params = $this->request->param(); $ids = $params['ids']; $state = $params['state']; if (is_array($ids)) { foreach ($ids as $id) { $result = $this->model->update(['state' => $state], ['id' => $id]); } } else { $result = $this->model->update(['state' => $state], ['id' => $ids]); } if($result){ $this->success('设置成功'); }else{ $this->error('设置失败'); } } }