123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?php
- namespace App\MicroApi\Services;
- use App\MicroApi\Exceptions\RpcException;
- use App\MicroApi\Facades\HttpClient;
- use App\MicroApi\Items\TokenItem;
- use App\MicroApi\Items\UserItem;
- use Illuminate\Support\Facades\Log;
- use App\MicroApi\Items\PasswordResetItem;
- class UserService
- {
- use DataHandler;
- protected $servicePrefix = '/user/userService';
- /**
- * @param $data
- * @return UserItem
- * @throws RpcException
- */
- public function create($data)
- {
- $path = $this->servicePrefix . '/create';
- $user = new UserItem();
- if (!empty($data['name'])) {
- $user->name = $data['name'];
- }
- if (!empty($data['email'])) {
- $user->email = $data['email'];
- }
- if (!empty($data['password'])) {
- $user->password = $data['password'];
- }
- $options = ['json' => $user];
- try {
- $response = HttpClient::post($path, $options);
- } catch (\Exception $exception) {
- Log::error("MicroApi.UserService.create Call Failed: " . $exception->getMessage());
- throw new RpcException("调用远程服务失败");
- }
- $result = $this->decode($response->getBody()->getContents());
- return $result->user;
- }
- public function getAll()
- {
- $path = $this->servicePrefix . '/getAll';
- try {
- $response = HttpClient::get($path);
- } catch (\Exception $exception) {
- Log::error("MicroApi.UserService.getAll Call Failed: " . $exception->getMessage());
- throw new RpcException("调用远程服务失败");
- }
- $result = $this->decode($response->getBody()->getContents());
- return $result->users;
- }
- public function getById($id)
- {
- $path = $this->servicePrefix . '/get';
- $user = new UserItem();
- $user->id = $id;
- $options = ['json' => $user];
- try {
- $response = HttpClient::post($path, $options);
- } catch (\Exception $exception) {
- Log::error("MicroApi.UserService.getByCondition Call Failed: " . $exception->getMessage());
- throw new RpcException("调用远程服务失败");
- }
- $result = $this->decode($response->getBody()->getContents());
- return $result->user;
- }
- public function auth($credentials)
- {
- $path = $this->servicePrefix . '/auth';
- $user = new UserItem();
- if (!empty($credentials['email'])) {
- $user->email = $credentials['email'];
- }
- if (!empty($credentials['password'])) {
- $user->password = $credentials['password'];
- }
- $options = ['json' => $user];
- try {
- $response = HttpClient::post($path, $options);
- } catch (\Exception $exception) {
- Log::error("MicroApi.UserService.auth Call Failed: " . $exception->getMessage());
- throw new RpcException("调用远程服务失败");
- }
- $result = $this->decode($response->getBody()->getContents());
- return $result->token;
- }
- public function isAuth($token)
- {
- $path = $this->servicePrefix . '/validateToken';
- $item = new TokenItem();
- $item->token = $token;
- $options = ['json' => $item];
- try {
- $response = HttpClient::post($path, $options);
- } catch (\Exception $exception) {
- Log::error("MicroApi.UserService.auth Call Failed: " . $exception->getMessage());
- throw new RpcException("调用远程服务失败");
- }
- $result = $this->decode($response->getBody()->getContents());
- return $result->valid;
- }
- public function getByEmail($email)
- {
- $path = $this->servicePrefix . '/get';
- $user = new UserItem();
- $user->email = $email;
- $options = ['json' => $user];
- try {
- $response = HttpClient::post($path, $options);
- } catch (\Exception $exception) {
- Log::error("MicroApi.UserService.getByEmail Call Failed: " . $exception->getMessage());
- throw new RpcException("调用远程服务失败");
- }
- $result = $this->decode($response->getBody()->getContents());
- return isset($result->user) ? $result->user : null;
- }
- /**
- * 创建密码重置记录
- *
- * @param $data
- * @return PasswordResetItem|null
- * @throws RpcException
- */
- public function createPasswordReset($data)
- {
- $path = $this->servicePrefix . '/createPasswordReset';
- $item = new PasswordResetItem();
- if (!empty($data['email'])) {
- $item->email = $data['email'];
- }
- if (!empty($data['token'])) {
- $item->token = $data['token'];
- }
- $options = ['json' => $item];
- try {
- $response = HttpClient::post($path, $options);
- } catch (\Exception $exception) {
- Log::error("MicroApi.UserService.createPasswordReset Call Failed: " . $exception->getMessage());
- throw new RpcException("调用远程服务失败");
- }
- $result = $this->decode($response->getBody()->getContents());
- return !empty($result->passwordReset) ? $result->passwordReset : null;
- }
- /**
- * 删除密码重置记录
- *
- * @param $email
- * @return bool
- * @throws RpcException
- */
- public function deletePasswordReset($email)
- {
- $path = $this->servicePrefix . '/deletePasswordReset';
- $item = new PasswordResetItem();
- $item->email = $email;
- $options = ['json' => $item];
- try {
- HttpClient::post($path, $options);
- } catch (\Exception $exception) {
- Log::error("MicroApi.UserService.deletePasswordReset Call Failed: " . $exception->getMessage());
- throw new RpcException("调用远程服务失败");
- }
- return true;
- }
- /**
- * 验证密码重置令牌
- *
- * @param $token
- * @return bool
- * @throws RpcException
- */
- public function validatePasswordResetToken($token)
- {
- $path = $this->servicePrefix . '/validatePasswordResetToken';
- $item = new TokenItem();
- $item->token = $token;
- $options = ['json' => $item];
- try {
- $response = HttpClient::post($path, $options);
- } catch (\Exception $exception) {
- Log::error("MicroApi.UserService.validatePasswordResetToken Call Failed: " . $exception->getMessage());
- throw new RpcException("调用远程服务失败");
- }
- $result = $this->decode($response->getBody()->getContents());
- return $result->valid;
- }
- /**
- * 更新用户信息接口
- *
- * @param UserItem $item
- * @return UserItem
- * @throws RpcException
- */
- public function update(UserItem $item)
- {
- $path = $this->servicePrefix . '/update';
- $options = ['json' => $item];
- try {
- $response = HttpClient::post($path, $options);
- } catch (\Exception $exception) {
- Log::error("MicroApi.UserService.update Call Failed: " . $exception->getMessage());
- throw new RpcException("调用远程服务失败");
- }
- $result = $this->decode($response->getBody()->getContents());
- return $result->user;
- }
- }
|