diff --git a/app/controller/admin/Ad.php b/app/controller/admin/Ad.php index 0aa69e71..b6108483 100644 --- a/app/controller/admin/Ad.php +++ b/app/controller/admin/Ad.php @@ -20,7 +20,6 @@ class Ad extends Base { * @title 广告位管理 */ public function index(Adservice $service) { - $list = $service->getAdPlaceList($this->request); $this->data = array( @@ -33,14 +32,13 @@ class Ad extends Base { /** * @title 广告位添加 */ - public function add() { + public function add(Adservice $service) { if ($this->request->isPost()) { - $data = $this->request->post(); - $result = AdPlace::create($data); - if (false !== $result) { - return $this->success("添加成功!"); - } else { - return $this->error('添加失败!'); + try { + $service->createAdPlace($this->request); + return $this->success("添加成功!", url('/admin/ad/index')); + } catch (\think\Exception $e) { + return $this->error($e->getMessage()); } } else { $this->data = array( @@ -53,22 +51,17 @@ class Ad extends Base { /** * @title 广告位编辑 */ - public function edit($id = null) { + public function edit(Adservice $service) { if ($this->request->isPost()) { - $data = $this->request->post(); - $result = AdPlace::update($data, ['id' => $data['id']]); - if ($result) { + try { + $service->updateAdPlace($this->request); return $this->success("修改成功!", url('/admin/ad/index')); - } else { - return $this->error('修改失败!'); + } catch (\think\Exception $e) { + return $this->error($e->getMessage()); } } else { - $info = AdPlace::find($id); - if (!$info) { - return $this->error("非法操作!"); - } $this->data = array( - 'info' => $info, + 'info' => $service->getAdPlaceDetail($this->request), 'keyList' => \app\model\ads\AdPlace::$keyList, ); return $this->fetch('admin/public/edit'); @@ -96,13 +89,10 @@ class Ad extends Base { /** * @title 广告列表 */ - public function lists($id = null) { - $map[] = ['place_id', '=', $id]; - $order = "id desc"; - - $list = AdModel::where($map)->order($order)->paginate($this->request->pageConfig); + public function lists(Adservice $service) { + $list = $service->getAdByPlace($this->request); $this->data = array( - 'id' => $id, + 'id' => $this->request->get('id', 0), 'list' => $list, 'page' => $list->render(), ); diff --git a/app/controller/admin/Index.php b/app/controller/admin/Index.php index 4a1f8f9e..5e04c88e 100755 --- a/app/controller/admin/Index.php +++ b/app/controller/admin/Index.php @@ -106,7 +106,7 @@ class Index extends Base { */ public function clear() { if ($this->request->isPost()) { - $clear = input('post.clear/a', array()); + $clear = $this->request->param('clear', []); foreach ($clear as $key => $value) { if ($value == 'cache') { \think\facade\Cache::clear(); // 清空缓存数据 diff --git a/app/services/AdService.php b/app/services/AdService.php index 64c99e9b..64fc81fa 100644 --- a/app/services/AdService.php +++ b/app/services/AdService.php @@ -26,4 +26,46 @@ class AdService{ return $list; } + + /** + * @title 获取广告位信息 + * + * @param [type] $request + * @return void + */ + public function getAdPlaceDetail($request){ + $id = $request->param('id', 0); + $place = AdPlace::where('id', '=', $id)->findOrEmpty(); + return $place->isEmpty() ? [] : $place; + } + + /** + * @title 创建广告位 + * + * @param [type] $request + * @return void + */ + public function createAdPlace($request){ + $data = $request->post(); + + return AdPlace::create($data); + } + + public function updateAdPlace($request){ + $data = $request->post(); + + return AdPlace::update($data, ['id' => $data['id']]); + } + + public function getAdByPlace($request){ + $param = $request->param(); + $order = "id desc"; + + if(isset($param['id']) && $param['id']){ + $map[] = ['place_id', '=', $param['id']]; + } + + $list = Ad::where($map)->order($order)->paginate($request->pageConfig); + return $list; + } } \ No newline at end of file diff --git a/composer.json b/composer.json index d28e03d2..1cd6749b 100755 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ { "name": "molong", "email": "molong@tensent.cn" - } + } ], "require": { "php": ">=7.1.0", @@ -52,5 +52,6 @@ "@php think service:discover", "@php think vendor:publish" ] - } + }, + "prefer-stable": true }