UserService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\MicroApi\Services;
  3. use App\MicroApi\Exceptions\RpcException;
  4. use App\MicroApi\Facades\HttpClient;
  5. use App\MicroApi\Items\TokenItem;
  6. use App\MicroApi\Items\UserItem;
  7. use Illuminate\Support\Facades\Log;
  8. class UserService
  9. {
  10. use DataHandler;
  11. protected $servicePrefix = '/user/userService';
  12. /**
  13. * @param $data
  14. * @return UserItem
  15. * @throws RpcException
  16. */
  17. public function create($data)
  18. {
  19. $path = $this->servicePrefix . '/create';
  20. $user = new UserItem();
  21. if (!empty($data['name'])) {
  22. $user->name = $data['name'];
  23. }
  24. if (!empty($data['email'])) {
  25. $user->email = $data['email'];
  26. }
  27. if (!empty($data['password'])) {
  28. $user->password = $data['password'];
  29. }
  30. $options = ['json' => $user];
  31. try {
  32. $response = HttpClient::post($path, $options);
  33. } catch (\Exception $exception) {
  34. Log::error("MicroApi.UserService.create Call Failed: " . $exception->getMessage());
  35. throw new RpcException("调用远程服务失败");
  36. }
  37. $result = $this->decode($response->getBody()->getContents());
  38. return $result->user;
  39. }
  40. public function getAll()
  41. {
  42. $path = $this->servicePrefix . '/getAll';
  43. try {
  44. $response = HttpClient::get($path);
  45. } catch (\Exception $exception) {
  46. Log::error("MicroApi.UserService.getAll Call Failed: " . $exception->getMessage());
  47. throw new RpcException("调用远程服务失败");
  48. }
  49. $result = $this->decode($response->getBody()->getContents());
  50. return $result->users;
  51. }
  52. public function getById($id)
  53. {
  54. $path = $this->servicePrefix . '/get';
  55. $user = new UserItem();
  56. $user->id = $id;
  57. $options = ['json' => $user];
  58. try {
  59. $response = HttpClient::post($path, $options);
  60. } catch (\Exception $exception) {
  61. Log::error("MicroApi.UserService.getByCondition Call Failed: " . $exception->getMessage());
  62. throw new RpcException("调用远程服务失败");
  63. }
  64. $result = $this->decode($response->getBody()->getContents());
  65. return $result->user;
  66. }
  67. public function auth($credentials)
  68. {
  69. $path = $this->servicePrefix . '/auth';
  70. $user = new UserItem();
  71. if (!empty($credentials['email'])) {
  72. $user->email = $credentials['email'];
  73. }
  74. if (!empty($credentials['password'])) {
  75. $user->password = $credentials['password'];
  76. }
  77. $options = ['json' => $user];
  78. try {
  79. $response = HttpClient::post($path, $options);
  80. } catch (\Exception $exception) {
  81. Log::error("MicroApi.UserService.auth Call Failed: " . $exception->getMessage());
  82. throw new RpcException("调用远程服务失败");
  83. }
  84. $result = $this->decode($response->getBody()->getContents());
  85. return $result->token;
  86. }
  87. public function isAuth($token)
  88. {
  89. $path = $this->servicePrefix . '/validateToken';
  90. $item = new TokenItem();
  91. $item->token = $token;
  92. $options = ['json' => $item];
  93. try {
  94. $response = HttpClient::post($path, $options);
  95. } catch (\Exception $exception) {
  96. Log::error("MicroApi.UserService.auth Call Failed: " . $exception->getMessage());
  97. throw new RpcException("调用远程服务失败");
  98. }
  99. $result = $this->decode($response->getBody()->getContents());
  100. return $result->valid;
  101. }
  102. }