From 652d2060bc156ce1f914545cf348f0c6060f3c3d Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Fri, 16 Sep 2022 21:07:05 +0800 Subject: [PATCH] add forum announcements --- .env.example | 3 ++ app/Http/Controllers/ForumController.php | 63 ++++++++++++++++++++++++ bootstrap/app.php | 1 + config/forum.php | 5 ++ routes/api.php | 7 +++ 5 files changed, 79 insertions(+) create mode 100644 app/Http/Controllers/ForumController.php create mode 100644 config/forum.php diff --git a/.env.example b/.env.example index ec5e413..546743f 100644 --- a/.env.example +++ b/.env.example @@ -31,3 +31,6 @@ MONGO_DB_AUTHENTICATION_DATABASE=admin LARAVELS_LISTEN_IP=127.0.0.0 ALIPAY_APP_ID= + +FORUM_BASEURL=https://forum.laecloud.com + diff --git a/app/Http/Controllers/ForumController.php b/app/Http/Controllers/ForumController.php new file mode 100644 index 0000000..03eec6d --- /dev/null +++ b/app/Http/Controllers/ForumController.php @@ -0,0 +1,63 @@ +baseUrl = config('forum.base_url'); + + $this->http = Http::baseUrl($this->baseUrl . '/api')->throw(); + } + + public function get($url) + { + + try { + $resp = $this->http->get($url)->json()['data']; + } catch (Exception $e) { + return $this->error($e); + } + + return $this->success($resp); + } + + + public function announcements() + { + + $resp = $this->cache(function () { + return $this->get('discussions?filter[tag]=announcements&page[offset]=0&sort=-commentCount'); + }); + + if (isset($resp->original['data'])) { + $resp = $resp->original['data']; + } else { + $resp = []; + } + + return $this->success($resp); + } + + + public function cache(Closure $callback) + { + // 获取调用方法名 + $method = debug_backtrace()[1]['function']; + + + return Cache::remember('forum.func.' . $method . '.user_' . auth()->id(), 60, function () use ($callback) { + return $callback(); + }); + } +} diff --git a/bootstrap/app.php b/bootstrap/app.php index 636fb66..1b51cdc 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -75,6 +75,7 @@ $app->configure('session'); $app->configure('alipay'); $app->configure('drops'); +$app->configure('forum'); /* |-------------------------------------------------------------------------- diff --git a/config/forum.php b/config/forum.php new file mode 100644 index 0000000..b753ecc --- /dev/null +++ b/config/forum.php @@ -0,0 +1,5 @@ + env('FORUM_BASEURL') +]; diff --git a/routes/api.php b/routes/api.php index 67a02dc..6bc8695 100644 --- a/routes/api.php +++ b/routes/api.php @@ -84,6 +84,13 @@ // }); }); + +$router->group(['prefix' => 'forum'], function () use ($router) { + $router->get('/announcements', [ + 'uses' => 'ForumController@announcements' + ]); +}); + $router->group(['prefix' => 'modules/{module}'], function () use ($router) { $controller = 'Remote\ModuleController@call'; $router->get('/{route:.*}/', $controller);