2022-09-16 13:07:05 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-06 11:28:22 +00:00
|
|
|
namespace App\Http\Controllers\Api;
|
2022-09-16 13:07:05 +00:00
|
|
|
|
2022-11-06 11:28:22 +00:00
|
|
|
use App\Http\Controllers\Controller;
|
2022-09-16 13:07:05 +00:00
|
|
|
use Closure;
|
2022-11-06 11:28:22 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
use function config;
|
|
|
|
|
2022-10-28 08:52:29 +00:00
|
|
|
// use Exception;
|
|
|
|
// use Illuminate\Http\Request;
|
2022-09-16 13:07:05 +00:00
|
|
|
|
|
|
|
class ForumController extends Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
private $http, $baseUrl;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->baseUrl = config('forum.base_url');
|
|
|
|
|
|
|
|
$this->http = Http::baseUrl($this->baseUrl . '/api')->throw();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get($url)
|
|
|
|
{
|
2022-09-16 13:36:13 +00:00
|
|
|
$resp = $this->http->get($url)->json()['data'];
|
|
|
|
return $resp;
|
2022-09-16 13:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function announcements()
|
|
|
|
{
|
|
|
|
$resp = $this->cache(function () {
|
2022-09-20 04:13:49 +00:00
|
|
|
return $this->get('discussions?filter[tag]=announcements&page[offset]=0&sort=-createdAt');
|
2022-09-16 13:07:05 +00:00
|
|
|
});
|
|
|
|
|
2022-09-16 13:36:13 +00:00
|
|
|
return $this->resp($resp);
|
2022-09-16 13:07:05 +00:00
|
|
|
}
|
|
|
|
|
2022-09-18 08:21:24 +00:00
|
|
|
public function pinned()
|
|
|
|
{
|
|
|
|
$resp = $this->cache(function () {
|
2022-09-20 04:13:49 +00:00
|
|
|
return $this->get('discussions?filter[tag]=pinned&page[offset]=0&sort=-createdAt');
|
2022-09-18 08:21:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return $this->resp($resp);
|
|
|
|
}
|
|
|
|
|
2022-09-16 13:07:05 +00:00
|
|
|
|
|
|
|
public function cache(Closure $callback)
|
|
|
|
{
|
|
|
|
// 获取调用方法名
|
|
|
|
$method = debug_backtrace()[1]['function'];
|
|
|
|
|
2022-09-18 08:26:20 +00:00
|
|
|
return Cache::remember('forum.func.' . $method, 60, function () use ($callback) {
|
2022-09-16 13:07:05 +00:00
|
|
|
return $callback();
|
|
|
|
});
|
|
|
|
}
|
2022-09-16 13:36:13 +00:00
|
|
|
|
|
|
|
|
2022-09-18 08:21:24 +00:00
|
|
|
public function resp($data)
|
|
|
|
{
|
2022-09-16 13:36:13 +00:00
|
|
|
$data['base_url'] = $this->baseUrl;
|
|
|
|
|
|
|
|
return $this->success($data);
|
|
|
|
}
|
2022-09-16 13:07:05 +00:00
|
|
|
}
|