first commit
This commit is contained in:
11
app/Rpc/BaseService.php
Executable file
11
app/Rpc/BaseService.php
Executable file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rpc;
|
||||
|
||||
class BaseService
|
||||
{
|
||||
protected function getResponse()
|
||||
{
|
||||
return new RpcResponse();
|
||||
}
|
||||
}
|
27
app/Rpc/CalculatorService.php
Executable file
27
app/Rpc/CalculatorService.php
Executable file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rpc;
|
||||
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\Rpc\Response;
|
||||
use Hyperf\RpcServer\Annotation\RpcService;
|
||||
|
||||
// #[RpcService(name: "xxx-oo", server: "jsonrpc-http", protocol: "jsonrpc-http", publishTo: 'nacos')]
|
||||
class CalculatorService
|
||||
{
|
||||
public function add(int $a, int $b)
|
||||
{
|
||||
// \Hyperf\Logger\LoggerFactory::get('default')->info("Add called with: $a + $b");
|
||||
// 这里是服务方法的具体实现
|
||||
return ['xxxx' => 888];
|
||||
return $a + $b;
|
||||
}
|
||||
|
||||
public function add2(int $a, int $b)
|
||||
{
|
||||
// \Hyperf\Logger\LoggerFactory::get('default')->info("Add called with: $a + $b");
|
||||
// 这里是服务方法的具体实现
|
||||
return ['xxxx' => 888999];
|
||||
return $a + $b;
|
||||
}
|
||||
}
|
41
app/Rpc/RpcResponse.php
Executable file
41
app/Rpc/RpcResponse.php
Executable file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rpc;
|
||||
|
||||
class RpcResponse
|
||||
{
|
||||
public int $code = 0;
|
||||
public string $msg = 'ok';
|
||||
public array $data = [];
|
||||
public array $meta = [];
|
||||
public string $title = '';
|
||||
public array $pageModule = [];
|
||||
|
||||
|
||||
public function setData(array $data)
|
||||
{
|
||||
$this->data = $data;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setCode(int $code)
|
||||
{
|
||||
$this->code = $code;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setMsg(string $message)
|
||||
{
|
||||
$this->msg = $message;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function send()
|
||||
{
|
||||
return [
|
||||
'code' => $this->code,
|
||||
'msg' => $this->msg,
|
||||
'data' => $this->data,
|
||||
];
|
||||
}
|
||||
}
|
19
app/Rpc/v1/IndexService.php
Executable file
19
app/Rpc/v1/IndexService.php
Executable file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rpc\v1;
|
||||
|
||||
use App\FormModel\rpc\v1\RpcIndexModel;
|
||||
use App\Rpc\BaseService;
|
||||
use Hyperf\RpcServer\Annotation\RpcService;
|
||||
|
||||
// #[RpcService(name: "index", server: "jsonrpc-http", protocol: "jsonrpc-http", publishTo: 'nacos')]
|
||||
class IndexService extends BaseService
|
||||
{
|
||||
public function index(): array
|
||||
{
|
||||
$model = new RpcIndexModel();
|
||||
$data = $model->index();
|
||||
$response = $this->getResponse()->setPageModule($data);
|
||||
return $response->send();
|
||||
}
|
||||
}
|
59
app/Rpc/v1/NewsService.php
Executable file
59
app/Rpc/v1/NewsService.php
Executable file
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rpc\v1;
|
||||
|
||||
use App\Model\AppNews;
|
||||
use App\Rpc\BaseService;
|
||||
use Hyperf\RpcServer\Annotation\RpcService;
|
||||
|
||||
#[RpcService(name: "news", server: "jsonrpc-http", protocol: "jsonrpc-http")]
|
||||
class NewsService extends BaseService
|
||||
{
|
||||
/**
|
||||
* 查看单个新闻详情
|
||||
* @url /news/view
|
||||
* @param $id
|
||||
* @return array
|
||||
*/
|
||||
public function view($id): array
|
||||
{
|
||||
$query = AppNews::find($id)->toArray();
|
||||
// 相关文章
|
||||
$query['about'] = AppNews::formatQuery(['created_at'])
|
||||
->select(['title', 'id'])
|
||||
->limit(10)
|
||||
->orderBy('id', 'desc')
|
||||
->get()
|
||||
->toArray();
|
||||
|
||||
// 上一篇文章
|
||||
$query['prevNews'] = AppNews::find($id - 1, ['title', 'id'])?->toArray();
|
||||
// 下一篇文章
|
||||
$query['nextNews'] = AppNews::find($id + 1, ['title', 'id'])?->toArray();
|
||||
|
||||
return $this->getResponse()->setData($query)->setCode(0)->send();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看所有新闻
|
||||
* @url /news/index
|
||||
* @param int $limit
|
||||
* @param int $page
|
||||
* @return array
|
||||
*/
|
||||
public function index(int $limit = 10, int $page = 1): array
|
||||
{
|
||||
$query = AppNews::formatQuery(['created_at'])
|
||||
->select(['id'])
|
||||
->orderBy('id', 'desc');;
|
||||
$pagination = $query->paginate($limit, page: $page);
|
||||
$ids = [];
|
||||
foreach ($pagination->items() as $item) {
|
||||
$ids[] = $item->id;
|
||||
}
|
||||
|
||||
$value = AppNews::query()->whereIn('id', $ids)->orderBy('id', 'desc')->get()->toArray();
|
||||
|
||||
return $this->getResponse()->setData($value)->setCode(0)->send();
|
||||
}
|
||||
}
|
27
app/Rpc/v1/ShowsService.php
Executable file
27
app/Rpc/v1/ShowsService.php
Executable file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rpc\v1;
|
||||
|
||||
use App\FormModel\rpc\v1\RpcIndexModel;
|
||||
use App\Model\AppArticle;
|
||||
use App\Rpc\BaseService;
|
||||
use Hyperf\RpcServer\Annotation\RpcService;
|
||||
|
||||
// #[RpcService(name: "shows", server: "jsonrpc-http", protocol: "jsonrpc-http", publishTo: 'nacos')]
|
||||
class ShowsService extends BaseService
|
||||
{
|
||||
public function view($aid): array
|
||||
{
|
||||
$data = AppArticle::formatQuery(['images', 'title', 'cover', 'brand_name'])
|
||||
->select(['brand', 'aid', 'title', 'cover', 'brand as brand_name', 'images', 'description'])
|
||||
->where('aid', $aid)
|
||||
->first()
|
||||
->toArray();
|
||||
|
||||
$data['more'] = AppArticle::formatQuery(['images', 'title', 'cover', 'brand_name'])
|
||||
->where('brand', '=', $data['brand'])
|
||||
->orderBy('published_at', 'DESC')
|
||||
->limit(25);
|
||||
return $this->getResponse()->setPageModule($data)->send();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user