UserService.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. use App\MicroApi\Items\PasswordResetItem;
  9. class UserService
  10. {
  11. use DataHandler;
  12. protected $servicePrefix = '/user/userService';
  13. /**
  14. * @param $data
  15. * @return UserItem
  16. * @throws RpcException
  17. */
  18. public function create($data)
  19. {
  20. $path = $this->servicePrefix . '/create';
  21. $user = new UserItem();
  22. if (!empty($data['name'])) {
  23. $user->name = $data['name'];
  24. }
  25. if (!empty($data['email'])) {
  26. $user->email = $data['email'];
  27. }
  28. if (!empty($data['password'])) {
  29. $user->password = $data['password'];
  30. }
  31. $options = ['json' => $user];
  32. try {
  33. $response = HttpClient::post($path, $options);
  34. } catch (\Exception $exception) {
  35. Log::error("MicroApi.UserService.create Call Failed: " . $exception->getMessage());
  36. throw new RpcException("调用远程服务失败");
  37. }
  38. $result = $this->decode($response->getBody()->getContents());
  39. return $result->user;
  40. }
  41. public function getAll()
  42. {
  43. $path = $this->servicePrefix . '/getAll';
  44. try {
  45. $response = HttpClient::get($path);
  46. } catch (\Exception $exception) {
  47. Log::error("MicroApi.UserService.getAll Call Failed: " . $exception->getMessage());
  48. throw new RpcException("调用远程服务失败");
  49. }
  50. $result = $this->decode($response->getBody()->getContents());
  51. return $result->users;
  52. }
  53. public function getById($id)
  54. {
  55. $path = $this->servicePrefix . '/get';
  56. $user = new UserItem();
  57. $user->id = $id;
  58. $options = ['json' => $user];
  59. try {
  60. $response = HttpClient::post($path, $options);
  61. } catch (\Exception $exception) {
  62. Log::error("MicroApi.UserService.getByCondition Call Failed: " . $exception->getMessage());
  63. throw new RpcException("调用远程服务失败");
  64. }
  65. $result = $this->decode($response->getBody()->getContents());
  66. return $result->user;
  67. }
  68. public function auth($credentials)
  69. {
  70. $path = $this->servicePrefix . '/auth';
  71. $user = new UserItem();
  72. if (!empty($credentials['email'])) {
  73. $user->email = $credentials['email'];
  74. }
  75. if (!empty($credentials['password'])) {
  76. $user->password = $credentials['password'];
  77. }
  78. $options = ['json' => $user];
  79. try {
  80. $response = HttpClient::post($path, $options);
  81. } catch (\Exception $exception) {
  82. Log::error("MicroApi.UserService.auth Call Failed: " . $exception->getMessage());
  83. throw new RpcException("调用远程服务失败");
  84. }
  85. $result = $this->decode($response->getBody()->getContents());
  86. return $result->token;
  87. }
  88. public function isAuth($token)
  89. {
  90. $path = $this->servicePrefix . '/validateToken';
  91. $item = new TokenItem();
  92. $item->token = $token;
  93. $options = ['json' => $item];
  94. try {
  95. $response = HttpClient::post($path, $options);
  96. } catch (\Exception $exception) {
  97. Log::error("MicroApi.UserService.auth Call Failed: " . $exception->getMessage());
  98. throw new RpcException("调用远程服务失败");
  99. }
  100. $result = $this->decode($response->getBody()->getContents());
  101. return $result->valid;
  102. }
  103. public function getByEmail($email)
  104. {
  105. $path = $this->servicePrefix . '/get';
  106. $user = new UserItem();
  107. $user->email = $email;
  108. $options = ['json' => $user];
  109. try {
  110. $response = HttpClient::post($path, $options);
  111. } catch (\Exception $exception) {
  112. Log::error("MicroApi.UserService.getByEmail Call Failed: " . $exception->getMessage());
  113. throw new RpcException("调用远程服务失败");
  114. }
  115. $result = $this->decode($response->getBody()->getContents());
  116. return isset($result->user) ? $result->user : null;
  117. }
  118. /**
  119. * 创建密码重置记录
  120. *
  121. * @param $data
  122. * @return PasswordResetItem|null
  123. * @throws RpcException
  124. */
  125. public function createPasswordReset($data)
  126. {
  127. $path = $this->servicePrefix . '/createPasswordReset';
  128. $item = new PasswordResetItem();
  129. if (!empty($data['email'])) {
  130. $item->email = $data['email'];
  131. }
  132. if (!empty($data['token'])) {
  133. $item->token = $data['token'];
  134. }
  135. $options = ['json' => $item];
  136. try {
  137. $response = HttpClient::post($path, $options);
  138. } catch (\Exception $exception) {
  139. Log::error("MicroApi.UserService.createPasswordReset Call Failed: " . $exception->getMessage());
  140. throw new RpcException("调用远程服务失败");
  141. }
  142. $result = $this->decode($response->getBody()->getContents());
  143. return !empty($result->passwordReset) ? $result->passwordReset : null;
  144. }
  145. /**
  146. * 删除密码重置记录
  147. *
  148. * @param $email
  149. * @return bool
  150. * @throws RpcException
  151. */
  152. public function deletePasswordReset($email)
  153. {
  154. $path = $this->servicePrefix . '/deletePasswordReset';
  155. $item = new PasswordResetItem();
  156. $item->email = $email;
  157. $options = ['json' => $item];
  158. try {
  159. HttpClient::post($path, $options);
  160. } catch (\Exception $exception) {
  161. Log::error("MicroApi.UserService.deletePasswordReset Call Failed: " . $exception->getMessage());
  162. throw new RpcException("调用远程服务失败");
  163. }
  164. return true;
  165. }
  166. /**
  167. * 验证密码重置令牌
  168. *
  169. * @param $token
  170. * @return bool
  171. * @throws RpcException
  172. */
  173. public function validatePasswordResetToken($token)
  174. {
  175. $path = $this->servicePrefix . '/validatePasswordResetToken';
  176. $item = new TokenItem();
  177. $item->token = $token;
  178. $options = ['json' => $item];
  179. try {
  180. $response = HttpClient::post($path, $options);
  181. } catch (\Exception $exception) {
  182. Log::error("MicroApi.UserService.validatePasswordResetToken Call Failed: " . $exception->getMessage());
  183. throw new RpcException("调用远程服务失败");
  184. }
  185. $result = $this->decode($response->getBody()->getContents());
  186. return $result->valid;
  187. }
  188. /**
  189. * 更新用户信息接口
  190. *
  191. * @param UserItem $item
  192. * @return UserItem
  193. * @throws RpcException
  194. */
  195. public function update(UserItem $item)
  196. {
  197. $path = $this->servicePrefix . '/update';
  198. $options = ['json' => $item];
  199. try {
  200. $response = HttpClient::post($path, $options);
  201. } catch (\Exception $exception) {
  202. Log::error("MicroApi.UserService.update Call Failed: " . $exception->getMessage());
  203. throw new RpcException("调用远程服务失败");
  204. }
  205. $result = $this->decode($response->getBody()->getContents());
  206. return $result->user;
  207. }
  208. }