完善RPC手动创建消费者类处理问题反馈逻辑

develop
fantasticbin 2 years ago
parent 144caadd3f
commit 0b6f55b429

@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace App\Controller\PunchCard\User;
use App\Controller\BaseController;
use App\JsonRpc\PunchCardSystemExternalServiceInterface;
use App\JsonRpc\UserExternalServiceInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
#[Controller(prefix: "kq")]
class UserController extends BaseController
{
/**
* 用户中心对外RPC服务
*
* @var UserExternalServiceInterface
*/
#[Inject]
protected UserExternalServiceInterface $userExternalService;
/**
* 考勤系统设置对外RPC服务
*
* @var PunchCardSystemExternalServiceInterface
*/
#[Inject]
protected PunchCardSystemExternalServiceInterface $punchCardSystemExternalService;
/**
* 获取个人资料
*
* @return array
*/
#[GetMapping(path: "user/information")]
public function information() : array
{
$openid = $this->request->input('openid', '');
return $this->getServiceResult($this->userExternalService->getUserInfo($openid, [
'user_name',
'user_phone',
]));
}
/**
* 保存个人资料
*
* @return void
*/
#[PostMapping(path: "user/save_information")]
public function saveInformation() : void
{
$phone = $this->request->input('phone', '');
// TODO 逻辑完善
}
/**
* 保存问题反馈
*
* @return array
*/
#[PostMapping(path: "user/save_feedback")]
public function saveFeedback() : array
{
return $this->getServiceResult($this->punchCardSystemExternalService->saveFeedback($this->request));
}
}

@ -74,14 +74,4 @@ class TestController extends BaseController
return new Paginator($users, $perPage, $currentPage);
}
/**
* RPC调用示例
*
* @return array
*/
public function user_info()
{
return $this->getServiceResult($this->userService->getUserInfo());
}
}

@ -0,0 +1,26 @@
<?php
namespace App\JsonRpc;
use Hyperf\RpcClient\AbstractServiceClient;
use Hyperf\HttpServer\Contract\RequestInterface;
class PunchCardSystemExternalServiceConsumer extends AbstractServiceClient implements PunchCardSystemExternalServiceInterface
{
/**
* 定义对应服务提供者的服务名称
* @var string
*/
protected $serviceName = 'PunchCardSystemExternalService';
/**
* 保存问题反馈
*
* @param RequestInterface $request
* @return array
*/
public function saveFeedback(RequestInterface $request) : array
{
return $this->__request(__FUNCTION__, $request->all());
}
}

@ -0,0 +1,16 @@
<?php
namespace App\JsonRpc;
use Hyperf\HttpServer\Contract\RequestInterface;
interface PunchCardSystemExternalServiceInterface
{
/**
* 保存问题反馈
*
* @param RequestInterface $request
* @return array
*/
public function saveFeedback(RequestInterface $request) : array;
}

@ -7,10 +7,11 @@ interface UserExternalServiceInterface
/**
* 获取用户信息
*
* @param string $name
* @param string $openid
* @param array $field
* @return array
*/
public function getUserInfo(string $name = 'test') : array;
public function getUserInfo(string $openid, array $field = []) : array;
/**
* 通过微信授权新建用户

@ -12,10 +12,7 @@ declare(strict_types=1);
namespace App\Model;
use Hyperf\DbConnection\Model\Model as BaseModel;
use Hyperf\ModelCache\Cacheable;
use Hyperf\ModelCache\CacheableInterface;
abstract class Model extends BaseModel implements CacheableInterface
abstract class Model extends BaseModel
{
use Cacheable;
}

@ -14,4 +14,5 @@ use Hyperf\JsonRpc\JsonRpcTransporter;
return [
JsonRpcTransporter::class => JsonRpcPoolTransporter::class,
App\JsonRpc\PunchCardSystemExternalServiceInterface::class => App\JsonRpc\PunchCardSystemExternalServiceConsumer::class,
];

@ -19,6 +19,7 @@ return [
// 这里示例自动创建代理消费者类的配置形式,顾存在 name 和 service 两个配置项,这里的做法不是唯一的,仅说明可以通过 PHP 代码来生成配置
$services = [
'UserExternalService' => App\JsonRpc\UserExternalServiceInterface::class,
'PunchCardSystemExternalService' => ''
];
foreach ($services as $name => $interface) {
$consumers[] = [

Loading…
Cancel
Save